Browse Source

Add helper class to read values and intervals from the sensor

master
Elis Hirwing 1 year ago
parent
commit
6417a3871f
Signed by: etu <elis@hirwing.se> GPG Key ID: D57EFA625C9A925F
2 changed files with 42 additions and 0 deletions
  1. +1
    -0
      Makefile
  2. +41
    -0
      src/csms12.py

+ 1
- 0
Makefile View File

@@ -2,6 +2,7 @@ DEVICE := /dev/ttyUSB3

copy:
ampy --port $(DEVICE) put src/pinouts.py
ampy --port $(DEVICE) put src/csms12.py

connect:
picocom $(DEVICE) -b115200

+ 41
- 0
src/csms12.py View File

@@ -0,0 +1,41 @@
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
))

Loading…
Cancel
Save