Skip to main content

Smart Bulb Control using Python Script

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

Smart Bulb Control 
The source code of the script is available on github Please do check the repo and feel free to clone the repo.

Let's get started by setting up the environment:

  1. Clone or download the repository to your local machine.
  2. Navigate to the directory containing the script files.
  3. Install the required Python libraries by executing the following command:
        
pip install -r requirements.txt

Prepare Device for the script

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. 
Lets see how to get above details step by step

  • 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.
  • Skip the configuration wizard but remember the Authorization Key: API ID and Secret for below
  • 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:
        python -m tinytuya wizard

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.

Once you've collected Device Id, version, IP, and Local Key,  Open the my_light.conf file and update respecitve values.

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 Scenarios

Let's explore a few scenarios to showcase the script's capabilities:

  1. Gradually turn on the light with increased brightness:
python my_light.py -s --on -g 'veryslow' -b 50% -t 100
  1. Gradually turn off the light with decreased brightness:
python my_light.py -s --off -g 'veryslow'
  1. 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

Thanks to tinytuya python library for providing such a wonerfull interface

Comments

Popular posts from this blog

Upgrade your kitchen with latest Tech Gadgets and Make cooking faster

Welcome to our latest blog post, where we embark on an exciting culinary journey to elevate your kitchen experience with cutting-edge technology gadgets. In this fast-paced world, cooking has become more than just a necessity; it's an art form that deserves the finest tools. Join us as we explore the innovative tech solutions that can revolutionize the heart of your home, making cooking not only faster but also more enjoyable and efficient. From smart appliances to intelligent cooking assistants, we'll introduce you to a range of futuristic kitchen companions that are sure to inspire your inner chef and transform the way you create delightful meals for yourself and your loved ones. Get ready to upgrade your kitchen and embrace the future of cooking!  In this blog post, we will narrow down the vast array of available kitchen tech gadgets and present only 10 items, carefully selected based on product ratings, price, and their suitability for Indian kitchens.  Are you a Telegram u

TataSky Refresh after Recharge - Activate after Recharge

If you are using TataSky and doing recharge occasionally and that too after disconnection, this post will be very helpful for you. Most of the time you will not get the channels listed as soon as you make the payment. It will display message to Subscribe for that channel. You can easily get channels back by simply giving a missed a call from your registered mobile number. Note: Make sure your TV and SetupBox is on and showing the error message Give missed call to  +91 80892 80892 (Soft Refresh)   wait for 1 minute and If still not active then try giving missed call to  +91 90405 90405 (Heavy Refresh). Ad: Planning to buy a Best low budget Smart TV? Consider  Acer 109 cm (43 inches) I Series 4K Ultra HD Android Smart LED TV AR43AR2851UDFL (Black)  - You can get this TV as low as for 20,000 Rs - which has Bluetooth, WiFi, Android and good customer ratings (4.4/5). Note: Price based on offers, click on the link to see current price on the web page If the above ste

Duplicate file finder/Remover using perl and SHA1

When you are using a computing devices (either a laptop or PC or a Tab) for your personal use after some time (let take some years) you will realise that your disk is full and most of the space are occupied by duplicate files (Same copy of file located in different locations). For ex: You might have a favourite music file in "My Favourite" folder as well as in the "Album" folder. But finding this duplicate manually is a biggest process. That too if the file names are different OMG!. There are lot of free utilities available to do this in automated way, but if you are a programmer, you will always prefer to do it on your own. Here are the steps we are going to do. This is purely on a linux - Ubuntu system.  (for windows you might need to change the path as per conventions ) Getting SHA1 for all the files recursively in a given directory Compare SHA1 with other files Remove the duplicate file Getting SHA1 of a file Using cpan module   Digest::SHA1 we c