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, does anyone already tried to adapt this to the Core?
Any suggestion on how to port this?
Thank you,

1 Like


Based on this I have developed the code below that connects to 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,

#define FEED_ID "YOUR_ID"

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

void setup()
    // Initialize D0 pin as output
    pinMode(led, OUTPUT);
    // Blink
    ledStatus(2, 500);

void loop() 
    int iTemperature = 0;
    int iHumidity = 0;
    // Get values
    iHumidity = 10+i;
    iTemperature = 20+i;
    //Serial.println("Connecting to server...");
    if (client.connect("", 8081)) 
        // Connection succesful, update datastreams
        client.print("  \"method\" : \"put\",");
        client.print("  \"resource\" : \"/feeds/");
        client.print("  \"params\" : {},");
        client.print("  \"headers\" : {\"X-ApiKey\":\"");
        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("        },");
        client.print("        {");
        client.print("          \"id\" : \"Temperature\",");
        client.print("          \"current_value\" : \"");
        client.print("        }");
        client.print("      ]");
        client.print("    },");
        client.print("  \"token\" : \"0x12345\"");
        ledStatus(3, 500);        
        // Connection failed
        //Serial.println("connection failed");
        ledStatus(3, 2000);
    if (client.available()) 
        // Read response
        //char c =;

    if (!client.connected()) 
    delay (5000);

void ledStatus(int x, int t)
    for (int j = 0; j <= x; j++)
        digitalWrite(led, HIGH);
        digitalWrite(led, LOW);

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;

    iTemperature = 20.0+i;

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:

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)

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
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”,");
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:


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.