Using an Arduino + Ethernet Shield to Update a ThingSpeak Channel
This tutorial demonstrates how to use the Arduino to read in the analog input and update a ThingSpeak Channel by sending an HTTP POST via the Arduino Ethernet Shield or the Arduino Ethernet (all-in-one). The analog input can be the output of a sensor, like a light sensor or temperature sensor.
ThingSpeak Client Arduino Sketch
This Arduino sketch takes the value of Analog Pin 0 and updates Field 1 of a ThingSpeak Channel using the ThingSpeak API.
Arduino_to_ThingSpeak.ino [Arduino 1.0+]
ThingSpeakClient.pde [Arduino 0023 or less]
Getting Started with ThingSpeak
- Sign Up for New User Account – https://www.thingspeak.com/users/new
- Create a New Channel by selecting Channels and then Create New Channel
- Enter the Write API Key in this sketch under “ThingSpeak Settings”
ThingSpeak API IP Address
The Arduino + Ethernet Shield does not have DNS by default, so you need to send data using an IP address. ThingSpeak has an API address dedicated for its API.
HTTP POST to ThingSpeak
One option to update a ThingSpeak Channel is to use HTTP POST. You can specify any number of fields, location, or status in one message.
Here is an example HTTP POST to ThingSpeak:
X-THINGSPEAKAPIKEY: (Write API Key)
Content-Length: (number of characters in message)
field1=(Field 1 Data)&field2=(Field 2 Data)&field3=(Field 3 Data)&field4=(Field 4 Data)&field5=(Field 5 Data)&field6=(Field 6 Data)&field7=(Field 7 Data)&field8=(Field 8 Data)&lat=(Latitude in Decimal Degrees)&long=(Longitude in Decimal Degrees)&elevation=(Elevation in meters)&status=(140 Character Message)
Included in the sketch is a watchdog timer for the Ethernet Shield. When 5 failed transactions are detected in a row, the Arduino re-initializes the Ethernet Shield (soft-reset). This will not solve every lockup, but it will make the setup more stable.
Update Multiple Fields
You can update multiple fields inside of a ThingSpeak Channel by adding on more fields in the update request. Here’s an example:
Use Short Field Names to Save Space
If you want to save space when making a ThingSpeak update, you can use short field names. “field1″ is “1″ and field2= “2″ and so on. Here’s an example of how the short field names would look in an Arduino update request for a ThingSpeak Channel:
The function “updateThingSpeak” is expecting a “String” as its input. So, if you use a “long” variable type, you need to convert the “long” into a “String” before passing it into “updateThingSpeak”.