MQTT Sketch Tutorial
To use the Engimusing modules for home automation we need to use the MqttHub library to enable the transfer of data through MQTT. This tutorial will explain how MQTT is implemented in the sketch.
First open the Arduino IDE and check the bottom right corner of the sketch that opens to see that you have EFM32ZGUSB (or the board you are using) selected and the correct serial port. Use the Tools menu to set the board and port correctly if you need to.
Next go to File->Examples->MqttHub->MQTT_Sketch_Tutorial to open the sketch. This sketch lets us control the 3 LEDs on the module using commands sent through the serial port. The sketch also sends the output of the temperature sensor inside the microcontroller and also the output of the internal voltage monitor.
In the Tools menu is an entry called "Serial Monitor", click on it. You should see this window come up:
Change the baud rate to 115200 at the bottom right of the window. Now click the upload arrow on the sketch. Then lines will start printing in the serial monitor that look like this:
The first three lines are the state of the LEDs on the EFM32 microcontroller module. These three LEDs are in a single package on the module. If you look at the module the RGB led will be off.
You will also see lines that have "PLD":"SUB" in them. These messages from the module are used to subscribe to the information sent by another MQTT device or an MQTT server.
The lines that have EFM/CPU/TEMP?/TMPC are printing out the CPU temperature in Celsius.
The lines that have EFM/CPU/TEMP?/TMPF are printing out the CPU temperature in Fahrenheit.
The lines that have EFM/CPU/VDD? are printing out the CPU voltage in volts.
Now find this line in the sketch:
EFMCPUVDD.begin(HUB, "EFM/CPU/VDD", 50);
The 50 represents the time between updates in tenths of a second. Change the 50 to 200. Then also replace the 50 to 200 in the two places in this line too:
EFMCPUTMP.begin(HUB, "EFM/CPU/TEMP", 50, 50);
Then click the right arrow and upload the revised sketch. You will see the temperature and voltage updates slow down a lot.
Now we will send some messages to control the LEDs. At the top of the sketch there are a number of comments. Select this one:
{"TOP":"EFM/BOARD/LEDR/CTL","PLD":"ON"}
and then copy and paste it into the text box at the top of the serial monitor like this:
Then click the Send button on the right. The red led should now be on. You can now send the OFF command:
{"TOP":"EFM/BOARD/LEDR/CTL","PLD":"OFF"}
and the red led will go off. The third command in the list is the status command that just reports the state of the led. Try it and the other commands in the list.
After you are finished turn back to the sketch and we will explain how the sketch works. After the comments at the top we have these four statements. These statements bring in the libraries we need:
#include "Arduino.h"
#include <MqttHub.h>
#include <MqttPort.h>
#include <MqttModule.h>
Each object that is created needs memory. The following instantiation statements allocate the memory for the objects:
// Instantiate a class object for the MQTT Hub object.
MqttHub HUB;
MqttSerialPort SerialPort;
// Instantiate class objects for each the LEDs on the EFMUSB CPU board
// The onOffCtlClass allows for turning the LEDs on and off and
// requesting their status
OnOffCtlModule LEDCtrl0;
OnOffCtlModule LEDCtrl1;
OnOffCtlModule LEDCtrl2;
// Instantiate class object for the CPU temperature and supply voltage
// These classes can be used to read the values or can be set up
// to periodically report the values
CpuVddModule EFMCPUVDD;
CpuTempModule EFMCPUTMP;
The code inside the setup() function runs once to set up the serial port and to set up each of the other objects. You can read the comments to see the meaning of the parameters used to set up the logic.
In the loop() function HUB.update() is called each time through the loop. This is where all the work gets done behind the scenes in the library code.
The topics in the MQTT messages can be anything you want. You will need to change the topics to match characteristics of your system. Think of a system that includes two EFM32ZGUSB boards. Each could have a different sensor module. If you use this default sketch on both of them there would be EFM/CPU/TEMP messages coming from two places. There would be no way to tell which place they were coming from!
For your home automation system you should change the topics in the sketch to be meaningful to you. So you would change every occurrence of the topic EFM/CPU/TEMP in the sketch to something like: LIVINGROOM/SENSOR1/TEMP and program that into the first module and File->Save As... the sketch to a new filename. Then you would open the MQTT_Sketch_Tutorial again and change EFM/CPU/TEMP to something like: BATHROOM1/SENSOR1/TEMP and program that into the second EFM32ZGUSB board saving the sketch to a new filename. You should choose the filenames so that you will remember which module they belong to.
You need to change any topics that might conflict with something else in your system to sensible topics for your system. When you set up the home automation server software these new topics will need to be added to the item definitions for openHAB.
Next you will want to install the MQTT software for your modules to communicate with.