Oulu
Hi everyone,

I've set a system with a raspberry zero hosting openplc and a program.
I used PSM coding to get the value of a temperature sensor (DS18B20) and to get my input and outputs GPIO.
It is working at first but after a while (average of 2 hours) i'm facing the following issue :
 
1) Temperature is still read by PSM.
2) GPIO inputs and outputs are not working anymore.
3) Modbus communication is working fine.
 
To get it work again for a few hours, i have to stop the program and run it.
 
When I start the program I get a message that PSM connection failed at first, and when i start it a second time connection is ok but doesn't last.
 
Here is my hardware code.
Maybe is something wrong in it but I don't know.
#import all your libraries here
import psm
import time
from w1thermsensor import W1ThermSensor
import RPi.GPIO as GPIO
#global variables
sensor = None
i_Pin=[2,3,4,17,27,22,10,9]
q_Pin=[14,15,23,24,25,8,7,12]

def hardware_init():
    #Insert your hardware initialization code in here
    global sensor
    psm.start()
    sensor = W1ThermSensor()
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    # Pin Configuration
    for i in range(0,7):
        GPIO.setup(i_Pin[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.setup(q_Pin[i], GPIO.OUT)

def update_inputs():
    #place here your code to update inputs
    global sensor
    temperature = sensor.get_temperature()
    temp_int = int(temperature)
    psm.set_var("IW10", temp_int)
    #Read inputs
    for i in range(0,7):
        i_Str = 'IX0.'+str(i)
        i_State = int(GPIO.input(i_Pin[i]))
        print (i_Str,i_State)
        psm.set_var(i_Str, i_State)

    
def update_outputs():
    #place here your code to work on outputs
    for i in range(0,7):
        q_Str = 'QX0.'+str(i)
        a = psm.get_var(q_Str)
        print(q_Str,a)
        if a == True:
            GPIO.output(q_Pin[i], GPIO.HIGH)
        else:
            GPIO.output(q_Pin[i], GPIO.LOW)

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()
 
Is there a way the see the logs of PSM part?
Thanks for your help
Reply 0 0
thiagoralves
Your print statements should be displayed at the runtime console on the dashboard. The code seems correct, although I don't know if something is interfering with the RPi.GPIO stopping the measurements. Are you still getting the print messages on the dashboard after it gets stuck?
Reply 0 0
Oulu
Hello Thiago,

Thanks for your answer.
I got the folowing message:
 
Traceback (most recent call last):
  File "/home/alex/OpenPLC_v3/webserver/./core/psm/main.py", line 76, in <module>
    update_inputs()
  File "/home/alex/OpenPLC_v3/webserver/./core/psm/main.py", line 51, in update_inputs
    temperature = sensor.get_temperature()
  File "/usr/local/lib/python3.9/dist-packages/w1thermsensor/core.py", line 277, in get_temperature
    raw_temperature_line = self.get_raw_sensor_strings()[1]
  File "/usr/local/lib/python3.9/dist-packages/w1thermsensor/core.py", line 260, in get_raw_sensor_strings
    raise SensorNotReadyError(self)
w1thermsensor.errors.SensorNotReadyError: Sensor 0823521db7cd is not yet ready to read temperature
 
Any idea?
Reply 0 0
thiagoralves
Looks like an issue with w1thermsensor. I'm not very much familiar with that library, just used it sparingly.
Reply 0 0
Oulu
ok, didn't have the time to go further on this. I had to set the previous program to get the system to work properly.
I have a little plateforme with another sensor on which i'll do some test. If I found anything i'll write it here!
Reply 1 0
Oulu
Hello Thiago,
I've tried the following :
1) Using only the RPi.GPIO code (I removed all the temperature code) in PSM. 2 days of testing without issues.
2) Doing same for temperature. At first I had PSM connexion failed without explanation. I restart the program and after deleting last PSM and connect to new, it worked.
I am now wainting 2 more days to wait for failure. I let you know in two days.
Reply 1 0
Oulu
Hi Thiago,
 
Finally i managed to make ti woked.
I installed the latest openplc on the raspberry and followed your video to set DS18B20 sensor.
The library must be installed on a "venv" of openplcv3 folder (i'll give the command to follow when i get home).
 
I have run it for 24 hours and all went well (my gpio's are working well while having temperature !)
 
So the solution was to install the latest openplc host on the raspberry and to upgrade the program with the latest openplc software.
Reply 0 0
fg71
Inside OpenPLC_v3 folder you type: .venv/bin/pip install <module_name>
The .venv folder is hidden folder but if you do -> ls -al you will see it.
 
I did 2 examples with PSM that I shared in this forum and installed 3 modules, gpiozero, lgpio and python-snap7
 
I could really recommend to use gpiozero the documentation is very good, you also need lgpio to be installed to use gpiozero.
 
For people who use RPi5 the new kernel gpio_chip instead of access the register directly.
gpiozero use the new method (kernel) and use lgpio as default
 
[203128%2F9142943%2Fmceclip0]
Reply 1 0
Reply