Introduction

Andrew Salvadore

First steps with IoT in your house

First steps with IoT in your house

In the past few years some really interesting hardware projects started getting more and more accessible to common mortals like me. Look at the Arduino platform and the entire ecosystem around it to see how different it is compared to a decade ago. There are pretty much sensors for anything you might want to measure and they are quite cheap too!

Bare esp8266 (no usb, DC converter, etc). Trying to run it on batteries is proving to be lots of fun!

By the end of this blog post you should be able to put together a temperature and humidity sensor that sends data to a home-assistant application running on a raspberry pi via MQTT.
You might ask, why would I want to record temperature and humidity in my house? Well, there are many things you could do with it. For examples you can:

Create a smart thermostat and control your heating from anywhere.

Evaluate your home energy efficiency in terms of heat dissipation.

Evaluate how quickly your heating can raise the house temperature by 1°C when outside is -5°C .

Figure out how daily activities like cooking or drying the laundry has an impact on how comfortable the home feels.

Maybe create a little weather station outside your house and contribute data to your own locality ?

Looking for a good IoT controller

I started with Arduino and quite quickly looked for some cheap alternatives with WiFi support. That is when I found the esp8266. A super cheap micro controller with WiFi integrated and full TCP/IP stack. Having tiny WiFi devices like that really enables a lot of interesting use-cases.

Overview

The hardware

The sensor will be controlled using a NodeMCU controller based on the ESP8266 to make USB connection and powering easier.

Collection and analysis will be done on a Raspberry pi version 3 with home assistant.

You can easily get all the hardware above on eBay / Amazon.

Programming the controller

Let's start with the NodeMCU programming to read a sensor. For this purpose you need to either use the Arduino IDE or lately I started using platformio which has some great tools like CLI and IDE integrations.

Install platformio

For the rest of this tutorial we'll use platform.io, so go ahead and download the IDE or simply install the core using brew:

brew install platformio

Create a new folder for your sensor project. I will refer to it as ovo-sensors.

The very next thing we are going to do is to use a handy little framework for the esp8266 called homie. Homie has some really handy features to deal with WiFi and MQTT connection / reconnection but also exposes a JSON API to configure the device and do other fancy things like OTA (Over The Air) updates of the firmware. Check the full list here

Initialize the project

So go back to the ovo-sensors and initialize your project

platformio init --board nodemcuv2

Add the dependencies

After running the command above you should have a file called platformio.ini. Add the lib_deps = Homie line at the end of the section

Setup the raspberry pi

Install the image

You can use the raspbian lite image for this as we don't really need any UI. There is a really handy trick to enable the raspbian ssh so you'll never need to plug in a screen / keyboard, just the network cable.

Install home-assistant

Home assistant runs on python 3. My preferred way to run it is using virtualenv. Here is a hacky script you can use for that.

Configure home assistant

Home assistant uses a yml file to drive the entire system. Everything is defined in this one file. You can obviously split it into multiple smaller files but that is up to you. For our configuration we will need

An MQTT server. You can either use the built in one or point to an external one. The passwords can be stored in an exernal file called secrets.yml and read using the !secret <key> syntax.

MQTT sensors to read the data from.

A database to store the data. I use postgres running locally but you can omit the db_url configuration and simply use the SQLite default database engine.

Conclusions

With this basic setup you can now easily add other sensors, create rules and complicated workflows to automate your home. I have a number of sensors in my house and control the heating and some lights using essentially this basic setup.
The next steps (maybe posts?) could be

Expose home assistant to the internet using DDNS to access it when you are out of the house.