Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make these LEDs stay on
#1
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()
Reply
#2
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.
Reply
#3
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?
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  functional LEDs in an array or list? // RPi user Doczu 5 1,601 Aug-23-2022, 05:37 PM
Last Post: Yoriz
Question How to make a button always stay on a point when resizing the window? _ShevaKadu 5 4,185 Nov-04-2020, 03:59 PM
Last Post: deanhystad
  Orange PI Win steering LEDs connect to GPIO djmcg 0 1,538 Dec-27-2019, 08:26 AM
Last Post: djmcg
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 4,419 Jun-17-2019, 06:45 PM
Last Post: perfringo
  Absolute newbie asking for help with blinking LEDs misterfrickel 0 2,069 Mar-15-2019, 08:54 AM
Last Post: misterfrickel
  Robot Stay Inside Circular Ring webmanoffesto 4 6,675 Dec-07-2016, 06:57 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020