Oulu
Hi everyone,
 
I got a raspberry zero wifi managing my swimingpool pump and light.
I went for a PSM to get the temperature of the water.
 
So far it works but I'm facing an issue that I don't know how to solve.
 
When I start the openplc program I have PSM error message.
I just have to stop et start the program, and it works. Sometimes I have to do it 4 or 5 time before it works.
The thing is that when electricity shutdown and turns back on, it always has the PSM error. I then have to connect to stop and start until it works.
 
Could you help me?
 
Log error :
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued stop_pstorage() command
PSM: Connection to psm failed
PSM: Error connecting to psm!
PSM: PSM is disabled
Warning: Persistent Storage file not found
Server: Client accepted! Creating thread for the new client ID: 9...
Server: waiting for new client...
Server: Thread created for client ID: 9


 
Log all good :
 
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Killing previous PSM modules...
PSM: Starting PSM...
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued stop_pstorage() command
Server: Client accepted! Creating thread for the new client ID: 8...
Server: waiting for new client...
Server: Thread created for client ID: 8
PSM: Connected to PSM
Warning: Persistent Storage file not found

PSM CODE :
 
#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.3) #You can adjust the psm cycle time here
psm.stop()
 
Thanks
Reply 0 0
Write a reply...
thiagoralves
Start the runtime from terminal (you may need to stop the openplc service with "sudo systemctl stop openplc" first). Then between your start and stop of the PLC, collect the terminal logs when PSM fails. Maybe something in the PSM logic is out of sync and failing. The terminal logs will give us a better hint of what that could be
Reply 0 0
Oulu
Hello Thiago, thanks for fast reply.
How can I get logs in terminal?
Reply 0 0
thiagoralves
Open terminal (or SSH into your raspberry pi from your computer), then go to the folder where you installed OpenPLC (most likely ~/OpenPLC_v3) and start the runtime from there. For example:
cd ~/OpenPLC_v3
sudo systemctl stop openplc
sudo ./ start_openplc.sh
Reply 0 0
Oulu
Here the results.
Any outcome?
 
