The Internet Of Things

IArduino with PIR Detector attended a sort of corporate symposia last week where my peers and I discussed the current technical state of affairs; most specifically to figure out how we could leverage this rapidly expanding realm of possibility.  One key area of discussion was “The Internet of Things.”  The basic idea is that more and more of the artifacts around us are becoming—at least in some sense—intelligent and interconnected!  Now I’m not necessarily talking about your toaster discoursing on Shakespeare with you, but even as I write that purposefully “absurd” phrase, it occurs to me that it isn’t so very absurd at all.  Everyday objects are being enhanced with more and more capabilities, and I have started to take advantage of this pleasing fact.

The project I’ll describe herein was extremely simple, but it should nonetheless be looked at as nothing less than profound.  I hooked up a Passive Infrared (PIR) sensor to an Arduino Uno R3 board then wrote a little bit of embedded code and a simple C#-based server.  From an effort basis, the whole thing was a breeze; something like 20 minutes start to end.  In a functionality sense, the whole shebang exists to capture motion events from the PIR, being sure to filter out repeats, then periodically forward those events onto my server.  Thereafter, anything could happen.  I could send out an email, maybe post a tweet or talk to a web service.  Importantly, I could also instruct the hardware to do react to my own signals, perhaps by moving a servomotor and/or updating a display.  The possibilities are quite frankly nothing less than endless.

In and over itself, the above does not add up to a revolution.  After all, people have been putting computers and hardware together for quite some time now.  The true revolution, though, comes from the exceptional ease of use and accessibility.

Aside from the cheap “off-the-shelf” hardware, all of the interesting bits are mediated by two very small bits of code.

Embedded Software (Sketches)

int pirPin = 7;
int delayInSeconds = 1;
long lastSend = -delayInSeconds * 1000;

void setup()
{
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  long now = millis();

  if (digitalRead(pirPin) == HIGH)
  {
    if (now > (lastSend + delayInSeconds * 1000))
    {
      Serial.println("MOVEMENT");
      lastSend = now;
    }
  }

  delay(200);
}

Server (C#)

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Diagnostics;

namespace Detector
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to terminate...");
            Console.WriteLine();

            var serialPort = new SerialPort("COM3", 9600);

            serialPort.DataReceived += (s, e) =>
                {
                    var data = serialPort.ReadLine();

                    // In a more useful program you could call an API,
                    // send an email, post a tweet, etc.
                    Console.WriteLine("{0} - {1}", DateTime.Now, data);
                };

            serialPort.ErrorReceived += (s, e) =>
                {
                    Console.WriteLine("ERROR: " + serialPort.ReadLine());

                    serialPort.Close();
                };

            serialPort.Open();

            Console.ReadKey();

            if (serialPort.IsOpen)
                serialPort.Close();
        }
    }
}

I highly recommend that you visit the Arduino Playground to see what sort of stuff you can do, especially if you’re a programmer. A word, of caution is in order though. The ability to both manipulate and respond to the real world can be highly addictive!

Do be sure to tell me where your explorations take you…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s