thehapyone / BareBoneSim800

A BareBone Arduino Library For SIM800 Modules
GNU General Public License v3.0
47 stars 18 forks source link

Can't put to sleep after HTTP GET #7

Closed jaka87 closed 5 years ago

jaka87 commented 5 years ago

Hi there. First of all I love the simplicity of this library!

Here is the code that I use

#include <BareBoneSim800.h>
BareBoneSim800 sim800("internet.bob.si", "bob", "internet");  //to declare the library with an APN, UserName and Password
 const char resource[] = "test.com/send.php";
 const int port = 80;

void setup() {
  Serial.begin(9600);
  sim800.begin();
  while(!Serial);
  delay(8000); 
}

void loop() {
 bool fullMode = sim800.setFullMode();
  if(fullMode)
  {  

// Connecting the the GPRS APN Network
 Serial.println(" Connecting to APN");
 bool netConnect = sim800.gprsConnect();

  if(netConnect)
  {    
    Serial.println("Making HTTP Get Request");
    String result = sim800.sendHTTPData(resource);
    Serial.println("Received Info: ");
    Serial.println(result);

    sim800.closeHTTP(); // disconnect from server
    bool netDisconnect=sim800.gprsDisconnect();
    sim800.flushSerial(5000);

      bool sleepActivated = sim800.enterSleepMode();
    Serial.println("sleeeeeeeeeep");
    Serial.println(sleepActivated); 

      if(sleepActivated)
        {    
         Serial.println("zzzzZZZZZZzzzzz"); 
         delay(30000);   
           bool disableSleep = sim800.disableSleep();

      } 
  }   
  }
}

It works fine up to when is time to put module in sleep mode. Serial print for sleepActivated returns 0 and I cant understand why? If I just connect module to network I can than put it into sleep but not after HTTP request. Any idea why?

thehapyone commented 5 years ago

So you are saying if you do HTTP Request the device doesn't go to sleep but goes to sleep without HTTP request?

thehapyone commented 5 years ago

Call sim800.setFullMode(); delay(1000); before going to sleep mode. I don't have hardware to test.

jaka87 commented 5 years ago

Yes when i do HTTP request i cant put module to sleep. I did some further tests and even when i just connect to GPRS the same problem appear

here is the code used in second case

#include <BareBoneSim800.h>

BareBoneSim800 sim800("internet.bob.si", "bob", "internet");  //to declare the library with an APN, UserName and Password

void setup() {
  Serial.begin(9600);
  sim800.begin();
  while(!Serial);

  Serial.println("Testing GSM module For Sleep & PowerDown Mode");
  delay(8000); // this delay is necessary, it helps the device to be ready and connect to a network

  Serial.println("Should be ready by now");
    bool deviceAttached = sim800.isAttached();
  if (deviceAttached)
    Serial.println("Device is Attached");
  else
    Serial.println("Not Attached");

}

void loop() {

   bool netConnect = sim800.gprsConnect();

    sim800.flushSerial(5000);
    bool netDisconnect=sim800.gprsDisconnect();
    Serial.println(netDisconnect);
       sim800.flushSerial(5000);
sim800.setFullMode(); 
delay(1000); 
    Serial.println("before sleep");

   // Enable Sleep mode
  bool sleepActivated = sim800.enterSleepMode();
  if(sleepActivated)
    Serial.println("Sleep Mode");

    delay(5000); // let it sleep for about 5secs
  // disable sleep mode

  bool disableSleep = sim800.disableSleep();
    if(disableSleep)
    Serial.println("Sleep Mode Disabled");
}

and here is the debug information

09:28:51.280 -> Testing GSM module For Sleep & PowerDown Mode
09:28:59.278 -> Should be ready by now
09:29:00.277 -> AT

09:29:00.277 -> OK
09:29:00.277 -> 
09:29:01.344 -> AT+CSCS="GSM"

09:29:01.344 -> OK
09:29:01.344 -> 
09:29:02.443 -> AT+CMGF=1

09:29:02.443 -> OK
09:29:02.443 -> 
09:29:03.510 -> AT+CMGL="ALL",0

09:29:03.510 -> OK
09:29:03.510 -> 
09:29:03.510 -> Device is Attached
09:29:04.542 -> AT+CIPSHUT

09:29:04.542 -> SHUT OK
09:29:04.542 -> 
09:29:06.109 -> AT+CGATT=0

