Smart Bulb Control using Python Script
Hello again, In this article, we will see how to control the smart bulb on the local network unsing a python script and will guide you through the process of setting up and using this script to seamlessly manage your smart lights.
Whether you want to turn your lights on or off, adjust brightness levels, or even change color settings, this script provides a convenient way to interact with your smart bulbs right from your Python environment.
Example
lits pytest
The light color will turn green if all tests pass or red if test fails. And while starting the bulb glows with a warm white (yellow) color
Currently the script is tested on Wipro Smart bulbs and Amazon Basics smart bulbs
Let's get started by setting up the environment:
- Clone or download the repository to your local machine.
- Navigate to the directory containing the script files.
- Install the required Python libraries by executing the following command:
pip install -r requirements.txt
To control and monitor your Tuya devices using the script, you'll need to gather some key information:
- Address: The IPv4 network address of your device (e.g., 10.0.1.100).
- Device ID: The unique identifier for your Tuya device.
- Version: The Tuya protocol version in use (e.g., 3.1, 3.2, 3.3, 3.4, or 3.5).
- Local Key: A security key required to access your Tuya device.
- Create a Tuya Developer account on iot.tuya.com. When it asks for the "Account Type", select "Skip this step"
- Click on "Cloud" icon -> "Create Cloud Project" on the top left side of the portal
- Remember the "Data Center" you select. This will be used by TinyTuya Wizard.
- Click on "Cloud" icon -> Select your project -> Devices -> Link Tuya App Account
- Click Add App Account and a QR Code will be displayed - scan this QR code using the Smart Life App from your phone (going to the "Me" tab in the Smart Life app and clicking on the QR code button [..] in the upper right hand corner of the app. When you scan the QR code, it will link all of the devices registered in your Smart Life app into your Tuya IoT project.)
- If no devices show up after scanning the QR code, you will need to select a different data center and edit your project (or create a new one) until you see your paired devices from the Smart Life App show up
- Under "Service API" ensure these APIs are listed: IoT Core and Authorization. To be sure, click subscribe again on every service. Very important: disable popup blockers otherwise subscribing won't work without providing any indication of a failure. Make sure you authorize your Project to use those APIs:
- Click "Service API" tab
- Click "Go to Authorize" button
- Select the API Groups from the dropdown and click Subscribe
- Run Setup Wizard from tinytuya:
Enter your API Key and API Secret when prompted. The Wizard will poll the Tuya IoT Cloud Platform and print a JSON list of all your registered devices with the "name", "id" and "key" of your registered device(s). The "key"s in this list are the Devices, Local_Key you will use to access your device. The wizard will save some json files in your current directory give yes to all the prompts.
Now, let's explore how to utilize the script:
The script provides various command-line options for flexible control:
Planning to buy something on amazon.in? use this link to get Best deals on Electronics
Example ScenariosLet's explore a few scenarios to showcase the script's capabilities:
- Gradually turn on the light with increased brightness:
python my_light.py -s --on -g 'veryslow' -b 50% -t 100
- Gradually turn off the light with decreased brightness:
python my_light.py -s --off -g 'veryslow'
- Change the light color based on command exit status or test case results (bashrc function):
export LITDIR="/path/to/project/directory"
export MY_LIGHT_CONF="$LITDIR/my_light.json"
lits() {
python $LITDIR/my_light.py -s on -b 50 -f -t 30
"$@"
ext=$?
if [[ $ext == 0 ]]
then
clr='green'
else
clr='red'
fi
python $LITDIR/my_light.py -s on -c $clr -b 100 -f -t 30
}
Run a command using the lits
function to change the light color based on the command's exit status:
lits pytest
The light color will turn green if all tests pass or red if any test fails.
You can also create cron jobs and script to turn on the light when its dark and also on/off light based on you computer screen status. Let me know in the comment section if you guys want me to add another blog post regarding the same.
Enjoy the seamless control of your smart lights using the
Comments
Post a Comment