ESP Easy + MQTT + Node-RED: bi-directional communication, dashboard

ESP Easy integrates several default communication protocols, several based on MQTT (Domoticz MQTT, OpenHAB MQTT and PiDome MQTT). These protocols are very focused “home automation servers”. If you develop your own applications (home automation or others) with Node-RED, we will see how to communicate with connected objects developed with ESP8266 and ESP Easy. MQTT allows two-way communication between the two platforms. This will make it possible to recover measurements or to know states but also to drive the GPIO of the ESP8266 cards.

Configuration used

 

ESP Easy Configuration

Give the device a name (here esp001). This will be the “header” of the Topic.

espeasy configuration protocole mqtt openhab

The device is named DHT22. Two variables are published (temperature and humidity). They will be used to build the MQTT Topic. Unnecessary but mandatory, it is necessary to indicate an IDX other than 0. On the shield DHT22, the measurements are retrieved on the GPIO2.

esp easy configuration device dht22

Domoticz, OpenHAB, PiDome: what differences?

ESPEasy offers three different MQTT configurations in the R120 version. They are adapted to communicate with home automation software Domoticz, OpenHAB and PiDome. The difference lies in the form of published and subscribed topics. Here is a summary table and an example published by ESP Easy.

Protocole Topic (publish) Example
Domoticz MQTT domoticz/in domoticz/in {“idx”:1,”nvalue”:0,”svalue”:”22.00;38.40;0″}
OpenHAB MQTT /%sysname%/%tskname%/%valname% /esp001/DHT22/temperature 22.00
/esp001/DHT22/humidity 38.40
PiDome MQTT /hooks/devices/%id%/SensorData/%valname% /hooks/devices/1/SensorData/temperature 22.00
/hooks/devices/1/SensorData/humidity 38.40

As you can see, it will be much easier to process messages based on the OpenHAB MQTT or PiDome MQTT protocol because ESP Easy publishes a topic for each variable. But the easiest to use and uncomplicated OpenHAB MQTT that very clearly identifies the ESP Easy object, the device and the variable.

Edit Publish Template:

If the mask does not fit you or you want to standardize it to make it identical to other projects, nothing prevents us from modifying the mask. To do this, simply go to Tools and then Advanced.

  • Subscribe Template: mask the message from the MQTT Broker, so coming from Node-RED (or other)
  • Publish Template: mask for publishing messages to the MQTT broker
  • Message Delay: this is also where you will be able to adjust the frequency of publication of messages (in milli-seconds).

esp easy configuration mqtt publish subsribe template message delay

Be careful though. Changes to the Template are not saved. If you change the communication protocol for any reason, the created Template will be reset to the default one of ESP Easy. It’s a shame but it’s good to know.

Is communication secure?

Absolutely not. All messages are sent in clear, no security is supported on the side of ESP Easy. It is not very annoying if the object is a temperature sensor, on the other hand it could quickly become a problem if it is a door opening command or the portal of your home! On the other hand, it is possible to secure the Broker MQT, ESP Easy integrating the authentication by password.

Create your own protocol

If you decidedly did not find your happiness, (you need to secure communication) you can follow this tutorial and create a new protocol. It’s quite simple to do with an existing module.

Retrieve and process MQTT messages in Node-RED

For this tutorial, I opted for the OpenHAB protocol which is very easy to process under Node-RED because the variables are quite distinct. Node-RED natively handles the MQTT protocol, so there is no plugin to install. Search the Nodes to find them.

If the module is not in the list, check that the Node is enabled from the palette manager (if necessary a tutorial to learn how to manage Node-RED modules).

node-red active mqtt node

Place an MQTT in node on the flow and open the configuration window. Start by setting the server by clicking the pencil to the right of server.

https://www.projetsdiy.fr/wp-content/uploads/2017/01/1-node-red-mqtt-esp-easy-in

If Node-RED and Mosquitto are installed on the same machine, specify localhost in the field otherwise specify the IP address of the MQTT Broker. Same for the port that by default is 1883.

node-red mqtt brocker configuration

Confirm. Back to the previous window, choose the server from the list and enter the Topic. Here we will recover the temperature from the DHT22 of the Shield Wemos D1 Mini on topic /esp001/DHT22/temperature .

Note. Do not forget the ‘/’ at the beginning of the Topic

Advice. If you have a lot of topics, it’s best to give the Node the name of the topic.

node-red mqtt node topic esp easy openhab

Confirm. Plug in a Debug Node and publish the flow. Open the Debug tab, the first values will soon arrive (depending on the publication frequency configured on the ESP Easy object).

node-red mqtt esp easy debug message payload

Et voilà !

Driving the GPIO from Node-RED (relay, led, sonof …)

It’s good to be able to receive data from an ESP Easy connected object, but it’s even better if you can control the GPIO (input / output). With the OpenHAB protocol, ESP Easy expects a message from the form / dev_name / command.

In this tutorial, we had seen how to drive the GPIO from Jeedom, we will apply the same method with the MQTT protocol under Node-RED.

Add an MQTT out node to the flow and open the setting window. Choose the Broker on which the messages will be sent. The topic must be in the form /device_name/gpio/gpio_number . The contents of the message should be 1 (On) or 0 (Off). The outputs pwm can also be controlled. In this case, it is enough to replace gpio by pwm in the previous topic.

node-red mqtt out send message esp easy openhab protocole

All that remains is to control the output. For that, nothing better than to create a Dashboard.

Note. While writing this tutorial, the GPIO did not respond to MQTT messages. After unplugging the Wemos D1 Mini, everything is in the other. In fact the problem seems to arise when publishing a message directly with the command mosquitto_pub.

Add a Dashboard (Node-RED) for your ESP Easy Objects

In the previous tutorials, we saw how to create a Dashboard to a Node-RED project. This is an opportunity to test on an ESP Easy object. Here we are not going to redo the previous tutorials (part 1 – add a dashboard, part 2 – add graphics). Place two gauges and connect each to a topic (temperature / humidity).

Then place a switch. Open the settings and change the output type to digital. For On payload, enter 1 and 0 for Off payload. Connect the switch to the previously created MQTT node.

node-red mqtt espeasy swich dashboard gpio 12

Here is the flow obtained

node-red mqtt espeasy dht22 gpio dashboard

And the flow code you just need to import

[{"id":"ead26598.68ece8","type":"mqtt in","z":"f725662f.6dde08","name":"/esp001/DHT22/temperature","topic":"/esp001/DHT22/temperature","qos":"2","broker":"f9d88ab3.7054b8","x":180,"y":100,"wires":[["fc102c9f.55303"]]},{"id":"fc102c9f.55303","type":"ui_gauge","z":"f725662f.6dde08","name":"Gauge : Température","group":"4a05f42b.44c96c","order":0,"width":0,"height":0,"gtype":"gage","title":"Température DHT22","label":"ESP Easy - MQTT","format":"{{value}}°C","min":0,"max":"50","colors":["#66ccff","#ff8000","#ca3838"],"x":440,"y":100,"wires":[]},{"id":"10194e45.fa8972","type":"mqtt in","z":"f725662f.6dde08","name":"/esp001/DHT22/humidity","topic":"/esp001/DHT22/humidity","qos":"2","broker":"f9d88ab3.7054b8","x":170,"y":160,"wires":[["3589c685.9054da"]]},{"id":"3589c685.9054da","type":"ui_gauge","z":"f725662f.6dde08","name":"Gauge : Humidité","group":"4a05f42b.44c96c","order":0,"width":0,"height":0,"gtype":"gage","title":"Humidité DHT22","label":"ESP Easy - MQTT","format":"{{value}}°C","min":0,"max":"50","colors":["#66ccff","#ff8000","#ca3838"],"x":450,"y":160,"wires":[]},{"id":"b722e79d.b0da18","type":"ui_switch","z":"f725662f.6dde08","name":"GPIO 12","label":"GPIO 12","group":"4a05f42b.44c96c","order":0,"width":0,"height":0,"passthru":true,"topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":220,"y":220,"wires":[["16383035.ed5a3"]]},{"id":"16383035.ed5a3","type":"mqtt out","z":"f725662f.6dde08","name":"/esp001/gpio/12","topic":"/esp001/gpio/12","qos":"","retain":"","broker":"f9d88ab3.7054b8","x":460,"y":220,"wires":[]},{"id":"f9d88ab3.7054b8","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"4a05f42b.44c96c","type":"ui_group","z":"","name":"ESP Easy + MQTT + Node-RED","tab":"cc4c9cae.cfb85","order":1,"disp":true,"width":"6"},{"id":"cc4c9cae.cfb85","type":"ui_tab","z":"","name":"ESP Easy","icon":"dashboard"}]

Deploy flow and open a new window in the browser at http://localhost:1880/ui

node-red mqtt esp easy mqtt mosquitto publish subscribe gpio dht22

And now you get the measurements of the DHT22 and you can activate or deactivate the GPIO12 (pin D6 on the Wemos D1 Mini)!

 

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: