#24: Home automation system architecture

Posted on September 21st, 2017

I’ve been lucky enough to have worked with an electrician to totally rewire my house for home automation, so it works in a very different way to a normal house. This episode traces through how power arrives at my house, is distributed to a pair of sub-switchboards, and from there goes out to loads such as lights. It also covers the important pieces of the system including MQTT and OpenHAB.

General information about MQTT is available at the official MQTT site. The site doesn’t get many updates because the protocol standard itself is fairly stable and well established, but it’s a good reference site with links to many MQTT-related projects. See mqtt.org.

There are many MQTT broker implementations available, written in various programming languages and with different features and levels of performance. I use Mosquitto, which has been around for many years and has never let me down. Note that your MQTT clients won’t care what broker you use or what language it’s written in, provided it supports the features they need. Mosquitto is written in Java, but I typically connect to it from Arduino-based devices. See mosquitto.org.

OpenHAB is currently the main rules engine that I use, which also takes care of state management and provides an app for iOS and Android. I’m still running the v1.x release series, but v2 is out now which is a major rewrite. See openhab.org.

For a general purpose rules engine that communicates using MQTT, check out Node-RED. With a drag-and-drop editor based on Node.js, you can create rules right in your browser. I don’t currently use this, but I’ll probably replace my current home-brew rules engine with Node-RED some time in the future. See nodered.org.

#23: Preparing your home automation system for death

Posted on April 12th, 2017

Building a home automation system is a lot of fun, but there’s a serious issue you need to consider: what will happen when you’re gone? Will anyone else be able to figure out the system that you put together?

A couple of years ago, software developer Chris Yeoh passed away after a battle with cancer. Even with time to plan, his passing still left some systems in his house inaccessible to his wife and young daughter. His situation made me think long and hard about what would happen to my family if the same thing happened to me.

I have a couple of suggestions for ways to prepare your system for dealing with a future without you.

Bootstrapping documentation

If you’re gone, and a random electrician is called in to figure out why the lights aren’t working, will they throw up their hands in disgust and walk away? The first thing they’ll do is open the switchboard to orient themselves, so put some obvious bootstrapping documentation right there at the switchboard.

When I first started taking electronics apart in the 1970s, it was normal for major appliances to have physical documentation inside them. If you take the back cover off an old black and white TV, it may have the complete circuit diagram glued inside for the benefit of service technicians. I’ve even opened up old TVs to find a complete factory service manual packed inside.

I keep a master document in Google Docs, and a hard copy on a clipboard hanging next to the main switchboard. Whenever I make a change to the cabling or anything else relevant, I write the changes in the hard copy. When a few changes have accumulated, I take the clipboard to my computer and edit my master copy. Then I print it out again, put it on the clipboard, and leave it with the switchboard.

My document contains:

  • Name and phone numbers of myself and my wife.
  • Name and phone number of our electrician.
  • WiFi SSID and password.
  • Name of my ISP, account ID, password, tech support phone number, and fault history.
  • Name of my cable TV provider, account ID, password, and fault history.
  • A plan of my house, with important locations (such as all switchboard locations) marked.
  • A definition of the labelling scheme for network connections.
  • A list of Ethernet ports and their uses.
  • A definition of the labelling scheme for power cables.
  • A list of relay outputs controlling loads in the house.

You can see a template version in Google Docs, and either duplicate it for your own use or download it as a Word doc or other format:

https://docs.google.com/document/d/1B9N5zjWe61BWN3E9MIRnA-a-sEtzdo1qJcFtI-x61R8/edit?usp=sharing

Storing source code

When you make something for your home automation system it’s usual to test the firmware, modify it until it’s working just right, and then walk away and forget it. The source code for your project is probably sitting in some obscure location on your computer, which nobody else can access. Or if they can access it, how do they know this particular Arduino is running the sketch called “SensorShield4MQTT” instead of the other one called “SensorInputsMQTT”? Or which versions of the libraries you compiled it with?

When I finish a project, I copy the source code and associated libraries onto a cheap USB memory stick or an SD card, and then put it inside the project box or attach it with a cable tie so that it always physically stays with the project.

Later, if I ever update the firmware, I also update the memory stick so that I know 100% for sure that it always contains the exact source code that is currently running on the device.

This has saved me hours of frustration on multiple occasions, and if anyone else ever wants to fix the systems I’ve built this should put a smile on their face.

Other ideas?

I’d love to know if you have other suggestions! Please comment below.

Finally, please consider supporting a cause such as Free To Breath and help fight cancer.

#22: AXA Electric Window Motors Controlled By LIN Bus And Arduino

Posted on March 29th, 2017

Electric window motors allow your home automation system to open and close your windows as required. This could be useful for opening windows to allow ventilation, or to make sure all windows are automatically closed and locked when you leave home.

I recently got my hands on some AXA electric window motors that include a LIN bus interface so they can be linked to a home automation system, but I’ve never used LIN before so I needed to learn a bit about how it works. I designed my own LIN interface module that allows me to connect my laptop to LIN devices and manually send messages, and the module also allows an Arduino to control the window motors.

Parts used

Source code

Simple example that provides a menu and sends commands to a single LIN interface: AXARemoteSerialLinbus

More complex example which includes MQTT, DHCP, reading a MAC address ROM, 4 LIN interfaces, a watchdog timer, and a temperature / humidity sensor: AXAWindowMQTT

Instructions and explanation

More details to follow! I’ll update this after the video is ready.

#21: Six Sonoff Secrets

Posted on January 4th, 2017

Customise your Sonoff home automation controllers with a memory upgrade, weatherproof housing, control switches, sensors, and custom firmware.

ITEAD keep releasing new Sonoff models so this isn’t a complete list, but some of the models mentioned in the video include:

Storage

winbond-25q32fvThe flash memory chip in Sonoffs is 8Mbit, which is only 1MByte. Then if you want to do OTA (Over The Air) updates you need to limit your program size to less than half the available memory so that a new program can be uploaded alongside the old one. And if you use a SPIFFS (SPI Flash File System) to store non-volatile data outside your program, you lose even more memory.

You can replace the flash memory chip with a Winbond 25Q32FV in SOP-8 package, which is a 32Mbit (4MByte) chip. You can buy them on eBay for about US$3 for a pack of 10.

The original flash memory on the Sonoff is a Winbond 25Q08FV, which is the 8MBit (1MByte) version of the same chip.

Thanks to Pete Scargill for this idea! You can see more on his original blog post: “32Mb ESP01 and Sonoff

Safety

Sure, you can just cut a power lead and screw it into a Sonoff, but it’s probably a bad idea. You need to consider how your Sonoff will be used, including physical protection (stop little fingers reaching the terminals!) and liquid protection from spills. You also need to make sure there is strain relief on the cables to prevent them being pulled out, and possibly exposing live mains connections. You can do it cheaply using a plastic project box, and with some cable ties around the cables just inside the box so they can’t be pulled out. The result can be a very neat setup that won’t look like a dodgy DIY cable, and should be safe for general use.

sonoff-powerboard

You can go even further and use an IP-rated (Ingress Protection) case and cable glands, to make your Sonoff waterproof and physically very strong.

sonoff-ip56-case

In this video I gave a very simple explanation of the two-digit IP codes. There are also extensions to the code for other attributes. You can find more information and tables showing the specific meaning of the numbers at en.wikipedia.org/wiki/IP_Code.

Switches

Internet control is fun, but usually you also want some way to manually turn the output on or off without using your phone. You can modify a Sonoff to connect an external button across the pins of the built-in button, allowing you to toggle the output by pressing the button manually. The built-in button is connected to GPIO0, so when the button is pressed it pulls GPIO0 to GND. This is used during power-up to put the Sonoff into bootloader mode, and can also be used to toggle the output or do other actions.

sonoff-gpio0-terminals

Alternatively, you can connect an external button between GND and GPIO14 so that your software running on the Sonoff can detect when it has been pressed. Some firmware, including Theo Arends’s TASMOTA, supports this out of the box.

GPIO14 is exposed on the internal header used to upgrade the software on a standard Sonoff. There is already a pull-up resistor on GPIO14, so you don’t need any other parts. Just connect a button across the GPIO14 and GND pins, and you’re done!

sonoff-header

Even better, TASMOTA has an option to support an external switch instead of an external button. The difference is that with a button, you want the output to change state each time you press and release the button. This means the firmware needs to treat both the button press and release (cycling from HIGH to LOW to HIGH) as a single event. But a switch just changes state (goes from HIGH to LOW, or goes from LOW to HIGH) and stays there because it latches in place. So your software needs to treat each level change as a separate event, and toggle the output. Once again, TASMOTA supports this out of the box.

Sensors

As explained in the section about switches, the regular Sonoff exposes GPIO14 on the internal header that is used for flashing new firmware. The same header also provides GND and 3.3V connections, and the GPIO14 pin is provided with a pull-up resistor. This makes it super easy to connect anything that needs a single digital pin, such as an external switch / button or a one-wire sensor.

sonoff-th-sensor-pinoutTo make it even easier, the Sonoff TH10A and TH16A both feature a 2.5mm 4-way (TRRS) socket that is intended for connecting external sensors. The socket provides 3.3V, GND, and GPIO14: the same I/O pin exposed on the internal header of a normal Sonoff. That means you don’t even need to modify the board, you can simply plug in your sensor or switch externally.

sonoff-th-with-cable-800

The 2.5mm socket also has a 4th connection, but there are parts missing inside the Sonoff so it’s left unused. You can fix this easily by putting a solder blob across two pads on the Sonoff PCB, and optionally installing a pull-up resistor.

In the video I said that there is a 10k pull-up from GPIO14 to 3.3V. However, I didn’t notice that there are actually 2 pull-ups in parallel, so the effective pull-up on GPIO14 is 5k! You can still use a 10k pull-up on GPIO4 if you like, or you can use a 4.7k resistor if you want them to be about the same. 4.7k is common for I2C, but 10k is generally fine too.

sonoff-th-bottom-breakouts

Software

There are many alternative firmware projects for the Sonoff. Personally I love Theo Arends’s firmware, but there are many others that may suit you better.

To learn about how to install new firmware, see SuperHouseTV Episode #17: Home automation control with Sonoff, Arduino, MQTT, and OpenHAB.

If you have any other suggestions please let me know in the comments below, and I’ll update this list.

Sites

Because the Sonoff uses an ESP8266 microcontroller, there are a huge number of sites with information that’s relevant to the Sonoff. These include:

#20: The $6 universal home automation button

Posted on November 11th, 2016

The “Flic” Bluetooth button inspired me to create something similar for my home automation system. But instead of using BLE, I just connected a $6 remote control and matching receiver module to an Arduino-compatible board, so that whenever I press the button it publishes to MQTT.

That’s incredibly handy, because it means you can just stick a button anywhere you like and have it trigger arbitrary events in your home automation system. Make it turn off every light in the house, or order you a pizza, or whatever you like.

In this project, I combined it with the ambient tile display that I showed back in Episode #14 so that I would never forget to take out the rubbish bins.

Each week, the home automation system turns on one of the LEDs so that it reminds me to take out one of the bins. A red LED says it’s time to take out the bin with the red lid, and a blue LED means the bin with the blue lid.

Then, when I take out the bin, I just push the button attached to the bin and it tells the home automation system that I’ve done my job. The notification is cleared until next week, when it comes on again.

#19: Husqvarna Automower basics

Posted on October 12th, 2016

Why mow the lawn yourself, when you can have a robot do it for you?

Husqvarna have very kindly supplied me with an Automower 320, which is a mid-range robot lawnmower that’s suitable to mid to large size suburban gardens.

Normally I like to focus on low-cost, DIY home automation projects, but this is just too good to pass up! In this episode I explain how the Automower works, how it’s different to a traditional manual mower, and (most importantly!) how my pets react to it.

In future episodes I’m going to try some creative things with the Automower. I can’t go totally crazy because the mower is supplied by Husqvarna as part of a pilot program and I’m not allowed to open it or modify it (yet!) but there are still some fun things we can do without voiding the warranty or having it repossessed.

The Automower range is listed on the Australian Husqvarna site: www.husqvarna.com/au/products/robotic-lawn-mowers

Disclosure: Husqvarna provided me with this mower as part of the Test Pilot program. www.automowertestpilots.com.au

#18: Installing the ANNKE 4-channel CCTV system

Posted on October 9th, 2016

Over the years I’ve slowly added individual IP cameras, both inside and outside the house. However, I’ve run into limitations such as how to store footage from the cameras for security purposes, and how to display multiple cameras simultaneously. I’ve used workarounds for these problems but it’s time to try out a proper camera system, rather than a collection of separate cameras.

