OctoPrint-TuyaSmartplug icon indicating copy to clipboard operation
OctoPrint-TuyaSmartplug copied to clipboard

Local Key? What is that and where do I get it?

Open Deses opened this issue 5 years ago • 41 comments
trafficstars

I have searched high and low in the SmartLife app for a Local key but can't find one!

Is it the OctoPrint API key I can get from "Access control"?

Deses avatar Feb 13 '20 22:02 Deses

OK, I failed to see this on the Plugin page.

https://github.com/clach04/python-tuya/wiki

Sorry 'bout that!

Deses avatar Feb 13 '20 22:02 Deses

I've been trying to get the Local Key for the past 3 hours but nothing works:

  • The apps have been updated and they work different now, and the old versions no longer work.
  • Capturing the packages with Packet Capture breaks the app and it only throws 50509 errors
  • The files under /data/data/com.tuya.smartlife/shared_prefs/ none seems to have the Key, and preferences_global_key<someid>.xml is an empty map tag.

I'm at my wits end... how am I supposed to get the LocalKey in 2020?

Deses avatar Feb 14 '20 02:02 Deses

Spent the day also struggling to set this up, I've got a step further then you, so let's see if you can take it one step further then me now.

The Packet Capture app can get the LocalKey from "Jinvoo Smart" version 1.03 from APKpure. As you found out, the other apps suggested don't even have versions old enough available, or if you can find the old versions, they can't connect, or they have that 50509 error.

Now I have the keys, but the plugin getting connection refused when trying to send the command.

rathus avatar Feb 14 '20 10:02 rathus

In my android 10 device (Oneplus 7 Pro) the version 1.0.3 of Jinvoo Smart is crashing as soon as I open it.

After a good night of sleep, I'm digging around more and this helpful comment said that only 3.6.1 and earlier write the preferences_global xml correctly. Here's the APK: https://www.apkmirror.com/apk/tuya-inc/smart-life-smart-living/smart-life-smart-living-3-6-1-release/

I'm now trying to get the key and I'll report back in a minute. :)

Deses avatar Feb 14 '20 10:02 Deses

Oh, ignore my last comment, a search of an issue earlier yielded a tidbit from the author. One concurrent connection on the switches. I force-closed the Jinvoo app, waited a minute, and now it works!

Good luck with getting the LocalKey. My phone is like 4-5 years old so maybe that's how i got away with the JinVoo 1.0.3 app. Now 5AM is bedtime.

rathus avatar Feb 14 '20 11:02 rathus

I don't know why didn't I though about using my old phone running android 8.... damn! Maybe it works there!

Deses avatar Feb 14 '20 11:02 Deses

Hello,

Yes sorry, I know those devices are a pain on that point (getting the 'Local Key'). I'm not using them anymore for that reason actually.

Last time I tried, I had to run tcpdump on my WiFi AP. I just gave it a try and I don't seem to be able to extract the key anymore from a fresh pcap. I guess your best chance is to try an old app on an old device :/

Sorry for the inconvenience.

ziirish avatar Feb 14 '20 11:02 ziirish

Oh, ignore my last comment, a search of an issue earlier yielded a tidbit from the author. One concurrent connection on the switches. I force-closed the Jinvoo app, waited a minute, and now it works!

Good luck with getting the LocalKey. My phone is like 4-5 years old so maybe that's how i got away with the JinVoo 1.0.3 app. Now 5AM is bedtime.

So what did you do? I haven the Jinvoo 1.0.3 app on my old phone and it works, and the Logcat shows stuff filtering by D/Business ggg... but I don't know what I'm looking for.

I'm pretty pissed off with the available documentation and how little specific it is. :(

Deses avatar Feb 14 '20 12:02 Deses

I didn't do any filtering, I just used the Packet Capture" app, set it to monitor only the Jinvoo app, then ran Jinvoo and added my device. The catcher then had one larger capture 10-20kb or so, and in there i found a JSON request with ~30 elements, right near the end was "localKey":"xxxxxxxxxxxxxxxx" along with some other entries like my device name, device ID, etc to confirm I had the right device.

On Fri, Feb 14, 2020, 6:06 AM Carlos R, [email protected] wrote:

Oh, ignore my last comment, a search of an issue earlier yielded a tidbit from the author. One concurrent connection on the switches. I force-closed the Jinvoo app, waited a minute, and now it works!

Good luck with getting the LocalKey. My phone is like 4-5 years old so maybe that's how i got away with the JinVoo 1.0.3 app. Now 5AM is bedtime.

So what did you do? I haven the Jinvoo 1.0.3 app on my old phone and it works, and the Logcat shows stuff filtering by D/Business ggg... but I don't know what I'm looking for.

