Day 22: MQTT Cheerlights
Today’s trial is about MQTT and a global project that was presented last year: the Cheerlights Project by Hans Schaaler. For everyone who doesn’t know the project: It’s about globally networked coloured lights that can be simultaneously controlled through Twitter commands. Another version of this project was part of last year’s Advent calendar. This year we have the version with MQTT and expanded functions.
Behind today’s door is a 1-kohm resistor. The layout consists of the RGB LED with series resistors and a button.
Download today’s programme. Before you can transfer the programme to the NanoESP, you have to install a library. This is the Crossfade library by Radek Wierzbicki (source: https://github.com/radekw/Arduino/tree/5f24ce7c8db9dfbb5252b59824c3217d851b3a3c). For practical reasons, a copy of the library version used is contained as a ZIP file in the Sketch folder. You can easily add integrate this through the Library Manager in the Arduino IDE by selecting the ZIP directory in the Sketch folder Sketch > Integrate library > Integrate .ZIP library. Now the programme compiles, and you can upload it to the NanoESP
After uploading the programme, now go back to the site www.hivemq.com/try-out/ and establish a connection to the broker.fkainka.de broker. There, with the topic
you can switch the mode between Cheerlights and personal colours by publishing a 1 or a 0. It’s important that for all messages you activate the Retain check box before you send the message with the Publish button next to it on the right. Retain means something like Save because the broker stores the message. Now, as soon as a device subscribes to the topic, the saved message is sent in its entirety to the device. You can thus save states. Incidentally, the button on the breadboard likewise switches the mode using a message with Retain flag. So later on, you don’t always have to go to the Try Out site to switch the mode.
In personal mode (mode 0), you can give the RGB LED a colour by publishing a message in the topic
in the form
The X stands here respectively for a PWM value between 0 and 255. The sequence of colours is red, green, blue. So for a green colour with maximum brightness, you have to publish
Don’t forget to check the Retain box.
In Cheerlights mode (mode 1), the board subscribes to a topic to which the current Cheerlights colour is published. You can change this colour by composing a Twitter message and thereby changing your colour and also the colour of all other Cheerlights users. First, the message must contain #Cheerlights, @Cheerlights or simply the keyword Cheerlights; second, one of the predefined colours must come after the keyword. The predefined colours include:
red, green, blue, cyan, white, warmwhite, purple, magenta, yellow, orange, pink
A potential tweet thus looks like this:
Testing my #cheerlights #MQTT project on my #NanoESP with the color blue
You thereby give yourself and the whole world a new colour. But if you prefer your personal colours without someone else butting in, simply switch back to the personal mode, and now the LED changes back to your predefined colour.