codetheweb / tuyapi

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

Error communicating with device. Make sure nothing else is trying to control it or connected to it. #34

Closed graze1234 closed 6 years ago

graze1234 commented 6 years ago

I've written a simple test function, it works and returns status: true or false, but frequently returns "(node:nnnnn) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it. ." - just wondered if the cause of the error is known?

graze1234 commented 6 years ago

if I set the IP: let tuya = new TuyaDevice({id: 'xx',key: 'xx', ip: 'xx', uid: 'xx'});

and don't call tuya.resolveIds() it seems more reliable.

graze1234 commented 6 years ago

TuyAPI Device(s): +0ms TuyAPI [ { id: '0020xxxxxxxxxce5e0', TuyAPI key: '9cdxxxxxx1c66a', TuyAPI ip: '192.x.x.x', TuyAPI uid: 'cimxxxxxxxCY3', TuyAPI type: 'outlet', TuyAPI port: 6668, TuyAPI version: 3.1, TuyAPI cipher: TuyAPI { algorithm: [Object], TuyAPI mode: [Object], TuyAPI blockSize: 16, TuyAPI _finish: false, TuyAPI _input: null, TuyAPI output: null, TuyAPI _op: [Function], TuyAPI _decrypt: false } } ] +14ms TuyAPI Payload: +33ms TuyAPI { gwId: '002xxxxxxce5e0', devId: '002xxxxxxe5e0' } +1ms TuyAPI Sending this data: 000055aa000000000000000a000xxxxxxxxxxxxxx23030373535363030313934346365356530222c226465764964223a22303032303xxxxxxxxx31393434636227d000000000000aa55 +8ms TuyAPI connect attempt 1 +8ms TuyAPI write attempt <Buffer 00 00 55 aa 00 00 xx xx xx xx xx xx 0a 00 00 00 46 7b 22 67 77 49 64 22 3a 22 30 30 32 30 30 37 35 35 36 30 30 31 39 34 34 63 65 35 65 30 22 2c 22 64 65 ... > +171ms TuyAPI failed to communicate { Error: write EPIPE at exports._errnoException (util.js:1020:11) at WriteWrap.afterWrite (net.js:812:14) code: 'EPIPE', errno: 'EPIPE', syscall: 'write' } +12ms (node:6222) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it.

graze1234 commented 6 years ago

possibly nothing to do with it - but google search of the EPIPE error came up with http://howcanfix.com/174188/nodejs-error-write-epipe-on-socks5-proxy - just wondered if related

graze1234 commented 6 years ago

OK- I feel a bit of an idiot posting here all my findings as you probably know them already :))

I'm using this code to try and automate a Jinvoo wifi plug using a raspberry pi, with cron jobs at the moment....the scheduling on the app is horrible if you need to be turning the plug on and off multiple times an hour...but with cron it's a few lines with an on and off script at whatever time intervals I need.

I'm getting those errors (EPIPE, ECONN etc) with the code when the Jinvoo app activates.. I guess it somehow takes priority and boots off other connections...if I "force stop" the app on my phone my code seems to work fine....maybe all I need is to install an app to make sure the jinvoo app is force stopped every time it opens and the pi can take over...and for others with the same error make sure nothing else can talk to the plug or device, ....sorry for boring you if you all know that already :)

graze1234 commented 6 years ago

ugh maybe that's all BS - cron job just failed to do what it should have done.

[added] it could be down to bad wifi on my network....trying to get this to automate some thing on a big area, could be amazing if I can hook it into weather forecast apis to do different things....if I can get it working :)

graze1234 commented 6 years ago

Sudo'ing the cron jobs seems to have solved it....as I said, i hope this isn't total newbie rubbish but might help someone in the future

codetheweb commented 6 years ago

The EPIPE error you were getting is interesting; I've never seen that before.

As the README says, you can't have anything else attempting to control it while using TuyAPI, including an app on your phone. As you've discovered, the app has to be force quit.

graze1234 commented 6 years ago

pretty horrible retry code..will make it cleaner and less duplication - but posting it in case it's any use to anyone (this is just on my status check script - same for on and off scripts, run via crontab on Pi) :

`tuya.get().then(status => console.log('Status: ' + status)).catch(function() { console.log('error - retrying'); setTimeout(retryFunction, 20000); // waits 20 seconds and runs retryFunction....not pretty.... });

function retryFunction(){

tuya.get().then(status => console.log('Status: ' + status)); }`