Tech, hardware, raspberryPi, iot

How to Automate Your Home. Dev Style

What can a developer do with a LEGO Power Functions Motor Set (which includes medium motor, battery box, switch, plus cable with 2 LEDs) and a motor shield?

It didn't take long for me to figure out the answer, once I got the above-mentioned gear from my fellow Around25-ers a few years back. This is when my whole IoT and home automation journey kicked off.

Since then, I kept playing around with automating stuff around my home and now it's time to share all about turning devices into IoT projects. With the help of a Raspberry Pi 3 and a bit of code, that is.

First, I'll walk you through a short intro on the prerequisites for the first IoT projects, then I'll reveal how I built my very first IoT gear.

Prerequisites

What I used for a start is this: an ESP8266 module - the NodeMCU development board -, then the next step was ordering some sensors, relays and NodeMCU boards.

What is a NodeMCU?

It stands for Node MicroController Unit - an open source software and hardware dev environment for an inexpensive System-on-a-Chip (SoC) called the ESP8266.

The ESP8266 presents all crucial elements of a computer:

  • CPU;
  • RAM;
  • wireless;
  • an operating system;
  • SDK.

As a chip, the ESP8266 is quite ... cheap. The only thing is, it’s also quite hard to use or upload code to it if you want to experiment with IoT.

So for my projects, I used ESP8266 12-E NodeMCU Kit – one of the most frequently used dev boards for ESP8266 - which has:

  • 4MB flash memory;
  • 11 general-purpose input/output (GPIO) pins;
  • 1 analog-to-digital converter (ADC);
  • an integrated voltage regulator - you can power it up on the mini USBor even use the VIN pin or 3.3v ones.

Board Setup

Now let's get to the nitty-gritty of this whole process: how to set up your board.

First, the operating system: I worked on a MAC (for Linux it should be the same) but if you have a Windows device, you’ll need to install pip  and Python.

Then, installing the USB to serial driver, depending on your NodeMCU. In my case, I used these:

Take into account you'll also need pin mapping, which looks something like this (find more details here):

Firmware

To install the firmware on the NodeMCU board, you'll have to go to NodeMCU build site. Besides what’s standard, I also used:

  • am2320 and dht (for temperature and humidity sensors);
  • the http module (provides an interface to do GET/POST/PUT/DELETE);
  • the mqtt module.

Once you select your modules and enter your email address, you just click Start your build. There will be options to download two versions: an integer one (smaller and faster, but with some issues) or a float one.

Programming the NodeMCU can be done in two ways:

  1. Using the default scripting language, Lua (it’s somehow similar to Node.js);
  2. Using C++ and Arduino IDE.

I detailed the process more on my blog, so head over here if you need more info.  

Setting the Raspberry Pi as a Hub

The credit-card sized single board computer - Raspberry Pi 3 Model B - is the third-generation Raspberry Pi. By adding keyboard, mouse, display, power supply, and SD card with Linux on it, I got a fully-functional computer I've been using with IoT purposes for 2 years now.

This hub runs on the Raspbian operating system with the addition of PiVPN that grants access access and control over my devices from outside of my network.

In the early versions of the devices, I used http requests to communicate with the hub, but later switched using the MQTT protocol – a publish-subscribe based messaging protocol. Make sure you check this for a detailed account on how to set the MQTT protocol.

Home Automation App

This is where everything comes together and allows for the IoT magic to happen.

I used Domoticz, with http requests to make the calls and have all the 'if-this-then-that' decisions made at the ESP module (e.g. stuff like: if there is movement and the light sensor says it’s dark, turn the light on).

Domoticz is just one option. In my case, at a later point, I switched to Home Assistant, changed the ESP module code from http requests to MQTT, and moved the conditions and execution logic to the hub using Home Assistant automatons.

This is how my dashboard looks like, with all automations, sensors and switches:

You can check this piece to learn how to set up your dashboard the same way.

My First Device

As I promised, I'll be uncovering the process I used in order to build my first IoT device and get closer to the smart home I envision.

What was the trigger? Well, just the mere idea of being able to turn lights on/off via the mobile phone, or even not needing to turn lights on and use a movement sensor.

What did I build it for? It is used for the bathroom and it automatically turns on bath lights when movement is detected and there isn’t enough light in the room. Same for turning off.

As for the 'hub' part, I used two home automation apps: Domoticz and Home Assistant. Since this was the first device, I experimented with both assistant apps -  having multiple iterations - until I got to the final version.

Home Assistant
Home Assistant – device tab – temperature, humidity, motion and light sensors, switches

As I wanted my device to be integrated with the wall switch, I had to place them right next to each other. The switch doesn't activate the lights but is connected to the ESP module and acts as a 'sensor', letting the ESP know when the switch was pressed. With the 2-channel relay, the ESP turns the lights on/off.

Did I face any issues? Of course. From time to time, the light would automatically turn on and off within the span of a few milliseconds.

The catch was, the switch had been designed for high voltage so it didn't handle low voltage that well. So I fixed this via software by adding a delay in code, for the cases where the device was being used. This removed the false triggers entirely, and only affected the switch usage by having 300ms delay between the press and the action happening.

As the switch isn’t connected to mains anymore, it doesn’t have on/off positions, and now acts as a toggle for the light.

How about hardware? For hardware, I have these:

  • power supply (220v micro-usb phone charger);
  • 5v wire
  • device box
  • ESP module using the micro USB.

And the whole setting looks like this:

inside the case...
inside the case…

As for the sensors, they are inside the bathroom, one meter away from the main ESP, having a different case for the movement, temperature and light sensor. For this I used one main wire, with 6 wires: 2 for powering all the sensors, 2 for temperature sensor, one for light sensor and one for the movement sensor.

What did I use software-wise? I started with Arduino and stuck to it.

I separated the setup in two parts (in Arduino, the setup() method is called once, when the app starts, and is used to initialise the pins, variables):

  • general method - used with all devices and included in setup.h preparing the wifi connection, serial and MQTT;
  • device-specific method - where I set up the pins, initialise the sensors and subscribe to MQTT channels for switches.

In my application loop, I added methods for each sensor (temperature, light and movement) and for the two switches:

Of course, there's more to the software part of my IoT project - including how I connected the smart home system to my phone - but it kind of exceeds the scope of this article so please head over here for a way more detailed account of the process.

What's Next in My IoT Projects Roadmap?

The device I wrote about is the first in a series I'm building for my home (next on my list there's a heating device). So if everything goes fine, I'll soon automate all of it and live in 3018.

Just kidding 🙂, for now I only want to share with everyone (especially my workmates to whom I owe the inception of this IoT journey) how they can set up their own devices and have fun with code in a different way.

Want to be part of the next steps in my IoT explorations? Just check my blog to see what I'll be up to.

Author image

by Piter

Have an app idea? It’s in good hands with us.

Contact us
Contact us