Project tutorial
Smart Home Sensor Node

Smart Home Sensor Node © GPL3+

Build a sensor node using Arduino/Genuino 101 which integrated with openHAB 2, which you can build a smart home control system in no time.

  • 9,149 views
  • 3 comments
  • 17 respects

Components and supplies

Apps and online services

About this project

Introduction

openHAM 2 can run on multiple platforms. I am building this project on Ubuntu 15.10 Willy (32 bit). This project is about making a sensor node using Arduino 101 and some sensors. The final product will look like the following image. openHAB 2 offers a single user interface to different form factors like desktop, tablet and phone. Only three sensors' readings are 'live', namely the temperature, humidity and photocell readings.

.

Prerequisites

openHAB 2 (follow here for installation on your preferred platform). I am installing openHAB 2 on Ubuntu 15.10 Willy (32 bit).

Setup the Arduino 101 and sensors

In this project, I make use of the DHT11 temperature and humidity sensor and photocell sensor. Arduino 101 is setup to use USB serial port to communicate with the openHAB2 which will be setup below.

The Arduino Sketch

The sketch is attached in the code section below. Inside the loop, temperature, humidity and photocell readings are collected and sent thru USB serial port. An openHAB 2 system is setup to received these readings and displayed on its dashboard.

 void loop() { 
 // Delay between measurements. 
 delay(delayMS); 
 // Get temperature event and print its value. 
 sensors_event_t event;   
 dht.temperature().getEvent(&event); 
 if (isnan(event.temperature)) { 
   temp = -1.0; 
 } 
 else { 
   temp = event.temperature; 
 } 
 // Get humidity event and print its value. 
 dht.humidity().getEvent(&event); 
 if (isnan(event.relative_humidity)) { 
   humid = -1.0; 
 } 
 else { 
   humid = event.relative_humidity; 
 } 
 light = analogRead(LIGTHPIN); 
 // print to serial 
 sprintf(buffer, "%.1f %.1f %.1f", temp, humid, light); 
 Serial.println(buffer); 
 //Serial.println(123); 
 // delay 
 delay(2000); 
} 

openHAB2 - initial setup

Start openHAB.

cd /usr/share/openhab2
sudo ./start_debug.sh

Open browser and point to http://localhost:8080 or http://IP-of-your-machine:8080

Select Standard package.

Select the PAPER UI.

Configuration of openHAB 2 and installing add-ons

Install Serial Binding and NTP Binding. These are for serial communication and getting the date time values.

Install Basic UI add-on. This will be the dashboard UI that we are going to see. It's corresponding sitemap configuration is listed the sitemap section below.

Add and configure Things in NTP Binding.

Using USB serial port

I am using a USB serial adapter. To use that, I need to add the following line to the file /etc/default/openhab2. The port should normally be /dev/ttyACM0 or /dev/ttyACM1.

EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyACM0"

Add and configure Items. Add a file named arduino.items to the directory /etc/openhab2/items with the following content. It describes the items that openHAB monitors and tracks. These items and its values can be displayed on a dashboard.

String	Arduino101	"Temperature [%s °C]"	{serial="/dev/ttyACM0@9600"} 
DateTime Date  "Date [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]"  { channel="ntp:ntp:local:dateTime" } 
Number	Temperature	"Temperature [%.1f °C]"	<temperature> 
Number	Humidity	"Humidity [%.1f %%]"	<humidity> 
Number	Light	"Light [%.1f L]"	<light> 
String	LightText	"Study Room Light [%s]"	<light> 

You can view these items in the PAPER UI view.

Add some rules. Add a file named arduino.rules to the directory /etc/openhab2/rules with the following content. This is the rules file. Input data transformation will be carried out here. For example, LightText will have the values of DARK, NORMAL or BRIGHT depending on the numeric value of photocell's readings.

import org.openhab.core.library.types.* 
import java.util.List 
rule "Send the temperature, humidity and light reading" 
when 
       Item Arduino101 received update 
then 
	var List<String> items = Arduino101.state.toString().trim().split(" ") 
	var Number value 
	//value = items.size() 
	value = Float.parseFloat(items.get(0)) 
	Temperature.postUpdate(value) 
	value = Float.parseFloat(items.get(1)) 
	Humidity.postUpdate(value) 
	value = Float.parseFloat(items.get(2)) 
	var String text 
	if (value > 800) { 
		text = "BRIGHT" 
	} else if (value > 200) { 
		text = "NORMAL" 
	} else { 
		text = "DARK" 
	} 
	LightText.postUpdate(text) 
