bool SIM900GPRS::turnOn() {
    // check if already on, if so return true
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT"));
#endif
    _cell->println(F("AT"));
    if(successfulResponse(100)) {
#ifdef DEBUG
        _debug->println(F("Already on"));
#endif
        return true;
    }

    powerOnCycle();

    // try to send AT and get OK back
    int i = 0;
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT"));
#endif
    _cell->println(F("AT"));
    while(!successfulResponse(250) && (i < 10)) {
        i++;
        _cell->println(F("AT"));
    }
    if(i == 10) { // did not get response after 10 AT commands, could not turn on
        _status = ERROR;
        return false;
    }
    _status = CONNECTING;

#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" ATE0"));
#endif
    _cell->println(F("ATE0"));
    if(!successfulResponse()) {
        return false;
    }

#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" ATV1"));
#endif
    _cell->println(F("ATV1"));
    if(!successfulResponse()) { //set verbose mode
        return false;
    }

    return true;
}
/**
 *
 * Parameters -
 * Returns -
 */
bool SIM900GPRS::deactivateDateTime()
{
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CLTS=0"));
#endif
    _cell->println(F("AT+CLTS=0"));
    return successfulResponse();
}
Пример #3
0
void MycosmosController::downloadFinished(QNetworkReply *reply){
    QUrl url = reply->url();

    if(reply->error() == QNetworkReply::NoError) {
        // Get the http status code
        int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();


        if (v >= 200 && v < 300){ // Success

        }
        else if (v >= 300 && v < 400) {// Redirection

            // Get the redirection url

            QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
            // Because the redirection url can be relative,
            // we have to use the previous one to resolve it
            newUrl = reply->url().resolved(newUrl);

            //            QNetworkAccessManager *manager = nReply->manager();
            //            QNetworkRequest redirection(newUrl);
            //            QNetworkReply *newReply = manager->get(redirection);

            return; // to keep the manager for the next request
        }

        // Here we got the final reply
        htmlReplyText = QString::fromUtf8(reply->readAll());

        // get view state string
        viewStateString =  getViewState(htmlReplyText);

        QList<QNetworkCookie>  cookies = nManager->cookieJar()->cookiesForUrl(url);
        for(int i=0; i<cookies.count(); i++){
            printf("COOKIE %d Name:%s Value:%s\n" ,i,qPrintable(QString::fromUtf8(cookies.at(i).name())),qPrintable(QString::fromUtf8(cookies.at(i).value())) );
        }
        printf("COOKIES for %s\n%s\n" ,qPrintable(url.host()) ,qPrintable(QString::number(cookies.count())));

        // save cookies inside a QVariant
        cookieData.setValue(cookies);

        emit successfulResponse();
    }
    else
    {
        // Error
        printf("Download of %s failed: %s\n",url.toEncoded().constData(),qPrintable(reply->errorString()));
    }

    printf("Url encoded Νένα 1:%s\n" ,qPrintable(QUrl::toPercentEncoding("Νένα 1",0,0)));


    // We receive ownership of the reply object
    // and therefore need to handle deletion.
    //    delete reply;
}
// TODO - Implement not supported features
NetworkStatus_t SIM900GPRS::begin(char* pin, bool restart)
{
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+IPR=0"));
#endif
    _cell->println(F("AT+IPR=0"));
    if(!successfulResponse()) { //set autoBaud (default not really needed)
        return ERROR;
    }

#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CMEE=1"));
#endif
    _cell->println(F("AT+CMEE=1"));
    if(!successfulResponse()) { //set extended error report
        // CMEE=0 just ERROR
        // CMEE=1 just error number
        // CMEE=2 full error
        return ERROR;
    }

#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CREG=0"));
#endif
    _cell->println(F("AT+CREG=0")); // no presentation of unsolicited result code +CREG
    if(!successfulResponse()) {
        return ERROR;
    }
//	_cell->println(F("AT+IFC=1,1"));
//	if(!successfulResponse()) { //set software flow control
//		return ERROR;
//	}

    return GSM_READY;
}
char* SIM900GPRS::getSoftwareVersion() {
    //
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CGMR"));
#endif
    _cell->println(F("AT+CGMR")); // Retrurns: Revision:1137B11SIM900M64_ST
    if(!successfulResponse()) { // no response
        return NULL;
    }
    char* end = strstr_P(_buffer, PSTR("OK"));
    end -=4; // end points att O in OK and is preceeded with two times '\r\n'
    end[0] = 0;
    return &_buffer[11]; // start from first character after '\r\nRevision:'
}
/**
 *
 * Parameters -
 * Returns -
 */
