codetheweb / tuyapi

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

Data is not decrypted #563

Closed buiductri closed 2 years ago

buiductri commented 2 years ago

Hi, I am new with tuyapi. I stumped upon this issue when I don't know how to fix it anymore. After getting the local id and key, detect which local ip it is, and run the example, I got this:

Connected to device!
Data from device: i;s
                     i�����`�\�a��q�^�kx��t�LK��
/root/tuya/index.js:33
  console.log(`Boolean status of default property: ${data.dps['1']}.`);
                                                             ^

TypeError: Cannot read property '1' of undefined
    at TuyaDevice.device.on.data (/root/tuya/index.js:33:62)
    at TuyaDevice.emit (events.js:198:13)
    at TuyaDevice._packetHandler (/root/tuya/node_modules/tuyapi/index.js:627:12)
    at packets.forEach.packet (/root/tuya/node_modules/tuyapi/index.js:507:43)
    at Array.forEach (<anonymous>)
    at Socket.client.on.data (/root/tuya/node_modules/tuyapi/index.js:503:19)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)

Look like data received is not correctly decrypt. If anyone have solution for this, please let me know. Thanks a lot guys.

codetheweb commented 2 years ago

Can you run with the env var DEBUG set to and post the output? (`DEBUG= node ...` in *sh).

buiductri commented 2 years ago

Sure @codetheweb, here is the output

root@ubuntu:~/tuya# DEBUG=* node index.js
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.xxx.xxx... +25ms
  TuyAPI Socket connected. +26ms
Connected to device!
  TuyAPI GET Payload: +10ms
  TuyAPI { gwId: 'ebf04c****************',
  TuyAPI   devId: 'ebf04c****************',
  TuyAPI   t: '1643178660',
  TuyAPI   dpId: [ 4, 5, 6, 18, 19, 20 ],
  TuyAPI   uid: 'ebf04c****************' } +0ms
  TuyAPI GET Payload: +25ms
  TuyAPI { gwId: 'ebf04c****************',
  TuyAPI   devId: 'ebf04c****************',
  TuyAPI   t: '1643178660',
  TuyAPI   dps: {},
  TuyAPI   uid: 'ebf04c****************' } +1ms
  TuyAPI Received data: 000055aa00000001000000120000002c00000001693b730b698ed0faccdb1660d55cc561d6d071c65e8d6b78949174ea4c4bff8bf273fede0000aa55 +16ms
  TuyAPI Parsed: +12ms
  TuyAPI { payload: 'i;s\u000bi�����\u0016`�\\�a��q�^�kx��t�LK��',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 18,
  TuyAPI   sequenceN: 1 } +1ms
  TuyAPI Received DP_REFRESH empty response packet. +2ms
  TuyAPI Received data: 000055aa000000020000000a0000002c00000001693b730b698ed0faccdb1660d55cc561d6d071c65e8d6b78949174ea4c4bff8bba31245f0000aa55 +2ms
  TuyAPI Parsed: +2ms
  TuyAPI { payload: 'i;s\u000bi�����\u0016`�\\�a��q�^�kx��t�LK��',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 2 } +0ms
  TuyAPI Received DATA packet +1ms
Data from device: i;s
                     i�����`�\�a��q�^�kx��t�LK��
/root/tuya/index.js:39
  console.log(`Boolean status of default property: ${data.dps['1']}.`);
                                                             ^

TypeError: Cannot read property '1' of undefined
    at TuyaDevice.device.on.data (/root/tuya/index.js:39:62)
    at TuyaDevice.emit (events.js:198:13)
    at TuyaDevice._packetHandler (/root/tuya/node_modules/tuyapi/index.js:627:12)
    at packets.forEach.packet (/root/tuya/node_modules/tuyapi/index.js:507:43)
    at Array.forEach (<anonymous>)
    at Socket.client.on.data (/root/tuya/node_modules/tuyapi/index.js:503:19)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
codetheweb commented 2 years ago

It's possible your local key changed. Can you try retrieving it again?

You might also want to try setting the protocol version to 3.3.

buiductri commented 2 years ago

Thank for the advice @codetheweb. I decided to reset all my devices and do it again from scratch. It works now. There are some steps might differ with the doc. So here are some key notes I gathered during the setup, hope it can help others:

const device = new TuyAPI({ id: 'eb29**', key: 'd08e****', ip: '192.168..', version: '3.3', issueRefreshOnConnect: true, });


I'm not sure where it went wrong before. So I list all of my steps here. Hope it can help and good luck.