beveradb / sonoff-lan-mode-homeassistant

Home Assistant platform to control Sonoff switches running the latest Itead firmware, locally (LAN mode).
MIT License
74 stars 36 forks source link

mine ( Sonoff POW) does not go to LAN mode. #1

Closed haffi78 closed 5 years ago

haffi78 commented 5 years ago

Not even the app see it if I disable its internet access. the port 8081 is open for sure. but nothing :(

beveradb commented 5 years ago

Sorry to hear that. How are you disabling internet access to the device? Does your LED flash like this? Sonoff basic controlled locally in LAN mode

Are you able to try this test script (modify IP, then run with nodejs sonoff-lan-mode-test.js): https://gist.github.com/beveradb/0def87f98205bd6647a574eea609b146 and let me know what it prints out?

haffi78 commented 5 years ago

Sending User Online Message: {"action":"userOnline","ts":"0","version":6,"apikey":"nonce","sequence":"1547575950201","userAgent":"app"} Received Message: {"error":0,"apikey":"ccdafee5-af3b-4aa4-b118-c02d5ada08ae","sequence":"1547575950201","deviceid":"1000470e23"} Switching On Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"on"},"sequence":"1547575959575","userAgent":"app"} Switching Off Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"off"},"sequence":"1547575964582","userAgent":"app"} Switching On Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"on"},"sequence":"1547575969581","userAgent":"app"} Switching Off Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"off"},"sequence":"1547575974587","userAgent":"app"} Switching On Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"on"},"sequence":"1547575979587","userAgent":"app"} Switching Off Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"off"},"sequence":"1547575984588","userAgent":"app"} Switching On Sending Update Message: {"action":"update","deviceid":"nonce","apikey":"nonce","selfApikey":"nonce","params":{"switch":"on"},"sequence":"1547575989591","userAgent":"app"} Switching Off

haffi78 commented 5 years ago

plus the light is just red... :( and rapid blinkinn blue besided it just like your green.

beveradb commented 5 years ago

Hmm, that's odd. Can you control it fine in LAN mode from the eWeLink app?

If so, the next step to getting this working would be to get a record (using tcpdump) of exactly what message the app is sending to the device to control it, so we can replicate that in this code.

If you don't know how to capture a tcpdump of the app communicating with the device, there's helpful guide (targeting Tuya devices using the "Smart Life" app, but it's the same concept) here: https://github.com/clach04/python-tuya/wiki/Setup

haffi78 commented 5 years ago

Nope lan mode on the ap does not work :(

Sent from my iPhone

On 19 Jan 2019, at 02:13, Andrew Beveridge notifications@github.com wrote:

Hmm, that's odd. Can you control it fine in LAN mode from the eWeLink app?

If so, the next step to getting this working would be to get a record (using tcpdump) of exactly what message the app is sending to the device to control it, so we can replicate that in this code.

If you don't know how to capture a tcpdump of the app communicating with the device, there's helpful guide (targeting Tuya devices using the "Smart Life" app, but it's the same concept) here: https://github.com/clach04/python-tuya/wiki/Setup

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

beveradb commented 5 years ago

Ah, sorry to hear that. I'm afraid if your device doesn't support LAN mode (that is, it can't be controlled by the eWeLink app), there's probably not much we can do to modify this component to support it either.

Your device (Sonoff POW) isn't in the list of supported devices on the eWeLink LAN Mode page, although there is a comment from eWeLink 6 months ago suggesting they were going to add more support "soon".

The fact that your device is actually listening for websocket messages on the right port, and indeed even responds (the "Received Message" section in your output above) does offer a glimmer of hope though.

Can you control your device using @peterbuga's component? He has implemented much more comprehensive support for multiple switch types by extracting UIID information from the eWeLink app.

Additionally, you may wish to try this new test script I implemented yesterday for the user in the other issue, as it may just work if your device actually broadcasts multiple "outlets" for some reason: sonoff-lan-mode-multi-outlet-test.js

beveradb commented 5 years ago

In addition to the above, I've just ordered a Sonoff POW to hopefully do my own trial/error investigation with it 😄 - it'll be here at some point in the next week or two so I'll let you know once it arrives and I've replicated your setup.

PlayedIn commented 5 years ago

I'm wondering how people are "blocking" the internet. My cable modem/router isn't able to block solely by IP address. I have to specify a range of ports. I tried 8081, 80, 443, etc... but I ended up just choosing a range of 1-10,000 before I was successful in making my sonoff go into lan mode.

beveradb commented 5 years ago

My Sonoff POW arrived yesterday so I've done a little more testing with it.

Firstly, it arrived with firmware version 2.0.4, so I soldered some jumper pins on, backed up that firmware over the serial interface, then updated it to 2.6.1 with the eWeLink app (backing up that version too - both are available to download here if they're useful to anyone; you can easily flash them with esptool).

img_20190127_003040

Next, I did some initial testing and I was definitely able to get the POW into LAN mode in the same way as with my Sonoff Basic - I just blocked access to all IP addresses other than my local network (192.168.0.0/16) in my router settings page. There are lots of ways to do this, but for my router (Virgin Media UK), this is what that looks like:

2019-01-27 02_06_55-hub 3 0

I know this worked because the blue LED was solid when the device was connected to the internet, but about a minute after I blocked it, it switched to the "two-flashes every 2 seconds" pattern:

sonoff-pow-lan-mode

At this point, I then reopened the eWeLink app and enabled LAN mode, and it connected successfully:

screenshot_ewelink_20190127-015431

I then connected to it successfully with the test_sonoff.py script in this repository, confirming the websocket is open and communication is possible:

(non-hass) andrew@thinkpad : /mnt/c/Users/andrew/AndroidStudioProjects/sonoff-lan-mode-homeassistant/non-hass-scripts $ python test_sonoff.py
2019-01-27 02:18:30 - DEBUG - Sonoff class initialising
2019-01-27 02:18:30 - DEBUG - initializing websocket
2019-01-27 02:18:30 - INFO - WebsocketListener initialising, connecting to host: ws://192.168.0.76:8081/
2019-01-27 02:18:30 - DEBUG - attempting to call WebSocketApp run_forever with ping_interval: 145
2019-01-27 02:18:30 - DEBUG - sending user online websocket msg: {"action": "userOnline", "userAgent": "app", "version": 6, "nonce": "109538681204596", "apkVesrion": "1.8", "os": "ios", "at": "at", "apikey": "apikey", "ts": "1548555510", "model": "iPhone10,6", "romVersion": "11.1.2", "sequence": "15485555105331254"}
2019-01-27 02:18:30 - DEBUG - received websocket msg: {"error":0,"apikey":"897159a4-c537-4e1e-8083-1a0b92f6eba3","sequence":"15485555105331254","deviceid":"100046f760"}
2019-01-27 02:18:30 - DEBUG - received hello from deviceid: 100046f760, no action required

So, as far as I'm concerned I consider the above proof that the device can definitely be put into LAN mode.

However: unfortunately, my device didn't seem to actually work in LAN mode... Even from the eWeLink app, despite it clearly connecting fine - it then showed an error when trying to actually toggle the switch:

screenshot_ewelink_20190127-015540

With my modified test script to send a state update message to the device to toggle the switch, it seemed to receive the message happily but not actually take effect - the red LED state didn't change and the relay didn't click.

So, I'm not really sure what the deal is with the POW at this point. It feels like they half-implemented LAN mode, so it's running a websocket server and responds to the "user online" message, but doesn't actually operate the switch? Really odd if that's the case - further investigation required.

peterbuga commented 5 years ago

yet another reason for flashing :| i just ordered a Sonoff POW R2 and S26 (but don't when they might arrive though) and i might be of help to test and see if this one behaves the same (hopefully not 🤞)

haffi78 commented 5 years ago

Ok This is the same I have and pretty much same expirance as yours Andrew, I did for the first time get the app to connect but I cannot controll the switch even though it says its online. I just blocked the box from my nat so it does not get Natted out via iptables( on linux) and so it gets no connection. I would like to know if you do know what you use on the computer side ( hardware ) to flash, do you have a usb to serial that you have "hacked" or what is the best way to put a custom firmware on there ?

beveradb commented 5 years ago

Well, while the purpose of this repository is to try and help folks who can't/won't flash their devices for whatever reason, it is actually pretty easy to flash them, and in this case I'd probably recommend it as there's not much more we can do with the POW unless Itead release an update.

There are lots of instructions and methods for flashing Sonoffs (or any other device which uses an ESP8266), but they basically all boil down to:

  1. Connect cables to 4 pins on your ESP-based device (pin locations and connection method will depend on your device, but for the Sonoff POW it's super easy as there are 4 holes at the far end of the board).

    • If you're comfortable soldering, many people choose to solder on pin headers to Sonoffs, to make it easier to re-connect in future. That's what I ended up doing - see my above photo.
    • However, you can just as easily make a connection by placing cables with bent jumper pins on the end through the PCB holes and applying a bit of tension with a rubber band or something. I actually did that first with this device to test the connection; see photo below.
  2. Connect those cables to some kind of serial interface. I leave this quite vague as there are several things you can use as a serial interface, depending on what you own already or want to spend.

    • The easiest thing to use is a 3.3V USB UART/TTL/FTDI adapter, as this plugs directly into your computer and requires no additional setup. If you want to buy one, I'd recommend searching for "CP2102" on eBay/Amazon etc. (that's the name of a common but well-supported chip) and getting the cheapest one which will arrive in a reasonable timeframe for you. Here's an example, and here's a guide with photos showing how to use it.
    • If you own an Arduino UNO, you can use the built-in USB interface for this. There are several guides showing how to do this, but this video is pretty clear, even if you don't speak German.
    • If you own a Rasbperry Pi, you can use this as a serial interface. Again, there are several guides and videos online to help with this, but I'd start with this guide in the Tasmota project as it is very clear.
  3. Once you have successfully connected your ESP8266+ device to your computer (or Pi), use the official tool esptool to flash custom firmware of your choosing to the device. Lots of instructions available, I'd recommend the Tasmota guide again - especially the Backup step!

Hope that helps! And as promised, here's the photo of my Sonoff POW with my hack-job connection with jumper cables.

img_20190126_224605

Good luck! 😄 Closing this thread now as "wontfix" sadly as there's not much more we can do to fix the Itead firmware - however feel free to comment or get in touch if you want any other help/advice in flashing your device.