char* SIM900GPRS::getTimeStr()
{
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CCLK?"));
#endif
    _cell->println(F("AT+CCLK?")); // RETURNS: +CCLK: "15/03/26,17:25:28+04"
    if(!successfulResponse()) { // no response
        return NULL;
    }
    char* end = strstr_P(_buffer, PSTR("OK"));
    end -=8; // end points att O in OK and is preceeded with two \r\n and a +zz"
    end[0] = 0;
    return &_buffer[10]; // start from character after first "
}
/**
 * Obtain modem IMEI (command AT)
 * Returns - string with modem IMEI number or NULL if failed
 * The IMEI string is on the format "AABBBBBBCCCCCCEE" and always 16 characters long.
 */
char* SIM900GPRS::getIMEI()
{
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+GSN"));
#endif
    _cell->println(F("AT+GSN")); //RETURNS: 0000646714 OK
    if(!successfulResponse()) { // no response
        return NULL;
    }
    char* end = strstr_P(_buffer, PSTR("OK"));
    end -=4; // end points att O in OK and is preceeded with \r\n
    end[0] = 0;
    return _buffer+2;
}
bool SIM900GPRS::isGPRSAvailable() {
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CGATT?"));
#endif
    _cell->println(F("AT+CGATT?")); // check if GPRS is activated
    // RETURNS: +CGATT: 1  OK
    if(!successfulResponse(3000)) { // no response
        return NULL;
    }
    char* end = strstr_P(_buffer, PSTR("OK"));
    end -=1;
    end[0] = 0;
    char* gprs = strchr(_buffer,':');
    if(gprs[2]=='1') {
        return true;
    } else {
        return false;
    }
}
/**
 * Gets signal quality, returns the rssi value
 * 	0 		-115 dBm or less
 * 	1  		-111 dBm
 * 	2..30 -110..-54 dBm
 * 	31		-52 dBm or greater
 * 	99		not known or detectable
 */
int SIM900GPRS::getSignalStrength() {
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CSQ"));
#endif
    _cell->println(F("AT+CSQ"));
    //RETURNS: +CSQ: 7,0
    /*
     +CSQ: <rssi>, <ber>
    	<rssi>
    		0 		-115 dBm or less
    		1  		-111 dBm
    		2..30 -110..-54 dBm
    		31		-52 dBm or greater
    		99		not known or detectable
    	<ber> (in percent)
    		0..7 	as RXQUAL values in the table GSM 05-08 [20] subclause 7.2.4
    		99 		not knonw or detectable
    */
    if(!successfulResponse()) { // no response
        return 99;
    }

    char* end = strstr_P(_buffer, PSTR("OK"));
    if(NULL == end ) {
        return 99;
    }
    end -=4; // end points att O in OK and is preceeded with two times '\r\n'
    end -=2; // skip the <ber> part and the ',' .
    end[0] = 0;
    char* gprs = strchr(_buffer,':');
    gprs +=2; // skip the blank space
#ifdef DEBUG
    _debug->print(millis());
    _debug->print(F(" RSSI: "));
    _debug->println(gprs);
#endif
    return atoi(gprs);
}
/**
 *
 * Parameters -
 * Returns - a string with current GMT time
 * Got time: 00/01/01,00:00:36+08
1999/12/31 22:00:36
 */
