Uploading Spark data to Xively


#1

Hello everyone, I have been trying to get this going for some time but can't seem to make it work and an external eye is most certainly required..

I've been following the code here but instead of growing that thread I thought I'd start a fresh one.

Here is my code:

#define FEED_ID "12345678" //note: fake id here.. 
#define XIVELY_API_KEY "i32i32i23i32i32i32i32i32i32ii32" //note: fake key here

TCPClient client;

int reading = 0;
int ledD = D0;
unsigned long LastUpTime = 0;
char whichApp[64] = "READ TEMPERATURE with XIVELY";

// This routine runs only once upon reset
void setup()
{
   //Register our Spark function here
  Spark.variable("whichapp", &whichApp, STRING);
  Spark.variable("reading", &reading, INT);
  Spark.function("degres", tempCalculation);
  Spark.function("volt", analogReading);
  pinMode(A0, INPUT);
  pinMode(ledD, OUTPUT);
  ledStatus(2, 100); //Blink
}

void loop()
{
  reading = analogRead(A0);
  int temp_calc = (reading*3.3/4095)*100 - 50;

   if (millis()-LastUpTime>2000)
   {
      xivelyTemp(temp_calc);
      LastUpTime = millis();
   }
}

void xivelyTemp(int temperature) {

   //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\" : \"/v2/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\" : \"bedroom_temp\",");
        client.print("          \"current_value\" : \"");
        client.print(temperature);
        client.print("\"");
        client.print("        }");
        client.print("      ]");
        client.print("    },");
        client.print("  \"token\" : \"0x123abc\"");
        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();
}


void ledStatus(int x, int t)
{
    for (int j = 0; j <= x-1; j++)
    {
        digitalWrite(ledD, HIGH);
        delay(t);
        digitalWrite(ledD, LOW);
        delay(t); 
   }
}

int tempCalculation(String command) {
    int tempCalc = (reading*3.3/4095)*100 - 50;
    return tempCalc;
}

int analogReading(String command) {
    return reading;
}

So I get the connection fine,my LED blinks 3 times to tell me that, but Xively doesn't receive any data.. Or doesn't appear to do.

Thanks for the help


Interface Spark Core with Xively
Adafruit MAX31855 [Ported]
#2

Self correction:
changed client.print(" \"resource\" : \"/v2/feeds/");

into this: client.print(" \"resource\" : \"/feeds/");

AND IT WORKS!!

Now the remaining issue is that it will stop working after a few minutes and that I get readings that are wrong.. but that's outside of the scope.


#3

Successfully tried the code. It is sending data to xively for more than two hours without interruption. Checked the formula with the data sheet and found no error. However I get a temperature of 58°C which should be 21°C.


#4

Try this: https://community.spark.io/t/temperature-readings-are-all-over-the-place/2388/2

courtesy of @BDub.. It did make it better for me. I still have a bit of work.

My code does crash after an hour or so still.. which is really annoying. I even tried to send an average every 5 seconds instead to avoid too much connection but still crashing.


#5

Good work guys!
I have tried it and it seems to work well besides the fact the temp is high however the data freezes after some time (hours). Not sure which part stops working but I either have to cycle the power or re-flash the core to get it going again. I increased the delay if (millis()-LastUpTime>200000) which may have helped. Any ideas?
Thanks!


#6

So has anybody got the Spark Core successfully sending sensor data though the internet to Xively reliably without the need to reset the Spark Core to maintain a consistent data output to Xively for graphing.


#7

Same here, my temp is almost 7 or 8 degrees too high and the upload will stop functioning after a few hours. I was hoping that the latest update from Spark would solve the issue (they said it would) but apparently not.. Disappointing.


#8

Our last update included some improvements for restarting the connection when the core went offline, but a lot of work is still happening to improve reliability on certain networks. We should be doing another merge / rollout of improvements next week.

Thanks!
David


#9

So actually after this morning reboot it didn't fail.. it's now about 12 hours later.. Crossing fingers..


#10

And NOT.. it seems that as soon as there is little bit of traffic either on the wifi or on the internet line (which is big at my place), the device just disconnects for some reason..


#11

apassemard - Since I eliminated the delay code back to 1000ms I have been holding a consistent WiFi connection all day even when I took the spark core in and out of range multiple times with sucessful reconnections to the WiFi network.

I'm curious to see if I ran your same code but linked to a Xivley account I set up if I would get the same results as you as far as not being able to maintain a connection to Xivley.

Do you think I could easily use your working code to see if I get the same results? I do know my I've been holding a good connection to the cloud for almost 24 hours now.


#12

you just changed this to

if (millis()-LastUpTime>1000) ?


#13

No, I was just saying that now that I have changed my LED Blink sketch delay to 1000ms I can have been able to hold a connection to the spark cloud for 24 hours now.

