codetheweb / tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.
MIT License
2.06k stars 339 forks source link

tuya gateway subdevice not found #305

Closed atus42 closed 4 years ago

atus42 commented 4 years ago

Describe the bug I have a json file from tuya smart data, which contains all device ID's and the localKey for the gateway. Tuyapi can connect (and/or discover) the gateway, but can not get data from any subdevice. It may be the fault of my coding...

To Reproduce DEBUG=* node tuya_gw.js (I posted the code below)

Expected behavior I expected to get any available dps data from the given subdevice.

Debug Output

# DEBUG=* node tuya_gw.js
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.0.26... +4ms
  TuyAPI Socket connected. +62ms
Connected to device!
  TuyAPI GET Payload: +3ms
  TuyAPI { gwId: 'bfd62f405900fd4511rabd',
  TuyAPI   devId: 'bfdcbeaf72ae6bfbe7zzoa' } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000001c0000000128794c6c00610d92a29f3b88f40a91210fd902350000aa55 +34ms
  TuyAPI Parsed: +3ms
  TuyAPI { payload: 'devid not found',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1 } +0ms
Data from device: devid not found
  TuyAPI Pinging 192.168.0.26 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +30ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 192.168.0.26 +1ms
  TuyAPI Disconnect +5s
  TuyAPI Socket closed: 192.168.0.26 +2ms
Disconnected from device.

# DEBUG=* node tuya_gw.js
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.0.26... +6ms
  TuyAPI Socket connected. +110ms
Connected to device!
  TuyAPI GET Payload: +3ms
  TuyAPI { gwId: 'bfd62f405900fd4511rabd',
  TuyAPI   devId: 'bf88a38b43cae1ca740e09' } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000001c0000000128794c6c00610d92a29f3b88f40a91210fd902350000aa55 +35ms
  TuyAPI Parsed: +4ms
  TuyAPI { payload: 'devid not found',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1 } +0ms
Data from device: devid not found
  TuyAPI Pinging 192.168.0.26 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +26ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 192.168.0.26 +1ms
  TuyAPI Disconnect +5s
  TuyAPI Socket closed: 192.168.0.26 +1ms
Disconnected from device.

Screenshots N/A

Desktop (please complete the following information):

Additional context tuya_gw.js:

const TuyAPI = require('tuyapi');

const device = new TuyAPI({
  ip: '192.168.0.26',
  key: 'xxxx',
  gwID: 'bfd62f405900fd4511rabd',
  id: 'bf88a38b43cae1ca740e09',
  version: 3.3
});

let stateHasChanged = false;

// Find device on network
device.find().then(() => {
  // Connect to device
  device.connect();
});

// Add event listeners
device.on('connected', () => {
  console.log('Connected to device!');
});

device.on('disconnected', () => {
  console.log('Disconnected from device.');
});
device.on('error', error => {
  console.log('Error!', error);
});

device.on('data', data => {
  console.log('Data from device:', data);
});

// Disconnect after 15 seconds
setTimeout(() => { device.disconnect(); }, 15000);

relevant sections from the json file:

{"activeTime":1583615453,"devId":"bf88a38b43cae1ca740e09","displayOrder":0,"dpMaxTime":1583875320214,"dpName":{},"dps":{"1":true,"101":false,"102":true,"103":"1","124":"","125":0,"126":"1","127":16,"128":22,"129":"4","130":"c","131":"B+QDChYW","132":0,"133":true,"134":0,"135":0,"136":true,"137":true,"138":true,"139":true,"2":160,"3":220},"errorCode":0,"iconUrl":"https://...","isShare":false,"key":"bf88a38b43cae1ca740e09","lat":"","lon":"","meshId":"bfd62f405900fd4511rabd","moduleMap":{},"name":"4853_Nappali","nodeId":"505745","productId":"foitasaq52xwyqmt","resptime":0,"runtimeEnv":"prod","timezoneId":"Europe/Budapest","uuid":"bf88a38b43cae1ca740e09","virtual":false},

