#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial gprs(5, 4); // RX, TX Pins
String apn = "internet.t-mobile"; //APN
String apn_u = "t-mobile"; //APN-Username
String apn_p = "tm"; //APN-Password
String pin = "";
String url = "http://..."; //URL for HTTP-POST-REQUEST
String serial = "1234";
void setup() {
Serial.begin(9600);
gprs.begin(9600);
delay(10000);
gprs.listen();
//deactivatePin();
gsm_connect();
}
void loop() {
String gpsParam = "";
gsm_sendhttp(gpsParam); //Start the GSM-Modul and start the transmisson
delay(60000); //Wait one minute
}
/* GPRS*/
bool gsm_connect() {
//Print GSM Status an the Serial Output;
ATCommand("AT", 4000, false);
//ATCommand("AT+CPIN?", 100,false);
ATCommand("AT+SAPBR=3,1,Contype,GPRS", 100, false);
ATCommand("AT+SAPBR=3,1,APN," + apn, 100, false);
ATCommand("AT+SAPBR=3,1,USER," + apn_u, 100, false);
ATCommand("AT+SAPBR=3,1,PWD," + apn_p, 100, false);
ATCommand("AT+COPS?", 1000, false); // Show all visible Provider
ATCommand("AT+CBAND?", 1000, false); // Show current Band
//ATCommand("AT+CBAND=ALL_BAND", 1000, false);
ATCommand("AT+CSQ", 1000, false); // Check Signal strength
ATCommand("AT+COPS?", 100, false);
ATCommand("AT+CLCK="PN", 2", 100, false); // check if sim800 is locked.
ATCommand("AT+IPR?", 100, false); //Check Baudrate. For me Autobaud is not working. Only Baud 9600
//ATCommand("AT+IPR=9600", 100, false); //Check Baudrate 0 = Autobaud
ATCommand("AT+SAPBR=1,1", 1000, false);
String res = ATCommand("AT+SAPBR=2,1", 1000, false);
if (res.indexOf("0.0.0.0") > -1) {
ATCommand("AT+CREG=2", 4000, false);
gsm_connect();
}
return true;
}
bool gsm_sendhttp(String params) {
int tries = 0;
while (ATCommand("AT+HTTPINIT", 100, false) == "ERROR" && tries < 5) {
gsm_connect();
tries++;
delay(1000);
}
if (tries >= 5) {
return false;
}
ATCommand("AT+HTTPPARA=CID,1", 100, false);
ATCommand("AT+HTTPPARA=URL," + url, 100, false);
ATCommand("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded", 100, false);
ATCommand("AT+HTTPDATA=192,10000", 100, false);
ATCommand("params=" + params, 10000, true);
ATCommand("AT+HTTPACTION=1", 5000, false);
ATCommand("AT+HTTPREAD", 100, false);
ATCommand("AT+HTTPTERM", 100, false);
return true;
}
String ATCommand(String cmd, unsigned long wait, bool printInline) {
if (printInline) { gprs.print(cmd); }
else { gprs.println(cmd); }
String res = "";
res = gprs.readString();
Serial.print(res);
delay(wait);
return res;
}
void deactivatePin() {
ATCommand("AT+CPIN="" + pin + """, 4000, false);
ATCommand("AT+CLCK="SC",0,"" + pin + """, 4000, false);
ATCommand("AT+CPIN?", 4000, false);
}