Home > Allwinner H-Series, Debian, Espressif, Hardware, Linux > How to use Sonoff POW with ESPurna Firmware and Domoticz Home Automation System

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.

  1. January 21st, 2017 at 19:59 | #1

    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.

  2. tkaiser
    January 21st, 2017 at 20:33 | #2

    @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 save an additional few mW by also disabling Ethernet). But this requires good firmware support on the router to make use of the usb0 device (has to be bridged with Ethernet, crappy vendor firmwares don’t provide that, the open source alternatives do)

  3. January 21st, 2017 at 20:36 | #3

    @Xose Pérez
    Thanks for letting me know. I thought I may have missed something. Keep up the good work!

  4. January 21st, 2017 at 20:43 | #4

    @tkaiser
    I’m running the stock firmware, so no such fancy features. I’d have to enable USB then, would that take less or more power than Ethernet?

    If I had a router with enough memory running Debian or Ubuntu, I would not need to use NanoPI NEO at all, as I’d just install Domoticz directly on the router. I think it’s probably much more challenging to install Domoticz on a LEDE router (which would need at least 256MB RAM).

  5. tkaiser
    January 21st, 2017 at 21:12 | #5

    @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 some impact on the storage location of the databases (both regarding random IO performance requirements when a lot of sensors are used and data gets flushed to disk ASAP and reliability concerns if storage media of low quality is used).

    BTW: Since you confirmed that installing Domoticz on one Armbian supported device works like a charme that means automagically that all +40 SBC are supported (since it’s only about software dependencies that are automagically met in this case — local I2C sensors and so on are a different story since drivers/kernel matter here). That means Domoticz can also run on beefy router hardware like the Clearfog Pro for example 😉

  6. rebeL
    January 22nd, 2017 at 02:07 | #6

    @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.

  7. January 22nd, 2017 at 11:25 | #7

    Quick update. This morning I’ve installed python3 and mqspeak to the board, and set the timezone

    The board appears to be happy just being powered from the USB port of my modem:

    • Athar
      January 22nd, 2017 at 20:33 | #8

      @cnxsoft

      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.

  8. January 22nd, 2017 at 20:37 | #9

    @Athar
    I installed Python 3 because it is required by mqspeak.

    I understand Pete’s script is only used on the Linux server, while ESPurna is installed directly on Sonoff, so they are separate.

  9. tkaiser
    January 22nd, 2017 at 21:12 | #10

    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 to ensure minimum consumption as top priority for those small IoT boards. With Armbian NanoPi NEO, Air and OPi Zero all consume less than 2W while booting due to pretty conservative settings (that are responsible for longer boot times too). With your current settings (max 912 MHz cpufreq and therefore feeding the H3 SoC all the time with just 1.1V VDD_CPUX and 2 disabled CPU cores) maybe NEO would even survive a cpuburn-a7 run when powered from the modem’s USB port.

    Anyway: the main problem with powering through Micro USB is that most people aren’t aware of this 500 mA limitation (many ‘smart’ hubs/chargers won’t provide more when device doesn’t signal that it’s capable of any of the USB power delivery specs) and use crappy cables that lead to severe voltage drops.

  10. March 27th, 2017 at 09:59 | #12

    @cnxsoft
    Finally, I bought a pack of 10 cables (4″ / 11 cm) for $4.59 @ http://www.ebay.com/itm/Lot-10-cables-4inch-11cm-568B-CAT5E-UTP-Ethernet-RJ45-Patch-Cable-Network-Cable-/200995317150

    I wish they had another color other than just “yellow fluo”.

  1. No trackbacks yet.