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

Unexpected token � in JSON at position 56 #12

Closed danlakey closed 6 years ago

danlakey commented 6 years ago

Hey guys,

Running NodeJS version 6.12.2 on Ubuntu 16.04. Installed tuyapi per npm instructions and setup a test file with per the Readme page: getStatus, setStatus, then another getStatus.

When I run the file I have the connected lamp turned on. I'm also making sure I'm out of any TuyaSmart apps on the the phone/tablet.

The first getStatus function returns a Status: true message. The next setStatus function does turn off the lamp and returns a Result of setting status false: true message. The next message this is displayed in the console is:

SyntaxError: Unexpected token � in JSON at position 56
    at Object.parse (native)
    at _send.then.err (/home/utladmin/node/tuyapi/node_modules/tuyapi/index.js:64:19)
    at process._tickCallback (internal/process/next_tick.js:109:7)

I would think maybe something could be wrong with the id, uid or key for the outlet, but the fact that it can turn lamp off makes me think these variables are correct.

Any thoughts/help would be very much appreciated. Thanks,

codetheweb commented 6 years ago

First make sure you're running the latest version of tuyapi. Running npm list tuyapi should result in output starting with tuyapi@1.1.1. If that looks good, your device may be the problem. Maybe it uses a slightly different protocol. Try running your test script a few times and see if the issue crops up each time.

clach04 commented 6 years ago

One of the things I've not yet had time to do is add debug trace logging to https://github.com/clach04/python-tuya for this sort of scenario where the response payload is unexpected.

danlakey commented 6 years ago

@codetheweb thank you for you're reply. Ran npm list tuyapi and the output was tuyapi@1.1.1 Ran the test script 6 more times with the outlet starting in the turned on state and the result as I posted originally was the same each time. Ran the test script a few more times with the outlet starting in the turned off state and the result is slightly different, in that in cannot return the status at all, goes right to the:

SyntaxError: Unexpected token � in JSON at position 56 at Object.parse (native) at _send.then.err (/home/utladmin/node/tuyapi/node_modules/tuyapi/index.js:64:19) at process._tickCallback (internal/process/next_tick.js:109:7)

Seems like the getStatus cannot communicate with the device when it's in the off state. The specific device I'm working with the MartinJerry mini Smart Plug.

Totally could believe that that this specific device is slightly different than what you guys are working with. I may also try to re-run through the instructions on how I pulled the access info (uid, key, etc) and see if I see any different/additional details.

Thanks again for everyone's time looking into my post, I do appreciate it.

codetheweb commented 6 years ago

Weird. I'm currently really busy, but I'll try to add a flag to the constructor in the next few days that outputs some debug information to the console. Hopefully that helps us pin down this issue.

clach04 commented 6 years ago

I think I know what is going on... I've no idea why though 🎱

Sometimes status returns an encrypted payload. Again I've no idea why/when this occurs. I can induce this sometimes by making a bunch of status requests in a row. See https://github.com/clach04/python-tuya/issues/6 for a Python test case (and fix).

I've resolved this by decrypting the result and sending back the status dictionary. What is interesting is that this payload has additional keys. t is epoch seconds, s I'm still trying to workout.

codetheweb commented 6 years ago

This should be fixed by v2.0 @danlakey. Please update tuyapi, update your code (v2.0 includes breaking changes), and it should now work.

danlakey commented 6 years ago

Apologies for the late response, guys. Winter storms knocked out my system and just got everything back online. The change v2.0 worked! I updated from NPM, then used the updated sample code and was able to turn the switch on/off. Thank you!

codetheweb commented 6 years ago

Glad to here it.