Taking Local Control of a Wipro Smart Plug with Python
I bought a Wipro smart plug with energy monitoring, and I wanted to do more with it than the app allowed. I wanted to read its power draw from a script, log it to a CSV, and switch it on and off from code, all without depending on a phone app. This post covers how I got it working, including the dead end I hit, so you can skip the guesswork.
The code is on GitHub: github.com/sunbird-sameer/Wipro-SmartPlug-Control
The key fact: it's a Tuya device
Wipro's plugs are rebranded Tuya devices, and Tuya devices can be controlled over your local network with a Python library called TinyTuya. The catch is that the plug encrypts its local traffic with a per-device secret called the local key. You can't read it off the plug or find it in the app. You have to fetch it once from Tuya's cloud, and after that everything runs locally.
The dead end: the Wipro app
My plug came set up with the Wipro Next Smart Home app, so I started there. To fetch the local key, you create a free developer project on Tuya's IoT platform and link your app account to it by scanning a QR code. When I scanned the code from the Wipro app and tapped confirm, it told me the QR code had expired. Retrying quickly didn't help.
The Wipro app is a white-label version of Tuya's app, and it doesn't support this account-linking flow. The error message is misleading, because the code hasn't really expired. The app just can't complete the link.
The fix: re-pair in Smart Life
The workaround is to use Tuya's own Smart Life app instead:
- Factory reset the plug by holding its button until the light blinks rapidly.
- Add it in Smart Life (Add Device → Socket) on 2.4 GHz Wi-Fi.
- Link your Smart Life account to the developer project by scanning the QR code with Smart Life's scanner.
The trade-off is that the plug leaves the Wipro app, and any Alexa or Google Home routines tied to it need to be set up again through the Smart Life skill.
Getting the local key
With the plug showing up in the Tuya console, one command does the rest:
pip install tinytuya
python -m tinytuya wizardThe wizard asks for your developer project's Access ID, Access Secret, and one device ID, then writes a devices.json file with each device's local key. A second command, python -m tinytuya scan, finds the plug's IP address and protocol version on your network. One thing to watch is the data center: if your devices don't appear in the console, you probably picked the wrong region when creating the project. For India, choose the India data center.
After this step you no longer need the cloud for control. Treat devices.json like a password file and keep it out of Git.
What the code does
I wrapped TinyTuya in a small module, plug.py, so the common tasks are one-liners:
from plug import *
d = load_device()
turn_on(d)
print(get_energy(d)) # watts, volts, amps
set_countdown(d, 600) # flip the state after 10 minutes
log_energy(d, "power.csv", interval=10)It also works from the command line, such as python plug.py toggle, python plug.py energy, or python plug.py log power.csv 10. The repo's README has the full function reference.
Things I learned
Datapoint numbers vary by model. Tuya devices expose features as numbered datapoints. On most plugs, 1 is on/off, 19 is power in tenths of a watt, 20 is voltage in tenths of a volt, and 18 is current in milliamps. These are common defaults rather than guarantees, so run python plug.py raw first and plug in something like a lamp to see which numbers change. The constants at the top of plug.py are easy to adjust.
The plug handles one local connection at a time. If you poll too fast, the app may show the plug as offline. I keep logging intervals at 5 to 10 seconds or more.
Keep the networking simple. Your computer and the plug need to be on the same subnet. A VPN, a guest network, or router AP isolation will stop the scan from finding the plug. Giving the plug a DHCP reservation keeps its IP stable.
I skipped scheduling. Tuya keeps recurring schedules in the cloud in a model-specific format. It was simpler to call python plug.py on and python plug.py off from cron or Task Scheduler.
Where to go next
Some ideas for extending this: graph the CSV log to see how much your appliances draw, set up an alert when something stays on too long, or run it on a Raspberry Pi so it logs around the clock. If you'd rather have dashboards and automations, Home Assistant has Tuya integrations too, though I've seen reports that LocalTuya is hard to connect on Wipro plugs.
Comments
Post a Comment