Project: Arduino YUN and sending data to ThingSpeak with POST

Arduino YUN is very powerful tool especially with data acquisition services like ThingSpeak. Today we had urgent need to get some data from sensors, and running simple sketch and making data available through ThingSpeak was a breeze:

Project: Arduino Yun sending data to TS

First, documentation for Bridge library leaves out a lot details, so getting curl to work was not an instant hit. Nothing specially hard, just required few tries.

You need a ThingSpeak account, and create there new channel for a data. ThingSpeak is a service for collecting data from many sources, right now (Apr 2014) it looks like it is free to use, but how long it will last? I’ve seen few similar services, and at some point You need to get money for a such service… Anyway, I’ll show You how to connect to ThingSpeak, but if You need to connect to different service You will have all needed information, probably You will have to change URL format.

On OpenWRT side You don’t have change anything, just configure it to have access to internet. Using Bridge Process class we will curl to pass our data to web service. What is curl?

curl is a command line tool for transferring data with URL syntax

or in other words, with this simple program You can call any URL, pass data and read response – all that goodies from shell level. And curl is a part of OpenWRT on Arduino YUN.

Arduino YUN: sending POST requests

Bridge library has HttpClient class for dealing with HTTP GET methods, however most webservices require You to send data with POST (GET is for retrieving data). We will use Process class to run curl command on OpenWRT, and this command will send our request.

curl syntax is: curl OPTIONS URL. URL for our API is (BTW it looks like TS does not provide HTTPS for uploading data…). OPTIONS has include two information: data we want to send to API and fact it should be POST. It will be enough to use --data PAYLOAD as an option. PAYLOAD has carry information about data, and switch --data tells curl to send POST request instead of default GET. ThingSpeak expects for each channel data set, and without any configuration on TS side first data should be encoded as field1, second as field2 and so on… URL encoding rules are (as long we talk about numeric values): NAME=VALUE&NAME2=VALUE2, so it is a list of pairs NAME/VALUE with = between. Sign of & is used as field separator.

Let’s look at example. Two values to send to ThingSpeak – -20.2 and 34.2 will be encoded as field1=-20.2&field2=34.2. To allow ThingSpeak which channel You want to write to You have to provide API write key (You can read it on API Keys tab on channel page on ThingSpeak) and provide it encoded in PAYLOAD with name key. Whole curl command will be: curl --data "key=XYZ123ABC&field1=-20.2&field2=34.2" Did You notice ” surrounding encoded parameters? It is important to add them, otherwise & sign will be interpreted by OpenWRT shell as a special character.

Function to send values to ThingSpeak will be:

void postToThingSpeak(String key, float value[]) {
  Process p;
  String cmd = "curl --data \"key="+key;
  for (int i=0;i<ARRAY_SIZE;i++) {
    cmd = cmd + "&field"+ (i+1) + "=" + value[i];
  cmd = cmd + "\"";

As You probably see it expects two arguments. First is ThingSpeak channel API write key, second one is array of numeric values. In case You need more values to send to TS just change ARRAY_SIZE define to have it done.

Whole sketch code reads humidity and temperature from DHT22 sensor and environmental information from gas sensors MQ5 and MQ9, and You can examine it on this gist.

Sensors getting data for this example or power of Internet of Things with Arduino YUN

If You are curious here is a quick look at sensors providing data for that channel, take a look:

Sensors providing data for ThingSpeak
Sensors providing data for ThingSpeak

There will be more sensors soon, and we will deploy similar devices in few places – this is ongoing project, it was quickly started it took less than two days from idea of this station to finishing this article. Of course – not two days working on this, we were doing other things too, so this was side project, I guess it can be completed in few hours. This is really demonstration how fast You can prototype with Arduino YUN…