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

Not being able to connect using device.connect() #635

Closed SatyamWats closed 11 months ago

SatyamWats commented 11 months ago

Hey @codetheweb I am trying to run this bit of code that I acquire from the 'Data not updating?' section in this project's ReadME.

const TuyAPI = require('tuyapi');

const device = new TuyAPI({
    id: 'xxxx',
    key: 'xxxx',
    ip: 'xxx.xxx.xxx.xxx',
    version: '3.3',
    issueRefreshOnConnect: true});

// Find device on network
device.find().then(() => {
  // Connect to device
  console.log('Found 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);

This code is displaying the following on the console:

Found Device!
Error! Error: connection timed out
    at Socket.<anonymous> (F:\docs\projects\node_modules\tuyapi\index.js:597:26)
    at Object.onceWrapper (node:events:627:28)
    at Socket.emit (node:events:513:28)
    at Socket._onTimeout (node:net:568:8)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)
F:\docs\projects\node_modules\tuyapi\index.js:599
        this.connectPromise.reject(new Error('connection timed out'));
                                   ^

Error: connection timed out
    at Socket.<anonymous> (F:\docs\projects\node_modules\tuyapi\index.js:599:36)
    at Object.onceWrapper (node:events:627:28)
    at Socket.emit (node:events:513:28)
    at Socket._onTimeout (node:net:568:8)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

Node.js v18.13.0

And the debug log is this:

$ DEBUG=* node app.js

  TuyAPI IP and ID are already both resolved. +0ms
Found Device!
  TuyAPI Connecting to xxx.xxx.xxx.xxx ... +6ms
Error! Error: connection timed out
    at Socket.<anonymous> (F:\docs\projectst\node_modules\tuyapi\index.js:597:26)
    at Object.onceWrapper (node:events:627:28)
    at Socket.emit (node:events:513:28)
    at Socket._onTimeout (node:net:568:8)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)
F:\docs\projectst\node_modules\tuyapi\index.js:599
        this.connectPromise.reject(new Error('connection timed out'));
                                   ^

Error: connection timed out
    at Socket.<anonymous> (F:\docs\projects\node_modules\tuyapi\index.js:599:36)
    at Object.onceWrapper (node:events:627:28)
    at Socket.emit (node:events:513:28)
    at Socket._onTimeout (node:net:568:8)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

Node.js v18.13.0

If it is necessary then, my laptop (through which I am running the code) and the device are not on the same network. I want to connect with the device and read its data.

Further Information:

Any help would be appreciated!

Apollon77 commented 11 months ago

In fact the device was found, so UDP stuff worked, but then when opening a TCP connection to the devcie it got a timeout ... soo check that tcp connections to the relevant ports are possible between the networks

SatyamWats commented 11 months ago

Hey @Apollon77 ! Thanks for the response. Could you further explain what you mean by "soo check that tcp connections to the relevant ports are possible between the networks".

My device and laptop are on different networks, do they have to be on the same one to work?

Apollon77 commented 11 months ago

As said: different networks normally have issues with UDP messages because they are normally not routed, but this seems to be no issue for you.

So the library finds a device (via UDP) and then tries a TCP connection to port 6668 and there it gets a timeout ... reason is normally network or the device itself.try if you can ping the device IP from the laptop, try if you con dfo a telnet to the said port ...

SatyamWats commented 11 months ago

I tried to ping the device but got this response: Ping request could not find host x.x.x.x:6668. Please check the name and try again.

I queried the device IP from the API explorer and can verify that the IP provided is correct however, the ping failed.

Apollon77 commented 11 months ago

Yeees ... becauzse you can only pong a host and not a port ...

SatyamWats commented 11 months ago

Well I tried to ping the IP itself but still the ping failed. This was the response:

Pinging x.x.x.x with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for x.x.x.x:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Apollon77 commented 11 months ago

Then you need to find out why you can not ping the device (if your rother between the networks do not hindert it), but honestly ... I can not give support for such low level network topics ...