Arduino & The Internet of Things

If you tinker with technology then chances are you already know what the “Internet of Things” is all about. If you don’t know, then my question to you is, where have you been!?

For those of you not in the know, here’s a quick primer.

“The Internet of Things is a network of Internet-enabled objects, together with web services that interact with these objects. Underlying the Internet of Things are technologies such as RFID (radio frequency identification), sensors, and smartphones.”[1] 

One way that folks put this concept into practice is the set up their “things” so they, like their owners, can update their status.  I like to think of it as Twitter for my stuff.  One site that gives your devices a place to update their status information is called Pachube (pronounced “PATCH-bay” [2]).  I’ve been using Pachube for a couple of years to report health data from my servers but I’ve been wanting to experiment with building a microcontroller based stand-alone device that could use sensors to gather environmental data and then report that data to Pachube without the need for a computer.

A microcontroller (in this context) is a pre-built device consisting of circuitry on a single printed circuit board (PCB) such as microprocessor, I/O circuits, clock generator, RAM, stored program memory, etc. The idea behind the microcontroller is that it is immediately available and useful to an application developer, without needing to spend time and effort in developing the controller board itself [3].

In the case of an Arduino, however, the idea is to make it easy to prototype solutions by building your own circuits and devices and proving them before committing your device to a PCB of it’s own or going into large-scale production

There are lots of microcontrollers on the market such as the Arduino, BASIC Stamp and others. I’ve been reading about the Open-Source Arduino Platfrom for a long time and had been looking for an excuse to buy one and expirament. To get my feet wet, I decided to buy the latest Arduino board, some photocells, a nice selection of resistors, and a simple breadboard.

Building The Circuit

When I received the parts, my first task was to build a very simple circuit. Since a photocell is basically a resistor, the easiest way to get a measurement was to connect one end to 5V and the other to a pull-down resistor to ground. Now all I had to do was make a connection from analog input to the point between the fixed pull-down resistor and the photocell.

Now we have a complete circuit and should be able to build a simple sketch (firmware program) to read the data and pump it to our serial port.


Writing The Sketch

In Arduino parlance, a sketch refers to the program that is written and uploaded to the ATMega chip (aka the firmware) that tells the Arduino what to do.

The easiest way to get up and going is to download the Arduino IDE from the Arduino website.  The IDE makes it easy to compile and upload our code and at this point, easy is what we want.

int ldrPin = 0;
void setup() {
void loop () {

Our sketch is very simple.  We start by defining the location of our photocell which in this case is Pin 0. We then open the serial port at 9600 baud and then start a perpetual loop that reads the voltage from the photocell, waits 1500 milliseconds (1.5 seconds) and then sends the result to the serial port.

When we read the analog port the value that is returned is a value between 0, representing zero volts, and 1024, representing five volts. I could easily use that number but I want to convert the number to actual voltage which I do by dividing the value returned by the analogRead function by 204.8.

Reading and Posting

As I stated in my opening, the goal with the Arduino is to have a stand-alone device that can post data to Pachube (or anywhere for that matter) independently.  Since we’re just experimenting with the fundamentals, in this instance, we’ll leave our Arduino connected to the computer to read the data and then post it.

On my Mac I opened a Terminal session and navigated to /dev to look for the device name of the Arduino.  In my case it’s cu.usbmodem621. To validate that we’re getting data, I opened a read-only connection to see what I would get (be sure to close the Arduino IDE first).

cat /dev/cu.usbmodem621

If you receive a value on a new line every 1.5 seconds then everything is functioning correctly.  Since I’ve been using Pachube for a while, I already have my API Key which is necessary to programmatically update the Pachube feed.  If you don’t have an account already, go to, set one up and grab your API Key.

Now that we know our device name, know that we’re getting data and have our Pachube API Key, we can now write our script.

You can use any scripting language to accomplish the task of reading serial data from the Arduino and then making the necessary calls to post the data.  My preference is PHP, so that’s what I’ll use here.

#!/usr/bin/php -q
$handle = fopen("/dev/cu.usbmodem621","r");
$str_ldr = fgets($handle);
$mintime = time();
$str_ldr = fgets($handle);
$str_ldr = trim( preg_replace( '/s+/', ' ', $str_ldr ) );
if( time()> $mintime+60) {
echo $str_ldr . "n";
echo exec("curl --request PUT --header "X-PachubeApiKey: 11112222333344445555566666777778888899999" --data "$str_ldr" "" --silent");
$mintime = time();

First thing we do is open a connection to the Arduino and read in some data and then wait for 5 seconds. You may have noticed that the first line of data you get from the device after you open a fresh connection is not always clean. Using this method allows the connection to “settle” before we start grabbing data to post.

Next we start an infinite loop to grab a line of data, strip the CRLF, call out to cURL to post our data, wait 60 seconds and then do it all over again.

The image below shows a Pachube 24h graph.

Pachube Graph


I was surprised how easy it was to prototype with the Arduino and to have a working solution up and running with so little effort. My next goal will be to add new sensors to extend my home automation system (track real-time electricity, gas and water usage) and then add an Ethernet Shield to remove the need for a computer to post my data.

Until next time – GEEK OUT!!


Leave a Comment

Your email address will not be published. Required fields are marked *