09:29:06.109 -> +SAPBR 1: DEACT
09:29:06.109 -> 
09:29:06.109 -> OK
09:29:06.109 -> 
09:29:07.143 -> AT+SAPBR=0,1

09:29:07.143 -> ERROR
09:29:07.143 -> 
09:29:08.309 -> AT+SAPBR=3,1,"CONTYPE","GPRS"

09:29:08.342 -> OK
09:29:08.342 -> 
09:29:09.509 -> AT+SAPBR=3,1,"APN","internet.bob.si"

09:29:09.542 -> OK
09:29:09.542 -> 
09:29:10.608 -> AT+SAPBR=3,1,"USER","bob"

09:29:10.608 -> OK
09:29:10.608 -> 
09:29:11.706 -> AT+SAPBR=3,1,"PWD","internet"

09:29:11.706 -> OK
09:29:11.706 -> 
09:29:12.739 -> AT+SAPBR=1,1

09:29:15.872 -> 
09:29:15.872 -> OK
09:29:15.872 -> 
09:29:17.907 -> AT+CGATT=1

09:29:17.907 -> OK
09:29:17.907 -> 
09:29:18.973 -> AT+CIPMUX=0

09:29:18.973 -> OK
09:29:18.973 -> 
09:29:20.073 -> AT+CSTT="internet.bob.si","bob","internet"

09:29:20.106 -> OK
09:29:20.106 -> 
09:29:21.106 -> AT+CIICR

09:29:21.106 -> OK
09:29:21.106 -> 
09:29:22.173 -> AT+CIFSR

09:29:22.173 -> 100.64.208.199
09:29:22.173 -> 
09:29:32.173 -> AT+CIPSHUT

09:29:32.173 -> SHUT OK
09:29:32.173 -> 
09:29:33.708 -> AT+CGATT=0

09:29:33.708 -> +SAPBR 1: DEACT
09:29:33.708 -> 
09:29:33.741 -> OK
09:29:33.741 -> 
09:29:34.740 -> AT+SAPBR=0,1

09:29:34.740 -> ERROR
09:29:34.740 -> 
09:29:34.841 -> 1
09:29:40.873 -> AT+CFUN=1

09:29:40.873 -> OK
09:29:40.873 -> 
09:29:46.900 -> before sleep
09:29:48.966 -> AT+CFUN=0

09:29:48.966 -> +CPIN: NOT READY
09:29:48.966 -> 
09:29:48.966 -> OK
09:29:48.966 -> 
jaka87 commented 5 years ago

I did some further testing and I think i finally found a solution. After some digging in SIM800 hardware design manual and while reading debug messages I assumed that there is an error in BareBoneSim800::enterSleepMode

bool BareBoneSim800::enterSleepMode(){
     // This set the device into a good sleep mode - AT+CFUN=0 and AT+CSCLK
      gsmSerial.print(F("AT+CFUN=0\r\n"));
     byte result = _checkResponse(10000);
     if (result != NOT_READY)
         return false;

     delay(50); // just chill small 
     // The GSM will send an OK response again
     result = _checkResponse(20000);
     if (result != OK)
         return false;
     delay(50);
     gsmSerial.print(F("AT+CSCLK=2\r\n")); // enable automatic sleep
      result = _checkResponse(20000);
     if(result != OK)
         return false;

     return true;
 }

First i changed all return false statements to Serial.println to see where the error is. It appears to be in the second paragraph where expected result is OK. Instead i got 99.

11:00:46.572 -> +CPIN: NOT READY
11:00:46.572 -> 
11:00:46.572 -> OK
11:00:46.572 -> 
11:01:06.633 -> error2
11:01:06.633 -> 99 

After changing to if (result != 99) it works and module can go to sleep. Im not 100% sure but from debug it appears there is another response after OK which breaks the function

thehapyone commented 5 years ago

Thanks.

This is some really interesting observation, but using 99 doesn't fix the problem. 99 is a hardcoded status I used in case you run into a timeout issue, so the device didn't send a 99 the code returned a 99 after waiting for an expected response for about 20ms.

I think when handling GPRS connectivity it sends more info than anticipated and probably takes a longer time to process.

thehapyone commented 5 years ago

Okay, I went through this observation again and the datasheet. There is actually no error generated. The timeout issue happened because the code was expecting an "OK" response that already has occured, thus waiting endlessly for an invisible response.

A simple way to fix this is to reduce the delay between the two response check. I made a new commit that fixed this error.