TuyaAPI / cli

🔧 A CLI for Tuya devices
MIT License
266 stars 43 forks source link

Unable To Link #2

Closed dlashua closed 6 years ago

dlashua commented 6 years ago

I'm trying to use "tuya-cli link-wizard".

I fill out all the information and eventually get a "Error: Timed out wating for device(s) to connect to cloud".

But, then I realized... the Tuya device is not connected to any network. How will tuyapi/cli or tuyapi/link communicate with it? Am I supposed to put the Tuya Device into AP mode and then connect to its WiFi SSID before attempting the link-wizard?

I know the Android Tuya app is able to register the device without it being in AP mode, but I also see the WiFi status of my Android device change as it's doing this. And, in looking over the code in tuyapi/link, I don't see any place where it's even attempting to do that. It's simply sending UDP packets to the broadcast address.

Please advise.

codetheweb commented 6 years ago

Tuya devices cleverly take advantage of two facts:

So setup data is encoded into the UDP packet lengths themselves, which devices then sniff.

If you provide the output of DEBUG=* tuya-cli link-wizard I can help.

(Credit for reverse engineering the protocol and providing POC code goes to @BillSobel.)

dlashua commented 6 years ago

That explains it entirely. The device I was running link-wizard on is connected via ethernet. I'll try again on another host. Thank you.

On Tue, Aug 14, 2018 at 9:33 PM Max Isom notifications@github.com wrote:

Tuya devices cleverly take advantage of two facts:

  • WiFi adapters can broadcast UDP packets of variable length when the said adapter is in any state. The adapter can be connected to a network, or just powered on.
  • It's very easy to sniff packet lengths (with or without being connected to a network).

So setup data is encoded into the UDP packet lengths themselves, which devices then sniff.

If you provide the output of DEBUG=* tuya-cli link-wizard I can help.

(Credit for reverse engineering the protocol and providing POC code goes to @BillSobel https://github.com/BillSobel.)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413075972, or mute the thread https://github.com/notifications/unsubscribe-auth/AD737_k4Wc9ATdZg5x_8uWK5eWG7_BQEks5uQ4hjgaJpZM4V9XTn .

BillSobel commented 6 years ago

Ethernet is fine as long as that broadcast data gets to a access point that transmits broadcast data...

On Tue, Aug 14, 2018 at 7:36 PM, Daniel Lashua notifications@github.com wrote:

That explains it entirely. The device I was running link-wizard on is connected via ethernet. I'll try again on another host. Thank you.

On Tue, Aug 14, 2018 at 9:33 PM Max Isom notifications@github.com wrote:

Tuya devices cleverly take advantage of two facts:

  • WiFi adapters can broadcast UDP packets of variable length when the said adapter is in any state. The adapter can be connected to a network, or just powered on.
  • It's very easy to sniff packet lengths (with or without being connected to a network).

So setup data is encoded into the UDP packet lengths themselves, which devices then sniff.

If you provide the output of DEBUG=* tuya-cli link-wizard I can help.

(Credit for reverse engineering the protocol and providing POC code goes to @BillSobel https://github.com/BillSobel.)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413075972, or mute the thread https://github.com/notifications/unsubscribe-auth/AD737_k4Wc9ATdZg5x_ 8uWK5eWG7_BQEks5uQ4hjgaJpZM4V9XTn .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413076432, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLj6PevjNLz1htihFcK8fUqaitBEQzTks5uQ4k8gaJpZM4V9XTn .

dlashua commented 6 years ago

It's connected to a WiFi router (Netgear Orbi) but I don't know if it transmits broadcast data.

DEBUG=* returns this output:

(note: the johndoe@example.com login appears to be hardcoded in cli/lib/link.js. I never input that data.

Registering devices(s)... @tuyapi/cloud Sending parameters: +0ms @tuyapi/cloud { a: 'tuya.m.user.email.register', @tuyapi/cloud deviceId: 'ahkwo4fe1k3loynyv00plggr85vcqsjm0y9ssvfwomyk', @tuyapi/cloud os: 'Linux', @tuyapi/cloud lang: 'en', @tuyapi/cloud v: '1.0', @tuyapi/cloud clientId: 'er34eeqngmadauffmvch', @tuyapi/cloud time: 1534301077, @tuyapi/cloud postData: '{"countryCode":"AZ","email":" johndoe@example.com","passwd":"8ac4253450b2e0eccdfe0fa24036b6d5"}', @tuyapi/cloud sign: '15b5a1fa546734d6f75a32124b7ccd7a' } +1ms ⠼ Registering devices(s)... @tuyapi/cloud Received response: +380ms @tuyapi/cloud {"t":1534301076966,"success":false,"errorCode":"USER_NAME_IS_EXIST","status":"error","errorMsg":"Username exists."} @tuyapi/cloud +0ms @tuyapi/cloud Sending parameters: +1ms @tuyapi/cloud { a: 'tuya.m.user.email.password.login', @tuyapi/cloud deviceId: 'ahkwo4fe1k3loynyv00plggr85vcqsjm0y9ssvfwomyk', @tuyapi/cloud os: 'Linux', @tuyapi/cloud lang: 'en', @tuyapi/cloud v: '1.0', @tuyapi/cloud clientId: 'er34eeqngmadauffmvch', @tuyapi/cloud time: 1534301077, @tuyapi/cloud postData: '{"countryCode":"AZ","email":" johndoe@example.com","passwd":"8ac4253450b2e0eccdfe0fa24036b6d5"}', @tuyapi/cloud sign: 'ac0014c48eb11486788abddd2d6c1aca' } +0ms ⠏ Registering devices(s)... @tuyapi/cloud Received response: +390ms @tuyapi/cloud {"result":{"timezone":"","tempUnit":1,"sid":"az153429Q1337828nOeCLfb3d263e9b3f74368087f8247c4796069c2","uid":"az1534291337828neCLf","nickname":"","phoneCode":"AZ","email":" johndoe@example.com ","snsNickname":"","improveCompanyInfo":false,"dataVersion":0,"sex":0,"accountType":1,"mobile":"","headPic":"","ecode":"z3z45311zz559Q1Q","domain":{"deviceHttpUrl":" http://a.tuyaus.com","deviceHttpsUrl":"https://a2.tuyaus.com ","deviceMediaMqttUrl":"s.tuyaus.com","deviceMqttsUrl":"m2.tuyaus.com ","gwApiUrl":"http://a.gw.tuyaus.com/gw.json","gwMqttUrl":"mq.gw.tuyaus.com ","mobileApiUrl":"https://a1.tuyaus.com","mobileMediaMqttUrl":"s.tuyaus.com ","mobileMqttUrl":"mq.mb.tuyaus.com","mobileMqttsUrl":"m1.tuyaus.com ","pxApiUrl":"http://px.tuyaus.com ","regionCode":"AZ"},"timezoneId":"","userType":1,"partnerIdentity":"p1026419","username":" johndoe@example.com"},"t":1534301077363,"success":true,"status":"ok"} @tuyapi/cloud +0ms @tuyapi/cloud Sending parameters: +5ms @tuyapi/cloud { a: 'tuya.m.device.token.create', @tuyapi/cloud deviceId: 'ahkwo4fe1k3loynyv00plggr85vcqsjm0y9ssvfwomyk', @tuyapi/cloud os: 'Linux', @tuyapi/cloud lang: 'en', @tuyapi/cloud v: '1.0', @tuyapi/cloud clientId: 'er34eeqngmadauffmvch', @tuyapi/cloud time: 1534301077, @tuyapi/cloud postData: '{"timeZone":"-05:00"}', @tuyapi/cloud sid: 'az153429Q1337828nOeCLfb3d263e9b3f74368087f8247c4796069c2', @tuyapi/cloud sign: '47c80c6da980ff9f1783d8d92ce8fc81' } +1ms ⠹ Registering devices(s)... @tuyapi/cloud Received response: +262ms @tuyapi/cloud {"result":{"secret":"St63","token":"MNDOCj1S"},"t":1534301077635,"success":true,"status":"ok"} @tuyapi/cloud +0ms @tuyapi/link:wizard Token: { secret: 'St63', token: 'MNDOCj1S' } +0ms @tuyapi/link:manual Sending SmartLink initialization packets +0ms @tuyapi/link:wizard Polling cloud for details on token... +5ms

... and then this over and over and over again....

Registering devices(s)... @tuyapi/cloud Sending parameters: +201ms @tuyapi/cloud { a: 'tuya.m.device.list.token', @tuyapi/cloud deviceId: 'gauj1ibxtmkqf2i61g99j6f8pab0diq7ywmxk29624jd', @tuyapi/cloud os: 'Linux', @tuyapi/cloud lang: 'en', @tuyapi/cloud v: '1.0', @tuyapi/cloud clientId: 'er34eeqngmadauffmvch', @tuyapi/cloud time: 1534300950, @tuyapi/cloud postData: '{"token":"j3N6xuwe"}', @tuyapi/cloud sid: 'az153429Y1337828n8eCLfY3da87716b161ad51b0e97b394226f2ec5', @tuyapi/cloud sign: '8c342723fe05da5bb3e1d67768b211fd' } +0ms ⠹ Registering devices(s)... @tuyapi/cloud Received response: +275ms @tuyapi/cloud {"result":[],"t":1534300950615,"success":true,"status":"ok"}

until finally:

✖ Device(s) failed to be registered! Error: Timed out wating for device(s) to connect to cloud at Promise (/home/hass/npm_global/lib/node_modules/@tuyapi/cli/node_modules/@tuyapi/cloud/index.js:275:12) at

On Tue, Aug 14, 2018 at 9:37 PM BillSobel notifications@github.com wrote:

Ethernet is fine as long as that broadcast data gets to a access point that transmits broadcast data...

On Tue, Aug 14, 2018 at 7:36 PM, Daniel Lashua notifications@github.com wrote:

That explains it entirely. The device I was running link-wizard on is connected via ethernet. I'll try again on another host. Thank you.

On Tue, Aug 14, 2018 at 9:33 PM Max Isom notifications@github.com wrote:

Tuya devices cleverly take advantage of two facts:

  • WiFi adapters can broadcast UDP packets of variable length when the said adapter is in any state. The adapter can be connected to a network, or just powered on.
  • It's very easy to sniff packet lengths (with or without being connected to a network).

So setup data is encoded into the UDP packet lengths themselves, which devices then sniff.

If you provide the output of DEBUG=* tuya-cli link-wizard I can help.

(Credit for reverse engineering the protocol and providing POC code goes to @BillSobel https://github.com/BillSobel.)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413075972, or mute the thread https://github.com/notifications/unsubscribe-auth/AD737_k4Wc9ATdZg5x_ 8uWK5eWG7_BQEks5uQ4hjgaJpZM4V9XTn .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413076432, or mute the thread < https://github.com/notifications/unsubscribe-auth/ADLj6PevjNLz1htihFcK8fUqaitBEQzTks5uQ4k8gaJpZM4V9XTn

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TuyaAPI/cli/issues/2#issuecomment-413076548, or mute the thread https://github.com/notifications/unsubscribe-auth/AD7379LmGJW1lJWCfC180Cu7UiSuOCJvks5uQ4l5gaJpZM4V9XTn .

codetheweb commented 6 years ago

@dlashua what kind of device are you trying to link?

I've experienced devices sometimes refusing to respond to the linking process until I restart them. If possible, restart your device and try again.

Also, there are two linking modes. The one we want is (usually) activated by holding down a button for ~5 seconds, the one we don't want (AP mode) is activated by holding down a button for ~10 seconds. Make sure you don't hold it for too long. Specific instructions for your device on putting it in linking mode can be found in the Tuya app.

dlashua commented 6 years ago

It's a Potensic Branded Electrical Plug module.

I've unplugged it and plugged it back in many many times.

I know SOMETHING is working, because it removes the device from my Tuya App every time I tried it. And I know the device works okay, because I'm able to add it back to the Tuya App.

On my device, a slow blinking LED is AP Mode and a Fast Blinking LED is "EZ Mode", which is what I think we want. I've tried both, just to be safe, and neither work with tuyapi/cli.

dlashua commented 6 years ago

So it must be my router. I moved execution to a wirelessly connected computer, and after a few attempts it seems to have worked. The first time, I could hear the device relay click like it was going to work, but it never did. So I unplugged, replugged, and tried again. Then it worked and returned the JSON formatted data I was expecting.

codetheweb commented 6 years ago

Glad it worked.