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
class GroveCapMoistureSensor: def __init__(self, channel, dry_voltage, wet_voltage): self.channel = channel self.adc = ADC() self.dry_voltage = dry_voltage self.wet_voltage = wet_voltage @property def moisture_percent(self): v = self.voltage normalised_value = abs(self.dry_voltage-v) dif = float(abs(self.dry_voltage - self.wet_voltage)) percent = (normalised_value/dif)*100 return percent @property def voltage(self): value = self.adc.read_voltage(self.channel) return value
adc = ADC() Offset = -2.2 print("name: {0:20}, version: {1:4}".format(adc.name, adc.version)) try: print('按下 Ctrl-C 可停止程式') lcd.clear() while True: lcd.cursor_pos = (0, 0) lcd.write_string("Date: {}".format(time.strftime("%Y/%m/%d"))) lcd.cursor_pos = (1, 0) lcd.write_string("Time: {}".format(time.strftime("%H:%M:%S"))) #voltage = adc.read_voltage(0)/1000 raw = adc.read_raw(0) voltage = adc.read_voltage(0) percent = adc.read(0) print("raw:{0:>6}, voltage:{1:>6}, percent(%):{2:>6} ".format( raw, voltage, percent)) # phValue = 7.0 + (voltage - 3.2) * (-5.5) # phValue= voltage*5.0/1024/6 # phValue=3.5*phValue+Offset # print(voltage, phValue) time.sleep(1) except KeyboardInterrupt: lcd.backlight_enabled = False print('關閉程式') finally: lcd.clear()
_serialnum = "28-00000b702a1d" try: print('按下 Ctrl-C 可停止程式') lcd.clear() while True: lcd.setCursor(0, 0) lcd.write("Date: {}".format(time.strftime("%Y/%m/%d"))) lcd.setCursor(1, 0) lcd.write("Time: {}".format(time.strftime("%H:%M:%S"))) # read sensor data voltage = [] temp_list = [] for i in range(1000): # get voltage from PH sensor voltage.append(adc.read_voltage(0) / 1000) for i in range(5): # get temperature filename = "/sys/bus/w1/devices/" + _serialnum + "/w1_slave" tfile = open(filename) text = tfile.read() tfile.close() secondline = text.split("\n")[1] temperaturedata = secondline.split(" ")[9] global temperature temperature = float(temperaturedata[2:]) temperature = temperature / 1000 temp_list.append(temperature) # print ('temperature:',temperature) firstline = text ccrrcc = firstline.split(" ")[11]
import time from grove.adc import ADC channel = 4 # pin A4 adc = ADC() while True: print(adc.read_voltage(channel)) time.sleep(0.5)