Page 1 of 1
HuaFan Wifi Switch
Posted: Friday 16 December 2016 22:14
by suborb
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.
Re: HuaFan Wifi Switch
Posted: Tuesday 20 December 2016 17:28
by suborb
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.
Re: HuaFan Wifi Switch
Posted: Tuesday 20 December 2016 21:13
by trixwood
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.
Re: HuaFan Wifi Switch
Posted: Wednesday 21 December 2016 1:07
by suborb
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.
Re: HuaFan Wifi Switch
Posted: Tuesday 16 May 2017 19:49
by e_boekhout
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]
pi@RPI:~/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:
pi@RPI:~/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?
Re: HuaFan Wifi Switch
Posted: Wednesday 17 May 2017 19:39
by e_boekhout
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?
Re: HuaFan Wifi Switch
Posted: Wednesday 17 May 2017 20:11
by qwerk
@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?
Re: HuaFan Wifi Switch
Posted: Wednesday 17 May 2017 22:11
by e_boekhout
Re: HuaFan Wifi Switch
Posted: Thursday 18 May 2017 0:04
by suborb
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
Re: HuaFan Wifi Switch
Posted: Friday 19 May 2017 14:29
by e_boekhout
@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.
Re: HuaFan Wifi Switch
Posted: Saturday 20 May 2017 23:56
by suborb
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.
Re: HuaFan Wifi Switch
Posted: Monday 29 May 2017 10:39
by Hesmink
Ordered one, I will have a go at getting it working.
Re: HuaFan Wifi Switch
Posted: Tuesday 13 June 2017 18:06
by Hesmink
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).
Re: HuaFan Wifi Switch
Posted: Wednesday 14 June 2017 13:11
by Hesmink
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.
Re: HuaFan Wifi Switch
Posted: Thursday 15 June 2017 22:56
by suborb
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.
Re: HuaFan Wifi Switch
Posted: Thursday 15 June 2017 23:04
by Hesmink
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.
Re: HuaFan Wifi Switch
Posted: Friday 16 June 2017 11:14
by Hesmink
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.