Python MinimalModbus + SHT20 Modbus RTU RS485 Temperature and humidity sensor

#! /usr/bin/python
import minimalmodbus
import time
# Set the value minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True 
# immediately after import minimalmodbus
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True

# default slave address = 1
instr = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1)
instr.serial.baudrate = 9600
instr.serial.parity  = "N"
instr.serial.timeout  = 0.8
#instr.debug = True
print(instr)

"""
read slave address, baud rate, temp. correction, hum. correction
"""
print(instr.read_registers(257, 4,functioncode=3))

"""
change of the slave address 
the change will take effect after reboot only! 
it is possible to continue using the old address till reboot
"""
slaveNewAddress = 5
#instr.write_register(257, slaveNewAddress, functioncode=6)

"""
change of the speed - take effect imediatelly
the change will take effect after reboot only! 
it is possible to continue using the old address till reboot
"""
slaveNewBaudrate = 9600
#instr.write_register(258, slaveNewBaudrate, functioncode=6) 

# read slave address, baud rate, temp. correction, hum. correction
print(instr.read_registers(257, 4,functioncode=3)) 

time.sleep(1)

while 1:
    try:
        print instr.read_registers(1,2, functioncode=4) # read temp and hum (list)
        print instr.read_register(1,1, functioncode=4) # read temp 
        print instr.read_register(2,1, functioncode=4) # read hum 
    except minimalmodbus.NoResponseError:
        print("No Response Error")
    time.sleep(1)
root@danfoss:/home/pi/danfoss# sudo pip install -U minimalmodbus
Collecting minimalmodbus
  Downloading https://files.pythonhosted.org/packages/6c/d5/77d42e8a0b73da2b5f97acd91900ac50e303b4cb959f76350cfbb38e05a0/minimalmodbus-1.0.2-py2.py3-none-any.whl
Requirement already up-to-date: pyserial>=3.0 in /usr/local/lib/python2.7/dist-packages (from minimalmodbus)
Installing collected packages: minimalmodbus
  Found existing installation: MinimalModbus 0.7
    Uninstalling MinimalModbus-0.7:
      Successfully uninstalled MinimalModbus-0.7
Successfully installed minimalmodbus-1.0.2
root@danfoss:/home/pi/danfoss# 

Description of the module here


Add Your Comment