I am having trouble with the GPRS module. It works well for a couple of times and then it stops working for a long while. then it works again. I wpould appreciate the help immensely!
Code:
Code:
unsigned long lastConnected=0;
void setup(){
// setup for Serial port over USB
USB.begin();
USB.println("USB port setup ok");
setupGPRS();
}
void setupGPRS(){
// setup for GPRS seial port
GPRS.ON();
USB.println("GPRS module ready... Waiting for GPRS to Connect to Network");
while(!GPRS.check());
USB.println("GPRS connected to the network");
// configure SMS and Incoming Calls
if(GPRS.setInfoIncomingCall())
{
USB.println("Info Incoming Call OK");
}
if(GPRS.setInfoIncomingSMS())
{
USB.println("Info Incoming SMS OK");
}
if(GPRS.setTextModeSMS())
{
USB.println("Text Mode SMS OK");
}
}
void loop()
{
Utils.setLED(LED0, LED_ON);
Utils.setLED(LED1, LED_ON);
RTC.getTemperature();
delay(2000);
USB.print("RTC temperature is ");
USB.print(RTC.temp,DEC);
USB.println(" C.");
// Configure GPRS Connection
if(GPRS.configureGPRS())
{
USB.println("GPRS Configured waiting for socket to open");
if(GPRS.createSocket("216.52.233.121","80",GPRS_CLIENT))//could replace GPRS_CLIENT with 0
{
USB.println("Socket Opened OK. Sending data to COSM");
USB.println("Session Number: ");
int i=0;
while( GPRS.socket_ID[i]!='\r' )
{
USB.println(GPRS.socket_ID[i]-'0',DEC);
i++;
}
i=0;
USB.println("Socket All OK");
/********************** Send Data to COSM *****************************************/
lastConnected=millis();
USB.println("Constructing Cosm Request ... ");
char* COSMReq=makeCOSMRequest(RTC.temp);
// Send data via socket
USB.println("COSM Request Constructed");
USB.println(COSMReq);
if(GPRS.sendData(COSMReq,GPRS.socket_ID))
{
USB.println("Data sent to COSM");
}
}
else
{
USB.println("Error: Failed to open TCP socket");
}
delay(2000);
// Close socket
if(GPRS.closeSocket(GPRS.socket_ID))
{
USB.println("Socket closed");
}
if(GPRS.deleteSocket(GPRS.socket_ID))
{
USB.println("Socket deleted");
}
}
else
{
USB.println("Error: Failed to configure GPRS");
}
}
char* makeCOSMRequest(float temp)
{
char *pdata= (char*) calloc(300,sizeof(char));
char *te = (char*) calloc(10,sizeof(char));
char *data =(char*) calloc(50,sizeof(char));
USB.println("Creating Arrays");
Utils.float2String(temp,te,2);
USB.println("convering string to float ");
replaceChar(te,',','.');
USB.println("change every . with te");
sprintf(data,"%s%s%s","1,",te,"\n");
USB.println("change data to lines");
int len=Utils.sizeOf(data);
USB.println("getting size of data");
sprintf(pdata,"%s%d%s%s", "PUT /v2/feeds/60549.csv HTTP/1.1\r\nHost: api.cosm.com\r\nAccept: */* \r\nAuthorization: Basic a196aWtyeTphenphbmFkYQ==\r\nContent-Length: " ,len ,"\n\n",data);
USB.println("change Pdata F");
return pdata;
}
/********************************** Helper function for strings *********************************/
int indexOf(char* str,char find)
{
int index=-1;
int sz=Utils.sizeOf(str);
for(int i=0;i<sz;i++){
if(str[i]==find){
index=i;
i=sz+1;
}
}
return index;
}
void replaceChar(char* str,char toReplace,char replaceWith)
{
int index=indexOf(str,toReplace);
str[index]=replaceWith;
}