Sunday, June 26, 2011

The "Internet of Things"

On an older post I mentioned that I have migrated the output of the sensors on my weather station to thingspeak. Thingspeak.com is a service that make an API available to you that takes data from your sensors and then makes it available for your (and others if you so desire) use in ways that go from chart building or any other customized way you can think of.

There's a lot of potential in that. For instance, in my case, I used to generate my own charts (this link may not be active at the time you read this) for my weather station. It worked but in a way it was a waste of my time. I spent ma lot of time writing the code to generate the charts(even though I used a graph generating library I still had to prepare the data to be displayed - things like fetching the data from the database, extracting the highest, calculation of averages and things like that). I wanted to concentrate on the hardware, the Arduino programming and the analysis
 of data. For my own applications, this is where thingspeak.com came in.

The idea is simple: make it easy for the user to upload sensor data to a server and then offer tools to manipulate that data while at the same time offering easy ways to make the raw data available in the case the user want to actually do something more customized that what can be done using the tools of the website.

To achieve the above, thingspeak developed an open source API that takes your data and makes it available to you later. Like in any other API, you need to follow a protocol (a way to interact with the API to post and retrieve data).

The API interaction is extremely simple; I was up and running in literally 5 minutes. To start, follow this simple steps (I use PHP to interact with the API, there's some other tutorials to teach you how to do the same with other platforms, including how to do it directly from the Arduino):

1. create an account on the thingspeak.com website.

2. login and go to the channels tab

3. click the "create new channel" button

4. change the channel name to something that makes sense to you and for others (this will later show up on the list of available channels) - in my case I chose "Weather Station Macau SAR, China".

5. go through the list and input other data as you see fit - you can always go back and edit the information. Click on the "Make Public?" check box to make the data public and therefore giving others the chance to use your data on their own projects.

6. then, add your sensors to the Field list. For example, if you have only one sensor that is a temperature sensor, add "Temperature" to the field 1 text box. Then click "Create Channel".


At the end of the steps above you have created a channel that can be thought of as an opening door to your "region" of the thingspeak server. The next step is to use the API to load data to that "region".

To add data do the following (using PHP):

1. on thingspeak.com return to the channels page

2. note the box on the right on the channels page and note the section "Sending Data" and specifically the following line: 
http://api.thingspeak.com/update?key=YOUR_KEY_HERE&field1=0

3. Make a note of the sequence of characters that make up your API keys and identified above as "YOUR_KEY_HERE"

4. on your PHP code add the following lines:
$post_thingspeak = "http://api.thingspeak.com/update?key=YOUR_KEY_HERE&field1=".$temp.""; $response = file_get_contents($post_thingspeak); echo "| thingspeak.com reply: $response"; Above, $temp is the variable that holds the temperature value. Replace "YOUR_KEY_HERE" by the characters that you noted on step 3.

And that is all you need to post to the API. After these steps are complete, it might be useful for debugging purposes to check your channel feed by going to the "Viewing Data" link on the channels page and checking the raw data being sent to the thinkspeak server. By the way this is also how you would access the data to be used on your own applications, but this is a subject for another post.

Hope this helps to cut the time of using the thingspeak.com service using PHP from the 5 minutes it took me to 4 minutes.

No comments: