CaptainButter
Hello,
 
I have an issue where the Webserver will be freezing or will refresh to the point I have no more log/console information, as well as monitoring data. I have experimented where I have commented out any PSM code pertaining to calculating distance, the Webserver will continue to work normal. Yet the calculating distance prints for brief moments are correct, before freezing. Furthermore, the Webserver would work when I changed update_inputs() with psm.set_var("MD1", distance) and the structured text RangeSensor to real and %MD1. Yet it continues to not correctly put flow and distance in the monitoring tab. Looking further into the logs of the runtime, "Bad File Descriptor" will happen during the Webserver freezing. I have verified that the code each component works with python scripts and not sure how to continue. 
Thanks
import psm
import time
from gpiozero import DigitalInputDevice
from gpiozero import DigitalOutputDevice

GPIO_Flow_Sensor = DigitalInputDevice(4, pull_up= False)        #GPIO pin 4 connected to flow sensor's (YF-S201) data
GPIO_Pump_Control = DigitalOutputDevice(14, active_high= True)  #GPIO pin 14 connected to pump's relay
GPIO_Range_Echo = DigitalInputDevice(15, pull_up= False)       #GPIO pin 15 connected to range's echo pin
GPIO_Range_Trigger = DigitalOutputDevice(18, active_high= True) #GPIO pin 18 connected to rang'es trigger pin
frequency = 0 #Freqeuncy of pulse from flow sensor

#Handle when flow sensor detects rising edge
def handle_flow_interrupt(channel):
    global frequency
    frequency += 1
GPIO_Flow_Sensor.when_activated = handle_flow_interrupt

def update_flow():
    global frequency
    #Divide frequency by 7.5 is roughly flow rate (L/min)
    flow = (frequency/7.5)*60
    #print(f"Flow Rate: {flow} L/Hr")
    #print(f"Frequency: {frequency}")
    psm.set_var("IW0", int(flow))
    frequency = 0
    flow = 0

def get_distance():
    # set Trigger to HIGH
    GPIO_Range_Trigger.on()

    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO_Range_Trigger.off()

    # Initialize start and stop times
    StartTime = time.time()
    StopTime = time.time()
    # save StartTime
    while GPIO_Range_Echo.value == 0:
        StartTime = time.time()

    # save time of arrival
    while GPIO_Range_Echo.value == 1:
        StopTime = time.time()

    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    return (TimeElapsed * 34300) / 2 # Return Distance

def hardware_init():
    #Insert your hardware initialization code in here
    psm.start()

def update_inputs():
    #place here your code to update inputs
    update_flow()
    distance = get_distance()
    print(f"Distance: {distance}")
    psm.set_var("IW1", distance)
    
def update_outputs():
    #place here your code to work on outputs
    GPIO_Pump_Control.on() if psm.get_var("QX0.0") else GPIO_Pump_Control.off()

if __name__ == "__main__":
    hardware_init()
    while (not psm.should_quit()):
        update_inputs()
        update_outputs()
        time.sleep(0.1) #You can adjust the psm cycle time here
    psm.stop()
 
Screenshot 2024-05-28 211137.pngimage (1).png
Reply 0 0
thiagoralves
I would avoid printing too many things on console. It is usually there only for debug purposes. Print messages can delay the scan cycle considerably. Try removing your print statements and check if the program runs correctly.
Reply 0 0
CaptainButter
Removing print message lead to the same thing of freezing and the bad file descriptor.
Reply 0 0
Reply