Using the SMT100 with Python
When I was young I started programming with punch cards, then moved to assembler which is still my favourite way of programming. From time to time I get interested in high level languages which are so popular these days. Python is among them and so I decided to make first step towards reading out the SMT100 soii moisture sensor from my PC or Raspberry Pi. The hardware required is a SMT100 in RS-485 ASCII version and a USB to RS-485 converter cable. A RS485 shield for the Raspberry Pi would be fine as well, but I simply started with the converter cable which provides power (5V), GND, RS-485A and RS-485B.
As a first step I developed this simpy Python code. Choose the correct COM port for your system.
import serial
import time
def GetTemperature():
ser.readline() # remove any unwanted characters
command = bytes("GetTemperature!\r\n", 'utf-8') # set command here!
ser.write(command) # write to serial port
line = ser.readline() # read from serial port
line = line.decode('utf-8') # convert bytes object to string
line = line[:-2] # remove CR+LF from right side of string
if line == '':
print("no response from sensor")
else:
print("Temperature: "+ line+" °C")
def GetWatercontent():
ser.readline()
command = bytes("GetWatercontent!\r\n", 'utf-8')
ser.write(command)
line = ser.readline()
line = line.decode('utf-8')
line = line[:-2]
if line == '':
print("no response from sensor")
else:
print("Watercontent: "+ line+" %")
port = 'COM3' # set comport here!
ser = serial.Serial( port,
baudrate=9600, # set baudrate here!
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
timeout=1)
while(1):
if(ser.isOpen() == False):
print ("error open serial port: " + port)
exit()
if ser.isOpen():
print("Please enter your command: ", end = '')
INPUT = input()
if INPUT == "GetTemperature!":
GetTemperature()
if INPUT == "GetWatercontent!":
GetWatercontent()
else:
print ("error communticating")
Usage
Please enter your command: GetTemperature!
Temperature: 24.7 °C
Please enter your command: GetWatercontent!
Watercontent: 96.46 %
Please enter your command: