Please, proceed carefully following the tips published in this blog, specially when Main Power is involved. I'm not responsible for any damages caused by what is written in this blog.
Thank you

Friday, 5 October 2018

Home Automation System - Two PZEM-004T and a single ESP8266

After my successful experiments with the PZEM-004T and a Nodemcu ESP8266 aim to monitor power consumption (devices are actually working well in my ground floor power monitoring system, see https://domoticsduino.blogspot.com/2018/05/home-automation-system-energy.html), I decided to update my photovoltaic production monitoring system, actually based on a Wifi 433Mhz receiver (see https://domoticsduino.blogspot.com/2017/08/home-automation-system-mqtt-and-rf433.html).

Actual system has a problem due to an invalid read of power values when the inverter is switched off. In fact it gives me about 250W power production...

My idea is to use the same devices used for the ground floor power monitoring system...so a PZEM-004T and a Nodemcu ESP8266.

These are the devices:



Because my inverter is near the electrical panel in the garage, I decided to let the things more complicated adding another PZEM-004T to also monitor the basement consumption with the same ESP8266.

Then I added also a temperature and humidity sensor to the same circuit.

My following step was to check that a single NODEMCU ESP8266 was able to control both PZEM-004T...My tests were successful

Here is it my first PZEM-004T linked to the main output cable of the inverter...on the right you can see the DHT11, temperature and humidity sensor (it's a not so accurate sensor, but it's right for my kind of measurement...)

This is the second PZEM-004T, linked to the main cable of basement power, inside the electrical panel

Complete view:

Now it's time to write the software and test it.

I decided names for the MQTT topics and I created items and sitemap objects for OpenHAB

This is what we could see in my OpenHAB BASIC UI

Energy Consumption value is the sum of all the sensor power (2 of them are already running)
Values are different related to the previous screenshot because they continuously change

Last step: I created some charts in the Grafana dashboard

My primary target was reached: my production monitor give about 0W when the inverter is switched off

Saturday, 1 September 2018

Home Automation System - SONOFF 4 Channel integration with OpenHAB

In the previous post, I spoke about my SONOFF 4 channel, describing the physical installation.

Now, this device will help me to drive 4 lights: 1 internal and 3 exterior; of course, OpenHAB will be my control system and MQTT will be the message protocol.

Let's go to setup the SONOFF, using its web interface

The first step will be to setup the module type, using the menù Configuration -> Configure Module

Then, in Configuration -> Configure MQTT, we will put the MQTT topic, splitted in TOPIC and FULLTOPIC as TASMOTA ask

In my case the device will use the following topic smarthome/sonoff4ch14/###. TASMOTA will replace ### with the type of message it will be sending (for example cmnd for commands, state for status information etc etc)

No more details on the other configuration options...they are the same as the other SONOFF

Now it's time for OpenHAB...we will add 4 items, one for each light.

Create a new file, called sonoff4ch.items, in the usual folder /etc/openhab2/items; in the file we will write the items configuration:

As you can see, MQTT topic is the same for all items. Only one thing changes...the property name, indexed by a number to indicate the relay number.

Finally, the last step: put the 4 items into a sitemap. So, write down 4 switch into the sitemap file

(these are the 3 items for exterior lights...)

And here is my complete sitemap:

Now let's push some buttons and see what happens.

Friday, 17 August 2018

Home Automation System - SONOFF 4 Channel

Finally I found a little time to continue my Home Automation System.

I already own a SONOFF 4 channel...I think the biggest one...

This device is able to drive 4 power outputs through a single Wifi connection. I will use it to power up my lights.

As well as input and output power connector,s it also has 4 pushbuttons which drive the 4 relays. Basically it's like 4 SONOFF Basic in a single device and with a single Wifi connection.

As usual I will use the TASMOTA firmware instead of the original one, which permits to use the device only through EWelink software. With TASMOTA firmware I'm able to easily integrate the device in OpenHAB using MQTT protocol.

Here is my SONOFF in flashing mode

As usual, it's easy to flash the firmware...just follow the official Wiki

Please, remember that this is an irreversible operation. You cannot recover the original firmware

In order to be able to control the SONOFF also through a traditional button, I decided to use the 4 pushbuttons integrated in the device... by connecting them appropriately to the buttons / wall switches ...

I never throw anything away and in this case a male / female old serial cable will be very useful to connect comfortably and plug and play the buttons / wall switches with the SONOFF buttons.

Thanks to Piero, we soldered the serial pins to the 4 pushbuttons on the SONOFF

getting the connector out of the SONOFF box

On the other side, we have an ethernet cable linked to the wall buttons

and we soldered to it the male serial connector

Ok, now it's time to configure the device to be integrated in OpenHAB through MQTT

Stay tuned...

Wednesday, 30 May 2018

Home Automation System - Energy consumption monitor...the definitive release

Several days have passed since my last post on the Home Automation System and we left a pending speech about the measurement of home consumption.

My prototipe works fine, so it's time to use it.

My final target is to build 3 power monitor, one for each floor. The first ready to use device is for the main floor.

I will plug the device where the main power cables arrive. So I plan to power on the 5V adapter for the ESP8266 and the PZEM-004T.

Here is the device linked to the main power cable and powered on:

Now, let's give a look to the MQTT messages, using mqtt-spy:

Perfect...all work as expected.

Now it's time for OpenHAB and Grafana.

I setup OpenHAB to get data as numerical variable, using the MQTT topic already defined. Then I edited my sitemap to view these data. Here is the result:

It's an interesting thing the comparison between production and consumption.

The last step is the visualization of historical data with Grafana, comparing production (green) and consumption (yellow):

Now, we just have to wait for other PZEM-004T...already ordered.

Stay tuned.

Wednesday, 18 April 2018

Home Automation System - Energy consumption monitor...mqtt protocol

Here we are again to speak about energy monitor and OpenHAB. I made some step forward with my experiments.

In the previous post I made some tests with the PZEM-004T Energy Module and an Arduino UNO.

The test was OK...and now I would try to replace Arduino UNO with a cheaper Esp8266 NodeMCU.

There are some differences, because Esp8266 NodeMCU works with 3.3V but Arduino works with 5V, as the PZEM-004T Energy Module.

In the previous test I took 5V from Arduino board and so there was no need to use an external supplier. Now with the Esp8266 NodeMCU it's different because it needs 3.3V.

But...my Esp8266 NodeMCU is a particular model, called LoLin and can be powered with 5V through microUSB cable and it has an output pin with the same voltage as main power.

So, I will use an old power supplier which provides 5V and 400mA.

Let's plug all together.

Last step: the software

Let's use the same software used for Arduino, just to check the everything works well.

Here is the serial output.

18W lamp:

100W lamp:

It's all OK...nice!

Let's edit the software to introduce MQTT protocol. This is the idea: if energy values are similar, I will send the data every 1 minute; otherwise, when a value differs from the previous one, send the data immediately.

This is my MQTT topic (just for testing purpose)


using MQTT-SPY I check the transmission:

All work as expected...

My next job will be to integrate this device with OpenHAB and see the data on its sitemap and on Grafana dashboard

Saturday, 10 March 2018

Home Automation System - Energy consumption monitor...the first test

After a little stop, I'm going to tell you about my Home Automation System.

In particular, I would like to speak about some test I made to build a power monitoring system.

I'm trying to build something easily integrable in OpenHAB. I need a system able to acquire data and send them to OpenHAB, to be remotely accessible and use them to trigger alarms and notifications.

I need a little device which can be plugged in some junction box...

Looking for it on internet I found the Eastron sdm120c Modbus...

A nice product which can be query through its RS485 serial interface to fetch data about energy consumption (voltage, current, power, etc etc). It can be easily connected to an Arduino or Raspberry.

I was buying it when I found this one: PZEM-004T Energy Module, a little device with a current clamp able to get power consumption. It has a serial interface and so its data can be read by Arduino / Raspberry. It has little dimensions and it's very cheap. I bought it for 8€, shipped at home

Currently I bought only one...if my tests will be ok, I'll buy others. Mi target is to monitor home power consumption for each floor.

Now I have to choose a MCU. I would like to use a Esp8266 NodeMCU; it's a little device, has built in wifi capabilities and is very cheap! If it will not work as expected, I will use an Arduino board.

Ok, let's begin with some experiment just to see if PZEM-004T Energy Module will work. I will use an Arduino board because it's easier to plug than the ESP8266 NodeMcu

If it will work good, I'll try the ESP8266 NodeMcu.

The device needs 5V for the low-voltage side and 220v for the power line

Let's begin to plug:
  • GND and 5V from Arduino to the device;
  • serial output from the device to 2 Arduino GPIO

To test a load, I will use a simple lamp.

I downloaded a simple software for Arduino, which writes the data acquired on the serial output. Here is the output when the lamp is switched off: no current, no power:

Switch on the lamp...

and we get the power...

Perfect. It works as expected. Next step will be to use an Esp8266 NodeMCU instead of Arduino and then use MQTT tu send data to OpenHAB.

Thursday, 25 January 2018

Home Automation System - Alarm and OpenHAB...Let's build it

As I said before, here is the second and last part of my post about Home Alarm and OpenHAB, just to monitor input status using remote connection...a thing that actually I'm not able to do...

Here is my checklist:

  • take all the alarm inputs to the Raspberry PI3
  • build a litte board to interface input signal with the raspberry GPIO and let the voltage be compatible (raspberry GPIO are tolerant to a max of 3.3v)
  • plug the input signal of alarm into the input of this board...and output pin of this board to Raspberry GPIO
  • install and setup the GPIO Binding in OpenHAB (see https://github.com/openhab/openhab1-addons/wiki/GPIO-Binding)
  • finally, add new items in a OpenHAB sitemap

Let's begin

Cables and others...

My raspberry is near the main central unit of the alarm, where we can find 3 of the 11 input signals which we must manage...the other 8 are managed by an expansion board, far from the central unit and connected to it through a serial bus...

Luckily, I found a couple of empty pipes which connect central unit and expansion board...So, it's easy to put a ethernet cable into a pipe and use as connection.

I chose an ethernet cable (thanks to Piero for the suggestion) because it has 8 wires, it's shielded and it's easy to place

So, I placed this cable...and then I linked all its wires to the 8 signals of the expansion board...

This is the junction box where the expansion board is placed (the white box in the bottom) and where all the input signal arrived from the respective sensors / contacts.

The blue one is the ethernet cable...its wires are connected to the black clamps which, in turn, are connected to the expansion board.

Ok, I can close now.

Let's go upstairs, where we can find the junction box for the central unit.

Here is it...you can see the blue ethernet cable

I found also the 3 remaining input signal not managed by the expansion board but directly connected to the central unit...

I took the 8 wires of the ethernet cable plus the other 3 to the new self-built voltage divider board. Here is it: on the righe side we have the inputs, on the left side we have the outputs...in the middel we can find the voltage divider resistors

Last step: connection between outputs and the Raspberry GPIO 

We have 12 wires instead of 11 because there is also GND in common between sensors and raspberry.

Ok, all is connected. Raspberry boot up and home alarm is still working...nothing wrong for now.

Just some test: I open and close the windows and see that the voltage drop to 0v and then go back to 3v (3v is the max voltage divider output)

OpenHAB configuration

Now it's time to switch to the software side of the project.

On OpenHAB I installed GPIO Binding, to use the GPIO output with OpenHAB.

Open PAPERUI (the web interface to configure OpenHAB) and follow the path Add-ons -> Bindings

Here you can find the official documentation

Then, I created new items on OpenHAB, for the 11 inputs. So, I created a new file called gpio.items in the folder /etc/openhab2/items and wrote down the 11 items, one for each GPIO.

Here is my file (Visual Studio Code editor with Platformio extension)

As you can see, every row is linked to a pin number, which is the GPIO number of the raspberry; activelow parameter is used to tell to OpenHAB that the contact is OPEN when the voltage is 0 ... in my case its value is "no" because when my contact is OPEN we have positive voltage...

Finally I can add the newly created items to a sitemap...here is the result: (OpenHAB app on mobile)

Now I have to verify that all is working well...🍀🍀🍀


I'm not responsible for any damages caused by what is written in this post. Please proceed carefully and by your intentions