Hello ,
I'm working with SIM808 zero
simple code that read GPS and send it to my server
everything is working great for 10-15 min then it's semms that the code get stuck
the device is still connected to the internet , still get GPS (beacuse of the leds - I can tell it's working)
but it's stuck
maybe someone will see something I'm missing?
#include<stdio.h>
#include<string.h>
#define DEBUG true
int POWER_KEY = 9;
int LED6 = 6;
bool SIM808_ON = false;
String answer = "";
int answer_size = 0;
String Rssi = "";
String CIPSEND = "";
String BootCIPSEND = "";
String SIM = "";
String IMEI = "";
String SIMNumber = "";
String BootMSG = "";
int BootMSG_size = 0;
String UnitName = "Device2";
void setup()
{
pinMode(LED6, OUTPUT);
pinMode(POWER_KEY, OUTPUT);
//********Turn on SIM808 when power up*********
digitalWrite(LED6, HIGH);
digitalWrite(POWER_KEY, HIGH);
delay(1000);
digitalWrite(POWER_KEY, LOW);
digitalWrite(LED6, LOW);
SerialUSB.begin(115200);
delay(2000);
SerialUSB.println("Start to run! V2_16.8.20 with LED ");
Serial1.begin(115200);
//********check open************
sendData("AT", 1000, DEBUG);
delay(1000);
//******************************
//********if SIM808 off, turn on it*********
if (!SIM808_ON)
{
digitalWrite(POWER_KEY, LOW);
delay(3000);
digitalWrite(POWER_KEY, HIGH);
SerialUSB.println("Turn on SIM808");
delay(10000);
}
digitalWrite(LED6, HIGH);
//GPS test
sendData("AT+CGNSPWR=1", 1000, DEBUG);//GNSS POWER CONTROL ON
delay(1000);
sendData("AT+CGNSSEQ=\"RMC\"", 1000, DEBUG);//
delay(1000);
sendData("AT+CGNSURC=0", 1000, DEBUG);// printf to serial every 1 second
delay(1000);
sendData("AT+CFUN?", 1000, DEBUG);// printf to serial every 1 second
SIMNumber = sendData("AT+CCID", 1000, DEBUG);//get SIM number
delay(1000);
sendData("AT+CSTT=internetg", 1000, DEBUG);// Setup APN uinternet - partner , internetg - cellcom
delay(1000);
sendData("AT+CGATT=1", 1000, DEBUG);// connected to the GPRS network
delay(1000);
sendData("AT+CIICR", 1000, DEBUG);// connected to the GPRS network
delay(1000);
sendData("AT+CIFSR", 1000, DEBUG);// Show IP address
delay(1000);
sendData("AT+CIPSTART=UDP,123.122.121.1,8888", 1000, DEBUG);//
delay(1000);
Rssi = sendData("AT+CSQ", 1000, DEBUG);// get signal RSSI
int Rssi_Data_Start = Rssi.indexOf(':');
Rssi = Rssi.substring(Rssi_Data_Start);
int Rssi_Data_End = Rssi.indexOf('\r');
Rssi = Rssi.substring(2, Rssi_Data_End);
IMEI = sendData("AT+GSN", 1000, DEBUG); //get IMEI Number
int IMEI_Data_Start = IMEI.indexOf('\r');
IMEI = IMEI.substring(IMEI_Data_Start);
IMEI_Data_Start = IMEI.indexOf('\n');
IMEI = IMEI.substring(IMEI_Data_Start);
int IMEI_Data_End = IMEI.indexOf('\r');
IMEI = IMEI.substring(1, IMEI_Data_End);
int SIMNumber_Data_Start = SIMNumber.indexOf('\r');
SIMNumber = SIMNumber.substring(SIMNumber_Data_Start);
SIMNumber_Data_Start = SIMNumber.indexOf('\n');
SIMNumber = SIMNumber.substring(SIMNumber_Data_Start);
int SIMNumber_Data_End = SIMNumber.indexOf('\r');
SIMNumber = SIMNumber.substring(1, SIMNumber_Data_End);
BootMSG = (UnitName + "!" + IMEI + "!" + SIMNumber + "!" + Rssi);
BootMSG_size = BootMSG.length();
BootCIPSEND = ("AT+CIPSEND=" + String( BootMSG_size));
SerialUSB.println("The BootMSG is going to be");
SerialUSB.println(BootMSG);
SerialUSB.println("the size is");
SerialUSB.println(BootMSG_size);
SerialUSB.println("*************");
sendData(BootCIPSEND, 1000, DEBUG);//
delay(1000);
// sendData("ON!\n\r", 1000, DEBUG);//HTTP POST //response: +HTTPACTION: 1,200,1
sendData(BootMSG, 1000, DEBUG);
delay(1000);
sendData("AT+CIPCLOSE", 1000, DEBUG);//
delay(1000);
// sendData("AT+CGNSINF", 1000, DEBUG);//
// delay(1000);
}
void loop()
{
answer = sendData("AT+CGNSINF", 1000, DEBUG);// get GPS data
delay(1000);
int GPS_Data_Start = answer.indexOf(':');
answer = answer.substring(GPS_Data_Start);
int GPS_Data_End = answer.indexOf('\r');
answer = answer.substring(2, GPS_Data_End);
Rssi = sendData("AT+CSQ", 1000, DEBUG);// get signal RSSI
int Rssi_Data_Start = Rssi.indexOf(':');
Rssi = Rssi.substring(Rssi_Data_Start);
int Rssi_Data_End = Rssi.indexOf('\r');
Rssi = Rssi.substring(2, Rssi_Data_End);
answer = (UnitName + ',' + answer + Rssi);
// answer = answer.substring(2);
answer_size = answer.length();
// answer = (answer + "\n\r");
SerialUSB.println(F("the replay from the modem was - \n\r"));
SerialUSB.println(answer);
SerialUSB.println(F("the size is - \n\r"));
SerialUSB.println(answer_size);
SerialUSB.println("*************");
sendData("AT+CIFSR", 1000, DEBUG); //just to be sure there is IP address - can remove after
delay(500);
CIPSEND = ("AT+CIPSEND=" + String(answer_size));
// SerialUSB.println("size of msg is - " + CIPSEND);
sendData("AT+CIPSTART=UDP,123.122.121.1,8888", 1000, DEBUG);//
delay(500);
sendData(CIPSEND, 1000, DEBUG);
delay(500);
sendData(answer, 1000, DEBUG);
delay(500);
sendData("AT+CIPCLOSE", 1000, DEBUG);
//delay(1500);
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
Serial1.println(command);
String commandpre = getcommand_pref(command);
//SerialUSB.println(commandpre);
long int time = millis();
while ( (time + timeout) > millis())
{
if (commandpre == "AT") {
if (Serial1.find("OK")) {
SIM808_ON = true;
SerialUSB.println("SIM808 opened");
}
}
while (Serial1.available())
{
String c = Serial1.readString();
response += c;
}
}
if (debug)
{
SerialUSB.println(response);
}
return response;
}
String getcommand_pref(String command) {
String command_pref = "";
char *cstr = new char[command.length() + 1];
strcpy(cstr, command.c_str());
char * token = strtok(cstr, "=");
int i = 0;
while (token != NULL) {
//SerialUSB.print(token);
//SerialUSB.println(" line" + (String)i);
switch (i) {
case 0:
command_pref = token;
break;
default:
break;
}
token = strtok(NULL, ",");
i = i + 1;
}
if (command_pref == "")
command_pref = command;
return command_pref;
}
I notice that when it get stuck
it get stuck after it print this line
"AT+CIPSEND=99"
*** just to be clear , if I look at the serial monitor output it send many times 99 ,
*** also it get stuck when the number is 100 \ 98 \102
*** so I don't think it have something to do with the value
maybe something in the flash \ RAM need to be clean?
Thanks ,