Dear Peter,
Thank. But I am trying it since a couple of month and I can not get a result. I have been oft in front of problem regarding the delay between AT request and I am still pending with that issue.
It the reason why I am looking for exemple code wich work in a repeating loop. For exemple send a data each 20 sec.
I fond some library but it still not work....
Would you have an exemple with socksatus?
This is an exemple.I found, but it only send once. After it return an error and it block. The script loop at while(1) loop
#include <SoftwareSerial.h>
SoftwareSerial cell(2,3); // Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin. connect this to the GSM module
const String apn = "internet"; // access-point name for GPRS
const String ip = "80.00.000.11"; // IP address of server we're connecting to
const String host = "www.yyy-xxx.net"; // required in HTTP 1.1 - what's the name of the host at this IP address?
const String request = "GET /temp/updatesql.php?lat=1.10&long=1.20&alt=1.30&speed=1.40&course=1.50&date=2013-4-10&time=19:12:00 HTTP/1.1";
const String useragent = "Mozilla/5.0"; // for our purposes the user agent doesn't matter - if I understand correctly it's helpful to use something generic the server will recognize
int GPRS_registered = 0;
int GPRS_AT_ready = 0;
void setup()
{
Serial.begin(9600);
Serial.println(F(""));
Serial.println(F("Starting SM5100B Communication..."));
cell.begin(9600);
/*
waitTil("+SIND: 4"); // keep printing cell output til we get "+SIND: 4"
Serial.println("Module ready");
*/
Serial.println(F("Wait for module is registered (+SIND: 11) and ready (+SIND: 4) ..."));
while (GPRS_registered == 0 || GPRS_AT_ready == 0) {
String ready = getMessage();
if(ready == "+SIND: 1"){
Serial.println(F("SIM is inserted ..."));
}
if(ready == "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1"){
Serial.println(F("SIM is ready ..."));
}
if(ready == "+SIND: 11"){
GPRS_registered = 1;
Serial.println(F("Module is registered to network ..."));
}
if(ready == "+SIND: 3"){
Serial.println(F("GPRS is partially ready ..."));
}
if(ready == "+SIND: 4"){
GPRS_AT_ready = 1;
Serial.println(F("GPRS is ready ..."));
}
if(ready == "+SIND: 7"){
Serial.println(F("Emergency only ..."));
}
}
//waitTil("+SIND: 4"); // keep printing cell output til we get "+SIND: 4"
Serial.println(F(" "));
Serial.println(F("READY TO GO"));
}
void loop()
{
Serial.println(F("Attaching GPRS..."));
cell.println("AT+CGATT=1");
waitFor("OK");
Serial.println(F("Setting up PDP Context..."));
cell.println("AT+CGDCONT=1,\"IP\",\""+apn+"\"");
waitFor("OK");
Serial.println(F("Activating PDP Context..."));
cell.println("AT+CGACT=1,1");
waitFor("OK");
Serial.println(F("Configuring TCP connection to TCP Server..."));
cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\",80");
waitFor("OK");
Serial.println(F("Starting TCP Connection..."));
cell.println("AT+SDATASTART=1,1");
waitFor("OK");
delay(5000); // wait for the socket to connect
// now we'll loop forever, checking the socket status and only breaking when we connect
while (1) {
Serial.println(F("Checking socket status:"));
cell.println("AT+SDATASTATUS=1"); // we'll get back SOCKSTATUS and then OK
String sockstat = getMessage();
waitFor("OK");
if (sockstat=="+SOCKSTATUS: 1,0,0104,0,0,0") {
Serial.println(F("Not connected yet. Waiting 1 second and trying again."));
delay(1000);
}
else if (sockstat=="+SOCKSTATUS: 1,1,0102,0,0,0") {
Serial.println(F("Socket connected"));
break;
}
else {
Serial.println(F("We didn't expect that."));
cellOutputForever();
}
}
// we're now connected and can send HTTP packets!
int packetLength = 26+host.length()+request.length()+useragent.length(); // 26 is size of the non-variable parts of the packet, see SIZE comments below
Serial.println(F("Sending HTTP packet..."));
cell.print("AT+SDATATSEND=1,"+String(packetLength)+"\r");
waitFor('>'); // wait for GSM module to tell us it's ready to recieve the packet
cell.print(request+"\r\n"); // SIZE: 2
cell.print("Host: "+host+"\r\n"); // SIZE: 8
cell.print("User-Agent: "+useragent+"\r\n\r\n"); // SIZE: 16
cell.write(26); // ctrl+z character: send the packet
waitFor("OK");
// now we're going to keep checking the socket status forever
// break when the server tells us it acknowledged data
while (1) {
cell.println("AT+SDATASTATUS=1"); // we'll get back SOCKSTATUS and then OK
String s = getMessage(); // we want s to contain the SOCKSTATUS message
if (s == "+STCPD:1") // this means server sent data. cool, but we want to see SOCKSTATUS, so let's get next message
s = getMessage();
//if (s == "+STCPC:1") // this means socket closed. cool, but we want to see SOCKSTATUS, so let's get next message
// s = getMessage();
waitFor("OK");
if (!s.startsWith("+SOCKSTATUS")) {
Serial.println(F("Wait, this isn't the SOCKSTATUS message!"));
cellOutputForever(); // something went wrong
}
if (checkSocketString(s) == packetLength) // checks that packetLength bytes have been acknowledged by server
break; // we're good!
else {
Serial.println(F("Sent data not yet acknowledged by server, waiting 1 second and checking again."));
delay(1000);
}
}
Serial.println("Yes! Sent data acknowledged by server!");
// we could skip the checking of SOCKSTATUS in the above while-loop
// instead we could just wait for one or both of these:
//waitTil("+STCPD:1"); // this means data is received
//waitTil("+STCPC:1"); // this means socket is closed
// TODO: actually check if we received data, don't just do this blindly
// Serial.println(F("Reading data from server..."));
// cell.println("AT+SDATAREAD=1"); // how we read data server has sent
// WARNING: this might not work - software serial can be too slow for receiving data
//Wait 10 sec and send again the data to server. Of course the data will change within that time
delay(10000);
}
In the next post, I past the following code, the function