Putting the “automation” in home automation with Arduino

Whether my kids or myself are to blame, it drives me crazy to see large electricity bills from cooling and heating the house when nobody’s actually in the house.

Looking for a solution to the problem and driven by “stream” technology, I decided to build my own smart home system to collect and monitor temperature, humidity, and motion in my house. As a platform, I chose the Arduino Uno board, and in less than a few days, I had a running system to collect the sensor data and visualize it in realtime.

However, visualizing the data was not enough. I needed a system that would track the data, understand normal patterns, and notify me when abnormal patterns emerged. For example, if there was no motion in the room, but the A/C was left on, the temperature and humidity would drop abnormally.

To solve this problem, I built on top of my existing IoT system and implemented two additional services:

  • PubNub: a realtime network to stream sensor data
  • Anodot: a smart platform to collect and automatically analyze data and alert when abnormal patterns are detected

Why Anodot and PubNub?

PubNub’s network provides a two-way data stream to my sensors (so later I can control devices in my home) and it has supported SDKs for Arduino and Node.js. PubNub also provides reliability, availability, and efficiency guarantees, so I don’t have to worry about data not being sent or received.

Anodot’s platform provides everything that I need for data visualization, analytics, and automated alerting based on its machine learning algorithms that automatically find abnormal patterns in my sensors’ data.

Here’s a block diagram of the system:

55d621261d20f-hp1

In action

The example below shows an anomaly that Anodot discovered. The temperature, motion level, humidity and barometric pressure from the living room are graphed in my Anodot dashboard.

The shaded area around each graph is the normal range Anodot’s algorithms learned. There is an anomaly in the temperature, humidity, and motion sensors happening at the same time – with the temperature in the room dropping significantly lower, while there is minimal motion in the living room. This is a clear indication of an A/C working without anyone around.

55d621626368e-hp2

Besides being alerted on sensor data sent, Anodot also provides the ability to create new signals that are functions of existing ones. For example, the dew point can be approximated using the temperature and relative humidity: DP = T – (100-RH)/5.

The graph below shows the dew point for my living room, calculated using Anodot’s analytics. Once defined, I can get anomalies on this new signal, and all without having to add an additional sensor or writing more code.

55d6219499d48-hp3

Making it all work

For the data collection system, I chose to use the Arduino board. The Arduino includes a wide variety of sensors and content, and the whole package is very affordable. Here’s a schematic of the design:

55d621b6bc5ce-hp4

For the base model, I used the Arduino Uno with the Ethernet shield. (Later I plan to replace it with Arduino Nano and the low cost ENC28J60 Ethernet adapter.)

To this base system, I connected the following sensors:

  • DHT11 temperature and humidity sensor
  • BMP180 for barometric pressure and temperature
  • HC-SR501 passive infrared motion sensor

I used the open-source DHT11 and Adafruit sensor libraries to read information from the BMP180 sensor and the DHT sensor.

The motion sensor is connected to an interrupt trigger that runs an attached function whenever it senses motion. This function sets a software flag to true.

void setup(){
.
.
    attachInterrupt(0,movement, HIGH);
.
}
volatile int movementFlag = 0;
void movement(){
    movementFlag = 1;
    return;
}

As you can see in the code below, every 20 seconds the device reads the temperature, humidity, barometric pressure and the motion status flag. It packs this information into a JSON object and publishes it to my PubNub channel by using the PubNub Arduino library.

void getTempAndHumidityJSON(char * pd){
    int TEMPHUMSENSORPIN = 5;
    int chk = DHT.read(TEMPHUMSENSORPIN);
    char humidity[5];
    char temp[8];
    char prs[8]
    float temperature;
    float pressure;
    bmp.getTemperature(&temperature);
    bmp.getPressure(&pressure);
    dtostr(DHT.humidity, 4, 1, humidity)
    dtostr(temperature,7 ,1 ,temp);
    dtostr(pressure/100.0F ,7 ,1 ,prs);
    sprintf(pd, "{\"temp\":%s,\"humidity\":%s,\"pressure\":%s,"\movement\":%d}",temp ,humidity ,prs , movementFlag);
    movementFlag=0; //reset after reported
}

void loop(){
    String response ="";
    char * pd = (char *) malloc(150);
    getTempAndHumidityJSON(pd);
    Ethernet.maintain();
    EthrenetClient * client;
    client = PubNub.publish(channel , pd);
    if (!client){
        Serial.println("publishing Error");
    }else{
        delete(pd);
        delay(20000);
    }
}

Client Side: Receiving and displaying the data

On the other side, I built two different clients that subscribe to the data channel. One is reading the information sent via PubNub and displaying it on a web UI in realtime. The other is a Node.js script that runs on Heroku, reads the information and sends it to Anodot.

Both scripts are using PubNub supplied libraries for their respective platforms (PubNub has more than 70 SDKs for every platform).

Here is the web interface:

55d622293d6a3-hp7

What’s next?

Smart home systems like the one I built, which collect and automatically analyze data, is a proof of concept demonstrating that the possibilities are endless with the technology that exists today. For example, the system can alert when home electricity deviates from normal patterns compared to others in a specific area. It could help understand if A/C is (or is becoming) inefficient compared to others so maintenance and improvements can be ordered. We can monitor motion and send alerts for possible home intrusions.

Advertisements

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