Yacoub Hanna
Hello, 
 
I was following this video on YouTube on how to read the temperature using DS18B20 on Raspberry Pi.

 
I followed all the steps. However, when I run the code I get an error with pymodbus.
 
Here is the log when I start the PLC
 
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Traceback (most recent call last):
File "/home/pi/Desktop/OpenPLC_v3/webserver/./core/psm/main.py", line 27, in <module>

import psm
File "/home/pi/Desktop/OpenPLC_v3/webserver/core/psm/psm.py", line 3, in <module>
from pymodbus.server.sync import ModbusTcpServer
ModuleNotFoundError: No module named 'pymodbus'
PSM: Error while starting Python interpreter
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued start_dnp3() command to start on port: 20000
DNP3 ID manager: Starting thread (0)
DNP3 ID DNP3_Server: Listening on: 0.0.0.0:20000
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
Skipping configuration of Slave Devices (mbconfig.cfg file not found)
Warning: Persistent Storage file not found
 
 
I installed pymodbus by doing pip3 install pymodbus and I'm still having the same error. 
 
 
Here is the PSM Code
import psm
import time
from w1thermsensor import W1ThermSensor

#global variables
sensor = None
def hardware_init():
#Insert your hardware initialization code in here
global sensor
psm.start()
sensor = W1ThermSensor()
def update_inputs():
#place here your code to update inputs
global sensor
temperature = sensor.get_temperature()
temp_int = int(temperature)
psm.set_var("IW0", temp_int)


def update_outputs():
#place here your code to work on outputs
pass

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()
 
 
I would really appreciate it if someone could help me with this. 
 
thank you. 
Reply 0 0
Yacoub Hanna
Just a quick update. I think the issue is with sudo. If I run psm.py with no sudo, it runs. but with sudo I got the same error. 
 
The picture is attached. 
Reply 0 0
Yacoub Hanna
Hello,
 
I was able to fix the issue with pymodbus. However, I'm still getting an error with reading the temperature. 
 
That is the log that I'm getting
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 start_dnp3() command to start on port: 20000
DNP3 ID manager: Starting thread (0)
DNP3 ID DNP3_Server: Listening on: 0.0.0.0:20000
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued stop_pstorage() command
PSM: Connected to PSM
Skipping configuration of Slave Devices (mbconfig.cfg file not found)
PSM: Error reading from digital inputs!
PSM: Error writing to digital outputs!
Warning: Persistent Storage file not found
PSM: Error reading from digital inputs!
PSM: Error writing to digital outputs!
PSM: Error reading from digital inputs!
PSM: Error writing to digital outputs!
PSM: Error reading from digital inputs!
PSM: Error writing to digital outputs!
PSM: Error reading from digital inputs!
PSM: Error writing to digital outputs!
PSM: Too many errors!
PSM: PSM is disabled
Traceback (most recent call last):
File "/home/pi/Desktop/OpenPLC/OpenPLC_v3/webserver/./core/psm/main.py", line 29, in <module>
update_inputs()
File "/home/pi/Desktop/OpenPLC/OpenPLC_v3/webserver/./core/psm/main.py", line 17, in update_inputs
temperature = sensor.get_temperature()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/w1thermsensor/core.py", line 277, in get_temperature
raw_temperature_line = self.get_raw_sensor_strings()[1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/w1thermsensor/core.py", line 260, in get_raw_sensor_strings
raise SensorNotReadyError(self)
w1thermsensor.errors.SensorNotReadyError: Sensor 3ce1e380c62b is not yet ready to read temperature
Server: Client accepted! Creating thread for the new client ID: 13...
Server: waiting for new client...
Server: Thread created for client ID: 13
Modbus Server: client ID: 13 has closed the connection
Terminating Modbus connections thread
Server: Client accepted! Creating thread for the new client ID: 13...
Server: waiting for new client...
Server: Thread created for client ID: 13
Modbus Server: client ID: 13 has closed the connection
Terminating Modbus connections thread
Server: Client accepted! Creating thread for the new client ID: 13...
Server: waiting for new client...
Server: Thread created for client ID: 13
 
I made sure that I connected the sensor correctly and I'm getting the temperature 
pi@raspberrypi:/sys/bus/w1/devices/28-3ce1e380c62b $ cat w1_slave
86 01 55 05 7f a5 a5 66 73 : crc=73 YES
86 01 55 05 7f a5 a5 66 73 t=24375
Reply 0 0
Reply