time_t SIM900GPRS::getUnixTime()
{
    char *localBufferPtr = _buffer; // create a local copy of the pointer since we will manipulate it
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CCLK?"));
#endif
    _cell->println(F("AT+CCLK?")); // RETURNS: +CCLK: "15/03/26,17:25:28+04"\r\n\r\nOK\r\n
    if(!successfulResponse()) { // no response
        return NULL;
    }

    localBufferPtr += 10; // start from character after first "
    char* end = strstr_P(localBufferPtr, PSTR("OK"));
    end -=5; // end points at O in OK and is preceeded with two \r\n and an "
    end[0] = 0;

    int year;
    int month;
    int day;
    int hour;
    int min;
    int sec;
    int tz;

    localBufferPtr[2]=0;
    year = atoi(localBufferPtr);
    localBufferPtr += 3;

    localBufferPtr[2]=0;
    month = atoi(localBufferPtr);
    localBufferPtr += 3;

    localBufferPtr[2]=0;
    day = atoi(localBufferPtr);
    localBufferPtr += 3;

    localBufferPtr[2]=0;
    hour = atoi(localBufferPtr);
    localBufferPtr += 3;

    localBufferPtr[2]=0;
    min = atoi(localBufferPtr);
    localBufferPtr += 3;

    tz = atoi(&localBufferPtr[3]); // parse including the + or - in the timezone

    end -=3; // remove the a +zz
    end[0] = 0;
    sec = atoi(localBufferPtr);

    tmElements_t tm;

    if(year<15) {
        // When the modem has not synced it's time it reports the date as 00/01/01,00:00:00+zz plus the time that has passed since it started.
        // This software was written in 2015 so we know that the year cannot be less than 15. If so return NULL to indicate invalid time.
        return NULL;
    }

    year += 30; // years since 1970
    tm.Year = year;
    tm.Month = month;
    tm.Day = day;
    tm.Hour = hour;
    tm.Minute = min;
    tm.Second = sec;

    // compensate for time zone so the result will be in GMT
    // tz indicates the difference, expressed in quarters of an hour, between the loacl time and GMT; range -47 ... +48
    return makeTime(tm) - (tz * 15 * 60);
}
NetworkStatus_t SIM900GPRS::attachGPRS(const char * const domain, const char * const username, const char * const password) {
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CREG?"));
#endif
    _cell->println(F("AT+CREG?")); // check if registered in network
    // RETURNS: +CREG: 1,x  OK
    if(!successfulResponse()) { // no response
        _status = ERROR;
        return _status;
    }
    char* end = strstr_P(_buffer, PSTR("OK"));
    end -=1;
    end[0] = 0;
    char* registered = strstr_P(_buffer, PSTR("+CREG: 0,"));
    if(registered[9] !='1') {
        _status = CONNECTING;
        return _status;
    }

    delay(15); // short delay needed for modem to detect next command
    // we only want a single IP Connection at a time.
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CIPMUX=0"));
#endif
    _cell->println(F("AT+CIPMUX=0"));
    if(!successfulResponse()) {
        _status = CONNECTING;
        return _status;
    }

    // we want non-transparent mode
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CIPMODE=0"));
#endif
    _cell->println(F("AT+CIPMODE=0"));
    if(!successfulResponse()) {
        _status = CONNECTING;
        return _status;
    }

    // start GPRS task and set APN, username and password
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CSTT="));
#endif
    _cell->print(F("AT+CSTT=\""));
    _cell->print(domain);
    _cell->print(F("\",\""));
    if(NULL != username) _cell->print(username);
    _cell->print(F("\",\""));
    if(NULL != password) _cell->print(password);
    _cell->println(F("\""));
    if(!successfulResponse()) {
        _status = CONNECTING;
        return _status;
    }

    // chip is in state IP START

    // bring up wireless connection with GPRS or CSD
#ifdef DEBUG
    _debug->print(millis());
    _debug->println(F(" AT+CIICR"));
#endif
    _cell->println(F("AT+CIICR"));
    if(!successfulResponse(15000)) {
        _status = CONNECTING;
        return _status;
    }

    // chip is in state IP CONFIG or IP GPRSACT
    _status = GPRS_READY;
    return GPRS_READY;
}