I installed an ANNKE system a DVR and cameras to replace 4 of my existing IP cameras, to test how well it works. I also installed long video cables and a wireless mouse so I could mount the DVR inside my ceiling and display the camera feed on a couple of LCD TVs, and tested BNC-to-UTP adapters to run Cat-5 cable to a distant camera.

Unfortunately I discovered that the DVR requires Windows for network access, because it relies on a plug-in that doesn’t work on Mac or Linux. That means I had to run long video cables to the TVs that I use to display the live feed from the cameras. The end result is great, but it’s not how I intended to set it up.

This is the particular ANNKE 4-camera system that I installed, which is the Australian version: www.annkestore.com/annke-8ch-1080p-hd-cvi-security-camera-system-with-4-x-2mp-bullet-cameras-c51bp.html

This is the US version of the system: www.annkestore.com/annke-4ch-1080p-hd-tvi-security-camera-system-with-4-x-bullet-cameras-1tb-hdd.html

There are a bunch of other packages as well. The AUD price seems to vary: I think it’s generated by converting from USD at whatever the daily exchange rate is, so it’s different now to when I got mine.

#17: Home automation control with Sonoff, Arduino, OpenHAB, and MQTT

Posted on September 21st, 2016

The Sonoff from Itead Studio is a fantastic little mains-switching module with a built-in ESP8266 and WiFi. If you want an easy way to control mains devices such as lamps and fans, this could be it!

Sonoff modules are preloaded with firmware that allows them to be controlled by a phone app, so they’re very easy to get started. But that’s just the beginning: using a USB-serial converter and the Arduino IDE, you can load your own software on the Sonoff and make it do your bidding. I used the Arduino IDE to load a sketch with MQTT support and OTA (over the air) updates, allowing it to be controlled by OpenHAB.

Sonoff: www.itead.cc/sonoff-wifi-wireless-switch.html (basic version)

Sonoff TH: https://www.itead.cc/sonoff-th.html (Temperature / Humidity version)

Slampher: www.itead.cc/slampher-wifi-wireless-light-holder.html (lamp holder version)

Start by adding ESP8266 support to the Arduino IDE by following the instructions at github.com/esp8266/Arduino. I used the simple “Boards Manager” method.

For OTA (over the air) update support you’ll also need to install Python 2.7.

You’ll also need a USB-serial converter with support for 3.3V I/O. I used the Freetronics USB-Serial Converter (of course!), which has a switch that lets you select 3.3V or 5V mode.

The “BasicOTABlink” example shown in the video can be found at github.com/superhouse/BasicOTABlink.

The “BasicOTARelay” example shown in the video can be found at github.com/superhouse/BasicOTARelay.

For more information, a fantastic resource is Pete Scargill’s blog post about Slampher and Sonoff.

If you want to upgrade the FLASH memory on your Sonoff, check out Pete Scargill’s blog post about upgrading ESP8266 memory.

Update 24 Sep 2016: fixed the example links.

#16: Compiling Arduino sketches with Pocket C.H.I.P.

Posted on July 25th, 2016

Do you ever find yourself inside your ceiling or under the floor working on your home automation system, and need to SSH to a remote machine or re-flash an Arduino?

Sure, you could drag your laptop under there with you, but I don’t want to have my expensive laptop lying around in the dirt or insulation.

I may have found the answer.

C.H.I.P. and Pocket C.H.I.P. will be available soon from getchip.com.

I used “arduino-mk” running on the Pocket C.H.I.P to compile and upload an Arduino sketch: github.com/sudar/Arduino-Makefile

The following sequence got everything working for me. You may want to do things a little differently, for example by managing the python-serial extension using Pip or storing your sketches in a different location. That’s totally up to you, just make sure you alter the paths shown below as necessary.

Start by making sure your package list is up to date, and that you have the latest packaged Arduino and Python serial extension installed on your C.H.I.P.:

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install python-serial arduino arduino-mk

Create a directory inside your home directory to keep all your Arduino sketches organised:

mkdir ~/sketchbook

Now go into your sketchbook and copy an example project out of arduino-mk, so that it includes the example Makefile:

cd ~/sketchbook
cp -a /usr/share/doc/arduino-mk/examples/Blink .

Note the space and the dot at the end of that line!

Now go into the Blink project and see what files it contains:

cd Blink
ls -l

You’ll see that there’s both the sketch itself and a Make file:

