HuaFan Wifi Switch

Python and python framework
Post Reply
suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

HuaFan Wifi Switch

Post by suborb » Friday 16 December 2016 23:14

These are fairly cheap wifi switches (about £10) that also measure power usage, as usual they are limited to 10A so not great for appliances, but given the time of year worked well for controlling Christmas lights. As you'd expect they use an esp8266.

By default these plugs phone home and I'm not so fond of that sort of behaviour so with a bit of wire sniffing and the usual decompilation trick they can now be controlled locally using a script that can be found here: https://github.com/suborb/huafan-wifiswitch

I've not (yet) reversed the AP registration so you'll have to use the hfsmart app to get it signed onto your network. At that point you should be able to firewall it off and the app will continue to work okay.

But where's the fun in that?

So you'll need to create two dummy devices:

1. A switch

The on command should be:

Code: Select all

script:///home/domoticz/hfswitch.py --host 192.168.0.2 on
and the off command:

Code: Select all

script:///home/domoticz/hfswitch.py --host 192.168.0.2 off
Change the path and IP address to whatever is appropriate.

2. An electric meter

Then you can set up a cronjob to periodically ask the switch the current power usage:

Code: Select all

/home/domoticz/hfswitch.py --domoticz_url http://localhost:8090 --domoticz_idx 848 --host 192.168.0.2 current
Change the domoticz_url to the address of the web interface for domoticz, domoticz_idx to the index of the electric meter, and host to the IP address of the switch.

suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by suborb » Tuesday 20 December 2016 18:28

I didn't realise that a Python hardware interface was in development.

So, I've created a python plugin to control these without the need for cron, external scripts etc. It's available from the repo mentioned in the previous post. Just copy domoticz/huafanwifiswitch.py into the plugins folder, remove plugins/manifest.xml and restart domoticz. It'll then be available as new hardware to add. Two devices are created when a switch is added: one switch, and one electric usage meter.

I suspect that this thread may now in the wrong forum. So please move if necessary.

trixwood
Posts: 510
Joined: Friday 18 March 2016 8:20
Target OS: -
Domoticz version: Offline
Contact:

Re: HuaFan Wifi Switch

Post by trixwood » Tuesday 20 December 2016 22:13

if they are esp8266 can you set 'em up without the cloud using:

http://www.pridopia.co.uk/pi-doc/ESP826 ... ndsSet.pdf

commands?

they would be perfect except the whole cloud thing to set 'em up.

suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by suborb » Wednesday 21 December 2016 2:07

Yes they can be setup locally without a cloud connection.

Details on the readme on the project but you send an AT+SSIDINFO to configure the network then an AT+SAPSTA to switch to station mode.

The standard commands I tried had no response.

e_boekhout
Posts: 6
Joined: Saturday 01 November 2014 16:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by e_boekhout » Tuesday 16 May 2017 19:49

Hi suborb,

I've got a HuaFan switch but i can't control the switch with the phyton script you provided.
Switch Type HF-SP16
I added the switch to my Wi-Fi network with the HF smart app.
I copied the phyton script to my raspberry and made it executabele.

When i execute the phyton script, it hangs after i press [enter]

[email protected]:~/domoticz/scripts$ ./hfswitch.py --host 192.168.180.49 on

When i CTRL-C i get:
^CTraceback (most recent call last):
File "./hfswitch.py", line 110, in <module>
main()
File "./hfswitch.py", line 88, in main
execCommand(args.host, "AT+SOPEN=1")
File "./hfswitch.py", line 39, in execCommand
data = sock.recv(1024)
KeyboardInterrupt

The phytonscript executes fine with no arguments:
[email protected]:~/domoticz/scripts$ ./hfswitch.py
usage: hfswitch.py [-h] [--host HOST] [--time TIME] [--raw RAW]
[--domoticz_url DOMOTICZ_URL] [--domoticz_idx DOMOTICZ_IDX]
command
hfswitch.py: error: too few arguments

Could the encryption key be different?

e_boekhout
Posts: 6
Joined: Saturday 01 November 2014 16:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by e_boekhout » Wednesday 17 May 2017 19:39

I also found this:
When I send a broadcast message targetted at port 7682 I get:
HF-W02,1,192.168.180.49,54-9A-11-C0-1E-9A\r\n

So this is a different version what is mention on the GitHub page (HF-W0B).

It may be possible that the encryption method or encryption key is different on this version.
Can you assist me how to decrypt the messages?

qwerk
Posts: 268
Joined: Tuesday 22 July 2014 7:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by qwerk » Wednesday 17 May 2017 20:11

