Pages: [1]   Go Down
Author Topic: Program to send data to xively using arduino 3G + GPS shield  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have a question on how to program my Arduino 3G + GPS shield from cooking hacks to upload data to xively cloud service.

3G + GPS shield:
http://www.cooking-hacks.com/documentation/tutorials/arduino-3g-gprs-gsm-gps/

I started of using the TCP client example for the shield and i am able to establish connection to the xively server:
char server[ ]="api.xively.com";
char port[ ]="8081";

I am not sure how to program the shield to write data to feeds using the Feed ID and API key.

Please Help...
« Last Edit: February 01, 2014, 08:10:16 am by Abelavit » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here are a few links that will help you out:

http://stackoverflow.com/questions/20929253/xively-status400-with-arduino-and-sim900-gprs-shield

the page above contains a link to the page below:


http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0#SoftwareSerial_library_Notes

-this page contains sample code—check the Send2Pachube function in the sample code …it will require a few changes though--- as specified on the following xively page


https://xively.com/dev/docs/api/communicating/sockets/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way, here is HTTP wrapped in JSON method for updating Xively datastream:

https://xively.com/dev/docs/api/communicating/sockets/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally managed to get the code working to update the humidity and temperature values.
The HTTP put method is stored in a string and finally sent as a packet to the TCP server.
Assuming the humidity and temperature are string variables.
Here is the code: 
Code:

int8_t answer;
int onModulePin= 2;

char aux_str[50];

char server[ ]="api.xively.com";
char port[ ]="8081";
String temperature = "2";
String humidity = "5";

String TCP_message ="{\"method\": \"put\",\"resource\": \"/feeds/***********/\",\"params\": {},\"headers\": {\"X-ApiKey\":  \"n49PBBOVhNjACyzWm6T5vuOwLXyudiYhtI5eoaE3pQVdrame\"},\"body\": {\"version\": \"1.0.0\",\"datastreams\": [{\"id\": \"Humidity\",\"current_value\": \"" + humidity + "\"},{\"id\": \"Temperature\",\"current_value\": \"" + temperature + "\"}]}}";

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(){
   
  char message_TCP [300];
  TCP_message.toCharArray(message_TCP, 300); // converting TCP_message to char inorder for it to pass through AT command

  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";
            String writeAPIKey = "n49PBBOVhNjACyzWm6T5vuOwLXyudiYhtI5eoaE3pQVdrame";
           
            sprintf(aux_str, "AT+TCPWRITE=%d", strlen(message_TCP));
           
            answer = sendATcommand(aux_str, ">", 20000);
              if (answer == 1)
            {
                sendATcommand(message_TCP, "Send OK", 20000);               
            }

            sendATcommand("AT+NETCLOSE", "OK", 20000);

        }
        else
        {
            Serial.println("Error opening the socket");   
        }
    }
    else
    {
        Serial.println("Error opening the network");   
    }

}

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;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i need you help

this code  in my case

Code:
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Starting...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make sure the network you are trying to use is not a GSM network. The shield only supports WCDMA and HSPA cellular networks. It seems like you are trying on a GSM network.
 
Logged

Pages: [1]   Go Up
Jump to: