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

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 th...

Make use of JavaScript console methods

When I say JavaScript console methods, the one and only thing that hit most of our mind is console.log() and we use it a lot and only that some might be familiar with error() and debug(). Did you ever think of checking if there are any other methods available in console other than log? Most of us don't do that, but knowing those methods might have saved lot of our development time. In this post I will show you some of the console methods which will be very useful in our day to day coding. Log based on condition console.assert(assertion, log_message); To print something on the console only if the assertion failed - assertion can be any expression which returns a boolean. Note: In chrome this log will print as an error with message saying "Assertion failed:" with the log_message passed. In FireFox its a normal log message. Log number of occurrence  console.count([label]); If we want to count something, it may be a click, may be a callback, or event triggers. We do...