How to use Sonoff POW with ESPurna Firmware and Domoticz Home Automation System

Sonoff POW is an ESP8266 based wireless switch with a power meter that comes pre-loaded with a closed-source firmware that works with eWelink app for Android or iOS by default. But we’ve also seen Sonoff POW, and other Sonoff wireless switches from the same family, can be flash with open source firmware supporting MQTT (Message Queuing Telemetry Transport) lightweight messaging protocol such as ESPurna, and I initially sent data from Sonoff POW to ThingSpeak via an MQTT broker (mosquitto) to draw some pretty charts. I did that with the switch connected to a lightbulb, but I’ve since installed Sonoff POW in my office to measure the room’s power consumption minus the ceiling light and aircon as shown below.

Wall Mounted Sonoff POW WiFi Switch – Click to Enlarge

Sonoff cable mechanism is really a pain for hard copper wires, as they are hard to push inside the mechanism, and something come out. I finally managed by it took longer than expected to install. I had to cut the mains cable, and rewire the gang box too. The good thing is that I did not need to drill a hole in my wall, as the device is very light.

I could see the power value updated in ESPurna web interface, depending on the load on my computer, and whether I turned on or off other devices. That’s all good, but instead of using ThingSpeak, whose open source implementation is not updated, I decided to try Domoticz, and already wrote a short guide showing how install Domoticz in NanoPi NEO ARM Linux development board. I had not gone through the setup yet, as I had to study a little more, and upgrade Sonoff POW firmware first. I also planned to use vThings CO2 monitor with Domoticz, but canceled since it can’t be configured remotely, and a USB connection is needed.

ESPurna OTA Firmware Update

So I’ll focus only on Sonoff POW in this post, and first we need to update the firmware since Domoticz support is only recent. I’ll assume you have already followed the post entitled How to Build and Flash ESPurna Open Source Firmware to Sonoff POW Wireless Switch.

First we need to update platformio and ESP8266 development platform to the latest version otherwise we’ll get some build issues:


I updated the source code with git pull, but for whatever reasons the build failed, even after cleaning the code. So I did what any developer with enough experience would do in that case: start with a fresh check out ;), and rebuild the OTA firmware from there:


In order to update the firmware over the network, you’ll need to change sonoff-pow-debug-ota section in platformio.ini with your own IP address (upload_port) and password (in upload_flags) used in ESPurna web interface:


Once it’s done, you can upgrade the firmware, and then the file system as follows:


The Sonoff POW will reboot, and cut the power for about 2 seconds after both updates. My Sonoff POW is controlling my computer power, but that’s OK since I’m behind a UPS. Now I can access the web interface, and one of the improvement is that you’re being asked to setup a new password right after the update.

Click to Enlarge

I was then redirected to the Status page showing power, voltage (a bit low?), current, and power factor.

Click to Enlarge

I then jumped to MQTT menu to set the IP address to my NanoPi NEO board, and clicked Update.

Click to Enlarge

There’s a new DOMOTICZ menu which we’ll check out a little later.

Installing and Configuring MQTT in Domoticz

ESPurna communicates with Domoticz via MQTT, so the first task was to follow and adapt Domoticz MQTT wiki.

First login to your Domoticz server (NanoPi NEO) and access a terminal window to update the packages, install npm, node.js, Node RED, and mosquitto:


We then need to go to the Hardware page in Domoticz and configure a new “MQTT Client Gateway with LAN Interface” as shown in the screenshot below.

Click to Enlarge

We can test whether it works or not by creating a new Dummy device in the same Hardware section

Then click on Create Virtual Sensors, to add a new Temperature sensor which we’ll call Fictive Temp.

Now go to the list of Devices (Setup->Devices) to check the idx value (1 in our case), and a publish a MQTT message to update the temperature value of our virtual sensor:


The temperature switch from 0 to 25°C. Our installation is working. Great!

Using Sonoff POW with Domoticz

In theory, we should be able to get two type of data for Sonoff POW: relay status and power levels. However, after looking at ESPurna source code, domoticz.ino only seems to handle the relay status that can be changed from Domoticz web interface, but the power values are only send in pow.ino to the MQTT server, with data not directly compatible with Domoticz. Maybe I missed something as Tinkerman – ESPurna developer – can use Sonoff SC to send temperature data to Domoticz. Alternatively, it might be possible to convert that data somehow with Node RED, but that’s something I’ll try later. So today, I’ll only try to control the switch from Domoticz.

To do so, I created another Dummy device called Sonoff POW Switch, and from there, another Virtual Sensor of Switch type.

Click to Enlarge
Click to Enlarge

We’ve already configured MQTT in ESPurna web interface, and from the screenshot above,we can see that “Sonoff POW Switch” Idx is 3, a value we need to update in the DOMOTICZ section of ESPurna web interface.

Now I can go Domoticz interface in my phone, and not my computer since my office’s Ethernet switch will be turned off, click on the Switch tab, and turn on and off Sonoff POW by clicking on the lightbulb as shown below.

Click to Enlarge

It works fine, however note that the initial switch status was wrong (off instead on on), despite the switch sending regular updates to the MQTT server.

NanoPi NEO Power Adjustments and Installation

Normally, at this stage, it should be easy simply install NanoPi NEO outside the office close to my router in the living room. But I’ve come across a few issues doing so, which I’m going to report.

First I decided to make a very short Ethernet cable to connect NanoPi NEO directly to my router. I have done a couple of Ethernet cables in the past a few meters long, and they all work. I tested my ultra short straight Ethernet cable connections with a multimeter, and the 8 wires were properly connected, however, when I connected NanoPi NEO to the router with that cable it failed to get a link. Maybe there was aonther issue with the cable, so I made another one just as short… Another fail. It turns out very short Ethernet cables may cause issues, which are normally solved by twister pairs, but with such short cables the length of the twisted pairs is also extremely short, maybe 2 to 3 cm which may not be sufficient. So I ended up using a “normal” 1.5 meter cable, not as neat but it works.

The power strip close to my router was full, and since I did not want to add another, I decided to use the spare USB port on my modem router in order to power NanoPi NEO board. A USB 2.0 port can only deliver 2.5W max, so I was clearly looking for problems here. In order to avoid an issues, I made use of h3consumption script to adjust the behavior of CPU cores and disable unused peripherals.

Let’s check NanoPi NEO current settings in a terminal:


h3consumption allows us to change the following settings:


So I decided to disable USB, and use two CPU cores at most in order to limit the board’s power consumption, and avoid random reboots:


The changes were properly applied after a reboot.


I powered the board with my modem router, and could use it without issue. I’ll monitor NanoPi NEO’s uptime to check if this works.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX RK3588 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
13 Comments
oldest
newest
Xose Pérez
7 years ago

Hi
Again, great review. Just a quick note to let you all know that ESPurna 1.5 will support sending telemetry data to Domoticz too (power, current, voltage, temperature, humidity,… that is if you have to hardware). Hopefully it will be out in a few days (maybe even this same weekend). It will also support telemetry over REST API, pulses (switching off after few seconds on), more boards and some bug fixes, including a more stable version for the POW.

tkaiser
tkaiser
7 years ago

@Jean-Luc Aufranc (CNXSoft): Next time you connect a NanoPi NEO to your USB modem router simply use just the single USB cable instead of USB and Ethernet cable 😉 Not kidding but in case your modem router has a good firmware running (LEDE for example) you can simply use g_ether module on the NEO to activate USB Ethernet gadget mode and then use the same cable to power the board for data transmission (a simple search for g_ether in Armbian forum reveals details — performance is slightly better than Fast Ethernet so it’s the better choice anyway and you can… Read more »

tkaiser
tkaiser
7 years ago

@Jean-Luc Aufranc (CNXSoft) Disabling Ethernet on the H3 boards saves 200mW, disabling all USB ports just 125mW. See #25 here for details: https://forum.armbian.com/index.php/topic/1614-running-h3-boards-with-minimal-consumption/?p=14381 Please note also: On the OPi Zero I measured only ~70mW difference between active/inactive Ethernet. Maybe that’s due to H2+ SoC being ‘Fast Ethernet only’ (according to specs) and on H3 boards the internal Gigabit Ethernet MAC consumes the additional 130mW? No idea and not worth a closer look. Regarding Domoticz running on a router… I’m still curious how Domoticz deals with database updates (flushing to disk immediately or only every n minutes) since this might have… Read more »

rebeL
rebeL
7 years ago

@Xose Perez
Good to hear Mr. Perez.
Could you make Youtube-Video after the integration of telemetry data flow is stable?
I like to see -step by step- how to get ESPurna on my Sonoff POW devices, and I hope you will investigate in voice assistance imigration with your ESPurna home-automation.
In the future, a Door-Cam integration without dubious China/USA-webserver would be nice also.

Athar
Athar
7 years ago

Isn’t Python 2.7/8 supposed to be the default version with all the big libraries, compared with Python 3.x?
Or just personal taste here?

@Xose Perez

Great job…Your FW is becoming quite popular, compared with Arendst.

Question: Can we install ESPurna and Pete Scargill’s The Script at the same time since some Pete configs seem value add, even though they can be added to ESPurna easily as complement.

tkaiser
tkaiser
7 years ago

cnxsoft : The board appears to be happy just being powered from the USB port of my modem: A web search for ‘SBC consumption/performance comparisons’ should point to a rather long thread/review of certain SBC with consumption numbers in Armbian forum. Important: NanoPi NEO PCB rev 1.0 uses an inefficient LDO voltage regulator leading to both higher board temperatures and consumption. This has been fixed by FriendlyELEC with PCB rev 1.1 (or 1.2, don’t remember exactly). Current NEO boards run cooler and consume less. When we added support for the NEO in Armbian we discussed the use cases and decided… Read more »

paglop
paglop
6 years ago

I have a probleme with energie in Domoticz , it,s joule with increment all minute.

Khadas VIM4 SBC