My home automation projects built with MQTT and Node Red

Part 5 – MQTT Code For Your ESP8266 Devices

I wasn’t very happy with the MQTT code that I’d been running on my devices, so decided to revamp it before sharing. Then I got a got a bit carried-away and added a few too may bells and whistles to make it usable for most situations.

I’ve now created a cut-down version of the code, which may still contain too many features for some people, but at least they are fairly easy to remove if they aren’t needed/wanted.
The code is designed to be used with a Sonoff S20 Wi-Fi Smart Switch, but because of the way that it’s structured it’s fairly easy to modify for other uses.

As the code uses multiple tabs in the Arduino IDE, which creates separate files within the same folder, it’s easier to download as a .zip file so I’ve uploaded it to GitHub:

If you want to set-up a simple Node-Red flow to operate the Sonoff from Blynk then here’s an example:

The code publishes data to 15 different topics. These provide data that falls in to several categories:

Firmware:

  • File name
  • Compiled date
  • Compiled time

Hardware:

  • MAC Address
  • IP Address

Settings:

  • MQTT Client ID
  • Heartbeat Frequency (randomised for each device)

Status:

  • Alive/Dead (using Last Will and Testament)
  • Wi-Fi Connect Count
  • MQTT Connect Count
  • RSSI (as part of the Heartbeat)
  • Free RAM (as part of the Heartbeat)
  • Uptime in DD:HH:MM:SS (as part of the Heartbeat)
  • Number of 49 Day Rollovers since reboot (untested)

Sonoff Specific:

  • On/Off Status (“1” or “0”)
  • Power (this is a subscribed topic, publishing a “1” will turn the Sonoff On, “0” will turn it Off)

Here’s an example of the output from a device:

Short_Filename = MQTT_base_config_for_Sonoff_S20.ino
Compiled_Date = May 5 2019
Compiled_Time = 16:36:01
MAC_Address = 5C:CF:7F:79:CB:B5
IP_Address = 192.168.1.220
MQTT_Client_ID = Sonoff_Test_Device_1_7982005
Heartbeat_Millis = 5222
Status = Alive
WiFi_Connect_Count = 1
MQTT_Connect_Count = 1
RSSI = -49
Free_RAM = 45480
Uptime = 00:01:51:18
Rollover_Count = 0
Sonoff_OnOff_Status = 0
Power = 0

You’ll see from the Node-Red screenshot that the Heartbeat values can be used to monitor if a device is online. In this case I’ve used the RSSI reading to trigger one of Pete Scargill’s Timeout nodes configured like this:

Note that version 1.1.1 of the Timeout node has a minor bug. The documentation for the node says “On start it sends out the SAFE message - then nothing as long as it is kept topped up by input”. But, the SAFE message isn’t actually sent when you Deploy the flow in Node-Red. The workaround is to tick the “Repeat message every second” checkbox. I’ve alerted Pete Scargill to this issue.

Feel free to either adopt and adapt the entire code, or use parts of it for your particular projects. If you come across any problems the please let me know.

One thing that’s worth mentioning…
At the time of writing the latest ESP Core is v2.5.0
I’ve had lots of problems with this version (as have many others) and I’ve downgraded to v2.4.2
If you have problems with Wi-Fi and MQTT connections and you are running v2.5.0 then please downgrade to the earlier version before contacting me to say that you have a problem.

Pete.

5 Likes