@e_boekhout , maybe it is easier to put new firmware on them ( esp8266 easpeasy)
I could not find an online shop to buy them. do you have a link for me?

e_boekhout
Posts: 6
Joined: Saturday 01 November 2014 16:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by e_boekhout » Wednesday 17 May 2017 22:11

@suborb
This is the link:
http://m.hktdc.com/suppliers-products/W ... 4/3043894/
It is a Europe version.

suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by suborb » Thursday 18 May 2017 0:04

Sorry I missed your question.

The encryption key is the same for all flavours of the switch as far as I can tell. The switch just won't respond if it doesn't understand the message.

So try changing the commands so they are AT+CLOSE rather than AT+SCLOSE etc - that's worked for some other flavours of the switch and I think it's the correct command structure for the W02

e_boekhout
Posts: 6
Joined: Saturday 01 November 2014 16:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by e_boekhout » Friday 19 May 2017 14:29

@suborb,

Thanks for your reply.
Unfortunately i've already flashed new firmware on the ESP, so i cant test it.
If someone else still has an original switch , it would be nice to know if the alternate commands work.

suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by suborb » Saturday 20 May 2017 23:56

Can you share how you flashed new firmware on it? I didn't get around to tracing the pins since I got it working, but I think I can sacrifice one switch to experiment with.

Hesmink
Posts: 153
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by Hesmink » Monday 29 May 2017 10:39

Ordered one, I will have a go at getting it working.

Hesmink
Posts: 153
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by Hesmink » Tuesday 13 June 2017 18:06

suborb wrote:Sorry I missed your question.

The encryption key is the same for all flavours of the switch as far as I can tell. The switch just won't respond if it doesn't understand the message.

So try changing the commands so they are AT+CLOSE rather than AT+SCLOSE etc - that's worked for some other flavours of the switch and I think it's the correct command structure for the W02
Received my switch, it identifies as HF-W02

Indeed, commands with S don't work, but removing the S fixes it.
Command that work:
on
off
version
getsignal

Command that returns nothing:
gettime

Command that doesn't work:
current

Unfortunately, I need current to work, but somehow it doesn't return anything (keeps waiting).

Hesmink
Posts: 153
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by Hesmink » Wednesday 14 June 2017 13:11

Hesmink wrote:
suborb wrote:Sorry I missed your question.

The encryption key is the same for all flavours of the switch as far as I can tell. The switch just won't respond if it doesn't understand the message.

So try changing the commands so they are AT+CLOSE rather than AT+SCLOSE etc - that's worked for some other flavours of the switch and I think it's the correct command structure for the W02
Received my switch, it identifies as HF-W02

Indeed, commands with S don't work, but removing the S fixes it.
Command that work:
on
off
version
getsignal

Command that returns nothing:
gettime

Command that doesn't work:
current

Unfortunately, I need current to work, but somehow it doesn't return anything (keeps waiting).
Also got gettime/settime working, but no current.

suborb
Posts: 9
Joined: Friday 16 December 2016 22:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HuaFan Wifi Switch

Post by suborb » Thursday 15 June 2017 22:56

That's a shame. I'm guessing that the hf app doesn't show it either?

If it does then you'll have to sniff the network traffic (easy if your AP is OpenWRT/ubnt) and feed the messages into the decode function of the script.

I've just re-read, and it looks like it's only the HF-W0B that supports current measuring - I'm guessing I was just lucky in that all of mine are that model.

Hesmink
Posts: 153
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by Hesmink » Thursday 15 June 2017 23:04

suborb wrote:That's a shame. I'm guessing that the hf app doesn't show it either?

If it does then you'll have to sniff the network traffic (easy if your AP is OpenWRT/ubnt) and feed the messages into the decode function of the script.

I've just re-read, and it looks like it's only the HF-W0B that supports current measuring - I'm guessing I was just lucky in that all of mine are that model.
I couldn't find any current measuring in the HF app. Tried both the Android and IOS version. The Android version has a bit more options, but no power usage.

The page describing the one I bought, does tell about power measuring, so I asked this question to the seller.

Hesmink
Posts: 153
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi
Domoticz version:
Location: The Netherlands
Contact:

Re: HuaFan Wifi Switch

Post by Hesmink » Friday 16 June 2017 11:14

I contacted Huafan, and it turns out the EU version is still the old version without power measuring.
A new EU version with power measuring will be available soon (and I will get a notification when it's available).

The information on the seller page was incorrect for EU sockets.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests