Thanks soleil.
From the resources online, i managed to modify the TCP client code from the 3G + GPS shield's tutorial:
http://www.cooking-hacks.com/documentation/tutorials/arduino-3g-gprs-gsm-gps
Here is my version of the code:
int8_t answer;
int onModulePin= 2;
char aux_str[50];
char server[ ]="api.xively.com";
char port[ ]="8081";
//char TCP_message[ ]="";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sendATcommand("AT+CPIN", "OK", 2000);
delay(3000);
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
// sets APN, user name and password
sendATcommand("AT+CGSOCKCONT=1,\"web.sktelecom.com\"", "OK", 2000);
sendATcommand("AT+CSOCKAUTH=1,1,\"\",\"\"", "OK", 2000);
}
void loop(){
sprintf(aux_str, "AT+NETOPEN=\"TCP\",%s", port);
answer = sendATcommand(aux_str, "Network opened", 20000);
if (answer == 1)
{
Serial.println("Network opened");
sprintf(aux_str, "AT+TCPCONNECT=\"%s\",%s", server, port);
answer = sendATcommand(aux_str, "Connect ok", 20000);
if (answer == 1)
{
Serial.println("Socket opened");
String humidity = "18";
String temperature = "30";
Serial.println("AT+TCPWRITE");
//while(Serial.read()!='>');
Serial.println("{\"method\": \"put\",\"resource\": \"/feeds/1350201254/\",\"params\"");
delay(500);
Serial.println(": {},\"headers\": {\"X-ApiKey\":");//in here, you should replace your xivelyapikey
delay(500);
Serial.println(" \"n49PBBOVhNjACyzWm6T5vuOwLXyudiYhtI5eoaE3pQVdrame\"},\"body\":");//xivelyapikey
delay(500);
Serial.println(" {\"version\": \"1.0.0\",\"datastreams\": ");
delay(500);
Serial.println("[{\"id\": \"Humidity\",\"current_value\": \"" + humidity + "\"},");
delay(500);
Serial.println("{\"id\": \"Temperature\",\"current_value\": \"" + temperature + "\"}]}}");
delay(500);
Serial.write(0x0D);
Serial.write(0x0A);
Serial.write(0x0D);
Serial.write(0x0A);
Serial.write(0x1A);
delay(500);
answer = sendATcommand(aux_str, ">", 20000);
if (answer == 1)
{
Serial.println("Send ok");
}
else
{
Serial.println("Send not ok");
}
/* sprintf(aux_str, "AT+TCPWRITE=%d", strlen(TCP_message));
answer = sendATcommand(aux_str, ">", 20000);
if (answer == 1)
{
sendATcommand(TCP_message, "Send OK", 20000);
}
sendATcommand("AT+NETCLOSE", "OK", 20000);*/
}
else
{
Serial.println("Error opening the socket");
}
}
else
{
Serial.println("Error opening the nertwork");
}
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1,
unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
I am able to connect to the Xively api server but i am unable to update the datastream. I am using HTTP method put from the Seeeduino gprs shield tutorial since it also uses AT commands:
http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0
Any pointers of what i am missing or doing wrong and whether the put method coding is okay.
Thanks.