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.
Contents [show]
Configuration used
ESP Easy Configuration
Give the device a name (here esp001). This will be the “header” of the Topic.
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.
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 uncomplicatedOpenHAB 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).
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 tutorialand 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).
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.
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.
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 TopicAdvice. If you have a lot of topics, it’s best to give the Node the name of the topic.
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).
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 gpioby pwm in the previous topic.
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.
0 comentarios:
Publicar un comentario