A while ago now we linked a My Baby’s Got LED board to twitter using Node-RED for the Love to Play festival In this post we are looking at another way to get playful using MQTT with a My Baby’s got LED board. MQTT is a simple transport protocol, now commonly used to communicate in IoT. We’re going to set up a board to listen to a public MQTT broker and change the lights whenever a message is published. This will mean anyone can change our lights and any other My Baby’s Got LED boards connected in the same way will also change,
MQTT works by allowing devices to publish messages on topics and/or subscribe to topics to see what has been published. There’s a lot more information about this protocol here. MQTT connections are handled by a broker which revives and directs messages appropriately. It’s often desirable to run your own broker, and there are a range of options available. However, to create our MQTT ‘playthelights’ example we are going to bypass the overhead of running a broker ourselves; we will use one of the publically available brokers instead. In this case we want other people to be able to control our IoT device so we need our messages are public.
There are only really two steps needed to set this up. First we are going to configure the board to subscribe to a broker and then publish messages to the appropriate topics on that broker.
Connecting to the MQTT broker
If you have configured your board to connect to your network, go to the address you gave it during the setup. We will need to connect to the internet to access the broker so if you haven’t configured wifi and are connected to the board as an access point, or if you don’t know what this means, see the setup documentation.
- Click on ‘Settings’ and then ‘Sync interfaces’ Scroll down until you find the ‘MQTT’ settings.
- Click the checkbox to ‘Enable MQTT’
- Set the broker to broker.hivemq.com and the port to 1883
- For ‘group topic’ use ‘wled/playthelights’, this is the MQTT topic we are subscribing to for messages with information about changing the lights.
- Scroll down to the bottom and click ‘Save’, the board will need rebooting for the changes to take effect.
We have now set up our board to connect to the hivemq broker and listen for messages on wled/playthelights
Publishing messages to the topic
Now that the board is subscribed to the topic the next step is to publish messages to it, for this you will need an MQTT client. There are many options available for different devices. In this example I’m using an online tool from the same organisation that provide the public broker we are using.
Change the address in ‘Host’ to broker.hivemq.com and click connect.
Now we are ready to start publishing messages to the topic to control the lights. Use the double arrows to exapnd the ‘Publish’ section and set the topic to ‘wled/playthelights’ In the ‘Message’ box type ‘ON’ and click publish. Now try publishing the message ‘OFF’. This should turn on the lights and then turn them off again. Sending ‘T’ toggles the On/Off state of the lights, try publishing the message ‘T’ a few times.
WLED also allows for changing colour over MQTT using the topic [mqttGroupTopic]/col by sending hex colour values. So change the ‘Topic’ to wled/playthelights/col and then publish a message containing a hex colour value Eg. publishing #ff0000 will turn the lights red.
The WLED interface also responds to messages posted to the topic [mqttGroupTopic]/api This responds to any of the HTTP API commands and gives a lot more control over the lights.
So, for example, publishing the message “FX=73” to wled/cheerlights/api will set the lights to the rainbow chase mode (Effect 73) A full list of WLED API effect settings can be found here.
The [mqttGroupTopic/]/api will also accept messages in the JSON api format. So, for example, publishing the message “{"seg":[{"col":[[0,255,200]]}]}”
to this topic is another way to change the colour of the first segment.
Remember your board is set up to accept all messages published on these topics on a public server, so anyone else from anywhere can also publish messages that change your lights. Equally messages that you publish will change the settings of every other My Baby’s got LED board that is subscribed to this broker.
An alternative to the web based client used there are apps available for mobile phones that can provide the same function. One example for Andriod is MQTT Dashboard