# Uploading sensor data to Thingspeak

**URL:** <https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497>\
**Category:** Project Share\
**Created:** [July 2, 2014, 2:38pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497 "2014-07-02T14:38:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccrumpacker](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ccrumpacker/32/2480_2.png) [@ccrumpacker](https://community.particle.io/u/ccrumpacker)\
**Post date:** [July 2, 2014, 2:38pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/1 "2014-07-02T14:38:10Z")

</div>

[[Repost from my blog]](http://crumpspot.blogspot.com/2014/07/uploading-sensor-data-to-thingspeak.html)

I was able to get the Spark Core to upload data to Thingspeak after looking at some of the examples for using the arduino with the Wifi shield. a few things to note, I needed to put a slight delay after the stream or the spark would close the TCP connection too quickly. 100 ms seemed to do the trick. Also while my example has a delay to keep updates to Thingspeak I suggest using timers because over time the delay (which is blocking) will kill the Spark Cloud connection.

I set up a Sin wav as fake sensor data just to make a cool looking chart. I also setup an inverse of it to demo two streams. Below is the output chart and the example code.

![](https://sea2.discourse-cdn.com/flex026/uploads/particle/2478/f6d4ee86e2ee186f.jpg)

```
 #include "math.h"
// Thinkspeak channel information
String writeAPIKey = "<<Enter your channel's API key here>>";
String channelID = "<<Enter your channel's ID here>>";

// TCP socket initialize
TCPClient client;

float x;
float y;

/*--------------------------------------------------------
Setup
--------------------------------------------------------*/
void setup()
{
    Serial.begin(9600);
    delay(10000);
    Serial.println("===Starting===");
}

/*--------------------------------------------------------
Main loop
--------------------------------------------------------*/
void loop() 
{
    if(Spark.connected())
    {
        for (float z = 4.712; z < 10.995; z = z + .15) // "z" sets the sin wave to the first zero crossing 4.712 and ends it on the next 10.995.
        {
          x = sin(z) * 127.5 + 127.5; // Making the sin wave all positive numbers and setting it to a scale of 0-255 (makes it easy to PWM an LED)
          y = 255 - (sin(z) * 127.5 + 127.5); // This inverts the sin wave so we have two streams.
          
          // Must convert data to Strings, make sure you use capital "S" in Strings
          ThingSpeakUpdate("field1="+String(x)+"&field2="+String(y));
          
          // I put this delay in place so we don't flood Thingspeak but you should really use a timer, delays screw with the sparkcloud connection some times.
          delay(15000);
        }
    }
}

/*------------------------------------------------
Sends sensor data to Thingspeak
Inputs: String, data to be entered for each field
Returns: 
------------------------------------------------*/
void ThingSpeakUpdate(String tsData)
{
    Serial.println("Date string: " + tsData);
    
    Serial.println("...Connecting to Thingspeak");
    
    // Connecting and sending data to Thingspeak
    if(client.connect("api.thingspeak.com", 80))
    {
        Serial.println("...Connection succesful, updating datastreams");
        
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(tsData.length());
        client.print("\n\n");
        
        client.println(tsData); //the ""ln" is important here.
    
        // This delay is pivitol without it the TCP client will often close before the data is fully sent
        delay(200);
        
        Serial.println("Thingspeak update sent.");
    }
    else{
        // Failed to connect to Thingspeak
        Serial.println("Unable to connect to Thingspeak.");
    }
    
    if(!client.connected()){
        client.stop();
    }
    client.flush();
    client.stop();
}

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [July 2, 2014, 2:52pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/2 "2014-07-02T14:52:34Z")

</div>

@ccrumpacker, the delay() function only “blocks” the user code but it does call SPARK\_WLAN\_Loop() during the wait. 😄

---

<div class="post-metadata">

**Author:** ![ccrumpacker](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ccrumpacker/32/2480_2.png) [@ccrumpacker](https://community.particle.io/u/ccrumpacker)\
**Post date:** [July 2, 2014, 3:05pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/3 "2014-07-02T15:05:00Z")

</div>

Ahhhh ok i found a few reference to it messing up if used for an always on project. Thanks for the heads up.

---

<div class="post-metadata">

**Author:** ![v8dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/v8dave/32/2616_2.png) [@v8dave](https://community.particle.io/u/v8dave)\
**Post date:** [July 4, 2014, 12:52pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/4 "2014-07-04T12:52:35Z")

</div>

I am running this code but I am not seeing regular connections. I check millis() to see if I am ready to send (every 60 seconds) but I don’t see the ThingSpeak update on a minute basis and yet I see the message on the LED display that it is sending every 60 seconds or at least trying to. I don’t get any timeouts or failures that I can see.

---

<div class="post-metadata">

**Author:** ![ccrumpacker](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ccrumpacker/32/2480_2.png) [@ccrumpacker](https://community.particle.io/u/ccrumpacker)\
**Post date:** [July 4, 2014, 1:30pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/5 "2014-07-04T13:30:29Z")

</div>

Can I see your code?

---

<div class="post-metadata">

**Author:** ![v8dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/v8dave/32/2616_2.png) [@v8dave](https://community.particle.io/u/v8dave)\
**Post date:** [July 4, 2014, 1:45pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/6 "2014-07-04T13:45:10Z")

</div>

Apart from the following in loop(), the rest is as posted above.

```
//
//See if time to send next update to ThingSpeak
//
currentTime = millis();

if(currentTime > thingTime)
{
    thingTime = currentTime + 60000; // Every minute
    
    if(Spark.connected())
    {
        sprintf(info, "field1=%0.1f&field2=%0.1f", celsiusU,celsiusL);

        ThingSpeakUpdate(info);
    }
}

```

thingTime and currentTime are declared as unsigned long.

---

<div class="post-metadata">

**Author:** ![ccrumpacker](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ccrumpacker/32/2480_2.png) [@ccrumpacker](https://community.particle.io/u/ccrumpacker)\
**Post date:** [July 4, 2014, 1:52pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/7 "2014-07-04T13:52:36Z")

</div>

Can you serial print the “tsData” and tsData.length() to verify the string is correct and the length is fine. Also if the field data is too long you can have issues. I kept my floats to 2 decimal places like this. String(floatVar, 2)

---

<div class="post-metadata">

**Author:** ![v8dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/v8dave/32/2616_2.png) [@v8dave](https://community.particle.io/u/v8dave)\
**Post date:** [July 4, 2014, 2:04pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/8 "2014-07-04T14:04:01Z")

</div>

The string is fine. It’s the same one I send on UDP as my device is located away from the computer. It’ appears fine over UDP.

That’s why I Iike this system as I can update without dragging my laptop around.

It’s only 2 sets of data and as you can see I limit it to 1 decimal place.

---

<div class="post-metadata">

**Author:** ![v8dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/v8dave/32/2616_2.png) [@v8dave](https://community.particle.io/u/v8dave)\
**Post date:** [July 4, 2014, 2:12pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/9 "2014-07-04T14:12:22Z")

</div>

Here’s a link to the ThingSpeak data. It should be every 60 seconds but seems to vary but is always some multiple of 60 seconds or there abouts.

[https://thingspeak.com/channels/14333](https://thingspeak.com/channels/14333)

---

<div class="post-metadata">

**Author:** ![ccrumpacker](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ccrumpacker/32/2480_2.png) [@ccrumpacker](https://community.particle.io/u/ccrumpacker)\
**Post date:** [July 4, 2014, 2:21pm UTC](https://community.particle.io/t/uploading-sensor-data-to-thingspeak/5497/10 "2014-07-04T14:21:07Z")

</div>

Oh ic. I thought you meant it never updated. Yeah I reported that same issue, I think there is some issue with the spark loop when using this sort of timer. I was hoping someone has an idea here;

> **[Using the Spark.sleep() and WiFi.status() as a simple timer](https://community.particle.io/t/using-the-spark-sleep-and-wifi-status-as-a-simple-timer/5498)**
>
> I was looking for a way to setup a simple timer that would allow me to upload data to a web service at intervals, then shut the wifi radio down but continue to collect sensor data. After monkeying with ways to run timers like: int updateFreq =...