Initializing OpenPLC in RUN mode...
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Interactive Server: waiting for new client...
PSM: Starting PSM...
Openning database
Enabling Modbus on port 502
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Disabling DNP3
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued stop_dnp3() command
Enabling EtherNet/IP on port 44818
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Server: Client accepted! Creating thread for the new client ID: 9...
Server: waiting for new client...
Disabling Persistent Storage
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Server: Thread created for client ID: 9
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued stop_pstorage() command
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
persistent.file removed!
Name: Hour
Location: %IW0
Type: INT
Name: Minute
Location: %IW1
Type: INT
Name: Seconde
Location: %IW2
Type: INT
Name: POMPE
Location: %QX0.0
Type: BOOL
Name: LIGHT
Location: %QX0.1
Type: BOOL
Name: CDE_MAN_PMP
Location: %IX0.3
Type: BOOL
Name: CDE_MAN_LIGHT
Location: %IX0.4
Type: BOOL
Name: CDE_STOP_AUTO
Location: %IX0.5
Type: BOOL
Name: IHM_CMD_PMP
Location: %QX10.0
Type: BOOL
Name: IHM_CDE_LIGHT
Location: %QX10.1
Type: BOOL
Name: IHM_CDE_HIVER
Location: %QX10.2
Type: BOOL
Name: IHM_CDE_00
Location: %QX11.0
Type: BOOL
Name: IHM_CDE_01
Location: %QX11.1
Type: BOOL
Name: IHM_CDE_02
Location: %QX11.2
Type: BOOL
Name: IHM_CDE_03
Location: %QX11.3
Type: BOOL
Name: IHM_CDE_04
Location: %QX11.4
Type: BOOL
Name: IHM_CDE_05
Location: %QX11.5
Type: BOOL
Name: IHM_CDE_06
Location: %QX11.6
Type: BOOL
Name: IHM_CDE_07
Location: %QX11.7
Type: BOOL
Name: IHM_CDE_08
Location: %QX12.0
Type: BOOL
Name: IHM_CDE_09
Location: %QX12.1
Type: BOOL
Name: IHM_CDE_10
Location: %QX12.2
Type: BOOL
Name: IHM_CDE_11
Location: %QX12.3
Type: BOOL
Name: IHM_CDE_12
Location: %QX12.4
Type: BOOL
Name: IHM_CDE_13
Location: %QX12.5
Type: BOOL
Name: IHM_CDE_14
Location: %QX12.6
Type: BOOL
Name: IHM_CDE_15
Location: %QX12.7
Type: BOOL
Name: IHM_CDE_16
Location: %QX13.0
Type: BOOL
Name: IHM_CDE_17
Location: %QX13.1
Type: BOOL
Name: IHM_CDE_18
Location: %QX13.2
Type: BOOL
Name: IHM_CDE_19
Location: %QX13.3
Type: BOOL
Name: IHM_CDE_20
Location: %QX13.4
Type: BOOL
Name: IHM_CDE_21
Location: %QX13.5
Type: BOOL
Name: IHM_CDE_22
Location: %QX13.6
Type: BOOL
Name: IHM_CDE_23
Location: %QX13.7
Type: BOOL
Name: Temp_EAU
Location: %IW10
Type: INT
* Serving Flask app 'webserver'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8080
* Running on http://192.168.1.100:8080
Press CTRL+C to quit
Exception in thread Thread-1 (run_server):
Traceback (most recent call last):
File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
self.run()
File "/usr/lib/python3.11/threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "/home/alex/OpenPLC_V3/webserver/core/psm/psm.py", line 132, in run_server
mtcp_server = ModbusTcpServer(context, address=("localhost", 2605))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alex/OpenPLC_V3/.venv/lib/python3.11/site-packages/pymodbus/server/sync.py", line 338, in __init__
socketserver.ThreadingTCPServer.__init__(self, self.address,
File "/usr/lib/python3.11/socketserver.py", line 456, in __init__
self.server_bind()
File "/usr/lib/python3.11/socketserver.py", line 472, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
PSM: Connection to psm failed
PSM: Error connecting to psm!
PSM: PSM is disabled
Warning: Persistent Storage file not found
Setting main thread priority to RT
Locking main thread memory
Getting current time
Reply 0 0
Oulu
Here when it works.
 
Initializing OpenPLC in RUN mode...
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Interactive Server: waiting for new client...
PSM: Starting PSM...
Openning database
Enabling Modbus on port 502
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Disabling DNP3
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued stop_dnp3() command
Enabling EtherNet/IP on port 44818
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Disabling Persistent Storage
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
Interactive Server: Client accepted! Creating thread for the new client ID: 6...
Interactive Server: waiting for new client...
Interactive Server: Thread created for client ID: 6
Issued stop_pstorage() command
Interactive Server: client ID: 6 has closed the connection
Terminating interactive server connections
persistent.file removed!
Name: Hour
Location: %IW0
Type: INT
Name: Minute
Location: %IW1
Type: INT
Name: Seconde
Location: %IW2
Type: INT
Name: POMPE
Location: %QX0.0
Type: BOOL
Name: LIGHT
Location: %QX0.1
Type: BOOL
Name: CDE_MAN_PMP
Location: %IX0.3
Type: BOOL
Name: CDE_MAN_LIGHT
Location: %IX0.4
Type: BOOL
Name: CDE_STOP_AUTO
Location: %IX0.5
Type: BOOL
Name: IHM_CMD_PMP
Location: %QX10.0
Type: BOOL
Name: IHM_CDE_LIGHT
Location: %QX10.1
Type: BOOL
Name: IHM_CDE_HIVER
Location: %QX10.2
Type: BOOL
Name: IHM_CDE_00
Location: %QX11.0
Type: BOOL
Name: IHM_CDE_01
Location: %QX11.1
Type: BOOL
Name: IHM_CDE_02
Location: %QX11.2
Type: BOOL
Name: IHM_CDE_03
Location: %QX11.3
Type: BOOL
Name: IHM_CDE_04
Location: %QX11.4
Type: BOOL
Name: IHM_CDE_05
Location: %QX11.5
Type: BOOL
Name: IHM_CDE_06
Location: %QX11.6
Type: BOOL
Name: IHM_CDE_07
Location: %QX11.7
Type: BOOL
Name: IHM_CDE_08
Location: %QX12.0
Type: BOOL
Name: IHM_CDE_09
Location: %QX12.1
Type: BOOL
Name: IHM_CDE_10
Location: %QX12.2
Type: BOOL
Name: IHM_CDE_11
Location: %QX12.3
Type: BOOL
Name: IHM_CDE_12
Location: %QX12.4
Type: BOOL
Name: IHM_CDE_13
Location: %QX12.5
Type: BOOL
Name: IHM_CDE_14
Location: %QX12.6
Type: BOOL
Name: IHM_CDE_15
Location: %QX12.7
Type: BOOL
Name: IHM_CDE_16
Location: %QX13.0
Type: BOOL
Name: IHM_CDE_17
Location: %QX13.1
Type: BOOL
Name: IHM_CDE_18
Location: %QX13.2
Type: BOOL
Name: IHM_CDE_19
Location: %QX13.3
Type: BOOL
Name: IHM_CDE_20
Location: %QX13.4
Type: BOOL
Name: IHM_CDE_21
Location: %QX13.5
Type: BOOL
Name: IHM_CDE_22
Location: %QX13.6
Type: BOOL
Name: IHM_CDE_23
Location: %QX13.7
Type: BOOL
Name: Temp_EAU
Location: %IW10
Type: INT
* Serving Flask app 'webserver'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8080
* Running on http://192.168.1.100:8080
Press CTRL+C to quit
PSM: Connected to PSM
Warning: Persistent Storage file not found
Setting main thread priority to RT
Locking main thread memory
Getting current time
IX0.0 1
IX0.1 1
IX0.2 1
IX0.3 0
IX0.4 0
IX0.5 0
IX0.6 0
QX0.0 True
QX0.1 True
QX0.2 False
QX0.3 False
QX0.4 False
QX0.5 False
QX0.6 False
Server: Client accepted! Creating thread for the new client ID: 9...
Server: waiting for new client...
Server: Thread created for client ID: 9
Reply 0 0
thiagoralves
It seems that something else is using the port that PSM needs to communicate. When that something is running, PSM cannot start
Reply 0 0
Oulu
Thanks for reply.

My raspberry is connected to a DS18B20 and no other equipement isconnected to it.
On the modbus side I have an ignition server communicating with that raspberry for my HMI.
 
I tried to turn off my ignition server but it is stil not working.

So I'm thinking maybe when the PSM code start and try to connect to my D18B20, perhaps the answer is too low for PSM CODE and it crashes.
Any thoughts?
Reply 0 0
Oulu
I asked chatgpt and got this answer.

The PSM error you're encountering is due to a port conflict on your system. Specifically, this part of the traceback is key:
 

🔍 Explanation:

  • The PSM (Persistent Storage Module) in OpenPLC is trying to start a Modbus TCP server on port 2605.
  • But it fails because another process is already using port 2605.
  • As a result, the PSM fails to start and gets disabled, leading to:

How to Fix It:

Option 1: Check what's using port 2605

Run the following command in your terminal to find what’s using the port:
sudo lsof -i :2605
 
You’ll see output like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 1234 alex 3u IPv4 123456 0t0 TCP localhost:2605 (LISTEN)

Then you can stop the conflicting process:
sudo kill 1234 # Replace with actual PID
 
 

Option 2: Change the PSM port

If you can’t stop the other process (or it’s essential), change the port used by the PSM.
 
Locate the psm.py file:
 
/home/alex/OpenPLC_V3/webserver/core/psm/ psm.py
 
Around line 132, find:
mtcp_server = ModbusTcpServer(context, address=("localhost", 2605))
 
Change the port to something else, like 2606:
 
mtcp_server = ModbusTcpServer(context, address=("localhost", 2606))
 
Save and restart OpenPLC.
 
Optional (but recommended): Ensure clean startup
 
Sometimes leftover processes can occupy ports. Before starting OpenPLC again, run:
ps aux | grep openplc
 
Kill any zombie processes:
 
sudo kill <PID>
Reply 0 0
Oulu
I tried changing 2605 port with 2606 but it was worse.
I tried to add in rc.local the following code :
 
 PORT=2605
PID=$(lsof -ti tcp:$PORT)
if [ -n "$PID" ]; then
echo "Killing process on port $PORT with PID $PID"
kill -9 $PID
fi
 
I put it to try to kill PID that last on the port before starting openplc.
It doesn't work but I think openplc runs PSM before rc.local.
 
I'm a bit lost, coud you help me to do so?
 
First kill process using port 2605
then start openplc.
 
thanks
Reply 0 0
thiagoralves
That’s what I meant with my statement before. Something (as in another program) is using the port that PSM needs. It is an odd numbered port, so I wonder what could be running there. I’m wondering also if you’re trying to start OpenPLC twice. Doing so with PSM enabled could generate a conflict (two PSMs running on the same port)
Reply 0 0
Oulu
Thiago,
 
Thanks for your replay.
I often have electrical shut down in my home.
When openplc is running without isssue, and suddenly shuts down then a process on port 2605 remains activ.
I know that because when I shut down openplc properly, no process remain on port 2605 and it allows to restart openplc without issue.

So my idea to fix this issue is when raspberry turn on, identify any process on 2605, kill it and then start openplc.
To do so, could you tell me which function on my raspberry start openplc when it turns on?

I will try to do that through SystemD.
 
Any advice to do so?

Let me know.
Reply 0 0
thiagoralves
OpenPLC is managed by systemd. You can start/stop/disable it using systemctl {command} openplc
Reply 0 0
Oulu
Hi thiago, sorry for delay.

Could you tell me the command to do a stop and start just of the program (like on the web interface) ?

Is it the command of your last answer?
 
I just tried this : "sudo systemctl stop openplc" and nothing happens.
Is there another way to do so?
 
 
Thanks
Reply 0 0
thiagoralves
There is no command for that. You need to use the web interface to start/stop the PLC program. The command I gave you is to start/stop the entire runtime (web interface, runtime core, everything).
Reply 0 0
Write a reply...
Reply