Making Your Christmas Smarter With Phidgets

by InformalAbsence in Circuits > Raspberry Pi

249 Views, 0 Favorites, 0 Comments

Making Your Christmas Smarter With Phidgets

20211220_223336.jpg
A Smart Christmas With Phidgets
20211220_213824.jpg

If you're anything like me you love Christmas lights, but forget to turn them off lots of the time. As most lights are cheaply made and heat up quite a bit it can be dangerous to leave them on for long periods of time, and it can shorten their lifespan considerably. Having to reach back behind the tree to plug it in isn't very convenient, and having a power bar or extension cord poking out tends to ruin the aesthetic, not to mention you still have to turn it on and off every day.


So, why not make it easier on yourself? With a Raspberry Pi, a couple of Phidgets, and about thirty minutes we can make our tree easier than ever to control.


The code used in this instructable is written in Python and ran on a Raspberry Pi with their Raspberry Pi OS, but if you prefer a different language I encourage you to rewrite the code given in the example.

Supplies

20211220_213819.jpg

Setting Up Your Phidgets

20211220_213937.jpg
20211220_214053.jpg
20211220_214334.jpg

The first thing you're going to need to do is to make sure that your Raspberry Pi is ready. You can find the Raspberry Pi OS here as well as for instructions on how to install it. You will temporarily need a monitor and keyboard, but once you fully set up your Pi and run the script you can leave the Pi and Phidgets alone. It's also very important that you set the proper timezone, or allow the Pi to set it automatically using the network, otherwise your lights won't turn on and off when you want them too.


Next, simply connect your Phidgets Plug to the VINT Hub Port 0 using a VINT Cable, and connect the VINT Hub to your Raspberry Pi using a USB Type-A to Mini-B cable. It's important that you connect it the same way as shown if you are going to use the code provided below, as otherwise the plug will not turn on and off.


Once you've done that you're all set, and can move on to the next step.

Write Your Script

Now that everything is hooked up together we need to write a script that will compare the current time to our set on and off times and figure out when to turn on and off the Christmas Tree. Start by making a file named treel


The core part of the timer will be utilizing the Python3 library "datetime". In order to make the math involved as easy as possible, we will use the .strftime("%H") command with %H giving us the current hour, according to a 24-hour clock cycle. The only caveat is the str part of strftime(), as it is going to give us the hours in a string instead of an int. In order to compare it to an integer value you can simply place int() around the "datetime" command, which all put together will look something like this.

int(datetime.now().strftime("%H")) >= ON_TIME and int(datetime.now().strftime("%H")) < OFF_TIME


Now that we have the hard part out of the way the rest of the code will look pretty straightforward if you have worked with Phidgets before, but no worries if you haven't. First you will need to import the libraries, like so

from datetime import datetime
from Phidget22.Devices.DigitalOutput import *
import time


Then I'm going to define a few constants just underneath the import statements. The first will be the time in which we want the lights to turn on, the second is the time at which they should turn off, and the third is the port in which the Phidgets Plug is connected to the VINT Hub.

# Active Hours
ON_TIME = 8 # Turn on at 8 AM 
OFF_TIME = 22 # Turn off at 10 PM

# Hub Port For Plug
PLUG_PORT = 0 


Now you will need to open the Phidgets. If you chose the same hub port as me, the following code should work without a problem. We first have to initialize an object, specifically the plug, then we assign it to the PLUG_PORT from earlier, and finally open it.

# Set Up Phidget Plug
plug = DigitalOutput()
# Assign Port
plug.setHubPort(PLUG_PORT)
plug.setIsHubPortDevice(True)
# Open
plug.openWaitForAttachment(1000)


Finally, we can create a loop that will check our time. We will need to use our comparison statement that we made earlier with "datetime", and that will look something like this

# Loop while checking time 
while True:
    # Print Out Current Time, just for refrence (Optional)
    print("Current Time {}".format(datetime.now().strftime("%H:%M:%S")))
    # If time is between active hours, turn on plug
    if int(datetime.now().strftime("%H")) >= ON_TIME and int(datetime.now().strftime("%H")) < OFF_TIME:
        # Turn on Plug
        print("Plug Is On.")
        plug.setState(True)
    else:
        # Turn Off Plug
        print("Plug Is Off.")
        plug.setState(False)
        
        # Sleep for 60 seconds
        time.sleep(600)


The print statements are completely optional, and it's up to you if you want to include them or not. Just in case you want to look over my code, or use it as a starting point, I have included the complete file below.

Downloads

Test Your Phidgets

20211220_223414.jpg

Depending on your setup, you may want to test your Phidgets first before you move them to their final location, but you don't have to. The general layout will be as follows,

  1. Plug your Phidgets Plug into an empty wall socket
  2. Plug your Christmas lights into the Phidgets Plug, They will be off by default
  3. Plug your Raspberry Pi into power, and start your script.


Once you've completed the first two tasks, you can move on to the next step where I will talk about running your script.

Running Your Script

The first thing you will likely need to do is import the Phidgets Libraries. To start, open a terminal and run the following command

python3 -m pip install Phidget22Native


Once completed you will simply need to navigate to the location of your file, in my case named treelights.py and run the following

python3 treelights.py


You should now see the current time, as well as the state of the plug printed to your terminal. If you want to run your program headlessly, meaning you can disconnect the terminal without stopping the program, you can do the following

python3 treelights.py & 


There you go, you should now have a hassle-free timer for your Christmas Lights.

Conclusion

I hope this project was fairly simple to follow along with, and helped you to not only make your own light timer, but also to spark new ideas for other automation-type projects. I love making things easier to use, especially if it is something that I use frequently, and let's be honest, who doesn't love having to do less!


I've made a part two where I go over how to control your Phidgets Plug over the web, if you're interested you can find it here.


Thanks for taking the time to read through this Instructable, and please feel free to use and build off of any of the code I have provided in this example. Happy Holidays!