Projects

MQTT Light Switch

Tim Blythman

Issue 3, September 2017

Everything has an app to control it these days, but wiring in electrical hardware to control a mains light switch is still beyond the reach of most people who aren’t actually qualified electricians. Here we put together some hardware and software, which makes controlling light switches (and other devices) a less shocking option.

For mains devices that run from a power point, it’s not quite as difficult to add some sort of remote controlled power point without worrying about messing with risky wiring, because the options are, literally, plug and play. If you want to interface into a light switch it usually means directly connecting to the wiring, with a relay to switch the power. Adding some extra hardware usually makes manual control more difficult as well.

This project creates a workaround, avoiding these two problems. The interface to the light switch does not require any mains wiring, and still allows manual control of the switch. This project uses the MQTT protocol because there are apps available that can be customised to do what we need, and it’s also a very simple protocol that an Arduino board can handle.

OVERVIEW

There are three separate parts to this project, which correspond to the three different roles that can exist in a system that operates on the MQTT protocol. A topic and message are published to a broker, which then forwards that message on to a subscriber (provided the subscriber has subscribed to that topic). In this case, I use an Android app to publish the custom data by sending a message to a topic, which is received by a single board computer running a broker program. An Arduino connected to a WiFi shield subscribes to the topic on the broker, and if it receives a message that corresponds to a command, it moves a servo motor to briefly operate a switch. It then moves the servo arm out of the way so that the switch can be manually operated. The way that MQTT can transmit different topics means that different devices can be configured to control or be controlled, while all passing through the same broker. This system lends itself to many small, simple devices like an Arduino attaching themselves to a single more sophisticated broker, which can do all the hard work of managing the messages.

HOW IT WORKS

The heart of any system using MQTT is a broker which mediates all communication. In this project, it is nothing more than a program running on a device on the network. For the sake of security, it is best that the broker is kept to being accessible only from inside the network. For testing purposes there are also MQTT brokers on the internet which can be used, such as test.mosquitto.org. If you don’t have your own broker set up, you can use this broker [1].

1
Broker

To control this project, we need something that can publish messages to the broker. I found an Android app called MQTT Dashboard [2], which was easy to configure and operate, and lets me operate the switches from a mobile device. After setting the address of the broker, a number of buttons (or sliders and other controls) can be configured to publish custom messages to any topic. The app can also be configured to subscribe to topics as well; for example, it could be set up to monitor a sensor variable such as temperature. If you don’t have this app or another one, there are also command line alternatives, which could be activated from a shortcut on a computer, and are also handy for testing.

2
MQTT Dashboard

Probably the most involved part of the build is the Arduino-based subscriber, which responds to messages from the broker. To help understand the MQTT protocol better, I worked to build the implementation from scratch. See the What the Tech article for a detailed discussion about the MQTT protocol and how it works.

The general sequence of events is that the Arduino connects to the WiFi network, then connects to the broker and sends it a connect message. When the broker sends the “connack” message, the Arduino sends a subscribe request, to which the broker replies with a “suback” message. The Arduino is now waiting for incoming messages, and when it receives a notification that matches one that it’s looking for, it activates one of the servo motors to operate the switch. The Arduino also occasionally sends a ping request to keep the connection alive. If the connection is broken, the Arduino attempts to reconnect.

The electronics are easy to build - the WiFi shield simply stacks on top of the Leonardo, and the servos can be plugged straight into the digital pin header breakouts, as they have the same pinout as the servo motors I’m using. I needed to extend the servo wires a short distance, so I used plug-socket jumper wires to do this.

The mechanical side of hardware is made of two 9G servo motors, which operate to actuate opposite sides of the switch. I had initially thought it might work with just one servo, but these have a throw of barely 180°, which isn’t enough to swing all the way around and operate both sides of the switch toggle.

Each of the servos is only activated for a second, while it moves the switch and then gets returned out of the way and into an idle position, which saves on power and also means that the servo horns are free to move if they get in the way.

THE BUILD

Parts Required: Jaycar Altronics
1 x Arduino UNO XC4410 Z6280
1 x ESP8266 Based WiFi Shield XC4614 -
2 x 9G Servo Motor YM2758 Z6392
Double Sided Tape NM2823 T2985

Want More?

Log in to unlock this article, or sign up now to receive two premium articles free!