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.
53 lines
1.7 KiB
53 lines
1.7 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 getRawValue(self): |
|
return self.Adc.read() |
|
|
|
# Do the varying scale of the moisture for Capacinance readers |
|
def getScaledValue(self): |
|
scaledValue = 0 |
|
rawValue = self.getRawValue() |
|
|
|
# Calculate percentage within the calibrated range |
|
scaledValue = ( |
|
((self.WaterValue - rawValue) * 100) / (self.WaterValue - self.AirValue) |
|
) |
|
|
|
# Make sure that we don't go out of range |
|
if scaledValue < 0: |
|
scaledValue = 0 |
|
|
|
if scaledValue > 100: |
|
scaledValue = 100 |
|
|
|
# Convert so 0% is completely dry and 100% is completely wet. |
|
return 100 - scaledValue |
|
|
|
# Get current interval of how wet the sensor is |
|
def getInterval(self): |
|
value = self.getScaledValue() |
|
|
|
if 75 <= value <= 100: |
|
return("Very Moist (Value: {}%)".format(value)) |
|
elif 50 <= value <= 75: |
|
return("Quite Moist (Value: {}%)".format(value)) |
|
elif 25 <= value <= 50: |
|
return("Kinda Dry (Value: {}%)".format(value)) |
|
elif 0 <= value <= 25: |
|
return("Very Dry (Value: {}%)".format(value))
|
|
|