I have a simple project just for learning…fetch the temperature value from pin=0 arduino duemilanove and try to send it 1 at a time using GPRS MODULE SIM900…how to make a connection to Cosm.com using AT command? I try
myself by referring here ,
seeedstudio.com/wiki/GPRS_Shield … using_GPRS
and edit it so that I can fetch my temperature value at arduino
duemilanove at pin 0… it seem working but, still there is no update
in my stream…could anyone expert inspect my code?
#include <NewSoftSerial.h>
#include <Wire.h>
float convertedtemp; // We then need to multiply our two bytes by a
scaling factor, mentioned in the datasheet.
int tmp102_val; // an int is capable of storing two bytes, this is
where we “chuck” the two bytes together.
float tempC;
int tempPin = 0;
NewSoftSerial GPRS_Serial(6,4);
void setup()
{
GPRS_Serial.begin(9600); //GPRS Shield baud rate
Serial.begin(9600);
setup_start:
Serial.println(“Turn on GPRS Modem and wait for 1 minute.”);
Serial.println(“and then press a key”);
Serial.println(“Press c for power on configuration”);
Serial.println(“press any other key for uploading”);
Serial.flush();
while(Serial.available() == 0);
if(Serial.read()==‘c’)
{
Serial.println(“Executing AT Commands for one time power on configuration”);
GPRS_Serial.flush();
GPRS_Serial.println(“ATE0”); //Command echo off
Serial.println(“ATE0 Sent”);
if(GPRS_Serial_wait_for_bytes(4,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.println(“AT+CIPMUX=0”); //We only want a single IP
Connection at a time.
Serial.println(“AT+CIPMUX=0 Sent”);
if(GPRS_Serial_wait_for_bytes(4,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.println(“AT+CIPMODE=0”); //Selecting “Normal Mode” and
NOT “Transparent Mode” as the TCP/IP Application Mode
Serial.println(“AT+CIPMODE=0 Sent!”);
if(GPRS_Serial_wait_for_bytes(4,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.println(“AT+CGDCONT=1,“IP”,“celcom3g”,“10.6.6.6”,0,0”);
//Defining the Packet Data
//Protocol Context - i.e. the Protocol Type, Access Point Name and IP Address
Serial.println(“AT+CGDCONT=1,“IP”,“celcom3g”,“10.6.6.6”,0,0 Sent!”);
if(GPRS_Serial_wait_for_bytes(4,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.println(“AT+CSTT=“celcom3g””); //Start Task and set
Access Point Name (and username and password if any)
Serial.println(“AT+CSTT=“celcom3g” Sent!”);
if(GPRS_Serial_wait_for_bytes(4,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.println(“AT+CIPSHUT”); //Close any GPRS Connection if open
Serial.println(“AT+CIPSHUT Sent!”);
if(GPRS_Serial_wait_for_bytes(7,10) == 0)
{
Serial.println(“Timeout”);
goto setup_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
}
}
void loop()
{
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature
Serial.print((byte)tempC); //send the data to the computer
delay(1000);
loop_start:
Serial.println(“Press a key to read temperature and upload it”);
Serial.flush();
while(Serial.available() == 0);
Serial.read();
Serial.print("Temperature = ");
Serial.println(tempC);
GPRS_Serial.println(“AT+CIPSTART=“TCP”,“api.cosm.com”,“80"”);
//Open a connection to cosm.com
Serial.println(“AT+CIPSTART=“TCP”,“api.cosm.com”,“80” Sent!”);
if(GPRS_Serial_wait_for_bytes(12,255) == 0)
{
Serial.println(“Timeout”);
goto loop_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print(”\n");
}
}
GPRS_Serial.flush();
GPRS_Serial.println(“AT+CIPSEND=12”); //Start data through TCP connection
Serial.println(“AT+CIPSEND Sent!”);
if(GPRS_Serial_wait_for_bytes(1,100) == 0)
{
Serial.println(“Timeout”);
goto loop_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.flush();
//Emulate HTTP and use PUT command to upload temperature datapoint
using Comma Seperate Value Method
GPRS_Serial.print(“PUT /v2/feeds/55223.csv HTTP/1.1\r\n”);
Serial.println(“PUT /v2/feeds/55223.csv HTTP/1.1 Sent!”);
delay(500);
GPRS_Serial.print(“Host: api.cosm.com\r\n”);
Serial.println(“Host: api.cosm.com Sent!”);
delay(500);
GPRS_Serial.print(“X-cosmApiKey:
wNxApJTHWVaqvaX-MnZ9pkeOH_OViiVyvHHfnNQ9hpU\r\n”); //REPLACE THIS KEY
WITH YOUR OWN cosm API KEY
Serial.println(“X-cosmApiKey:
wNxApJTHWVaqvaX-MnZ9pkeOH_OViiVyvHHfnNQ9hpU Sent!”); //REPLACE THIS
KEY WITH YOUR OWN cosm API KEY
delay(500);
GPRS_Serial.print(“Content-Length: 12\r\n”);
Serial.print(“Content-Length: 12 Sent!”);
delay(300);
GPRS_Serial.print(“Connection: close\r\n\r\n”);
Serial.print(“Connection: close Sent!”);
delay(500);
GPRS_Serial.print(“Temp,”); //You may replace the stream name
“TMP102” to any other string that you have choosen.
delay(3500);
GPRS_Serial.print(tempC);
delay(300);
GPRS_Serial.print("\r\n");
delay(300);
GPRS_Serial.print("\r\n");
delay(300);
GPRS_Serial.print(0x1A,BYTE);
delay(300); //Send End Of Line Character to send all the data and
close connection
if(GPRS_Serial_wait_for_bytes(20,255) == 0)
{
Serial.println(“Timeout”);
goto loop_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
GPRS_Serial.flush();
GPRS_Serial.println(“AT+CIPSHUT”); //Close the GPRS Connection
Serial.println(“AT+CIPSHUT Sent!”);
if(GPRS_Serial_wait_for_bytes(4,100) == 0)
{
Serial.println(“Timeout”);
goto loop_start;
}
else
{
Serial.print(“Received:”);
while(GPRS_Serial.available()!=0)
{
Serial.print((unsigned char)GPRS_Serial.read());
Serial.print("\n");
}
}
}
char GPRS_Serial_wait_for_bytes(char no_of_bytes, int timeout)
{
while(GPRS_Serial.available() < no_of_bytes)
{
delay(200);
timeout-=1;
if(timeout == 0)
{
return 0;
}
}
return 1;
}
could anyone simplify this code for me?Or at least give me a guidance