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.
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:
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”.