end 

Add the Basic sitemap. Add a file named basic.sitemaps to the directory /etc/openhab2/sitemaps with the following content. This is the configuration file for the BASIC UI dashboard. The three 'live' items are Temperature, Humidity and LightText (see explanation above).

sitemap basic label="My home automation" { 
   Frame label="Date" { 
       Text item=Date icon="calendar" 
   } 
   Frame label="Demo" { 
       Text item=Temperature valuecolor=[>35="orange",>20="green",<=20="blue"] 
       Text item=Humidity valuecolor=[>55="orange",>45="green",<=45="blue"] 
       Text item=LightText valuecolor=[=="BRIGHT"="orange",=="NORMAL"="green",=="DARK"="gray"] 
       Group item=Heating icon="fan" 
       Switch item=Lights icon="light" mappings=[OFF="All Off"] 
       Text item=Multimedia_Summary label="Multimedia" icon="video" { 
           Selection item=TV_Channel mappings=[0="off", 1="DasErste", 2="BBC One", 3="Cartoon Network"] 
           Slider item=Volume 
       } 
   } 
} 

The Dashboard (BASIC UI sitemap)

Visit http://localhost:8080 or http://IP-of-your-machine:8080 and select BASIC UI to view the final dashboard, as shown below. The 'live' sensors are Temperature, Humidity and Study Room Light (see the red box in the image) as setup in section Setup Arduino 101 and sensors above.

Video

Follow this link to view a video to see the three sensors' readings go live. In the video, I used a hair dryer to increase the temperature. You will be able to see the temperature going up and then down. And I used a torch light to shine on the photocell and you will see the Study Room Light status changed to BRIGHT.

Summary

openHAB is easy to use and configure. It is packed with lots of function and features. Checkout its website to find out more.

Demonstrated in the project, the data flow is from sensors to openHAB. In fact, openHAB can send commands to devices/actuators such as turning on a light or lowering an ACs temperature.

This project uses serial binding for communication. Network binding with MQTT is a good option. But, I do not have a WiFi shield ready for this project. At the moment, the BLE integration is not finished yet. Work is still in progress for the BLE binding implementation.

Code

smart_home_sensor_node.inoC/C++
// smart howe sensor node

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define LIGTHPIN          0

#define DHTTYPE           DHT11     // DHT 11 

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

float temp, humid, light;
char buffer[20];

void setup() {
  Serial.begin(9600); 
  // Initialize device.
  dht.begin();
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  // Delay between measurements.
  delay(delayMS);

  // Get temperature event and print its value.
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    temp = -1.0;
  }
  else {
    temp = event.temperature;
  }
  
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    humid = -1.0;
  }
  else {
    humid = event.relative_humidity;
  }


  light = analogRead(LIGTHPIN);
  
  // print to serial
  sprintf(buffer, "%.1f %.1f %.1f", temp, humid, light);
  Serial.println(buffer);

  // delay
  delay(2000);
}

Schematics

Smart Home Sensor Node
Smart home sensor node bb rnltbqma9i

Comments

Similar projects you might like

Touch Sensor and Sound Sensor Controlling AC/DC Lights

Project showcase by Anbumachi

  • 1,262 views
  • 2 comments
  • 5 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 6,215 views
  • 4 comments
  • 12 respects

Grovey sLocks - Access Control Through A Smart Door Lock

Project tutorial by gerrikoio

  • 998 views
  • 0 comments
  • 6 respects

Alexa, Turn On THIS Lamp: Smart 3D Sensor for Amazon Echo

Project tutorial by 3 developers

  • 4,157 views
  • 6 comments
  • 26 respects

Smart Home - Smart Rules using ARTIK Cloud & Photon

Project tutorial by Raghavendra Ponuganti

  • 3,864 views
  • 2 comments
  • 12 respects

Make a Smart Mug Using LEDs and Arduino

Project tutorial by ElectroPeak

  • 3,170 views
  • 1 comment
  • 28 respects
Add projectSign up / Login