-rw-r--r-- 1 chip chip 509 Jul 12 2014 Blink.ino
-rw-r--r-- 1 chip chip 61 Jul 12 2014 Makefile

There’s nothing unusual about the Blink sketch. It’s just standard Arduino code that you could compile with the regular IDE. The magic is invoked through the Make file.

Open the Make file in your favorite text editor, and you’ll see that it looks like this:

BOARD_TAG = uno
ARDUINO_LIBS =
include ../../Arduino.mk

The “BOARD_TAG” value specifies that you want to compile it for an Uno or compatible board. You can change this to match other boards found in the Arduino board hardware definition file, which is located at /usr/share/arduino/hardware/arduino/boards.txt. In that file you’ll find other handy board types that I’ll list at the end. For now, we’ll assume that you’re using an Uno and leave that unchanged.

The “ARDUINO_LIBS” value allows you to specify a path to your own custom libraries in case you need them linked during compilation. We’ll ignore that for now, so leave that line unchanged.

The “include” line is a problem. It assumes the project is still located in the shared location where it was installed by the package, but we’ve made a copy in our local sketchbook. To fix that, change the “include” line to read:

include /usr/share/arduino/Arduino.mk

There are many other options you can add to the Make file to override the default settings within Arduino. For example, you can define the path to a specific version of AVRDUDE if you want to bypass the version that’s bundled with the Arduino environment.

But for a minimal example, you don’t need to change anything else.

Now you can compile the sketch, and upload it to your Arduino. To compile the sketch without attempting to upload it, just type:

make

That’s it! Can’t be much simpler. If there are any compilation errors, you’ll see them in the terminal.

To upload (and automatically compile if necessary) make sure your Arduino is plugged into your Pocket C.H.I.P. using a USB cable, and type:

make upload

You’re all done! Your sketch should now have been uploaded to your Arduino.

There are other commands available as well. Check out the Arduino-MK documentation for more information:

github.com/sudar/Arduino-Makefile

Alternative board types

These are all the board tags list in the standard /usr/share/arduino/hardware/arduino/boards.txt file:

uno: Arduino Uno
atmega328: Arduino Duemilanove w/ ATmega328
diecimila: Arduino Diecimila or Duemilanove w/ ATmega168
nano328: Arduino Nano w/ ATmega328
nano: Arduino Nano w/ ATmega168
mega2560: Arduino Mega 2560 or Mega ADK
mega: Arduino Mega (ATmega1280)
leonardo: Arduino Leonardo
esplora: Arduino Esplora
micro: Arduino Micro
mini328: Arduino Mini w/ ATmega328
mini: Arduino Mini w/ ATmega168
ethernet: Arduino Ethernet
fio: Arduino Fio
bt328: Arduino BT w/ ATmega328
bt: Arduino BT w/ ATmega168
LilyPadUSB: LilyPad Arduino USB
lilypad328: LilyPad Arduino w/ ATmega328
lilypad: LilyPad Arduino w/ ATmega168
pro5v328: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
pro5v: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
pro328: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
pro: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
atmega168: Arduino NG or older w/ ATmega168
atmega8: Arduino NG or older w/ ATmega8
robotControl: Arduino Robot Control
robotMotor: Arduino Robot Motor

You can also use custom board profiles, but that’s beyond the scope of this post! Most common Arduino compatible boards match something on this list. For example, the Freetronics EtherMega operates just like an Arduino Mega with a 2560 processor, so you’d select “mega2560” as the BOARD_TAG in the Make file.

Update 6 Jan 2017: Quick Setup Script

My friend Alec has written a script to simplify the process of installing the packages I used in the video and also to fix a few things he doesn’t like about the default setup. His script installs the necessary packages, blocks root access to the terminal and SSH, locks the root account, changes the default password, changes the hostname, and forces public key authentication via SSH. Definitely worth a look:

gist.github.com/alecthegeek/434325b6ea261ba84499f4966795073e

#15: Watchdog timers for Arduino home automation

Posted on June 23rd, 2016

A watchdog timer helps keep your embedded devices running, by making sure they are responding properly and automatically hitting reset or cycling the power if they die. I’ve added a simple hardware watchdog timer to an Arduino compatible board running inside my ceiling, where it’s difficult to access if I need to reset it manually.

View directly on YouTube: https://youtu.be/GC6dGypGctQ

Links for this episode: