Next: NodeMCU, MQTT and Domoticz - part 2
In a previous post, I set up a tool chain to program a Wemos D1 mini with the NodeMCU firmware and showed how to upload and execute Lua scripts on the ESP8266 chip. In this post I continue with setting up a WiFi connection and updating a device in the home automation software Domoticz, a temperature sensor in two ways: using MQTT and using an HTML request.
Connecting to a Wifi networkWhen I checked the wireless IP address of D1 mini, I expected a nil answer, but instead something valid came back!
Check that the connection is ok, ask to see the IP address of the device.
You can log in on the router to look at connected devices. That way I found that the D1 mini identity was "ESP_B3B4B5" which is made up of the last 3 digits of its MAC address.
Publishing to an MQTT Broker
Lets replicate Elliot Williams's article MINIMAL MQTT: NETWORKED NODES. In what follows, I am assuming that there is an MQTT broker on a computer with IP address 192.168.0.49 on the network. Here is how I installed a broker on the Raspberry Pi hosting my home automation software.
Create a subscriber to all messages on the topic "home" from the MQTT broker on the Raspberry Pi.
We will create an MQTT client on the D1 mini which can both subscribe
and publish topics to an MQTT broker. We will look at publishing here. Once
the client is created, it must connect to the broker before publishing to a
topic. The following Lua code fragment does that publishing a message to
And as expected, the subscriber will see the message:
There is no feedback at the D1 mini end. Let us add at the very least a message that confirms that the connection to the MQTT server was successful.
Sending the Temperature to MQTT
Let's return to Elliot William's tutorial, to the section entitled Sensor Node. Following along with it, we will simulated a temperature sensor that is connected to the D1 mini which will publish the temperature to the MQTT broker. First we will create the sensor simulator which is essentially a random walk generator.
Test the function by printing the value returned by
Finally publish the temperature to the MQTT broker
Instead of doing this in a terminal, you could create a file, say
temp2mqtt.lua (or download it).
Upload the file and execute it as explained above.
Temperature Device in Domoticz
Since Domoticz is set up to
subscribe to MQTT messages with the topic
domoticz/in, a simple modification of the published message
in the previous script will update a temperature device in the home automation
The first step is to create a virtual temperature sensor. The steps are
- Click on the Setup tab.
- Click on the Hardware menu item.
- Click on the
Virtual. If you do not have
Dummy (Does nothing, use for virtual switches only)hardware, you will have to create it before hand.
in the dummy hardware row which on my system I called
- Name the device (I called it
Create Virtual Sensorwindow and change the sensor type to
- Click on the OK button. A message saying that the device was created and that it can be found in the devices list should appear.
- Click on the Setup tab.
- Click on the Devices menu item.
- Find the newly created device by name in the Name column and note its index number in the Idx column. Alternatively, start typing the name in the Search box, the corresponding device row will quickly be displayed.
- Click on the Setup tab.
- Click on the Settings menu item.
- Make sure that Temperature is checked under Active Menus at the bottom of the page.
- If a change was made here, go back to the top of the page and click on the Apply Settings
Verify that all is working correctly by manually changing the displayed
temperature using an HTML call in a web browser. This is the URL to display
a 10°C temperature:
The IP address has to be adjusted and so does the device index to the right of idx. The temperature is sent as a string value (
the numeric value (
nvalue) has no significance.
Once that is working, tweak the temperature script to create
temp2domoticz_mqtt.lua (available for dowload):
Every 10 seconds, the temperature is published by the D1 mini to the MQTT broker which then pushes it to Domoticz MQTT client and consequently Domoticz updates its database and Web interface. This indirect communication takes a few seconds so the D1 mini polling interval cannot be reduced too much.
Doing without MQTT
Of course, it is possible to bypass the MQTT broker. The firmware (the one I obtained from NodeMCU custom builds, not Elliot Williams build) contains an HTTP client capable of performing requests. First we check how it works in the terminal by setting the temperature to 8°C and then we will change the Lua script. It is necessary to use a callback function in the HTTP request. The one below comes from the HTTP module documentation.
Here is a modified Lua script (temp2domoticz_http.lua).
The next step is to subscribe to Domoticz' outgoing
MQTT messages that have for topic
domoticz/out. Those messages are
JSON strings as one would expect with Domoticz. That is
why I added the CJSON package to the version of NodeCMU installed on the D1 mini.