I've been thinking about how to approach home automation for some time. I wanted something simple, open and easy to deploy with low maintenance cost. I was even considering developing my own custom solution but then came across the Domoticz home automation system, which turned out to satisfy pretty much all my needs and even brings some interesting unseen possibilities that might come in handy in the future. All this served in a nicely polished package, open source and free of charge, nice!

What's essential for me is that Domoticz can be easily installed on a Raspberry PI and that it supports the concept of virtual devices (sensors, switches, etc.) that can be queried and controlled via simple HTTP requests. This gives one great freedom and mainly enables me to use cheap and widely available ESP8266-based nodes as Domoticz clients, these can be deployed around my house in huge quantities serving all kinds of purposes.

I won't cover how to install Domoticz on a Raspberry PI in much detail, but it's very simple and straightforward (nicely covered here), basically all you need is to ssh into your PI and trigger the command bellow:

Then it's time to create a virtual device. In this case, we will populate the device with temperature, humidity and barometric pressure data coming from a BME280 sensor connected to an ESP8266 development board via I2C. The actual hardware looks like this:

The image blow shows how to proceed, first we need to add a new virtual hardware. To do this, open Domoticz in your web browser, go to "Setup/Hardware" and name the new virtual hardware e.g. "esp_virtual" (1), then choose "Dummy" (2) as its type, and finally click "Add" (3).

After the new hardware has appeared in the above list, click "Create Virtual Sensors" (4), then give your virtual device a name e.g. "ESP Storage" (5). For the BME280 sensor, choose the "Temp+Hum+Baro" (6) sensor type, this specifies the type and URL format of the data that will be pushed into the virtual device via HTTP requests.

After that, you can navigate to "Setup/Devices" to see the newly created virtual device as shows the image below. Please note that each of the devices has its own unique index "Idx", we will need this information later when issuing HTTP requests from the Lua script that drives the ESP8266.

At this point, it is very simple to verify that the virtual device has been set up properly. All you need is to slightly modify the URL below, namely change the IP address to match your Raspberry PI running Domoticz (10.0.1.20 in my case) and the index of the virtual device you've just added (7 in my case). Then you can open your web browser and enter the resulting URL, a modified version of this:

If you've already pinned up the virtual device in the dashboard, Domoticz should immediately reflect the values (temperature=15, humidity=59, barometric pressure=1006) that you've just sent encoded in the URL.

The final step is to program the ESP8266 so that it periodically fetches data from the BME280 connected via I2C and send it over to Domoticz. For this, I have prepared a Lua script (for NodeMCU) that you might want to reuse:

After running the script for several hours, Domoticz will reward you with a nice interactive graph:

I've been running this setup for several days now with two ESP8266 nodes and everything seems to be rock solid, it even survived a power outage without any trouble (all nodes automatically booted up and resumed once power was restored). Overall, I'm pretty happy with this setup and will definitely invest more into this infrastructure in the near future, stay tuned!

Can you explain how to connect the BME280 to the ESP8266? I found the ground and the 5 volt on the module but I am not sure which pins to use for the I2C SCL and SDA.Also it is not clear for me how to load the NodeMCU firmware and the Lua script. I tried the ESPlorer but with no succes.I hope you can help.Regards,Pino

Regarding the NodeMCU firmware, I use the nodemcu-build.com service that lets me build a custom firmware. Then I use NodeMCU Flasher to flash the firmware into the NodeMCU module. Finally, take a look at ESP8266 Lua Loader, it allows to upload Lua scripts into the module.

I can't go into much detail right now, but let me know if you need more help,FipS

Thanks, this helps. I m feeling that I am almost there.I have successful flashed with ESP8266Flasher.exe the ESP8266:

in file ESP8266MAC.txt you see this:20170331135810 AP MAC:1A-FE-34-D1-78-06STA MAC:18-FE-34-D1-78-06

So that looks ok. The blinking LED stops. (this would be the default program from the factory I guess)With LUAloader (you need to start it with administrator rights in Windows 10) I managed to upload the LUA script. (your script placed in txt file and renamed to init.lua)Set baud rate manual to 9600.

I would recommend checking if the sensor is properly connected, and that the firmware contains the BME280 module (I believe the firmware prints out the list of included modules to the serial output each time ESP restarts).

Also, check the i2c address of your BME280 sensor. It seems that the NodeMCU firmware expects that the address is either 0x76 or 0x77 (BME280_I2C_ADDRESS). I can imagine there might be a BME280 sensor with a different address in which case bme280.temp() would return nil to indicate unsuccessful readout.

I found what I have done wrong. On my ESP you see only D0 D2 D2 D3 etcAnd because I did not see D1 I used SD1 and SD2. But is seems to be a misprint. Now I have connected it to the D2 and D2 and it works. Thanks a lot for your help.

Regarding the PIR motion sensor, it should be pretty easy to hook it up to ESP8266. It uses just one pin named OUT to report its state. Basically, each time motion is detected the OUT pin goes HIGH. So it should be enough to just monitor changes on the pin, something like this:

Other sensor types might be handled similarly, but be aware the more data the sensor provides the more complicated it is to hook it up, especially if there is no direct support for the sensor in the NodeMCU firmware.