You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

41 lines
1.5 KiB

import machine
class CSMS12:
AirValue = 849 # Totally dry in air
WaterValue = 374 # Totally wet in water
Interval = 0
SoilMoistureValue = 0
def __init__(self, pin = 0):
# Set up port for reading values
self.Adc = machine.ADC(pin)
# Calculate range of intervals for Wet/Okay Wet/Dry
# This is based on the ranges of wet and dry values that I found by
# having the sensor in water and by having it in dry air.
self.Interval = (self.AirValue - self.WaterValue) / 3
# Just a wrapper to read the value
def getValue(self):
return self.Adc.read()
# Get current interval of how wet the sensor is
def getInterval(self):
value = self.getValue()
if self.WaterValue <= value <= (self.WaterValue + self.Interval):
return("Very Wet (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
elif (self.WaterValue + self.Interval) <= value <= (self.AirValue - self.Interval):
return("Wet (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
elif (self.AirValue - self.Interval) <= value <= self.AirValue:
return("Dry (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
else:
return("Out of range (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))