I'm pretty pissed off with the available documentation https://github.com/clach04/python-tuya/wiki and how little specific it is. :(

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ziirish/OctoPrint-TuyaSmartplug/issues/8?email_source=notifications&email_token=AAXF27YFMXSMM6XUMOEQJR3RC2CM5A5CNFSM4KU4AEBKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELY2EXY#issuecomment-586261087, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAXF276F6ZXMI23XIZK7UWDRC2CM5ANCNFSM4KU4AEBA .

rathus avatar Feb 14 '20 12:02 rathus

Oh my Goodness, finally! I got it with Packet Capture. Thank you, @rathus!!

@ziirish your plugin is working beautifully! Thank you for creating it! :)

Deses avatar Feb 14 '20 12:02 Deses

Hey guys, I'm wondering how you got it working. I was able to get the device ID as well using an old android phone but I'm still getting a status unknown error.

VoarL avatar Feb 19 '20 02:02 VoarL

@VoarL The Device ID is something that any app will show in any app, that's not something hidden. The LocalKey on the other hand... oh boy.

I managed to get it through an old Android 8.0 phone using Jinvoo 1.0.3 and Packet Capture to capture the data. Just go into Packet capture settings and install the certificate or you'll get error 50509 all the time.

Hope this helps!!!

Deses avatar Feb 19 '20 02:02 Deses

@Deses So does the LocalKey ever change? I got it using Jinvoo 1.0.3 but I wasn't able to control the device using the app since it was asking me to update, if that matters. I'm sure I got the IP right since I assigned it myself, is there anything else I can check?

VoarL avatar Feb 19 '20 02:02 VoarL

LocalKey should not change, no.

I'm not really sure what you are trying to do, but you can uninstall the apps from the phone now and put the ID, LocalKey, IP, 3.3 protocol, etc to the OctoPrint plugin and you should be done with it.

If you want to still be able to control the plug through the app, I'd suggest uninstalling the old app and getting the latest SmartLife app and pair it just as you would normally do... you already got the Key after all! :)

Deses avatar Feb 19 '20 02:02 Deses

Thanks but its still not working. I tried reinstalling this plugin and it still didn't work. Do you know what Plug Slot means? I did not see that in the documentation.

VoarL avatar Feb 19 '20 02:02 VoarL

When I try changing that, I instantly get errors similar to when I enter the wrong IP address. Usually it takes a few seconds before the error pops up.

VoarL avatar Feb 19 '20 02:02 VoarL

Leave the plug slot as default if you don't have more than one plug. That's for smart power strips.

The LocalKey you got was in an xml looking file that clearly said that the number was the LocalKey? Maybe you don't have the key after all.

Also, have you checked the Version 3.3 protocol box?

image

Deses avatar Feb 19 '20 02:02 Deses

I should add that the log file is filled with Connection refused and Network errors. Changing the Plug slot gave "something went wrong". I'm using Teckin plugs and I'm sure they're Tuya based.

VoarL avatar Feb 19 '20 02:02 VoarL

Yeah, I got it using what you guys mentioned earlier with PacketCapture and it was like "localkey": ...

VoarL avatar Feb 19 '20 02:02 VoarL

That should be it, then! Double check the Device ID and use something like Angry IP Scanner to see if the plug is using the right IP. I had to wait until the DHCP lease expired to set the final IP.

Deses avatar Feb 19 '20 02:02 Deses

I'm using the Device ID provided by the app. Spent too much time on this, I guess this is a sign I should stop being lazy and use a relay. If you have any ideas let me know. Thanks!

VoarL avatar Feb 19 '20 03:02 VoarL

Funny, I switched to trying to use a smart plug because I tried using a relay first and the startup current was enough that the contacts in the relay always welded closed (they were cheap relays though).

This all was very helpful. I'd just like to add as an alternative for those viewing this in the future that I also found success using Smart Life 3.6.1 as @Deses mentioned. I used BlueStacks (an android emulator, which I learned about here) since I didn't want to root my phone. I set up my smart plug with the current Smart Life app on my phone, then installed Smart Life 3.6.1 on BlueStacks and logged in, viewed the plug's information and turned it on and off a couple times. Then I used a file explorer to go to /data/data/com.tuya.smartlife/shared_prefs and viewed the file preferences_global_key<chars and nums>.xml which, thankfully, featured the localKey and not just an empty map tag like the current version. I just did a search in the document for the word "key" to find it in the jumble of "&quot"s and letters and numbers in the file.

MrAwesome1996 avatar Feb 23 '20 02:02 MrAwesome1996

Using an android emulator is a galaxy brain move that I'm sure no one else thought before! I'm definitely doing that next time so I don't have to mess with my old phone.

About the relays... That thing that happens to your relays is exactly why I didn't bother. Everyone is using that cheap Arduino relay rated for 10A and that's not nearly enough for a printer. I've read enough horror stories to realize that a relay is not a good idea.

Deses avatar Feb 23 '20 04:02 Deses

I Installed SmartLife app on my phone and paired the plug and it stopped working through the OctoPrint plugin... the LocalKey changed!

Anyway, thanks to @MrAwesome1996 explanation of what he did, I'm back up and running again.

Just a heads up: Bluestacks doesn't have root enabled and you need it to access /data/data/com.tuya.smartlife/shared_prefs, so I had to use https://bstweaker.tk/ to root it.

Here's a good guide: https://sangams.com.np/how-to-root-bluestacks-latest-version/

Deses avatar Feb 25 '20 13:02 Deses

Hi,

I, ve got a TECKIN smart plug that is weel configured and working with smart life from tuya. I managed to get the IP, ID, and Key with an old phone and packet capture. But when i try with the tuya smartplug in Octopi, it doesn't work, saying 'Status unknown for 192.168.1.110. Double check IP Address\Hostname in tuyasmartplug Settings'. I've checked that all Smartlife app are off on all other devices. I don't know what to do. I've done all the process twice and retrieved the same key and id. the IP is manually assigned at router level and verified. I have uninstalled and installed again the plugin... how to retrieve a log on octopi? How to check with another app the id and key?

Thanks for your help. Philippe

capillonpub avatar Jul 13 '20 00:07 capillonpub

Hello,

You could try checking the Version 3.3 option.

ziirish avatar Jul 15 '20 04:07 ziirish

Hi Ziirish. I've tried with the option v3.3 not successfuly. I've also forced the closing of smart life app on the phone used to retrieve the local key. The hardware is a Teckin sp21. Thanks

capillonpub avatar Jul 15 '20 08:07 capillonpub

You may need to enable debugging and have a look at the logfile as explained here: https://github.com/ziirish/OctoPrint-TuyaSmartplug/issues/5#issuecomment-528269185

ziirish avatar Jul 15 '20 17:07 ziirish

hi here is the log (changed ip, id & key):

[2020-07-19 20:40:56,582] DEBUG: Received M80 command, attempting power on of 192.168.0.110. [2020-07-19 20:40:56,620] DEBUG: {'autoConnect': True, 'v33': True, 'ip': '192.168.0.110', 'btnColor': '#808080', 'autoDisconnectDelay': '2', 'sysCmdOff': False, 'gcodeEnabled': True, 'gcodeOnDelay': '7', 'id': 'ebaxxxxxxxxxxxxx80wrhs', 'slot': 1, 'countdownOnDelay': 0, 'label': 'q5', 'warnPrinting': False, 'gcodeOffDelay': '2', 'sysCmdOnDelay': 0, 'localKey': '7ceb8bxxxxxxxxxx', 'countdownOffDelay': 0, 'sysRunCmdOff': '', 'useCountdownRules': False, 'autoDisconnect': True, 'sysRunCmdOn': '', 'icon': 'icon-bolt', 'displayWarning': True, 'autoConnectDelay': '7', 'currentState': 'unknown', 'sysCmdOn': False, 'sysCmdOffDelay': 0} [2020-07-19 20:41:03,626] DEBUG: Turning on q5. [2020-07-19 20:41:03,626] DEBUG: Sending command: info to q5 [2020-07-19 20:41:03,903] DEBUG: Something went wrong while running the command [2020-07-19 20:41:03,905] DEBUG: {'autoConnect': True, 'v33': True, 'ip': '192.168.0.110', 'btnColor': '#808080', 'autoDisconnectDelay': '2', 'sysCmdOff': False, 'gcodeEnabled': True, 'gcodeOnDelay': '7', 'id': 'ebaxxxxxxxxxxxxx80wrhs', 'slot': 1, 'countdownOnDelay': 0, 'label': 'q5', 'warnPrinting': False, 'gcodeOffDelay': '2', 'sysCmdOnDelay': 0, 'localKey': '7ceb8bxxxxxxxxxx', 'countdownOffDelay': 0, 'sysRunCmdOff': '', 'useCountdownRules': False, 'autoDisconnect': True, 'sysRunCmdOn': '', 'icon': 'icon-bolt', 'displayWarning': True, 'autoConnectDelay': '7', 'currentState': 'unknown', 'sysCmdOn': False, 'sysCmdOffDelay': 0} [2020-07-19 20:41:03,905] DEBUG: Sending command: on to q5 [2020-07-19 20:41:04,505] DEBUG: Something went wrong while running the command [2020-07-20 07:19:00,985] DEBUG: Checking status of q5. ...... plugin_tuyasmartplug_debug.log

Don't know how to interpret this! thanks

capillonpub avatar Jul 20 '20 11:07 capillonpub

Can you try again with v3.3 off?

ziirish avatar Jul 20 '20 11:07 ziirish