I'm loading your code above and setting up a Xivley account so I can see if I get the same results you are getting or if I can hold a stead connection to Xivley since I have been able to hold a stead wifi connection all day.

If I run your code and start getting Wifi Errors then we will know it has something to do with the code and how its affecting the Spark Core.


#14

I have the code up and running and I'm seeing the data being received by Xivley every few seconds.

I'll let this run and see if I can keep a constant reliable data feed to Xivley.

I have no temp sensor hooked up FYI.


#15

Ok I was pushing data to Xivley for 2+ hours and then tried to ramp up the data on my internet so downloaded a movie torrent and streamed a youtube video which caused my wifi access point which is my Verizon cell phone to cut the data. Since there was no data and no connection to the Spark Cloud but it was connected to the WiFi network it showed me the flashing Blue LED.

After the Blue Flashing LED started it never tried to reconnect to the Spark Cloud and it never tried to log back onto the WiFi network after I reset the Hot Spot on my Verizon cell phone.

So now I know what this Flashing Cyan Death Sign is all about smiley

Its pretty cool to see the data show up on the web though. It was really easy to setup.

Once they figure out how to fix the re connection issue this should be a pretty stable device.

A quick fix for now would be to simply insert some code that would rest the Spark Core every 5 - 10 - 20 - 30 mins to guarantee that the Spark stays connected to the WiFi Network since it always connects to the wifi network on startup.


#16

I've added this to my main loop:

   if (millis()-LastCloudCheck > 1000*60*5) { //check every 5 min to see if the connection still exists
       if(!Spark.connected()) Spark.connect();
       LastCloudCheck = millis();
   }

it seems to help, I haven't dropped since last night.


#17

Ok, although I maintained a constant connection to my Wifi and the Spark Colud based on the LED status of the Spark Core it still stopped sending data to Xivley eventually.

The blue LED stopped flashing that shows 3 blinks after a successful data transmission to Xivley.

I had to reset the spark core and then the data sent to Xivley resumed instantly.

There are Certainly issues here.


#18

I'll try adding your code also and see if it helps.

Can you provide a copy of your complete sketch? I'm trying to add your code but keep getting errors and I don't have a lot of time to figure it out. But I will run it and see if we get the same results.


#19
 #define FEED_ID "Xxxxxxxxxxxx"
 #define XIVELY_API_KEY "abcabcabcabcabcabcbacbacbacbacbcab"

 TCPClient client;

int reading = 0;
int ledD = D7;

int count = 0;
int total_temp = 0;
int temp_calc = 0;
unsigned long LastUpTime = 0;
unsigned long LastCloudCheck = 0;
char whichApp[64] = "READ TEMPERATURE with XIVELY";

// This routine runs only once upon reset
void setup()
{
   //Register our Spark function here
  Spark.variable("whichapp", &whichApp, STRING);
  Spark.variable("reading", &reading, INT);
  Spark.function("degres", tempCalculation);
  Spark.function("volt", analogReading);
  pinMode(A7, INPUT);
  pinMode(ledD, OUTPUT);
  ledStatus(5, 100); //Blink
}

void loop()
{
  reading = analogRead(A7);
  temp_calc = (reading*3.3/4095)*100 - 50;

       if (millis()-LastUpTime>1000)
       {
          if (count <= 5) {
              total_temp += temp_calc; 
              count++;
          }
          else {          
                  xivelyTemp(total_temp/count); //Send the average of the last 5 readings
                  count = 0;
                  total_temp = 0;
          }
          LastUpTime = millis();
       }

       if (millis()-LastCloudCheck > 1000*60*5) { //check every 5 min to see if the connection still exists
           if(!Spark.connected()) Spark.connect();
           LastCloudCheck = millis();
       }
}

void xivelyTemp(int temperature) {

  ledStatus(5, 100); 
   //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\" : \"bedroom_temp\",");
        client.print("          \"current_value\" : \"");
        client.print(temperature-8); //adjustment for some weird reason..
        client.print("\"");
        client.print("        }");
        client.print("      ]");
        client.print("    },");
        client.print("  \"token\" : \"0x123abc\"");
        client.print("}");
        client.println();

        ledStatus(3, 1000);        
    } 
    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();
}


void ledStatus(int x, int t)
{
    for (int j = 0; j <= x-1; j++)
    {
        digitalWrite(ledD, HIGH);
        delay(t);
        digitalWrite(ledD, LOW);
        delay(t); 
   }
}

int tempCalculation(String command) {
    int tempCalc = (reading*3.3/4095)*100 - 50;
    return tempCalc;
}

int analogReading(String command) {
    return reading;
}

#20

Thanks! I'm running it now.

Will update on how it goes.