Connected to device, but getting garbled data
See original GitHub issueI’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:
- Created 5 years ago
- Comments:38 (22 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, can you run your script with
DEBUG=* node <script.js>
@ramon18?Looks like your device may be closing the socket prematurely.
I was facing the same issue (json obj data unvalid). Here is a wireshark capture TCP dump:
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:
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?Hope this helps someone.