Type your search keyword, and press enter

Light control with MQTT on Arduino

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

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.

The plan:

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:

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

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.

Simple!?

The prototype:

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:
https://github.com/knolleary/pubsubclient
with more details here:

Arduino Client for MQTT

New projects: Pi’s and Arduinos

Recently I’ve been working on several new projects, all of which use either a Raspberry Pi or an Arduino.

Site-To-Site VPN

A friend of mine recently opened a new satellite office, and as part of his day to day work had a requirement to connect the two together so resources on either side could see each other. Site-to-site VPN I thought, and what better kit for the task than a couple of Raspberry Pi’s. Nice and small so they can be kept along side the routers. Each site had a simple ADSL broadband link, with a couple of PC’s connecting up.

I’ll run through the complete setup in a seperate post as a Howto/tutorial, but the process was so simple to get them setup literally flash raspbian to a couple of SD cards, go through the motions to get a working console. I removed any unneeded packages, then installed and setup OpenVPN. After a bit of broadband/user error at one of the sites, simply opening the ports on the routers firewalls it all connected up beautifully. Fileshares and network based resources were all available wherever they wanted.

Home Automation
For a long time now I’ve been wanting to implement some kind of home automation system. After looking at X10 i was a little disappointed, as the technology was very dated and slow but also very expensive. Any other newer technologies were a little out of my price range, and also a very closed shop. So after about 12 months of it being on the edge of a project, a lot of research has been done and I think I am now in a position to put something in place. I’ll be using a mixture of Arduinos (or shrimps if I can), Raspberry Pi’s and other tech.

The idea is now coming to fruition due to a decision to transform all the lights in the house to 12v from 240v. This will make working with lighting a lot easier and safer, and cheaper due to not having to employ a spark to do all the connecting for me. The rough plan of attack will be to combine arduinos and mosfet transistors as switching mechanisms. Also implemented on these arduinos will be ethernet shields in order to connect the circuits up and have them centrally managable.

Essentially taking a traditional, feed -> switch -> bulb circuit and turning it into feed -> transformer -> arduino -> “switch” -> bulb. the switch component will be defined by the use of the light and could comprise of a push button SPST, an android app, a PIR or a combination of those. I plan to use a combination or software technologies for running all this, MQTT, python, PHP etc.

I plan to replace any fluorescent lights with LED strips, and have been toying with the idea of RGB here too which will bring a whole new depth to the lights. Many things to consider and a lot of exciting tech to play with, that is accessible, cheap and using open standards.

Exciting times.