The ESP-Now HUB is the bridge between my ESP-Now sensors and my MQTT server. The messages are passed through as JSON. Without any processing. Making the ESP-Now HUB very flexible. In my setup I use two ESP8266 board with an ESP-12F. The board are my own design and have been designed to be flexable in usage. The reason for having two ESP board is to improve the HUB's range. ESP-Now also operates on the 2.4 GHz range and this interferes with the regular Wifi signal.
Why use ESP-Now over regular Wifi? This answer is very simple: SPEED. Sending sensor data via Wifi takes the sensor 2-3 seconds. Doing this via ESP-Now it takes 0.2-0.3 seconds!! The battery life of my ESP-Now sensors increases dramatically. After sending the update the sensors go into deepsleep till they are needed again to send data. An ESP8266 uses only ~20 µA in deepsleep.
Download the source code and binaries here.
If you want to compile it yourself (and perhaps make some additional changes to the source code). You also need the following libraries that I have created: WifiConfig and DebugUtils. Make sure both are installed in your Arduino Library.
Other libraries used are downloadable via the Library Manager:
PubSubClient: https://pubsubclient.knolleary.net/
ArduinoJson: https://arduinojson.org/
Data flow
The data flow and format will be illustrated with the following examample:
1. ESP-Now Sends a JSON formatted message with topic and the Domoticz MQTT message. | "{"topic":"domoticz/in","message":"{"command":"switchlight","idx":86,"switchcmd":"On"}"}" |
2. Module "Receive 2 Serial" receives the message and relays it via the serial port + \n indicating message end. | "{"topic":"domoticz/in","message":"{"command":"switchlight","idx":86,"switchcmd":"On"}"}" + "\n" |
3. Module "Serial 2 MQTT" receives the message via the serial port. Splits the message into a MQTT topic and an MQTT message (Domotitcz) | topic : "domoticz/in" message : "{"command":"switchlight","idx":86,"switchcmd":"On"}" |
More information on how the Domoticz MQTT messages need to be build can be found on: https://www.domoticz.com/wiki/MQTT#Domoticz_MQTT_communication
Module "Receive 2 Serial"
This module requires no configuration. On startup it will show via the serial port its MAC address. This MAC address is the receivers MAC address and is needed to put into all EPS-Now Sensors. Make sure you write it down, you will need if for every ESP-Now sensor you want to connect to this HUB.
The module pupose is to receive the ESP-Now sensor data and pass it through via de serial port. Simply flash this with the source code available. No changes are required. It is a really simple and small program.
Module "Serial 2 MQTT"
You may want to change the following value in the source code before flashing:
const int iConfigPin = -1; // Pin used to enter config mode. Set to -1 to enter config mode on power on.
With the -1 value the ESP8266 automatically enters the setup mode when powering on. (This will only happen during power on, not during a reset!). You can use a different pin if you want.
With my specially designed ESP8266 board I can use pin 2 as the OTA/SETUP pin. It works in conjunction with pin 0.
At first boot there is no configuration set and it will automatically enter the Setup. You need to connect to the device via Wifi. One you have a Wifi connection browse to: http://192.168.4.1/.
You can now enter the following details:
SSID | Your Wifi |
Password | Your Wifi Password |
MQTT Server | Your MQTT Server (IP address or FQDN) |
MQTT Port | Your MQTT Port (Default is 1883) |
Other details are not required unless you want to use a fixed IP Address for the HUB.
Save your setting and Restart.