Has anyone tried integrating HuaFan smart sockets?

Hey all,

I found some cheapo chinese made smart sockets on amazon and ebay. They are Esp8266 based it seems.


Here is the mfrs website:

I also found someone who has written python code to integrate with Domoticz:

With this code would it be trival to integrate to HA? Or could it be a big piece of work?
Seems to me that it would be worth it since the sockets are very cheap and also have sensor for power usage etc.

Not quite trivial, but you should be able to create a command_line switch and use the python code as the command.

Having got a pair of these switches, I am also interested in using with HA without outside servers. Especially not servers in China! Nice compact ESP8266 based switches though.

My inexperience with developing HA components is a problem for me!

Thanks for the reply.
Looks like local control is possible from the link I posted earlier, and with some config on your router you should be able to firewall the switches from the offshore servers.
Sadly I dont have the skills to create component for these switches :frowning:

Firewalling is fine and there are so many IoT devices that require putting an app on a smartphone to get them going that I worry about hacking. Since I gave a poor rating about these plugs due to my worries, someone from the manufacturer has tried to find out why so. I do object to being addressed as ‘my friend’ as he is not a friend and I am a customer.

All that said, I am in the process of working on esp8266 boards (esp01) with a view to providing my own micropython code. It has been a bit of a learning curve but I have now got micropython working and the WiFi operating. I now need to re-program the HuaFan switches with a customised version of micropython. Much information on esp8266 and micropython is available at Home-assistant blog with links, Preparing the HuaFan Socket for flashing.

I will have to make temporary wire links to the sockets and experiment to get the right effect. I suspect the current monitoring and usage elements will be sacrificed for simplicity. I will also have to be very careful to ensure I do not plug the socket into the mains source for safety’s sake. Once micropython is in place, it should be possible to reflash locally rather than relying on external servers.

I will add a message about progress when I can. Hopefully I can make the binary available if I’m successful in reflashing these devices.

1 Like

Just to let you know I have (after much dexterous careful soldering) managed to get webREPL access to the first HuaFan switch. The unit is now in its box, connected to the mains and handles basic pin commands at the console.

>>> Disconnected                                                                                                                                      

Welcome to MicroPython!
WebREPL connected

import machine

pin = machine.Pin(0, machine.Pin.OUT)
pin = machine.Pin(1, machine.Pin.OUT)
Welcome to MicroPython!
WebREPL connected

pin = machine.Pin

pin = machine.Pin(0, machine.Pin.OUT)

It was a fiddle getting the TX data pin wired for console access to upload and download the original HuaFan binary and the MicroPython Binary. At one point, I thought I had fried the esp_8266 as i stupidly wired the power and ground to the RX and TX pins. However once I had sorted that out, all was well. Phew!

Now I have MicoPython working, I can proceed to generating the code to drive the switch. I’ll keep you posted.

1 Like

Are the devices easy to disassemble and to reassemble so that they are safe?
Can you share any photos?


In a word Yes.

Teardown instructions are available through one of the links I posted above. So long as you are not too brutal separating the plug top from the bottom (there are no screws) and do not damage the lugs holding the parts together it should be no problem. Certainly I do not have qualms about the modified plug/socket falling apart in use.

I do not have photos of the plug undergoing modification and in any case the teardown pictures in one of the above links show exactly what needs to be done. I have to say I did remove the daughter board with the esp8266 on board while experimenting (I have a powerful desoldering iron for surface mount component replacement) but I had replaced it into the motherboard before I flashed it. Another thing is that rather than soldering wire to the plug for flashing, I used wire wrap pins which are rigid and allow easy attachment of jumper leads. Much easier than flimsy wire.

AT NO TIME WAS MAINS POWER USED while the socket was disassembled for flashing. Far too dangerous!

3.3V was provided by a redundant Raspberry Pi and I used an old Intel based mini laptop running Debian Linux, xfce4 desktop and minicom for serial access.

I now have an issue in that the original MAC address of the socket has changed with flashing. Since I have more than one of these sockets (and maybe more in future), I need to change the MAC address back to the factory supplied address so there is no confusion with IP addressing. I have records of all the MAC addresses on my network so once i locate the value in flash, it should be simple to change.

1 Like

The link (https://github.com/arendst/Sonoff-Tasmota/wiki/Preparing-the-HuaFan-Smart-Socket--Smart-Plug-for-flashing) is broken for me :frowning:

I found the correct URL.
This is it:

I have a few of these up and running for a while now using patched versions of both the Sonoff-Tasmota Firmware and the Esp8266 Core for Arudino. This provides the same MQTT capability for relay control and the power/voltage/current readings as the Sonoff Power device.

Instructions to connect the HuaFan for programming are on the Sonoff-Tasmota Wiki (post above) but require some very fine control soldering. Way beyond me. Luckily there are a few connection points that you can get at with clip probes. There is a +5V and GND contact on the left side of the board and the right LED legs give access to GPIO0 and ESP RXD. The tricky bit is the ESP’s TXD line which is only accessible on the daughter board via the chip pin or an smd pad above. @apemberton, I am in awe of anyone that can solder onto this pad! So for me, I ended up connecting an alligator clip to a sewing pin 8) to make a probe and holding it in place with my finger during the firmware programming. Using the pcb contacts for power meant I had to use a 5V power supply as the FTDI was not able to source enough current to power the entire board.

I really like the idea in a previous post of using a Raspberry Pi if you don’t have a dedicated power supply. In this case, you can vamp off the 5V rail and a few of the GND pins on the RPi header.

As a slight precaution, I did wire everything up with the usb unplugged from the computer and the 5v supply switched off. I then got the TXD “pin” placed on the pad. When feeling secure, plugged in the usb cable and power on the 5v. You can then disconnect GPIO0 and tap the Arduino IDE to upload with your free hand. Hey, it only needs to work once as then you can flash OTA!

The process to build the HuaFan firmware is broadly the same as explained in the Sonoff-Tasmota Wiki but you need the latest release candidate of ESP8266/Arduino library (2.4.0-rc1) and the Sonoff-Tasmota firmware (5.2.4) plus a few edits to three files in the Sonoff-Tasmota repo as follows:

  • arduino/version 2.4.0-rc1/boards.txt: Needs the patch from PR 3288 which adds a Crystal Frequency menu to the Arduino IDE and lets you select the non-standard 40Mhz crystal used in the HuaFan.
  • sonoff/sonoff_template.h: Needs the HuaFan settings added to the list of supported modules as provided in PR 479. Note that an additional setting, 0, needs to be inserted between the first and second fields to reflect a change since the PR was raised.
  • sonoff/xsns_hlw8012.ino: Need to reduce the calibration lower limits for power (HLWPCAL) and current (HLWICAL) given that the HuaFan uses different resister values in the monitoring hardware. I used 2,999 and 1,499 respectively.

Happy to talk through if anyone is interested as this is a great little device with a really useful form factor.

1 Like

@PeteB, Thanks for your post, I wish I had the info you have provided beforehand. I don’t have any Arduino here so I’m unfamiliar with the PR’s quoted.

@hijinx, Thanks for noting the problem with the link. I have editted it (pesky backslashes!).

As I have had no experience of Python before coming to Home Assistant, (I have used several structured languages over the years but my first love is C), I am undergoing a big learning curve into the syntax of Python. Hopefully MicroPython has enough capability to serve my needs.

Now for encoding my MAC addresses from a string and putting in boot.py!

@apemberton, sorry, my post wasn’t very clear, ESP8266/Arduino is a very function rich C library based on the hugely popular Arduino API (but running natively on the ESP8266). It was created to help developers port their existing Arduino code across and speed the adoption of the ESP. But many people use this to develop solely for the ESP given the breadth of functionality it provides. Sonoff-Tasmota is built on top off this library. if your preference is C, then you might want to check it out ESP8266/Arduino. I have edited my earlier post to be a bit clearer.

@PeteB, I have taken your advice on C for Arduino as per 8266. I have the Arduino IDE running on one of my Solid-Run Cubox-i’s (quad core imx6 armhf boxes) and have compiled an run simple compilations. I am using an Adafruit Huzzah for development with the intention of transferring the files to the HuaFan switches (of which I now have 3 + 1).

I also have admit I have ruined one of these switches daughter boards with a shaky hand while soldering. I have bought several ESP12 boards so I will replace the ruined daughter board with one of those. A bit of a size problem maybe but I think this can be resolved. I have also bought a smaller solering iron with a pointed tip. I am considering making a jig with sprung test pins (I now know as Pogo Pins and have ordered 100 fine pointed and 100 ball point pins plus Adafruit make a test clip with an attached Pogo Pin (ADA1969) which I have also ordered but not due for a couple of weeks) to match the daughter board at the edge connector and R5. But first I will run Wireshark on an unmodified plug to see if there is a simpler way of loading a new binary to the plugs - a man-in-the-middle approach if you like.

I wish I had more time to dedicate to this project! Not helped by spending a complete day restoring my network and servers from a mains power glitch overnight. Bah!

URL is now: