Laouz Hamed
Laouz Hamed

Reputation: 11

Python code for retrieving data from SEM225 soil sensor connected a Raspberry Pi

I'm trying to find the right way to retrieve the data from the SEM225 soil sensor using Python that is connected to a Raspberry Pi 3 Model B V1.2.

Here is a picture of the sensor:

soil sensor

The connection is established successfully, so that the port name and baudrate are correct.

The problem is that I keep getting the same error message when trying to read the data register from the sensor.

Here is the Python code:

from pymodbus.client.sync import ModbusSerialClient
port = "/dev/ttyUSB0"  # Replace with your actual serial port
baudrate = 9600
slave_id = 1
temperature_address = 0x0000 
# Create Modbus client
client = ModbusSerialClient(method="rtu", port=port, baudrate=baudrate)
# Read sensor data
try:
   # Read temperature register
   temperature_response = client.read_holding_registers(temperature_address, 1, unit=slave_id)
   print("Data:", temperature_response)
except Modbus.Exception as e: 
    print("Modbus Error:", e)
# Close the connection
client.close()

This is the error message that I keep getting:

Modbus Error: [Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 4 bytes (0 received)

What you suggest for me to resolve this error and establish a valid connection with the sensor in order to get the data.

Upvotes: 1

Views: 61

Answers (0)

Related Questions