Hi,
I have a problem with my Arduino uno and my 3G + GPS shield.
After I launch the arduino, the program sometimes just suddenly stops,
or after sending the data to the server a few times, it stops.
The aim of my project is to send info to a server (here Cosm) on a
regular basis and to also send SMSs.
I'm using two libraries, one to send the SMSs and one to send data to
the server. (code below)
I put in some debug prints so I could see what was going on.
sometimes it would stop here, at the beginning of the connection: (once every two connections)
AT+CGSOCKCONT=1,"IP","gprs.swisscom.ch"
Then during the process of sending data at regular intervals, the arduino stops after a random
number of times. it stops at this:
AT+TCPWRITE=287
For these problems, I'm thinking the Arduino simply doesn't respond, but I don't know why...
here is the code for the libraries:
TCP_3G.cpp
#include "Arduino.h"
#include "TCP_3G.h"
TCP_3G::TCP_3G(){
}
void TCP_3G::begin(int led, int onModulePin){
_led = led;
_onModulePin = onModulePin;
x = 0;
delay(2000);
pinMode(_led, OUTPUT);
pinMode(_onModulePin, OUTPUT);
char _server[] = "api.cosm.com";
char _port[] = "8081";
// Set PDP parameters
Serial.println("AT+CGSOCKCONT=1,\"IP\",\"gprs.swisscom.ch\"");
//
//Blocks here every other time
//
Serial.flush();
Serial.print(Serial.read());
while(Serial.available()==0 || Serial.read()!='K');
// Set connection parameters
Serial.print("AT+NETOPEN=\"TCP\","); Serial.println(_port);
Serial.flush();
x=0;
do {
while(Serial.available()==0);
data[x]=Serial.read();
//if((data[x]!='\n') && (data[x]!='\r')) Serial.print(char(data[x]));
x++;
} while(!(data[x-1]=='d'&&data[x-2]=='e')); //waits for response "Network opened"
while(Serial.available()==0 || Serial.read()!='K');
// Connect to server
Serial.print("AT+TCPCONNECT=\""); Serial.print(_server); Serial.print("\","); Serial.println(_port);
Serial.flush();
while(Serial.available()==0 || Serial.read()!='K');
}
void TCP_3G::switchModule(){
digitalWrite(_onModulePin,HIGH);
digitalWrite(_onModulePin,LOW);
delay(2000);
}
void TCP_3G::sendInfo(int bottleN, float waterHeight, float derivative){
char COSM_API_KEY[] = "KrGtesAnlf8No0_C9_pLrVGt8biSAKhbViRKFks2eXJ1ez0g";
char COSM_FEED[] = "*****";
//Doing this for the size
char sbottle[6];
itoa(bottleN,sbottle, 10);
char scm[6];
sprintf(scm,"%.0f",waterHeight);
char sderiv[6];
sprintf(sderiv,"%.0f",derivative);
char feedStr[] = "{\"method\":\"put\",\"resource\":\"/feeds/12345\",\"params\":{},\"headers\":{\"X-PachubeApiKey\":\"KrGtesAnlf8No0_C9_pLrVGt8biSAKhbViRKFks2eXJ1ez0g\"},\"body\":{\"datastreams\":[{\"id\":\"0\",\"current_value\":\"\"},{\"id\":\"1\",\"current_value\":\"\"},{\"id\":\"2\",\"current_value\":\"\"}]},\"token\":\"0x12345\"}";
Serial.print("AT+TCPWRITE="); Serial.println(sizeof(sbottle) + sizeof(scm) + sizeof(sderiv) + sizeof(feedStr));//
//
//Blocks here after a while
//
Serial.flush();
Serial.print(Serial.read());
while(Serial.available()==0 || Serial.read()!='>');
Serial.print("{\"method\":\"put\",\"resource\":\"/feeds/");
Serial.print(COSM_FEED);
Serial.print("\",\"params\":{},\"headers\":{\"X-PachubeApiKey\":\"");
Serial.print(COSM_API_KEY);
Serial.print("\"},\"body\":{\"datastreams\":[{\"id\":\"0\",\"current_value\":\"");
Serial.print(bottleN);
Serial.print("\"},{\"id\":\"1\",\"current_value\":\"");
Serial.print(waterHeight);
Serial.print("\"},{\"id\":\"2\",\"current_value\":\"");
Serial.print(derivative);
Serial.println("\"}]},\"token\":\"0x12345\"}");
Serial.println("AT+NETCLOSE");
Serial.flush();
while(Serial.available()==0 || Serial.read()!='K');
}
SMS_3G.cpp
#include "Arduino.h"
#include "SMS_3G.h"
SMS_3G::SMS_3G()
{
}
void SMS_3G::begin(int led, int onModulePin)
{
_onModulePin = onModulePin;
_led = led;
//Serial.begin(brate); // UART baud rate
delay(2000);
pinMode(led, OUTPUT);
pinMode(onModulePin, OUTPUT);
switchModule(); // switches the module ON
for (int i=0;i<5;i++){
delay(5000);
}
Serial.println("AT+CMGF=1"); // sets the SMS mode to text
delay(100);
}
void SMS_3G::switchModule(){
digitalWrite(_onModulePin,HIGH);
delay(2000);
digitalWrite(_onModulePin,LOW);
}
void SMS_3G::sendSMS(char phone_number[], char msg[])
{
delay(1500);
Serial.print("AT+CMGS=\""); // send the SMS number
Serial.print(phone_number);
Serial.println("\"");
delay(1500);
Serial.print(msg); // the SMS body
delay(500);
Serial.write(0x1A); //sends ++
Serial.write(0x0D);
Serial.write(0x0A);
delay(5000);
}
Thanks a lot for your help!