This chapter was replaced by Home Assistant - Mosquitto broker add-on.
I'll leave this chapter here in case you would like to connect to a public MQTT server.
This chapter was replaced by Home Assistant - Mosquitto broker add-on.
I'll leave this chapter here in case you would like to connect to a public MQTT server.
Message Queuing Telemetry Transport is a publish-subscribe based messaging protocol designed for machine-to-machine (M2M) communication and it is widely used among Internet of Things (IoT) devices. ESP-link supports both publish and subscribe methods, the Gbox420 sketch currently uses the publish method to push sensor readings to an MQTT broker. The broker can be a public MQTT broker like HiveMQ or a locally installed Eclipse Mosquitto running on a PC/Raspberry Pi.
Testing with a public MQTT Broker
To test the MQTT functionality we are going to publish fake sensor readings to a public MQTT broker and allow incoming control messages to modify their values. In this example, I`m going to use HiveMQ, but you can use any available MQTT broker, even your own server. Due to the low bandwidth requirement and reporting frequency, the free packages of most Cloud MQTT providers will do just fine. In the below test we just need to make sure MQTT messages can be sent to and received from a Public MQTT broker by the Arduino sketch.
The first thing we need to do is configure the MQTT client on the ESP-Link website under the REST/MQTT tab. Tick the Enable MQTT client and Enable SLIP on serial port check-boxes and fill in the following details:
Server hostname or IP: Name or IP of the MQTT broker, in this example broker.hivehq.com
Server port: The default un-encrypted MQTT port is 1883, some servers also offer port 8883 for SSL encrypted message transfer, but Arduino and ESP-Link cannot handle encryption. Use port 1883
Client ID: Unique ID that identifies the device, it can be anything. You can have multiple devices registered under the same username, the Client ID helps to figure out where the message came from.
Client Timeout (seconds): Length of time the MQTT client waits for a response from the MQTT broker when initiating the connection. Leave it on default for 50 seconds.
Keep Alive Interval (seconds): Maximum time that can pass between two communications, if the MQTT broker does not receive a packet within this interval it considers the client “dead” and sends out the client's Last Will and Testament (LWT) message to all subscribers. We`ll talk about setting an LWT later. Leave it on default 60 seconds unless the MQTT broker guide specifies otherwise.
Username: Your account name on the MQTT broker. HiveHQ does not require registration, you can enter anything here for this test.
Password: Your account password on the MQTT broker. HiveHQ does not require registration, you can enter anything here for this test.
After entering all details press the Update server settings! button and reset the ESP-link either by pressing the physical reset button on the board or from the Debug log tab - Reset esp-link button. After this go back to the REST/MQTT tab where the MQTT client state should now display connected state. If it still displays disconnected double-check the server details, make sure SLIP and MQTT are enabled, and verify that the ESP-Link is connected to a WiFi network that has internet access.
The test sketch will publish fake data with the type of char array, bool, int, and float to the MQTT broker in JSON format under the GrowBoxGuy/Gbox420/ topic. Before you upload the sketch locate the MqttTopic variable and update it to point to your unique topic. The topic can be anything, just make sure it is unique. Once the sketch is uploaded it will start publishing the fake sensor readings every 10 seconds while listening for control messages. The sketch subscribes to all sub-topics of the MqttTopic using the # wildcard character, this allows it to capture any incoming command over MQTT. On the serial log, you will see an MQTT disconnect message during initialization followed by an MQTT connected message, this is normal when the connection builds up:
This is how the JSON formatted published data looks like:
{
"Counter":"1",
"InternalFan":"0",
"Brightness":"50",
"LightOn":"0",
"AeroPressureHigh":"7.10",
"MqttString":"Gbox420"
}
Below are the MQTT topics the test sketch uses for reporting and controlling the grow box. Control messages are just examples of handling different data types. They update the corresponding variables and print the result on the serial output.
Publishing messages:
MqttTopic/Gbox420 - Fake sensor readings in JSON format
MqttTopic/LWT - Message the MQTT broker should send out to all subscribers in case the device goes offline. Set to a fixed “Gbox420 offline” message
Subscribed to control messages:
MqttTopic/InternalFan - Expects an integer with values 0, 1 or 2. Adjusts fake internal fan speed (OFF, LOW, HIGH)
MqttTopic/Brightness - Expects an integer between 0-100. Adjusts fake brightness.
MqttTopic/LightOn - Expects a bool value of 0 or 1. Turns fake lights ON/OFF
MqttTopic/AeroPressureHigh - Expects a float value like 7.1. Adjusts fake maximum pressure for the Aeroponics setup
MqttTopic/MqttString - Expects a string (char array) like “Mary Jane”
Finally, it is time to see the MQTT messages in action, for this you will need an MQTT client that can subscribe to the Gbox420 messages and send control messages. HiveHQ provides a web-based test client that is perfect for this. Open the site and click Connect on the top right corner, once connected select the Add New Topic Subscription button. The topic you need to subscribe to is your MqttTopic/# ( For example GrowBoxGuy/Gbox420/# ) notice the # at the end, this tells the broker you are interested in every subtopic of the topic:
After you subscribed to the messages from the test sketch should start coming in every 10 seconds. Time to send a test control message! Under Publish section type in your MqttTopic/ControlCommand and specify the value you would like to set in the message box, hit Publish to send the message.
Two things should happen now:
On the website the control message should appear as we subscribed to the root topic:
On the serial output, the received control message and the triggered function`s output will be displayed. Notice that the next MQTT publish already contains the updated value for InternalFan
This is how the Main module's MQTT report looks like if sent to HiveMQ under the GrowBoxGuy/Gbox420/ topic, the arriving message is a valid JSON object:
In this section, we are going to set up an MQTT broker on a local server to receive messages from the Gbox420 sketch. The server can be based on Windows, Linux (Debian, Raspberri Pi, Ubuntu), or Mac, you can find the installers at https://mosquitto.org/download. In this guide, I'm going to set up the MQTT Broker on a PC running Linux Mint 20.1. It is recommended to use a DHCP reservation or Static IP address for the server hosting the MQTT broker.
In the example we will have two devices with DHCP reserved IP addresses:
192.168.1.228: Linux Mint PC running the Mosquito MQTT broker
192.168.1.131: The ESP8266 SoC running the ESP-link firmware and relaying the sensor readings from the Main module.
On the PC open a terminal window and add the Mosquitto repository, then install the Mosquitto MQTT Broker and MQTT Client:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
At this point, the Mosquitto MQTT Broker is installed and should be running. If you would like to make sure it's running or restart it, try:
sudo pkill mosquitto
mosquitto -v
Set to automatically start-up:
sudo systemctl enable mosquitto.service
Check current status:
sudo systemctl status mosquitto.service
Once the Mosquitto broker is installed test subscribing to a topic from a command prompt window, and publishing a message from another terminal window on the same machine the broker is installed on.
Subscribe to a topic in Terminal 1:
mosquitto_sub -t "RootTopic/SubTopic"
Publish a message to a topic in Terminal 2 --> Shows up on Terminal 1:
mosquitto_pub -m "Hello, World!" -t "RootTopic/SubTopic"
Next, we will generate user credentials for connecting to the MQTT broker. In the below example I'm generating a user called GrowBoxGuy, the script will ask for a password twice and store the encrypted credentials in the /etc/mosquitto/passwd file:
sudo mosquitto_passwd -c /etc/mosquitto/passwd GrowBoxGuy
Create the default.conf file under /etc/mosquitto/conf.d with the following content:
sudo touch /etc/mosquitto/conf.d/default.conf
sudo chown -R $USER:$USER /etc/mosquitto/conf.d/default.conf
sudo echo "allow_anonymous false" >> /etc/mosquitto/conf.d/default.conf
sudo echo "password_file /etc/mosquitto/passwd" >> /etc/mosquitto/conf.d/default.conf
sudo echo "listener 1883" >> /etc/mosquitto/conf.d/default.conf
The config file will disable connecting to the MQTT server without a username and password and set up listening for incoming connections on port 1883. To apply the changes stop the existing mosquitto service and start it with the new config file:
pkill mosquitto
mosquitto -c /etc/mosquitto/conf.d/default.conf
Now open a new terminal window and subscribe with the new username and password:
mosquitto_sub -t 'GrowBoxGuy/Gbox420' -u "GrowBoxGuy" -P "SuperSecretPassword"
Then from a new terminal window publish a test message:
mosquitto_pub -m "Hello, World!" -t "GrowBoxGuy/Gbox420" -u "GrowBoxGuy" -P "SuperSecretPassword"
In case the service is not running you will get an "Error: Connection refused" message when subscribing or publishing messages:
To receive messages from other devices we need to allow incoming TCP connections on port 1883 in the MQTT Broker's firewall:
Non-persistent temporary rule (Removed at reboot):
sudo iptables -I INPUT -p tcp --dport 1883 --syn -j ACCEPT
You can verify the rule by running: sudo iptables -L
Store as persistent firewall rule (Based on nixCraft Guide) :
sudo mkdir /etc/iptables/
sudo chown -R $USER:$USER /etc/iptables
sudo iptables -I INPUT -p tcp --dport 1883 --syn -j ACCEPT
sudo iptables-save > /etc/iptables/rules.v4
sudo ip6tables-save > /etc/iptables/rules.v6
sudo iptables-restore < /etc/iptables/rules.v4
sudo ip6tables-restore < /etc/iptables/rules.v6
sudo apt install iptables-persistent
sudo systemctl status netfilter-persistent.service
Now that the MQTT Broker is configured and running we need to connect the ESP-Link's MQTT client to it. On the ESP-Link's website navigate to the REST/MQTT tab and change the Server IP and Port number to point to the MQTT Broker, and set the Username and Password fields. When ready click the Update server settings! button and restart the ESP-link firmware from the Debug log tab using the Reset esp-link button. Wait a few seconds for the ESP-link firmware to reload, then restart the Arduino board using the: μC Console tab - Reset μC button. After this go back to the REST/MQTT tab where the MQTT client state should now display connected state:
The last step is setting up the publish - subscribe topics and LWT (Last Will and Testament) message in the Gbox420 sketch. This can be done in two ways:
From the Main module's Setting.h file by editing the following variables:
MqttPubTopic: Topic to send reports to
MqttSubTopic: Listen for commands under this topic
MqttLwtTopic: When the Gbox420 sketch goes offline unexpectedly the MQTT broker will send out a message in this topic to notify all subscribers of this event.
MqttLwtMessage: The content of the LWT message
2. From the Settings tab. These changes are stored to the EEPROM and kept between reboots, however, they get lost if the sketch defaults are restored from Settings.h.
Before pressing the Report to: MQTT button highlighted on the previous screenshot, create a subscription to the Topic (MqttTopic variable) on the MQTT broker server to see the JSON log getting relayed from through ESP-link:
mosquitto_sub -t 'Gbox420/' -u "GrowBoxGuy" -P "SuperSecretPassword"
When pressing the MQTT button, you should see two JSON formatted reports arriving at the subscribers of the MQTT Topic:
Event log: {"EventLog":["Refilling tank","Refresh triggered","Mixing reservoir","Aero pump IDLE"]}
Sensor readings Log: {"Log":{"IFan":{"S":"1"},"EFan":{"S":"1"},"APump1":{"S":"1"},"Lt1":{"S":"1","CB":"82","B":"82","T":"1","On":"14:20","Of":"02:20"},"Lt2":{"S":"1","CB":"91","B":"91","T":"1","On":"10:20","Of":"02:20"},"LtSen1":{"R":"939","D":"0"},"DHT1":{"T":"24.80","H":"40.80"},"Pow1":{"P":"560.20","E":"732.74","V":"231.80","C":"2.54","F":"50.00","PF":"0.95"},"Hemp1":{"S":"1","H1":"1","P1":"1","PS1":"100","PT1":"120","DT1":"300","WB1":"19.23","WR1":"6.73","DW1":"19.00","WW1":"0.00","ET1":"2.00","OT1":"0.20","WL1":"13.00","H2":"1","P2":"1","PS2":"100","PT2":"120","DT2":"300","WB2":"19.95","WR2":"7.60","DW2":"18.57","WW2":"20.57","ET2":"2.00","OT2":"0.20","WL2":"13.00"},"Aero1":{"S":"1","P":"5.94","W":"4.52","Ma":"7.00","Mi":"5.00","AS":"1","LS":"6.04","PSt":"1","PS":"100","PT":"420","PP":"10","SE":"1","D":"3.00","DI":"6","NI":"13"},"Res1":{"S":"1","P":"1.13","T":"1200.00","W":"39.93","WT":"16.75","AT":"24.20","H":"36.50"},"Main1":{"M":"1","D":"0"}}}
You can use JSON Lint or JSON Formatter to check the validity of these messages and format them: