here is the code. i am trying to post the analog voltage to a thinkspeak channel using GPRS.
#include <SoftwareSerial.h>
#define HTTP_DOWNLOAD_LATENCY 5000
#define Diag Serial
SoftwareSerial GSM_PORT(8, 9); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Diag.begin(57600);
do
{
Diag.println("Waiting for GSM modem to initialise" ) ;
}while( !IsModemAlive );
// set the data rate for the SoftwareSerial port
GSM_PORT.begin(9600);
if( IsSIMRegstrd() )
DiagMsg( "SIM registered to n/w", 0 );
else
DiagMsg( "Pls insert working SIM", 1 );
}
int IsModemAlive( void )
{
if( SendCommand("AT", "OK", 200) )
return 1 ;
return 0 ;
}
int IsSIMRegstrd( void )
{
int count = 5 ;
do
{
if( SendCommand("AT+CPIN?", "+CPIN: READY", 200) )
if( SendCommand("AT+CREG?", "+CREG: 0,1", 200) )
return 1 ;
else
count-- ;
else
count-- ;
}while( count>0 ) ;
return 0 ;
}
int SendCommand(char *cmd, char *PosResp, int RxDelay )
{
char RxBuf[50] ;
int len ;
long int startTime ;
//send the command
GSM_PORT.println(cmd);
Diag.print(cmd) ;
delay(200);
startTime = millis() ;
do
{
len = 0 ;
while( GSM_PORT.available() )
{
RxBuf[len++] = GSM_PORT.read() ;
//Diag.print( RxBuf[len-1] ) ;
}
}while( !strstr( RxBuf, PosResp) && !strstr( RxBuf, "ERROR") && ((millis()-startTime)<RxDelay) ) ;
if( strstr( RxBuf, PosResp) )
{
Diag.println(" ---- + resp");
//Diag.print( "RxBuf=" );
//Diag.println( RxBuf );
return 1;
}
else
{
Diag.println(" ---- - resp");
//Diag.print( "RxBuf=" );
//Diag.println( RxBuf );
return 0;
}
}
int updateTSField(char * key, int field_idx, int value)
{
char httpdata[40]="key=", buf[5] ;
strcat(httpdata, key);
strcat(httpdata,"&field");
strcat(httpdata,itoa(field_idx, buf, 10));
strcat(httpdata,"=");
strcat(httpdata,itoa(value, buf, 10));
if( httpPOST( httpdata ) )
return 1 ;
else
return 0 ;
}
int httpPOST( char *data )
{
int state, timeout ;
char httpdata_cmd[50]="AT+HTTPDATA=" ;
char buf[5] ;
//setup bearer settings for GPRS
if( !setupGPRS() )
{
DiagMsg( "IP address not assigned", 0 );
return 0;
}
//construct the AT+HTTPDATA=<no_of_bytes_for_download>,<latency_time>
strcat(httpdata_cmd, itoa(strlen(data), buf, 10));
strcat(httpdata_cmd, ",");
strcat(httpdata_cmd, itoa(HTTP_DOWNLOAD_LATENCY, buf, 10));
Diag.print("httpdata_cmd= ");
Diag.println(httpdata_cmd);
Diag.print("data= ");
Diag.println(data);
state = 0 ;
//continue with HTTP POST
if( SendCommand( "AT+HTTPINIT", "OK", 200) )
if( SendCommand( "AT+HTTPPARA=\"CID\",1", "OK", 200) )
if( SendCommand( "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update\"", "OK", 500) )
if( SendCommand( httpdata_cmd, "DOWNLOAD", 800) )
if( SendCommand( data, "OK", 200) )
if( SendCommand( "AT+HTTPACTION=1", "+HTTPACTION:1,200", 7000) )
state = 1 ;
else
SendCommand( "AT+HTTPREAD", "OK", 3000);
SendCommand( "AT+HTTPTERM", "OK", 200);
return(state) ;
}
int setupGPRS( void )
{
int timeout = 5 ;
do
{
if( SendCommand( "AT+SAPBR=2,1", "+SAPBR: 1,1", 200) )
break ;
if( !SendCommand("AT+CGATT?","+CGATT: 1", 200) )
DiagMsg( "GPRS not attached", 0 );
if( !SendCommand( "AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 200) )
DiagMsg( "SAPBR=3,1,contype,gprs failed", 0 );
if( !SendCommand( "AT+SAPBR=1,1", "OK", 2000) )
DiagMsg( "SABPR=1,1 failed", 0 );
if( SendCommand( "AT+SAPBR=2,1", "+SAPBR: 1,1", 200) )
break ;
else
timeout = timeout - 1 ;
}while( timeout ) ;
if( timeout>0 )
return 1 ;
else
return 0 ;
}
void DiagMsg( char *msg, int loop)
{
Diag.println( msg ) ;
if( loop )
while( 1 ) ;
}
void loop() // run over and over
{
int postfail_count = 5 ;
do
{
int adcValue = analogRead( A0 ) ;
int voltage = adcValue * (5000.0/1023.0) ;
Diag.print("voltage= ");
Diag.print(voltage);
Diag.println("mV");
if( !updateTSField( "D9R6K3Y1NNMNZPCD", 1, voltage ) )
{
DiagMsg( "Channel post - failed", 0 );
postfail_count--;
}
else
DiagMsg( "Channel post - success", 0 );
delay(3000);
}while( postfail_count>0 );
DiagMsg( "Posting stopped due to repeated failure in posting", 1 ) ;
}