//SIM800 initialization procedure for simple SMS and call features void AGS::SIM800Init() { while (Serial.available() > 0) Serial.read(); Serial.println F("****************************************"); while (sendATcommand("AT", "OK", 2000) == 0); Serial.println F("SIM800 available!"); delay(2000); //otestuj registraci do site while ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) || sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0); while (Serial.available() > 0) Serial.read(); Serial.println F("SIM800 registered to GSM!"); //parameters to obtain time stamp from GSM network while (sendATcommand("AT+CLTS=1", "OK", 500) == 0); while (sendATcommand("AT+CENG=3", "OK", 500) == 0); while (Serial.available() > 0) Serial.read(); Serial.println("Time stamp from GSM:" + timeStamp()); Serial.println("GSM signal quality:" + getQuality()); Serial.println("GSM provider:" + getProviderName()); sendATcommand("AT+CMGF=1", "OK", 500); //zakaz indikace SMS sendATcommand("AT+CNMI=0,0", "OK", 500); //CLIP enabled sendATcommand("AT+CLIP=1", "OK", 1000); //smaz vsechny SMSky sendATcommand("AT+CMGD=1,4", "OK", 2000); sendATcommand("AT+CMGD=1", "OK", 2000); Serial.println F("SIM800 has been configured!"); Serial.println F("****************************************"); }
//Send SMS void AGS::sendSMS(String number, String sms) { if (_debug==1) { Serial.println("SMS pro:" + number); Serial.println("Obsah:" + sms); delay(500); } clrSIMbuffer(); //number = "+420"+number; SIM800.println("AT+CMGS=\"" + number + "\""); delay(200); //toSerial(); SIM800.println(sms); // message to send delay(100); SIM800.write((char)26); //CTRL+Z delay(100); SIM800.println(); delay(100); sendATcommand("AT+CMGD=1", "OK", 2000); sendATcommand("AT+CMGD=1,4", "OK", 2000); sendATcommand("AT+CMGD=1", "OK", 2000); delay(500); clrSIMbuffer(); if (_debug==1) { Serial.println("SMS odeslana!"); Serial.println F("****************************************"); } }
void setup(){ pinMode(onModulePin, OUTPUT); Serial.begin(115200); printf("Starting...\n"); power_on(); delay(3000); // sets the PIN code sendATcommand("AT+CPIN=****", "OK", 2000); delay(3000); // sets APN, user name and password sendATcommand("AT+CGPSPWR=1", "OK", 2000); sendATcommand("AT+CGPSRST=0", "OK", 2000); // waits for fix GPS while( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) || sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ); }
void power_on(){ uint8_t answer=0; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0){ // Send AT every two seconds and wait for the answer answer = sendATcommand("AT", "OK", 2000); } } }
bool AGS::GPRSInit(String APN) { APN = "AT+SAPBR=3,1,\"APN\",\"" + APN + "\"" ; char APNchar[sizeof(APN)+1]; for (uint8_t i; i < (sizeof(APN) + 1); i++) APNchar[i] = APN[i]; while( (sendATcommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", 500)) == 0 ); //APN = "AT+SAPBR=3,1,\"APN\",\" + APN + "\""; while( (sendATcommand(APNchar, "OK", 1000)) == 0 ); SIM800.println("AT+SAPBR=1,1"); delay(1000); SIM800.println("AT+SAPBR=2,1"); delay(1000); clrSIMbuffer(); while( (sendATcommand("AT+HTTPINIT", "OK", 3000))|| (sendATcommand("AT+HTTPINIT", "ERROR", 3000))== 0 ); //while( (sendATcommand("AT+HTTPINIT", "OK", 3000))); if (_debug == 1) { Serial.println F("GPRS Attached"); Serial.println F("****************************************"); } return 1; }
void init3G_GPS_Module() { int8_t answer; int onModulePin= 2; pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial1.begin(115200); Serial.println("Starting..."); answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin,HIGH); delay(3000); digitalWrite(onModulePin,LOW); // waits for an answer from the module while(answer == 0) { // Send AT every two seconds and wait for the answer answer = sendATcommand("AT", "OK", 2000); } } delay(3000); Serial.println("Connecting to the network..."); while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); Serial.println("Connected!"); // sets APN, user name and password sendATcommand("AT+CGSOCKCONT=1,\"IP\",\"internet\"", "OK", 2000); sendATcommand("AT+CSOCKAUTH=1,1,\"user_name\",\"password\"", "OK", 2000); }
String AGS::sendDataGPRS(String dataToSend) { int dataLength; dataToSend = "\""+dataToSend+"\""; delay(10); clrSIMbuffer(); if (_debug == 1) Serial.println F("***********GPRS communication***********"); SIM800.println("AT+HTTPPARA=\"URL\"," + dataToSend); while (SIM800.find("OK")); delay(10); clrSIMbuffer(); while( (sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:", 10000)) == 0 ); delay(10); clrSIMbuffer(); //read response SIM800.println("AT+HTTPREAD"); delay(500); char gsmc; String content=""; if(SIM800.find("+HTTPREAD:")) { if (_debug == 1) Serial.println("Reading response ..."); while(SIM800.available()>0) { gsmc = SIM800.read(); content += gsmc; if (gsmc == '\n') { content = content.substring(0,content.length()-2); dataLength = content.toInt(); if (_debug == 1) Serial.println("Data Lenght:" + String(dataLength)); content = ""; break; } } //read content if (_debug == 1)Serial.print("Content:"); delay(100); while(SIM800.available()>0) { gsmc = SIM800.read(); content += gsmc; dataLength --; if (dataLength < -1) { content = content.substring(0,content.length()-2); if (_debug == 1) Serial.println(content); content = ""; break; } } } else { return "COMMUNICATION FAILURE"; } delay(10); clrSIMbuffer(); if (_debug == 1) Serial.println F("****************************************"); return content; }
//Check if SIM800 is attached to GSM bool AGS::isConnected() { if ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) || sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 1) return true; else return false; }
//Call End void AGS::callEnd() { sendATcommand("ATH","OK",2000); return; }
//Check SIM800 if call or SMS is present uint8_t AGS::checkCallAndSMS() { char g; String gcmd; uint8_t gindex; //check call presence while (SIM800.available()>0) { g = SIM800.read(); gcmd += g; if (g == '\n') { gcmd = gcmd.substring(0, gcmd.length() - 2); gindex = gcmd.indexOf('+'); if (gcmd.substring(gindex+1,gindex+6) == "CLIP:") { //+CLIP: "420739822476" //cut +CLIP: //check if + is present and parse according this character gcmd = gcmd.substring(gindex+6); if (gcmd.indexOf('+') != -1) { gindex = gcmd.indexOf('+'); gcmd = gcmd.substring((gindex + 1), (gindex + 13)); } //parse according first " character else { gindex = gcmd.indexOf('"'); gcmd = gcmd.substring((gindex + 1), (gindex + 13)); } if(_debug == 1) { Serial.println("Call received!"); Serial.println("Number:"+gcmd); } callInProgress = false; number = gcmd; return 1; } else { //get timeStamp //actTime = timeStamp(); return 0; } } } gcmd = ""; //test SMS presence clrSIMbuffer(); SIM800.println("AT+CMGR=1"); delay(100); //echo surpress while (SIM800.available()>0) { g = SIM800.read(); gcmd += g; if (g == '\n') { gcmd = ""; break; } } //read first line with command response while (SIM800.available()>0) { g = SIM800.read(); gcmd += g; if (g == '\n') { if (gcmd.substring(0, 2) == "OK") return 0; if (gcmd.substring(0, 5) == "+CMGR") { if (_debug == 1) { Serial.println F("****************************************"); Serial.println F("New SMS alert!"); } //first + "+CMGR" gindex = gcmd.indexOf('+'); if (gindex < 0) return 0; //cut off first + character gcmd = gcmd.substring(gindex + 1); //sender number gindex = gcmd.indexOf('+'); number = gcmd.substring(gindex + 1, gindex + 13); Serial.println("From:" + number); //read SMS content gcmd = ""; delay(50); while (SIM800.available()>0) { g = SIM800.read(); gcmd += g; if ((g == '\n') && (gcmd.length()>2)) { gcmd = gcmd.substring(0, gcmd.length() - 2); Serial.println("SMS Content:" + gcmd); sendATcommand("AT+CMGD=1", "OK", 2000); sendATcommand("AT+CMGD=1,4", "OK", 2000); clrSIMbuffer(); if (_debug == 1) { Serial.println F("SMS deleted."); Serial.println F("****************************************"); } SMScontent = gcmd; return 2; } } gcmd = ""; sendATcommand("AT+CMGD=1", "OK", 2000); sendATcommand("AT+CMGD=1,4", "OK", 2000); clrSIMbuffer(); //get timeStamp //actTime = timeStamp(); return 0; } } } //get timeStamp //actTime = timeStamp(); return 0; }
void loop(){ // Basic // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request Basic string sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000); counter = 0; answer = 0; memset(Basic_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ Basic_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(Basic_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); Basic_str[counter-3] = '\0'; //********************************************************** // GGA // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request GGA string sendATcommand("AT+CGPSINF=2", "AT+CGPSINF=2\r\n\r\n", 2000); counter = 0; answer = 0; memset(GGA_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ GGA_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(GGA_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); GGA_str[counter-3] = '\0'; //********************************************************** // GLL // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request GLL string sendATcommand("AT+CGPSINF=4", "AT+CGPSINF=4\r\n\r\n", 2000); counter = 0; answer = 0; memset(GLL_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ GLL_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(GLL_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); GLL_str[counter-3] = '\0'; //********************************************************** // RMC // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request RMC string sendATcommand("AT+CGPSINF=32", "AT+CGPSINF=32\r\n\r\n", 2000); counter = 0; answer = 0; memset(RMC_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ RMC_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(RMC_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); RMC_str[counter-3] = '\0'; //********************************************************** // VTG // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request VTG string sendATcommand("AT+CGPSINF=64", "AT+CGPSINF=64\r\n\r\n", 2000); counter = 0; answer = 0; memset(VTG_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ VTG_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(VTG_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); VTG_str[counter-3] = '\0'; //********************************************************** // ZDA // Clean the input buffer while( Serial.available() > 0) Serial.read(); delay(100); // request ZDA string sendATcommand("AT+CGPSINF=128", "AT+CGPSINF=128\r\n\r\n", 2000); counter = 0; answer = 0; memset(ZDA_str, '\0', 100); // Initialize the string previous = millis(); // this loop waits for the NMEA string do{ if(Serial.available() != 0){ ZDA_str[counter] = Serial.read(); counter++; // check if the desired answer is in the response of the module if (strstr(ZDA_str, "OK") != NULL) { answer = 1; } } // Waits for the asnwer with time out }while((answer == 0) && ((millis() - previous) < 2000)); ZDA_str[counter-3] = '\0'; printf("*************************************************\n"); printf("Basic string: "); printf("%s\n",Basic_str); printf("GGA string: "); printf("%s\n",GGA_str); printf("GLL string: "); printf("%s\n",GLL_str); printf("RMC string: "); printf("%s\n",RMC_str); printf("VTG string: "); printf("%s\n",VTG_str); printf("ZDA string: "); printf("%s\n",ZDA_str); delay(15000); }