def __get_raw_data(self, part): # min_moist and max_moist are 'best guess' for now sensor = chirp.Chirp(bus=self.__device_number, address=self.__address, read_moist=False, read_temp=False, read_light=False, min_moist=200, max_moist=770, temp_scale='celsius', temp_offset=0) value = None sensor.read_temp = 'temperature' == part sensor.read_moist = 'moisture' == part sensor.read_light = 'light' == part # sensor.reset() sensor.trigger() if 'temperature' == part: value = float(sensor.temp) if 'moisture' == part: value = float(sensor.moist_percent) if 'light' == part: value = float(sensor.light) return value
def _get_data(self): data = None with self._open_hardware() as i2c_bus: # TODO: Fix the bus number.... sensor = chirp.Chirp( bus=1, address=self.device[0], read_moist=True, read_temp=True, read_light=False, min_moist=self.__min_moist, max_moist=self.__max_moist, temp_scale='celsius', temp_offset=0 ) # Temperature offset is handled by the TerrariumEngine # Hack to overrule the Chirp bus, so it will be closed when we are done. # Close old smbus(1) connection to the sensor sensor.bus.close() # Replace it now with the new smbus2 connection sensor.bus = i2c_bus sensor.trigger() data = {} data['temperature'] = float(sensor.temp) data['moisture'] = float(sensor.moist_percent) # data['light'] = 100.0 - ((float(sensor.light) / 65536.0) * 100.0) return data
def load_data(self): data = None try: data = {} gpio_pins = self.get_address().split(',') sensor = chirp.Chirp( bus=1 if len(gpio_pins) == 1 else int(gpio_pins[1]), address=int('0x' + gpio_pins[0], 16), read_moist=True, read_temp=True, read_light=True, min_moist=self.get_min_moist_calibration(), max_moist=self.get_max_moist_calibration(), temp_scale='celsius', temp_offset=self.get_temperature_offset_calibration()) sensor.trigger() data['temperature'] = float(sensor.temp) data['moisture'] = float(sensor.moist_percent) data['light'] = 100.0 - ((float(sensor.light) / 65536.0) * 100.0) # Dirty hack. The Chirp sensor does not close it connection. We will force it here sensor.bus.close() except Exception as ex: print(ex) return data
def __get_raw_data(self, force_update=False): if self.__address is None: return starttime = int(time.time()) if force_update or starttime - self.__cached_data[ 'last_update'] > terrariumChirpSensor.__CACHE_TIMEOUT: # min_moist and max_moist are 'best guess' for now sensor = chirp.Chirp(bus=self.__device_number, address=self.__address, read_moist=False, read_temp=False, read_light=False, min_moist=self.__min_moist, max_moist=self.__max_moist, temp_scale='celsius', temp_offset=self.__temp_offset) sensor.read_temp = True sensor.read_moist = True sensor.read_light = True # sensor.reset() sensor.trigger() self.__cached_data['temperature'] = float(sensor.temp) self.__cached_data['moisture'] = float(sensor.moist_percent) self.__cached_data['light'] = 100.0 - ( (float(sensor.light) / 65536.0) * 100.0) self.__cached_data['last_update'] = starttime
def initializeTheSensor(): return chirp.Chirp(address=0x20, read_moist=True, read_temp=True, read_light=True, min_moist=min_moist, max_moist=max_moist, temp_scale='celsius', temp_offset=0)
def init_sensor(chirp_address): # Initialize the sensor. chirp_sensor = chirp.Chirp(address=chirp_address, read_moist=True, read_temp=True, read_light=True, min_moist="0", max_moist="1000", temp_scale='celsius', temp_offset=0) return chirp_sensor
def init_chirp(options_json): moist_min = options_json.get('moist_min', 222) moist_max = options_json.get('moist_max', 577) if moist_min >= moist_max: LOG.fatal('Incorrect options: moist_min (%s) < moist_max (%s)', moist_min, moist_max) sys.exit(1) return chirp.Chirp(address=options_json.get('i2c_addr', 0x20), read_moist=True, read_temp=True, read_light=False, min_moist=moist_min, max_moist=moist_max, temp_scale='celsius', temp_offset=options_json.get('temp_offset', 0))
def read_chirp_sensor(chirp_address, min_moist, max_moist, temp_offset=0): # Initialize the sensor. chirp_sensor = chirp.Chirp(address=chirp_address, read_moist=True, read_temp=True, read_light=True, min_moist=min_moist, max_moist=max_moist, temp_scale='celsius', temp_offset=0) chirp_sensor.trigger() moist = chirp_sensor.moist moist_p = chirp_sensor.moist_percent temp = chirp_sensor.temp light = chirp_sensor.light return moist, moist_p, temp, light
soilSens1_addr = 0x20 soilSens2_addr = 0x21 soilSens3_addr = 0x22 soilSens1_min = 238 # calibrations were made by testing the sensor in open air vs dunked in water soilSens1_max = 540 soilSens2_min = 244 soilSens2_max = 500 soilSens3_min = 232 soilSens3_max = 550 # Initialise the soil sensors soilSens1 = soilSens1 = chirp.Chirp(address=soilSens1_addr, read_moist=True, read_temp=True, read_light=True, min_moist=soilSens1_min, max_moist=soilSens1_max, temp_scale='celsius', temp_offset=0) soilSens2 = soilSens2 = chirp.Chirp(address=soilSens2_addr, read_moist=True, read_temp=True, read_light=True, min_moist=soilSens2_min, max_moist=soilSens2_max, temp_scale='celsius', temp_offset=0) soilSens3 = soilSens3 = chirp.Chirp(address=soilSens3_addr, read_moist=True, read_temp=True, read_light=True,
def setUp(self): self.chirp = chirp.Chirp()
elif argu == "-flags": print("current=[0x01]") print("new=[0x20]" + str(log_path)) sys.exit(0) if chirp_address == None or new_addr == None: print(" You need specify both the current and new address") print("") print("") show_help() sys.exit(1) chirp_sensor = chirp.Chirp(address=chirp_address, read_moist=True, read_temp=True, read_light=True, min_moist=0, max_moist=1000, temp_scale='celsius', temp_offset=0) try: chirp_sensor.sensor_address = new_addr print(" Chirp address changed from " + str(text_address) + " to " + str(text_new_address)) except IOError: print(" Attempted to change chirp sensor at " + str(text_address) + " to " + str(text_new_address)) print(" Chirp module reported an IOError, either the address was wrong") print(" or the chirp sensor isn't responding correctly.") print(" ") raise
def sense_chirp(sensor_name, min_moist, max_moist, address): # The highest and lowest calibrated values #min_moist = min #max_moist = max counter = 0 avg_moist = 0 avg_moist_percent = 0 avg_temp = 0 data_list = [] # Initialize the sensor. chirpsense = chirp.Chirp(address=address, read_moist=True, read_temp=True, read_light=True, min_moist=min_moist, max_moist=max_moist, temp_scale='celsius', temp_offset=0) # First measurement could be false try: chirpsense.trigger() print( "First pull: Moisture value: {}, Moisture: {}% Temp: {}C ".format( chirpsense.moist, chirpsense.moist_percent, chirpsense.temp)) except RuntimeError as error: print(error.args[0]) # Try to take 3 measurements for better accuracy while counter < 3: try: chirpsense.trigger() # Not counting false data spikes if chirpsense.temp > -8 and chirpsense.temp < 47 and chirpsense.moist > min_moist and chirpsense.moist < max_moist: avg_moist += chirpsense.moist avg_moist_percent += chirpsense.moist_percent avg_temp += chirpsense.temp counter += 1 else: counter += 0.3 print("Moisture value: {}, Moisture: {}% Temp: {}C ".format( chirpsense.moist, chirpsense.moist_percent, chirpsense.temp)) except RuntimeError as error: print(error.args[0]) logging.error('RuntimeError@sense_chirp:', exc_info=error) time.sleep(4.0) data_list.append(sensor_name) data_list.append(round(avg_temp / counter, 1)) #data_list.append(int(avg_moist/counter)) #Moisture value data_list.append(None) #Humidity data_list.append(round(avg_moist_percent / counter, 1)) now = datetime.now() data_list.append(now.strftime("%Y-%m-%d %H:%M:%S")) print(data_list) upload(data_list)
# This is your main script. import machine import chirp from time import sleep i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=300000) sensor = chirp.Chirp(bus=i2c, address=0x20) print("Sensor Ready") while True: print("moisture: ", sensor.moisture) print("temp: ", sensor.temperature) sleep(5)
# This script should be used to change the address of a Chirp! i2c Soil Moisture # Sensor. Just enter the current address and the address you want to change to # in the fields below. # Part of the CSAD IoT Crop Care Project # Aidan Taylor 5th January 2018 import chirp sensorAddress = 0x20 sensorNewAddress = 0x21 # change this to the address you want to use soilSens = chirp.Chirp(address=sensorAddress, read_moist=True, read_temp=True, read_light=True, min_moist=200, max_moist=500, temp_scale='celsius', temp_offset=0) soilSens.sensor_address = sensorNewAddress print 'Sensor reassigned to...' print sensorNewAddress print 'done!'
# This script finds any connected chirp import machine import chirp # GREEN == SDA YELLOW == SCL from https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/ i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=300000) for addr in range(0, 127): try: print("trying {}".format(addr)) sensor = chirp.Chirp(bus=i2c, address=addr) chirp_add = sensor.sensor_address print('Sensor Found -- {}'.format(addr)) except: pass
#!/usr/bin/env python3 import time import traceback import graphitesend import chirp graphitesend.init(graphite_server="monitoring.local.yawk.at", prefix="i2c.chirp") sensor = chirp.Chirp() print("Beginning read loop.") while True: # noinspection PyBroadException try: data = {"temp.1": sensor.temp() * 0.1, "moist.1": sensor.moist()} except: data = None traceback.print_exc() if data: graphitesend.send_dict(data) time.sleep(20)
###############i2c params###################### I2C_ADDR = 0x27 # LCD I2C device address DEVICE = 0x76 # DME280 I2C address SMBUSID = 1 # 1: Pi 3 B SMBUS ############################################### #---------------------------------------------- ############Soil Moisture Sensor params######## # These values needs to be calibrated for the percentage to work! # The highest and lowest value with wet and dry soil. min_moist = 335 max_moist = 700 chirp = chirp.Chirp( address=0x21, # soil sensor i2c address read_moist=True, read_temp=True, read_light=True, min_moist=min_moist, max_moist=max_moist, temp_scale='farenheit', # 'farenheit' or 'celcius' temp_offset=0) # temp offset ############################################### #---------------------------------------------- ###############Thingspeak info################# INTERVAL = 1 # Delay between each reading (mins) THINGSPEAKKEY = 'ZEX2JMIAZHUXTG58' # API Write Key THINGSPEAKURL = 'https://api.thingspeak.com/update' # API URL tz_local = 'America/Chicago' # Local Timezone ############################################### #---------------------------------------------- ################LCD stuff###################### LCD_WIDTH = 20 # Maximum characters per line
sensor21= "21" # These values needs to be calibrated for the percentage to work! #For sensor 20 min_moist20 = 245 max_moist20 = 655 #For sensor 21 min_moist21 = 260 max_moist21 = 579 # Initialize the sensor chirp20 = chirp.Chirp(address=0x20, read_moist=True, read_temp=True, read_light=False, min_moist=min_moist20, max_moist=max_moist20, temp_scale='celsius', temp_offset=0) # Initialize the sensor chirp21 = chirp.Chirp(address=0x21, read_moist=True, read_temp=True, read_light=True, min_moist=min_moist21, max_moist=max_moist21, temp_scale='celsius', temp_offset=0) try:
import chirp import time # These values needs to be calibrated for the percentage to work! # The highest and lowest value the individual sensor outputs. min_moist = 240 max_moist = 790 # Initialize the sensor. chirp = chirp.Chirp(address=0x20, read_moist=True, read_temp=True, read_light=True, min_moist=min_moist, max_moist=max_moist, temp_scale='celsius', temp_offset=0) try: print('Moisture | Temp | Brightness') print('-' * 31) while True: # Trigger the sensors and take measurements. chirp.trigger() output = '{:d} {:4.1f}% | {:3.1f}°C | {:d}' output = output.format(chirp.moist, chirp.moist_percent, chirp.temp, chirp.light) print(output) time.sleep(1) except KeyboardInterrupt: print('\nCtrl-C Pressed! Exiting.\n')