Make these LEDs stay on - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Make these LEDs stay on (/thread-33238.html) |
Make these LEDs stay on - duckredbeard - Apr-08-2021 When I run this in Thonny IDE, the LEDs light up like they are supposed to and the rest of the code works fine. when I run this as a cronjob I get everything except the LEDs. How do I make the LEDs persist until the cron job runs again on the next tenth minute (*/10 * * * *)? import time import sys import requests from gpiozero import LED from gpiozero import Button #Define LED indicators - these are GPIO numbers LED1 = LED(21) #Blue LED2 = LED(26) #Red LED3 = LED(20) #Amber LED4 = LED(19) #Green LED5 = LED(16) #Green LED6 = LED(13) #Green LED7 = LED(6) #Green LED8 = LED(12) #Green LED9 = LED(5) #Pink EMULATE_HX711=False reset = Button(18) #def reset(): # average = keg1 # print("Average has been reset to current weight") #reset.when_released = reset referenceUnit = 1 if not EMULATE_HX711: import RPi.GPIO as GPIO from hx711 import HX711 else: from emulated_hx711 import HX711 hx = HX711(27, 22) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) #hx.reset() #hx.tare() keg1 = hx.get_weight() + 250000 keg1 = keg1 - 75700 print(keg1) alert = "Keg1Q " + str(keg1) base = "autoremotejoaomgcd.appspot.com/sendmessage?key=SecretDeviceIDHiddenForSecurity&message=" alerturl = "https://"+base+alert r = requests.post(alerturl) percent = (keg1/502700) * 100 print(percent) percentT = str(round(percent, 2)) print(percentT) if 1 >= percent <= 102: #keg is outside of usable range - flashes when keg is missing (indicating tare is complete at startup) LED1.blink(.2,.2) else: LED1.off() if 1.1 >= percent >= 5: #Red less than 5% - DANGER LED2.blink(.5,.5) else: LED2.off() if 101 >= percent >= 10: #Amber less than 10% - Warning LED3.on() else: LED3.off() if 101 >= percent >= 20: # Green 10-20% - Caution LED4.on() else: LED4.off() if 101 >= percent >= 40: #Green2 20-40% LED5.on() else: LED5.off() if 101 >= percent >= 60: #Green3 40-60% LED6.on() else: LED6.off() if 101 >= percent >= 80: #Green4 60-80% LED7.on() else: LED7.off() if 101 >= percent >= 80.1: #Green5 80-100% LED8.on() else: LED8.off() hx.power_down() hx.power_up() RE: Make these LEDs stay on - duckredbeard - Apr-08-2021 I know the program is running because lines 50-53 send the variable to my Android phone and Tasker processes the variables and I see that happening. RE: Make these LEDs stay on - deanhystad - Apr-08-2021 When you run in an IDE the program remains active when it completes. This is nice because you can peek around at variables and see their values. When you run this outside the IDE the program exits when completed and the LEDs all turn off. Your program might take a millisecond to run, so that is how long the LEDs will remain lit. Instead of having using cron to run the job, why not put your code in a loop an let it loop forever? RE: Make these LEDs stay on - duckredbeard - Apr-08-2021 I added a time.sleep(580)near the end to see if that will do it. That should keep the light on for all but a few seconds of each time it is run. It accomplishes pretty much the same thing as a loop, but the looping is controlled by the crontab file. RE: Make these LEDs stay on - deanhystad - Apr-08-2021 The problem with using cron is the program forgets everything between runs. If you ever implement your tare function you will have to save that info to a file and load it in each time the program is run. cron is not meant to do this kind of task. RE: Make these LEDs stay on - duckredbeard - Apr-08-2021 I understand that. I have not needed to do the tare because I am processing the raw data on the device that gets the data. I know what a full and empty keg's numbers should be, so I just have Tasker do the math. When I change out the keg, I verify the full value, double check the empty value. What I like about the cron job is that I can make adjustments on the fly without having to kill the looping task. I also won't have to disable it in the crontab -e and reboot. It will simply run the latest edit on time. Since I am still tweaking this, this is the better option for me, as I am editing this a few times per day. With the time.sleep(580), the LEDs are only dark for a few seconds. I expect I will bump that to 590 after I witness how long the lights really are out. Ideally, it would only be a second when all is done. |