It’s been a while since I stayed up most of the night writing code, mainly down to having a young daughter but also down to the fact I haven’t found anything that needed a late night hack session to produce a result. This weekend changed all that, I’ve been playing around with home automation for a while but am now actually taking the plunge. I purchased a load of Arduino and electronics kit over the last few days to start prototyping the setup.
I’m planning to replace all the lights in the house with 12v LED based bulbs, mainly as a power saving excercise but also in the idea that it will make designing control circuits easier and safer, and also able to save money on getting an electrician to do the wiring. Once the transformers are wired up it will just be a matter of splicing in my control circuit. This will also allow me to convert a room, or even lamp/light at a time and not cause too much of a fuss in the house.
The control circuit:
This will consist of an arduino with an ethernet shield, and a number of mosfet transistors to do the switching. A fairly simple circuit really, which will use a single pin for each light. I could, and probably will get a little bit more fancy introducing timers and dimmers but lets not get ahead of myself here, besides this will be a small change which could be done without too much disruption at a later date.
So working on the research I’ve been doing around the technology I decided to opt for MQTT as the messaging between devices, its really simple but also really powerful at the same time. By assigning each device a name and designating a topic to that device a simple message can be transmitted to the network by a publisher and the relevant arduino which subscribes to that topic will pick up the message, and depending upon the message will perform an action.
A little bit about MQTT:
So to explain MQTT a little more, there are 3 components to MQTT which we are interested in, Broker, Publisher and Subscriber. First of all to use MQTT you need a broker, this is essentially a daemon or service running on a server on your (or external) network. In my case I am using the excellent Mosquitto (yes 2 t’s), at the moment I am also running with the default config I literally just started the service. Next you need a publisher and a subscriber, these are exactly what the sound like.
The subscriber basically connects to the broker server and listens for messages on a particular topic. At its simplest this would look like this on the linux command line using the mosquitto tools:
mosquitto_sub -h 192.168.1.1 -t Test/topic
So this subscriber is listening for messages on the topic Test/topic on the broker 192.168.1.1. The publisher will send a message via the broker to a particular topic so to send a message to the above subscriber the following command using the mosquitto tools would be required.
mosquitto_pub -h 192.168.1.1 -t Test/topic -m “hello world”
Any device subscribing to the Test/topic topic would now see a simple message of hello world appear.
So the plan is to utilise MQTT messaging and assign a topic per device and have the PubSubClient libraries present on an arduino to subscribe to the topics that particular device controls. So for example in the Study there will be 4 LEDs, so their topics would be house/study/led0 up to led3, the arduino will have 4 pins dedicated to the control of these so when a message is recieved on these topics a control signal will be sent.
Moving onto the switching side of things, it will be an arduino publishing the messages to the relevant topics based on push button switches being pressed to close signals on certain arduino pins.
So the messaging flow would look something like this:
switch 0 pressed -> Arduino pin 3 recieves HIGH signal -> Arduino publishes “On” MQTT message to topic house/study/led0
Arduino controlling lights recieved MQTT message on the house/study/led0 topic that it is subscribing to -> Arduino sends HIGH signal to pin 3 which in turn switches the LED light on.
After deciding on a plan of action, a prototype setup is required. I have setup an Arduino with pins 3 and 5 activating an LED each, attached to a breadboard via a resistor. After testing the LEDs using the blink sketch it was now time to get the board recieving MQTT messages and activating the said LEDs. Once I was happy that the Arduino was network connected using the Ethernet libraries I added the PubSubClient library and had it subscribe to the above topics – I did one first then added a second later.
In order to debug the MQTT I literally had it output the payload to serial and send messages from the linux command line tools.
All that seemed to go together quite well, my next problem was implementing an if statement to recognise the content of the payload. Because I was using the web service written my Jonathan Oxer it already had some device IDs preprogrammed so I used these until I decide upon my own. So LED0 on was 2-42 and off was 2-43. I wrote and rewrote the if statement but could not get it to recognise the contents, partly down to my lack of C++ knowlege. I eventually managed to get the payload contents output to serial and noticed it was suffixed with a ‘d’. Deciding I could live with this for now I included it in the search and everything lit up, literally. Need to get to the bottom of the ‘d’ though. Now I was happy with the program I added the second LED into the mix which worked fine.
Using a slightly modified version of Jonathan Oxers web service sending out messages via phpMQTT I can now turn on and off the prototype LEDs. Next step is to order some more Arduino and create the on/off switches to complete the circuit.
I have uploaded my code for the control module sketch to github which can be found here: https://github.com/jfarcher/mqttlightcontrol_lightside
I have also forked Jonathans web page and service and will be soon uploading my differences.
I will also be adding a video of the process to youtube soon.
The pubsubclient library for Arduino by Nick O’Leary can be found on GitHub:
with more details here: