probonopd / ESP8266HueEmulator

Emulate a Philips Hue bridge running on an ESP8266 using the Arduino IDE.
MIT License
411 stars 93 forks source link

New Apps require Push-Link-Button #45

Closed janus-reith closed 7 years ago

janus-reith commented 7 years ago

New Apps, like the 2. gen Android App find the bridge, but won't get past waiting for the user to press the Push-Link-Button on the Bridge.

Same happens for the Luminance Hue App for Linux(relies on phue Phyton library), which throws an exception at the Push-Link screen: http.client.RemoteDisconnected: Remote end closed connection without response

probonopd commented 7 years ago

Can you send a pull request that presses the emulated button automagically?

janus-reith commented 7 years ago

I have to dig into the code first, as I don't have profound knowledge about the hue api yet

janus-reith commented 7 years ago

Do you have a reference code of how the hue bridge works?

Because i just tried this Java based Hue Bridge Emulator on my computer, and it behaved strange:

It does have an implementation to emulate the press the Link Button, however the 2. gen Hue App didn't find it at all.

The 1. gen Hue App found it, but then asked to push the Link Button: So I (virtually) pressed the button, and it connected and worked afterwards.

When i use the ESP8266 based bridge with the 1. gen App, it doesnt ask for the press of the Link button, so I wonder if there already is some kind of implementation for that here?

probonopd commented 7 years ago

I also mainly studied the Java based Hue Bridge Emulator. Philips has some documentation, too.

probonopd commented 7 years ago

Seems to be working with both iOS apps as of 1ad3705

marekhalmo commented 7 years ago

I have an issue with this with the original android app..

Could you help me?

probonopd commented 7 years ago

Please describe the issue in detail, and state the exact app version.

marekhalmo commented 7 years ago

No of the apps i tried are working (able to pair).. They discover the hue bridge but are not able to pair.. The original hue app (https://play.google.com/store/apps/details?id=com.philips.lighting.hue2) requests me to push "link button".. but there is no such thing in code.. I'm not sure if there is some external button that i need to connect.

I'm using esp-12e

marekhalmo commented 7 years ago

Hello, i did some research and i found that the linking works in the way that the HUE app sends post request to http://[IP]/api

After that the HUE bridge must respond with a username which the app can use to access the bridge.. in this case [{"success":{"username":"api"}}]

so i constructed following html:

<form action="http://[IP]/api" method="POST">
    <input type="submit" value="Submit">
</form>

if i launch this html in browser on my phone and hit submit i get an url not available error. If i enter the same url to a browser (e.g. do a GET request) http://[IP]/api i will get a response so the esp is available on network.

If i try the same page on my PC it works as expected, [{"success":{"username":"api"}}] is returned

I was thinking that some csrf policy on android might be the problem but the HUE app probably does not use a browser to execute the post..

Any ideas on what could block the post request?

marekhalmo commented 7 years ago

Hello, any new ideas on this??

2xAA commented 7 years ago

I tried this myself, for some reason my Android phone can't connect to http://[IP]/api with a standard GET request using Chrome but my macOS laptop can.

I've no ideas, but at least this is triaged.

marekhalmo commented 7 years ago

Hm.. OK.. probably the phone/network or ESP8266WebServer is the issue here.. I just opened the HelloServer sketch..

the page looks like this

<form action="http://192.168.254.15/api" method="POST">
    <input type="submit" value="Submit">
</form>

From pc it works fine From android the page is unavailable..

It does not matter if i enter a port after the IP

I will try to do a post from java by android app and let you know what error code will be returned

2xAA commented 7 years ago

Very strange, potentially a bug in the ESP8266WebServer? But Chrome is crosscompiled, so the results should not be different.

Could you try with Firefox on Android?

marekhalmo commented 7 years ago

The android test was done using webview only.. i can't launch it with ff or chrome.. let me try

marekhalmo commented 7 years ago

chrome from android works, default browser also (not htmlview)

marekhalmo commented 7 years ago

Now let's switch to the hueEmulator sketch and see if it logs a request

marekhalmo commented 7 years ago

Mkay.. so with Chrome this works on Android.. it might be something completely different :(( Aaaaaaaaaaaaaaaaggghhhrrrrr!

2xAA commented 7 years ago

This is weird, I'll try again when I'm back home with Chrome on Android as it didn't work for me before.

2xAA commented 7 years ago

Calling the api GET now works for me using Chrome on Android, WebView (Chrome) and Firefox...

marekhalmo commented 7 years ago

Get works as expected.. that was never an issue... Also the put method fails only in webview.. chrome for android works ok...

I'm not sure now if the post for /api never gets send from the app or what. There is nothing being logged to console if i put serial.print into the auth method of lightservice.. i was reading the HUE api spec but i dont know if there is s9mething failing before that..

marekhalmo commented 7 years ago

Ok.. so i put Serial debug info into every single function.. and this is what i get when i try to pair the bridge to official hue app: After i hit search: configFn GET 274163 {"name":"hue emulator","swversion":"81012917","bridgeid":"18FE34FFFED2C582","portalservices":false,"linkbutton":true,"mac":"18:FE:34:D2:C5:82","dhcp":true,"ipaddress":"192.168.254.37","netmask":"255.255.255.0","gateway":"192.168.254.1","apiversion":"1.3.0

After that no requests are being made.. so it seems that the app does not try the authFn.. after a while i get descriptionFn

10http://192.168.254.37:80/urn:schemas-upnp-org:device:Basic:1Philips hue (192.168.254.37)Royal Philips Electronicshttp://www.philips.comPhilips hue Personal Wireless LightingPhilips hue bridge 2012929000226503http://www.meethue.com18:FE:34:D2:C5:82uuid:2f402f80-da50-11e1-9b23-18:FE:34:D2:C5:82index.htmlimage/png484824hue_logo_0.pngimage/png12012024hue_logo_3.png

.. but that is just some software pinging the ip (i guess)..

What methods should be called in order for the app to connect?

marekhalmo commented 7 years ago

I'm not sure why the json is not ending with } ,... is this just stripped?

marekhalmo commented 7 years ago

Mkay .. now we are getting somewhere.. The json from /api/config is stripped! Why?

also the wholeConfigFn returns stripped json (invalid)

marekhalmo commented 7 years ago

Ah crap! I had 2 aJson libraries.. one on my lib folder and one in documents (without the 4096 byte modification as specified in the sketch)..

So after i fixed the lib, everything works as expected!

Thanx everyone for help.. Nice coding exercise 🐇

probonopd commented 7 years ago

@janus-reith is it also working for you now, so that we can close this issue?

marekhalmo commented 7 years ago

Yes, you can.. thank you!

marekhalmo commented 7 years ago

Just a note for others.

HUE ap looks for the bridge usind SSDP protocol. This worked ok.. and returns bridge ip. After that it makes a get request to /api to retreive bridge capabilities. This returned incomplete json. Unfortunately no error was displayed in app.

This caused the app never calling the auth (post to /api)