{"activeTime":1583171103,"devId":"bfdcbeaf72ae6bfbe7zzoa","displayOrder":0,"dpMaxTime":1583875320494,"dpName":{},"dps":{"1":true,"101":false,"102":true,"103":"1","125":0,"126":"1","127":25,"128":25,"129":"4","130":"c","131":"B+QDChYW","132":0,"133":true,"134":0,"135":0,"136":true,"137":true,"138":true,"139":true,"2":250,"3":253},"errorCode":0,"iconUrl":"https://...","isShare":false,"key":"bfdcbeaf72ae6bfbe7zzoa","lat":"","lon":"","meshId":"bfd62f405900fd4511rabd","moduleMap":{},"name":"4947_Lilla","nodeId":"608745","productId":"foitasaq52xwyqmt","resptime":0,"runtimeEnv":"prod","timezoneId":"Europe/Budapest","uuid":"bfdcbeaf72ae6bfbe7zzoa","virtual":false},

{"activeTime":1583170512,"devId":"bfd62f405900fd4511rabd","displayOrder":0,"dpMaxTime":0,"dpName":{},"dps":{},"errorCode":0,"iconUrl":"https://...","isShare":false,"key":"bfd62f405900fd4511rabd","lat":"47.5865865","localKey":"xxxx","lon":"19.0965987",
"moduleMap":{"wifi":
{"bv":"30.04",
"cadv":"1.0.2",
"isOnline":true,
"pv":"2.2",
"verSw":"1.1.6"}
},"name":"Gateway","productId":"tefahiby9c10n9zp","resptime":0,"runtimeEnv":"prod","timezoneId":"Europe/Budapest","uuid":"xxx","virtual":false}],

Tuya smart app shows the very same device ID's as I see in the json file. Sub devices regulary send broadcast messages:

  TuyAPI Received data: 000055aa00000000000000080000005b00000000332e330000000000015fea0000000156aca002d94a27d5d64756255260ce9e22efac65a96cde6fec763dbcb9b73c759ca31a70b72fb234efe32a4f1aec78db4c7e0ceafe1bcbb2bf327fb2c2fee55b26848c930000aa55 +6s
  TuyAPI Parsed: +1ms
  TuyAPI { payload: { dps: { '131': 'B+QEAxYj' }, cid: '608745', t: 1585946101 },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 8,
  TuyAPI   sequenceN: 0 } +0ms
Data from device: { dps: { '131': 'B+QEAxYj' }, cid: '608745', t: 1585946101 }
  TuyAPI Received data: 000055aa00000000000000080000005b00000000332e330000000000015feb0000000156aca002d94a27d5d64756255260ce9e22efac65a96cde6fec763dbcb9b73c75c4fd2867246c6d871451a9cf29d861c4708fda48f3b814766376e4201f44d4606a05c1a50000aa55 +336ms
  TuyAPI Parsed: +1ms
  TuyAPI { payload: { dps: { '131': 'B+QEAxYj' }, cid: '505745', t: 1585946102 },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 8,
  TuyAPI   sequenceN: 0 } +0ms
Data from device: { dps: { '131': 'B+QEAxYj' }, cid: '505745', t: 1585946102 }

Please help me by finding the right code to get dps data or by improving tuyapi to support this kind of device.

codetheweb commented 4 years ago

Can you post a link to the product page for your device?

Generally, the devId parameter will be different for sub-devices than the gwId parameter.

atus42 commented 4 years ago

Sorry for the delay. The package I ordered was this: https://www.aliexpress.com/item/4000303908100.html?spm=a2g0s.9042311.0.0.194e4c4dCtWaZQ

It has an SWG-03 gateway (bfd62f405900fd4511rabd) and two SEA801-APP thermostat (bfdcbeaf72ae6bfbe7zzoa and bf88a38b43cae1ca740e09)

If you see/search the relevant sections from the json file part of my issue report you can find the corresponding productId's. They may help you.

In the tuya_gw.js file, you can see that the id and gwID parameters are different.

codetheweb commented 4 years ago

Sorry, should have looked closer at that.

