question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connected to device, but getting garbled data

See original GitHub issue

I’m using a no brand wifi smart switch bought on Amazon (this) The switch works, it was connected to the Tuya network, and I got the key and device id using this tutorial. I can telnet to the device on port 6668, so I’m assuming this library should work.

I’m running a simple test: const TuyaDevice = require(‘tuyapi’);


const device = new TuyaDevice({
  id: 'myid',
  key: 'mykey',
  ip: '192.168.0.142',
  persistentConnection: true});

device.on('connected',() => {
  console.log('Connected to device.');
});

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

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

  const status = data.dps['1'];

  console.log('Current status:', status);

  device.set({set: !status}).then(result => {
    console.log('Result of setting status:', result);
  });
});

device.on('error',(err) => {
  console.log('Error: ' + err);
});

device.connect();

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

But the output I get is this:

C:\Users\scognito\dev\nodejs\tuya>node app.js
Connected to device.
Data from device: γ†øS̞ÄwšÚÌuÛ(ãê(µùLŸe}ÙàßÅÀ
C:\Users\scognito\dev\nodejs\tuya\app.js:20
  const status = data.dps['1'];
                         ^

TypeError: Cannot read property '1' of undefined
    at TuyaDevice.device.on.data (C:\Users\scognito\dev\nodejs\tuya\app.js:20:26)
    at TuyaDevice.emit (events.js:182:13)
    at Socket.client.on.data (C:\Users\scognito\dev\nodejs\tuya\node_modules\tuyapi\index.js:522:16)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

Am I missing something?

Thank you

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:38 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
codethewebcommented, Feb 9, 2019

Hmm, can you run your script with DEBUG=* node <script.js> @ramon18?

Looks like your device may be closing the socket prematurely.

1reaction
ramon18commented, Feb 9, 2019

I was facing the same issue (json obj data unvalid). Here is a wireshark capture TCP dump:

REQUEST
00000000  00 00 55 aa 00 00 00 00  00 00 00 0a 00 00 00 46   ..U..... .......F
00000010  7b 22 67 77 49 64 22 3a  22 35 30 37 35 38 30 31   {"gwId": "5075801
00000020  34 63 63 35 30 65 33 31  61 37 65 38 39 22 2c 22   4cc5xxxx xxxxx","
00000030  64 65 76 49 64 22 3a 22  35 30 37 35 38 30 31 34   devId":" 50758014
00000040  63 63 35 30 65 33 31 61  37 65 38 39 22 7d 00 00   cc5xxxxx xxxx"}..
00000050  00 00 00 00 aa 55                                  .....U

ANSWER
00000000  00 00 55 aa 00 00 00 00  00 00 00 0a 00 00 00 21   ..U..... .......!
00000010  00 00 00 01 6a 73 6f 6e  20 6f 62 6a 20 64 61 74   ....json  obj dat
00000020  61 20 75 6e 76 61 6c 69  64 d2 53 78 69 00 00 aa   a unvali d.Sxi...
00000030  55                                                 U

After using the Official App and executed some action on the switch it resets and tuyapi was able to “get” status again.

After some trial and error, found that some caution needs to be taken when calling “set”, if the parameters in the json payload are wrong/incompatible it will “break” the device internal state again.

For my device this works:


// IMPORTANT: Values should be a string, not a number!
var actions = {
   close: '1',
   open: '2',
   pause: '3'
}

device.set({set: actions.open}).then(result => {
  console.log('Result of setting status to: ' + result);
})

Update: Fixed, see posts below

The only minor issue I’m facing right now is the error below, the switch actions works as expected, but it shouldn’t give errors, right @codetheweb?

  TuyAPI Error event from socket. 10.3.2.25 Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:406:19)
    at writeOrBuffer (_stream_writable.js:394:5)
    at Socket.Writable.write (_stream_writable.js:294:11)
    at connect.then (d:\prj\node\curtain-tuya\node_modules\tuyapi\index.js:414:21)
    at process._tickCallback (internal/process/next_tick.js:68:7) +1ms

Hope this helps someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix internet connection problems on Android devices
If restarting doesn't work, switch between Wi-Fi and mobile data: Open your Settings app and tap Network & internet or Connections. Depending on...
Read more >
How To Fix An Intermittent Internet Connection In Windows ...
In this guide, we're going to show you the usual suspects behind a bad internet connection and possible solutions that can resolve the ......
Read more >
Internet Keeps Dropping? Here's why, and what you need ...
Internet keeps dropping? Here are some quick and easy fixes that you can apply if your internet broadband keeps disconnecting.
Read more >
How to Fix Packet Loss: Causes, Simple Solutions & Top ...
Packet loss can cause any size of business some real issues with VoIP and internet connectivity. We cover common causes and how to...
Read more >
How to Fix Android WiFi is Connected But No Internet?
You can do that by turning the plug for your router to the off position. Then, wait for about a minute, and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found