In previous episodes I’ve spent most of my time writing PC software, it’s time now to turn our attention to some firmware. Our ultimate aim of course is to cut our ties from the clunky, power-hungry PC so we can send MQTT messages (e.g. measurement data) from smaller, more mobile devices.
The Pretzel-Board we introduced in the previous installment is a stand-alone board containing an ATmega328 which can be programmed by the Arduino development system via a USB port and bootloader program. You will recall from the last installment that we sent commands to control the ESP8266 Wi-Fi module via the ATmega from the PC and that replies from the Wi-Fi chip were sent via the USB port back to the PC where my program was able to display them in a small text window.
For my first step I will be using the PC program and standard firmware just described. Step by step I will transfer the tasks performed by the program running on the PC to the ATmega328 program. As a simple user-interface I have hooked up a pushbutton (so I can trigger an action) and an LED (for display) on a plug-board connected to the Pretzel board which initially, at least is connected to the PC via a USB port.
First Step: In the last installment almost everything such as resetting the ESP8266, logging in to my home WiFi network, establishing a TCP/IP-link to a Test-MQTT server on the internet and sending a MQTT-CONNECT command over this link, was initiated by the PC software. Now we want to send a MQTT-PUBLISH message using the same topic we used in the previous installment: ‘/ElektorMyJourneyIoT/TestTopic/test’ and a small ‘txt’ payload directly from the ATmega when the button is pressed. I wrote the necessary bytes directly hardcoded into my Arduino sketch; I started with the P01_SoftwareSerial standard-firmware, and expanded it to read the push button on the plug board. Yippee, this worked: In operation once the setup process is made by the PC program, I can press the button and the Pretzel board publishes a message which can then be read by any other MQTT client (such as a PC running the program we used in installment 5, for example).
Second Step: I converted the functions:
From the PC program into an Arduino sketch. That wasn’t so complicated because both C# and Arduino-C++ are extensions of C. C purists will no doubt look on my Arduino code with a certain amount of scorn but the aim was to show a clear correspondence between the two programs (you can download the sketch and also the code for the PC below).
Now the ATmega can itself generate the necessary bytes for the MQTT-PUBLISH command.
Third Step: I transferred the remaining functions of my small TCP/IP and MQTT library to the Arduino. The PC however still resets the ESP8266 and establishes the Wi-Fi connection. When the button on the plug-in board is pressed, the Pretzel board independently establishes a TCP/IP connection with the test server then sends an MQTT-CONNECT request followed by the MQTT-PUBLISH message. It should be also be possible to send a series of messages one after another and in this case it’s important for the Pretzel board to be aware of the TCP/IP and MQTT connection status. The test server will, for example break the connection when it receives two MQTT-CONNECT requests one after the other. I could take a closer look at the response and error messages from the ESP8266 and test server but I will leave that to a future installment. For the time being, as a work around I just simply make the ESP8266 drop the TCP/IP link before it’s reestablished. This ensures that the connection to the MQTT test server is broken and I can then send the MQTT-CONNECT request to reestablish the link followed by the next PUBLISH message; albeit with a slight delay in between. Admittedly that’s not such an elegant solution but it works quite reliably when sending a sequence of messages.
Fourth Step: Now I just need to implement the reset function and Wi-Fi login which up till now we achieved with the PC over the USB connection. I have entered the necessary AT-commands directly into the Arduino sketch in the setup routine. Now when the Pretzel board is hooked up to a USB cable (just for power now) it automatically establishes a link to the Wi-Fi network. The corresponding LED lights up.
It works: To check that a new MQTT message is sent out each time the button is pressed I just implemented a simple counter. It increments by one on each button press from zero to nine and its new value is sent each time the button is pressed. You can download the sketch from below and run it on your Pretzel board; you will of course need to insert some vital personal information into the code (the SSID and Password) so that it can connect to your own WiFi network.
After you connect up the power supply (as shown, a 9 V battery works well also) the Pretzel board should now log onto your network. After each button press the message ‘button 0’ ‘button 1’ and so on, is published by the HiveMQ test server under the topic given above.
So that’s been a big step forward but there’s still lots to do! See how we get on in the next installment!
- on Internet of Things
My Journey into the Cloud (12): Stand alone MQTT
October 20, 2016 | 23:09
In previous episodes I’ve spent most of my time writing PC software, it’s time now to turn our attention to some firmware. Our ultimate aim of course is to cut our ties from the clunky, power-hungry PC so we can send MQTT messages (e.g. measurement data) from smaller, more mobile devices...