Source code for grove.grove_moisture_sensor
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Grove Base Hat for the Raspberry Pi, used to connect grove sensors.
# Copyright (C) 2018 Seeed Technology Co.,Ltd.
'''
This is the code for
- Grove - Moisture Sensor <https://www.seeedstudio.com/Grove-Moisture-Sensor-p-955.html>`_
Examples:
.. code-block:: python
import time
from grove.grove_moisture_sensor import GroveMoistureSensor
# connect to alalog pin 2(slot A2)
PIN = 2
sensor = GroveMoistureSensor(PIN)
print('Detecting moisture...')
while True:
m = sensor.moisture
if 0 <= m and m < 300:
result = 'Dry'
elif 300 <= m and m < 600:
result = 'Moist'
else:
result = 'Wet'
print('Moisture value: {0}, {1}'.format(m, result))
time.sleep(1)
'''
import math
import sys
import time
from grove.adc import ADC
__all__ = ["GroveMoistureSensor"]
[docs]
class GroveMoistureSensor:
'''
Grove Moisture Sensor class
Args:
pin(int): number of analog pin/channel the sensor connected.
'''
def __init__(self, channel):
self.channel = channel
self.adc = ADC()
@property
def moisture(self):
'''
Get the moisture strength value/voltage
Returns:
(int): voltage, in mV
'''
value = self.adc.read_voltage(self.channel)
return value
Grove = GroveMoistureSensor
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.ADC)
pin = sh.argv2pin()
sensor = GroveMoistureSensor(pin)
print('Detecting moisture...')
while True:
m = sensor.moisture
if 0 <= m and m < 300:
result = 'Dry'
elif 300 <= m and m < 600:
result = 'Moist'
else:
result = 'Wet'
print('Moisture value: {0}, {1}'.format(m, result))
time.sleep(1)
if __name__ == '__main__':
main()