Configuration to connect ESP modules to Domotics
To install LedDomotics the following components are required:
- A MQTT broker (http://mosquitto.org)
- Node-RED wiring tools (http://nodered.org)
For NodeMCU boards:
For the H801 box:
- Arduino IDE with ESP8266 support and MQTT library
Follow the instructions at http://mosquitto.org/download/
Execute the following commands to install Node-RED sudo npm install -g node-red node-red
Go to node-red_addr:1883 and start creating the links between all nodes. The following set-up is an example of a working Node-RED architecture for Domoticz to LedDomotics message delivery.
The following function converts the Domoticz output (light level 0-31) to LedDomotics
var position = msg.payload.svalue1;
var level = 0;
if(position === 0){
} else if(msg.payload.nvalue === 0) {
} else {
// position will be between 0 and 31
var minp = 0;
var maxp = 31;
// The result should be between 0 and 1023
var minv = 0;
var maxv = Math.log(1023);
// calculate adjustment factor
var scale = (maxv-minv) / (maxp-minp);
level = Math.exp(minv + scale*(position-minp));
}
level = Math.floor(level);
return {"payload": {"led":2, "l": level, "ft": 1000}};
In this message led
refers to the output pin of the ESP, l
is the PWM level ranging from 0 to 1023 and ft
is the fadetime, the effective time to use for a transition between levels.
NodeMCU is a platform to run lua scripts on the ESP8266. Make sure to modify the settings in init2 for the correct wifi credentials and mq.lua for the correct MQTT-broker location. To run the scripts simply flash the NodeMCU firmware and upload the three base files to the flash memory.
Each device will identify using its MAC address by default. By looking at the messages send via MQTT (tip: use the websocket interface of mosquitto) you will find a new topic with the correct identifier for each node.
The connection between nodes and Domoticz runs via MQTT. To enable this connection activate the MQTT plugin of Domoticz. Doing so makes Domoticz to send all updates via MQTT to the Domoticz/out topic. As Node-RED listens to this topic, it transforms the message to a shorter message processable by the ESP node.
To add the device we need a hardware module in Domoticz. As the communication depends on a virtual device, and we dont use Domoticz drivers, you should create a dummyHW device in Domoticz. For the led dimmer works best using a LightRF type. The autogenerated ID for the device is used to match messages with hardware in Node-RED.
When MQTT is enabled this device should automatically send messages when its status changes, a.k.a. you change the slider.
To help update nodes in the local network a script upload.py
is created. This script takes a local file and uploads it to a remote node. After uploading it automatically restarts the node.
This is useful for system upgrades as it allows over-the-air script deployment. Note that the available storage capacity on the ESP chip is very limited. Another common failure is low memory available due to many modules loaded. This can cause the MQTT protocol to fail.