ThePiHut / rgbxmastree

Code examples for the RGB Xmas Tree
90 stars 38 forks source link

lights off #34

Open satlover1 opened 8 months ago

satlover1 commented 8 months ago

i use cron on boot and for shutdown. i use the cron command 30 23 * /sbin/shutdown -h now,but when the pi shutdown to red light the lights of christmas tree freezes,they did not turn off,can you please advise me a simple cron command to auto turn them off ?

bennuttall commented 8 months ago

The shutdown command means you lose control of the Python process and the lights stay on. You're probably better off managing the shutdown from Python, or a minute before cron shutdown, turn the lights off in Python.

You can use gpiozero's TimeOfDay to turn the lights off:

https://gpiozero.readthedocs.io/en/stable/api_internal.html#timeofday

And if you want to manage shutdown too, you could do this after turning the lights off:

https://gpiozero.readthedocs.io/en/stable/recipes.html#shutdown-button

stroggprog commented 3 months ago

I added the following code in the interrupt section:

except KeyboardInterrupt:
    tree.off()
    tree.close()

In my crontab I added this line:

0 0 6 1 * /usr/bin/killall -s SIGINT randomsparkles.py

Obviously change the times to suit yourself and whatever script you are running. My cron turns the tree on on Dec 1st and off on Jan 6th, with a bash script checking the date on reboot in case the machine reboots during the 'on' period. I uploaded the scripts here: https://github.com/stroggprog/xmas-tree