Interface Spark Core with Xively

Dear all,

I would like to realize a system that permits me to log data a display graphics of environmental sensors connected to the Core.
I was thinking to Xively, since it seems pretty easy to be used instead of developing a personal solution.
I was looking to this tutorial, https://xively.com/dev/tutorials/arduino_wi-fi/ does anyone already tried to adapt this to the Core?
Any suggestion on how to port this?
Thank you,
dk

1 Like

Hello,

Based on this http://xively.com/dev/docs/api/communicating/sockets/ I have developed the code below that connects to api.xively.com and updates two datastreams such as Humidity and Temperature.

Unfortunately I had some issue using the serial output and flashing the core, so I used ledStatus connecting a led to pin D0.

I don’t know if I have used the functions TCPClient functions correctly so please feel free to review and make corrections or improvements of the code below. If you have further ideas or better ways to connect to xively please feel free to suggest as well.

The code seems to work… I have noticed that each 4, 5 iterations the core starts blinking green, blinking blue, than gets the connection again and everything continues (the i counter does not start from 0).

Thank you for your support and advices,
dk

#define FEED_ID "YOUR_ID"
#define XIVELY_API_KEY "YOUR_KEY"

int led = D0;
int i = 0;
TCPClient client;

void setup()
{
    // Initialize D0 pin as output
    pinMode(led, OUTPUT);
    // Blink
    ledStatus(2, 500);
    //Serial.begin(9600);
    //Serial.println("Started");
}

void loop() 
{
    int iTemperature = 0;
    int iHumidity = 0;
    i++;
    
    // Get values
    iHumidity = 10+i;
    iTemperature = 20+i;
    
    //Serial.println("Connecting to server...");
    if (client.connect("api.xively.com", 8081)) 
    {
        // Connection succesful, update datastreams
        client.print("{");
        client.print("  \"method\" : \"put\",");
        client.print("  \"resource\" : \"/feeds/");
        client.print(FEED_ID);
        client.print("\",");
        client.print("  \"params\" : {},");
        client.print("  \"headers\" : {\"X-ApiKey\":\"");
        client.print(XIVELY_API_KEY);
        client.print("\"},");
        client.print("  \"body\" :");
        client.print("    {");
        client.print("      \"version\" : \"1.0.0\",");
        client.print("      \"datastreams\" : [");
        client.print("        {");
        client.print("          \"id\" : \"Humidity\",");
        client.print("          \"current_value\" : \"");
        client.print(iHumidity);
        client.print("\"");
        client.print("        },");
        client.print("        {");
        client.print("          \"id\" : \"Temperature\",");
        client.print("          \"current_value\" : \"");
        client.print(iTemperature);
        client.print("\"");
        client.print("        }");
        client.print("      ]");
        client.print("    },");
        client.print("  \"token\" : \"0x12345\"");
        client.print("}");
        client.println();
        
        ledStatus(3, 500);        
    } 
    else 
    {
        // Connection failed
        //Serial.println("connection failed");
        ledStatus(3, 2000);
    }
    
    
    if (client.available()) 
    {
        // Read response
        //char c = client.read();
        //Serial.print(c);
    }

    if (!client.connected()) 
    {
        //Serial.println();
        //Serial.println("disconnecting.");
        client.stop();
    }
    
    client.flush();
    client.stop();
    
    delay (5000);
}

void ledStatus(int x, int t)
{
    for (int j = 0; j <= x; j++)
    {
        digitalWrite(led, HIGH);
        delay(t);
        digitalWrite(led, LOW);
        delay(t); 
   }
}
2 Likes

thank you! I have been playing around trying to get this to work with HTML but haven’t had any luck.

This code works for me but it seems to cause the spark core to freeze up. Every so often it will go from cyan to flashing green and then back to cyan but when it does this the program stops and won’t start again until the core is reset. Anyone have any ideas about that?

never mind figured it out. It was the long loop time causing it to hang. replacing the delay(5000); with a check against millis() seems to have fixed it.

   void loop() 
{
    if (millis()-lastUp>5000)
    {
    float iTemperature = 0;
    
    i++;

    
    iTemperature = 20.0+i;
    Xively_temp(iTemperature);
   }
   }

Hi, thank you for testing it!
Hum… I try to remove the delay and test it again later, any ideas why this issue with the delay?

EDIT: found the issue: https://community.spark.io/t/known-issue-long-delays-or-blocking-code-kills-the-connection-to-the-cloud/950

1 Like

The code seems to work on my Spark, I’ve got the blinking LEd and everything but no data in Xively. One thing I find surprising os the port you connect to 8081… Shouldn’t you be connecting with SSL through 443? (which I don’t know how to do)
Thanks

Update: Apparently the Spark Core cannot handle HTTPS requests so you need to use an intermediary. That said the 8081 port is strange… should work on port 80 though.

Update2: Definitely no data is going into anything in Xively… hard to debug. Any tips? Would you be able to post your entire code? I’ve solve the hanging issue as well - that’s a nasty bug…

Hi, the code is based on the information found on the Xively knowledge base.
The port is the one reported here http://xively.com/dev/docs/api/communicating/sockets/.
Make sure the you have placed the correct FEED_ID and XIVELY_API_KEY and make sure you changed the label of datastreams with the name you used in:
client.print(" “id” : “Humidity”,");
and
client.print(" “id” : “Temperature”,");

I suggest as well to reduce all the “delay” under 2000 otherwise it will stuck.
Let me know if it works for you.

hey d82k, thanks for the link… It does seem pretty straightforward so I’m not sure what I’m doing wrong. It’s hard to debug as well…

I’ve actually started a new thread with my code here: https://community.spark.io/t/uploading-spark-data-to-xively/2379

thanks!

It works now… I was trying to be smart and replace the end point by /v2/feeds/ instead of just /feeds/ like the doc says and shouldn’t have.

My readings are completely unstable but I do get the data in Xively.