codetheweb / tuyapi

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

After getting my id and key, the code won't work: "connection timed out" #472

Closed WouterMolhoek closed 4 weeks ago

WouterMolhoek commented 3 years ago

I followed the instructions correctly but i am stilling ending up with the follow error message: "(node:15468) UnhandledPromiseRejectionWarning: Error: connection timed out"

const TuyAPI = require('tuyapi');

const device = new TuyAPI({ id: 'XX', key: 'XX', ip: 'XX, });

// 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('dp-refresh', data => { console.log('DP_REFRESH data from device: ', data); });

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

});

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

(node:15468) UnhandledPromiseRejectionWarning: Error: connection timed out at Socket.client.setTimeout (C:\Users\woute\Desktop\SmartHome\node_modules\tuyapi\index.js:465:18) at Object.onceWrapper (events.js:286:20) at Socket.emit (events.js:198:13) at Socket._onTimeout (net.js:442:8) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10) (node:15468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:15468) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

codetheweb commented 3 years ago

Please post your debug log (start your script with the environment variable set like DEBUG=*).

corey-mitchell commented 3 years ago

Hello, I'm having a similar issue.

For a little more back story, I've already gotten this program to work with one of my devices so I bought two more Tuya devices. For some reason though, my original device connected just fine and both of these new devices have the problem described above.

My code (should look familiar lol):

const device = new TuyAPI({
    id: xxxxxxxxx,
    key: xxxxxxx
});

device.find().then(() => {
    device.connect();
});

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

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

device.on('error', error => console.log(error));

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

setTimeout(() => { device.disconnect(); }, 10000);

My debug output:

TuyAPI Finding missing IP undefined or ID <REDACTED> +0ms
  TuyAPI Received UDP message. +1s
  TuyAPI UDP data: +1ms
  TuyAPI {
  TuyAPI   payload: {
  TuyAPI     ip: <REDACTED>,
  TuyAPI     gwId: <REDACTED>,
  TuyAPI     active: 2,
  TuyAPI     ablilty: 0,
  TuyAPI     encrypt: true,
  TuyAPI     productKey: <REDACTED>,
  TuyAPI     version: '3.3'
  TuyAPI   },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 19,
  TuyAPI   sequenceN: 0
  TuyAPI } +0ms
  TuyAPI Received UDP message. +2s
  TuyAPI UDP data: +0ms
  TuyAPI {
  TuyAPI   payload: {
  TuyAPI     ip: <REDACTED>,
  TuyAPI     gwId: <REDACTED>,
  TuyAPI     active: 2,
  TuyAPI     ablilty: 0,
  TuyAPI     encrypt: true,
  TuyAPI     productKey: <REDACTED>,
  TuyAPI     version: '3.3'
  TuyAPI   },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 19,
  TuyAPI   sequenceN: 0
  TuyAPI } +0ms
  TuyAPI Received UDP message. +831ms
  TuyAPI UDP data: +0ms
  TuyAPI {
  TuyAPI   payload: {
  TuyAPI     ip: <REDACTED>,
  TuyAPI     gwId: <REDACTED>,
  TuyAPI     active: 2,
  TuyAPI     ablilty: 0,
  TuyAPI     encrypt: true,
  TuyAPI     productKey: <REDACTED>,
  TuyAPI     version: '3.3'
  TuyAPI   },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 19,
  TuyAPI   sequenceN: 0
  TuyAPI } +0ms
  TuyAPI Connecting to <REDACTED>... +2ms
(node:19320) UnhandledPromiseRejectionWarning: Error: Error: connection timed out
    at index.js:45:49
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:19320) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19320) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  TuyAPI Socket closed: <REDACTED> +5s

Additional possibly useful information:

OS: Windows 10 Pro Node Version: 14.16.0 NPM Version: 6.14.11 The original device was a light, but the two new devices are smart plugs. All the devices, according to the debug, are using firmware version 3.3 so I assumed they should work together since the plugs only need to receive an on/off command but I could be totally wrong and the plugs may work differently... Since I redacted all the device IDs and IPs, the first listed device is my original and the next two are the new devices.

Thank you very much for the help and more importantly thank you for this package!

[Edit]: I was able to successfully connect to the devices after I switched from Ethernet to Wi-Fi. For some reason, I can't seem to connect to the devices when connected via Ethernet. I attempted to pass the IP instead of the device ID, as well as alongside the device ID, but still received the same problem. It's particularly odd because my light is also running on the same Wi-Fi network but connects just fine while I'm connected to the Ethernet.

I suppose my question now is, how can I connect to the device so that I don't have to connect to Wi-Fi just to turn a plug on? Seems counterintuitive to have to do so.

codetheweb commented 3 years ago

I suppose my question now is, how can I connect to the device so that I don't have to connect to Wi-Fi just to turn a plug on? Seems counterintuitive to have to do so.

Sounds like that might be a network issue, not a TuyAPI issue. A few things:

corey-mitchell commented 3 years ago

I had tried passing the version number as well, sorry should have mentioned that. Also, I just completely turned off my phone to ensure there was no background interference. Same results.

However, I believe you may be correct as to this being a network issue.

I tried running arp -a which showed me that my computer was unable to locate the devices on the network (or at the very least that these devices haven't yet communicated with my computer). After that, I attempted to ping the devices. Interestingly enough both devices failed to pong for the first two pings, then they finally ponged. Afterwards I was able to see them in the list when I ran arp -a a second time.

After doing this one of the devices had connected normally, but the other would only connect with the IP which I though was weird but whatever. So I connected it through IP and the data I received back from the device was in blobs, again weird but whatever I only need to send a toggle command not read data for this device.

I then attempted to send an on/off command but the device which sent data blobs timed out on the response. This is when I realized, thanks to the response timeout comment, that the ID for the device had CHANGED since yesterday. SUPER WEIRD! But I just re-ran the tuya-cli wizard command and changed my environment variables to match the new ID and KEY. Lo and behold I was finally able to not only connect to my devices but send them on/off commands!!!!! Yay!!!!!!

But, this is not a fool proof method. Whenever I restart my computer I am back to square one and have to rerun the entire list of commands. Including running multiple pings for each device, but thankfully excluding the ID and KEY changing (at least so far). I suppose I'll just have to write a script to have this occur as part of the devices' connection for now while I see about setting up a local space on my network for my smart devices.

TL;DR These devices behave oddly on my network but not caused by TuyAPI and nothing a script can't fix.

codetheweb commented 3 years ago

Very strange. It's normal for the key to change when re-linking your devices, but the ID should stay the same.

Apollon77 commented 4 weeks ago

No details, no follow up infos. closing. Please reopen if still relevant and happening with most recent versions.