My Journey into the Cloud (14): Replies from the MQTT server

February 9, 2017 | 00:00
My Journey into the Cloud (14): Replies from the MQTT server
My Journey into the Cloud (14): Replies from the MQTT server
In the last installment of my journey I built a small sensor board based on the Pretzel Board (which comes with an on-board ATmega and ESP8266 Wi-Fi chip) and available to order from the Elektor shop. I added a phototransistor, a pushbutton to initiate actions and an RGB LED to provide status information. On power up the ESP8266 logs onto a Wi-Fi network and when this link is established the RGB LED lights up green. By pressing the button the ATmega is configured to sensor mode.  Here it periodically measures the light level provided by the phototransistor and supplies the ESP8266 with the AT commands to establish a TCP/IP connection to a HiveMQ test server via the Internet; a MQTT connect request is then sent followed by MQTT publish command (using the measured light-level value as the payload).

I was already aware of some basic error handling: The ESP8266, for example returns ‘ERROR’ when a TCP/IP link could not be established. Also not evaluated yet are the incoming TCP/IP messages from the server. The MQTT server, for example responds to a connect-request with a byte sequence ‘32 2 0 0’, when the connection request has been successful and you can then go on to publish messages.

I added the function

TCPClient_Receive(byte TCPBytesReceivedBuffer[])

to my small  TCP/IP library. This asks the ESP8266 if bytes have been received from the server and supplies a parameter defining the memory space where the bytes are to be stored. With the function

MQTTClient_Connect(String clientid)

in the MQTT-library the received bytes can be evaluated. The function returns the value 1 if successful or 0 if not. In the calling function (which is executed periodically, as mentioned above) the returned value is read and the red LED is lit when the connection request was not successful, which can occur from time to time.

So now I was able to receive and evaluate bytes from the server it encouraged me on to progress further and expand the software.  I added a function to the MQTT library that makes a subscribe-request to subscribe to messages and of course evaluates the reply from the server. After a short time I was able to use the Pretzel board to receive messages that had been sent by other MQTT clients on the internet.  To do this, the new function

MQTTClient_Get(byte MQTTBytesPayloadBuffer[])

must be called periodically. What I was not aware of is that the broker sends the entire content of the MQTT publish commands 1:1 from the sending MQTT client to all clients subscribed to the topic (including Topic, the payload data of course and the message length byte). I will go a bit more into the detail of receiving MQTT messages with the Pretzel board in the next instalment, together with a small demo application.

The download for this instalment consists of the expanded version of the software for my small sensor board. As well as the more comprehensive error handling, I have tidied up the code somewhat and improved the level of abstraction. For Example there are now separate functions for the RGB status LED and the blue LED on the board, which makes it simpler to port to other hardware. The code has improved comments and if you want to go a little deeper you can download the Arduino sketch from the last installment and compare it with the most recent version.

Firmware Software Exec icon png
Software IOTJourney14
Loading comments...
related items