It should be working. Could you use Wirecapture / tcpdump to sniff traffic between your phone and your device to see what it sends as a GET payload?

atus42 commented 4 years ago

I made two pcaps width tpacketcapture app. One was an old phone width an old tuya smart version (that saved the local key, and IDs), the other was a newer phone width latest tuya smart app. None of the captures contained http requests to the gateway. They only communicated width http://crl.godaddy.com/gdig2s2-12.crl http://ocsp.godaddy.com/ http://certificates.godaddy.com/repository/gdig2.crt http://crl.godaddy.com/gdroot.crl and such.

This was probabli because the tpacketcapture app uses a vpn connection to capture traffic. If you have a good/working way to capture needed traffic please tell me.

I will try to capture traffic on the unifi AP later. Didn't know it it is even possible.

atus42 commented 4 years ago

It looks like the app didn't 'speak' to the gateway itself, but the tuya cloud. Attached You can find the packets I captured on the unifi AP. Both the phone and the tuya gateway are connected to this AP.

tuya_traffic.pcap.gz

when I filter traffic between the phone and the gateway, only arp requests are matched the filter.

codetheweb commented 4 years ago

Thank you. Given that, (assuming your phone can ping your gateway's IP) the app/protocol must not support that specific device type for some reason.
Unfortunately, if the app doesn't support controlling it locally, there's not much I can do either.

If you find out that there was a network issue preventing local communication between the gateway and your phone, please re-open. Otherwise I'm closing it for now.

danielgiovanni commented 4 years ago

@atus42 did you solved it? I also cannot send to the sub devices

atus42 commented 4 years ago

@danielgiovanni : Unfortunately I was not able to solve it yet. It appears, that even the tuya smart app didn't communicate to the gateway directly, but through the tuya cloud.

I plan to create an artifical block towards the internet on the AP the devices connected to (Phone, tuya GW), and see if the phone tries to communicate the gateway directly if there is no internet connection to the tuya cloud.

If this didn't help, I don't know what else can I do.

tasict commented 4 years ago

@danielgiovanni : Unfortunately I was not able to solve it yet. It appears, that even the tuya smart app didn't communicate to the gateway directly, but through the tuya cloud.

I plan to create an artifical block towards the internet on the AP the devices connected to (Phone, tuya GW), and see if the phone tries to communicate the gateway directly if there is no internet connection to the tuya cloud.

If this didn't help, I don't know what else can I do.

Hi @atus42, did you have any update?

atus42 commented 4 years ago

@danielgiovanni : well, I have not take time to do this (summer, other things, like covid-19). Shortly I'll got a Mikrotik device, width which I can easily do this. Autumn is coming, so this project will get more time on my side.

tasict commented 3 years ago

I found something interesting that the same code test with tuya wire zigbee hub and result is different.

/homebridge # DEBUG=* node gateway.js
TuyAPI IP and ID are already both resolved. +0ms TuyAPI Connecting to 10.1.9.28... +6ms TuyAPI Socket connected. +5ms Connected to device! TuyAPI GET Payload: +2ms TuyAPI { TuyAPI gwId: 'ebdd7925c7531bf4falakg', TuyAPI devId: 'ebd2e8be35c5cc00d3nkp9', TuyAPI t: '1599384049', TuyAPI dps: {}, TuyAPI uid: 'ebd2e8be35c5cc00d3nkp9' TuyAPI } +1ms TuyAPI Received data: 000055aa000000010000000a0000001c00000001a4fd046f973e79562bee4dfbd672a9ca03a3f1f40000aa55 +15ms TuyAPI Parsed: +2ms TuyAPI { TuyAPI payload: 'devid not found', TuyAPI leftover: false, TuyAPI commandByte: 10, TuyAPI sequenceN: 1 TuyAPI } +1ms Data from device: devid not found TuyAPI Pinging 10.1.9.28 +10s TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +5ms TuyAPI Parsed: +0ms TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms TuyAPI Pong from 10.1.9.28 +0ms TuyAPI Disconnect +5s TuyAPI Socket closed: 10.1.9.28 +2ms Disconnected from device.