def init_sensors(motion_function): global LIGHT_SENSOR global CO2_SENSOR print("Setting GPIO mode.") # Use GPIO numbers instead of pin numbers GPIO.setmode(GPIO.BCM) ### Motion Sensor ### print("Initializing MOTION sensor.") # Init the motion sensor GPIO.setup(MOTION_SENSOR_PIN, GPIO.IN) # We need to wait until the sensor is ready. print("Waiting for the MOTION sensor.") #while GPIO.input(MOTION_SENSOR_PIN) != 0: # time.sleep(0.1) # Add a callback function to be triggered everytime the # sensor detects motion and falls back again GPIO.add_event_detect(MOTION_SENSOR_PIN, GPIO.BOTH, callback=motion_function) # i2c bus i2c = busio.I2C(board.SCL, board.SDA) ### CO2 Sensor ### CO2_SENSOR = adafruit_ccs811.CCS811(i2c) while not CO2_SENSOR.data_ready: time.sleep(0.1) ### Light Sensor ### LIGHT_SENSOR = adafruit_tsl2561.TSL2561(i2c) LIGHT_SENSOR.enabled = True
def __init__(self, _i2c=None): self.i2c = busio.I2C(board.SCL, board.SDA) or _i2c # Create the ADC object using the I2C bus self.light = adafruit_tsl2561.TSL2561(self.i2c) self.light.enabled = True # Set gain 0=1x, 1=16x self.light.gain = 0
def _sensor_setup(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) sensor.gain = os.environ.get("TSL2561_GAIN", 0) sensor.integration_time = os.environ.get("TSL2561_INTERGRATION_TIME", 2) sensor.sensor_id = os.environ["TSL2561_SENSOR_ID"] return sensor
def getLight(path_to_record, interval, seconds_duration): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) print(path_to_record) N = datetime.now() # Do not touch finish_time = N + timedelta(seconds=seconds_duration) # Do not touch # print("N") # print(N) # print("Finish time") # print(finish_time) while datetime.now() < finish_time: # Finishes when the capture stops #print(datetime.now()) try: lux = sensor.lux #temperature_f = temperature_c * (9 / 5) + 32 luminosity = sensor.luminosity broadband = sensor.broadband file = open(path_to_record + "Light.txt", "a+") #print("Time {} Lux: {:.1f} luminosity: {} broadband {}".format( datetime.now(), lux, luminosity, broadband)) file.write( "Time {} Lux: {:.1f} luminosity: {} broadband {} \n".format( datetime.now(), lux, luminosity, broadband)) file.close() except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) time.sleep(interval)
def loop(self): """ Light sensor loop. Watches the TSL2561 lux value and, if it jumps by a great enough threshold, initiates the berry-selected message. """ logging.info('Entering light selection loop') try: import board import busio import adafruit_tsl2561 import time i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) count = 0 # Get initial reading by first waiting a bit (so we ignore the # useless initial value), then gathering the initial set of values # and averaging them together time.sleep(1) lux_readings = [] for i in range(0, LIGHT_NUMBER_VALUES): lux_readings.insert(0, sensor.lux) time.sleep(INITIAL_LIGHT_SENSOR_DELAY) average_lux = sum(lux_readings) / float(LIGHT_NUMBER_VALUES) while True: lux = sensor.lux if lux is not None: # Check if we're above threshold and if so, send the message change_rate = lux / average_lux if (change_rate > LIGHT_CHANGE_RATE_THRESHOLD and count == 0): self.select() # Don't keep sending select messages until after the # selection delay is over (decrement this each pass through # the loop) count = SELECTION_DELAY_COUNT # Update the average # TODO: make this more elegant lux_readings.insert(0, lux) lux_readings.pop() average_lux = sum(lux_readings) / float( LIGHT_NUMBER_VALUES) # Wait time.sleep(LIGHT_SENSOR_DELAY) # Delay count if count > 0: count -= 1 except Exception as ex: logging.error( '\n *** ERROR, light sensor thread died: {}'.format(ex, ), )
def main(): databaseinit() i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_si7021.SI7021(i2c) tsl = adafruit_tsl2561.TSL2561(i2c) print("Configuring TSL2561...") # Enable the light sensor tsl.enabled = True time.sleep(1) # Set gain 0=1x, 1=16x tsl.gain = 0 # Set integration time (0=13.7ms, 1=101ms, 2=402ms, or 3=manual) tsl.integration_time = 1 print("Getting readings...") # Get raw (luminosity) readings individually broadband = tsl.broadband infrared = tsl.infrared while True: lux = tsl.lux humidity = sensor.relative_humidity temperature = sensor.temperature print("\nTemperature: %0.1f C" % temperature) print("Humidity: %0.1f %%" % humidity) if lux is not None: print("Lux = {}".format(lux)) else: print( "Lux value is None. Possible sensor underrange or overrange.") lux = 0 a = { "awakenTime": str(int(math.ceil(timestamp()))), "humidity": str(int(math.ceil(humidity))), "lightValue": str(int(math.ceil(lux))), "temperature": str(int(math.ceil(temperature))) } try: db.child("userdata/" + login['localId']).push(a) print("Database writing success!") except: print( "Database cannot be written, check the permission on database") time.sleep(30.0)
def _connect_to_hardware(self): import board import busio import adafruit_tsl2561 i2c = busio.I2C(board.SCL, board.SDA) # Create the TSL2561 instance, passing in the I2C bus self.sensor = adafruit_tsl2561.TSL2561(i2c) # Enable the light sensor self.sensor.enabled = True # Set gain 0=1x, 1=16x self.sensor.gain = 0 # Set integration time (0=13.7ms, 1=101ms, 2=402ms, or 3=manual) self.sensor.integration_time = 1
def sensor_read (): i2c = busio.I2C(board.SCL, board.SDA) # Create the I2C bus tsl = adafruit_tsl2561.TSL2561(i2c) # Create the TSL2561 instance, passing> #tsl.enabled = True # Enable the light sensor time.sleep(1) tsl.gain = 0 # Set gain 0=1x, 1=16x tsl.integration_time = 1 # Set integration time (0=13.7ms, 1=101ms, 2=402ms> # Get raw (luminosity) readings individually broadband = tsl.broadband infrared = tsl.infrared # Get raw (luminosity) readings using tuple unpacking # broadband, infrared = tsl.luminosity # Get computed lux value (tsl.lux can return None or a float) lux = tsl.lux if lux is None: lux = 0 return lux
def setup_i2csensors(): i2c = busio.I2C(board.SCL, board.SDA) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76) tsl2561 = adafruit_tsl2561.TSL2561(i2c) # TODO: Automatically scrape sea level pressure from the weather station at sydney airport # http://www.bom.gov.au/products/IDN60801/IDN60801.94767.shtml bme280.sea_level_pressure = 1030.2 bme280._iir_filter = adafruit_bme280.IIR_FILTER_X16 bme280._overscan_temperature = adafruit_bme280.OVERSCAN_X16 bme280._overscan_humidity = adafruit_bme280.OVERSCAN_X16 bme280._overscan_pressure = adafruit_bme280.OVERSCAN_X16 tsl2561.enabled = True time.sleep(1) # set high gain mode tsl2561.gain = 1 # set integration time tsl2561.integration_time = 1 # 101ms return bme280, tsl2561
def __init__(self, i2c: busio.I2C, gain: int=0, integration_time: int=1): self._sensor = adafruit_tsl2561.TSL2561(i2c) self._sensor.gain = gain self._sensor.integration_time = integration_time
def main(): databaseinit() i2c = busio.I2C(board.SCL, board.SDA) #sensor = adafruit_si7021.SI7021(i2c) uncomment this for true temp and humid tsl = adafruit_tsl2561.TSL2561(i2c) PIR_input = 12 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(PIR_input, GPIO.IN) print("Configuring TSL2561...") # Enable the light sensor tsl.enabled = True time.sleep(1) # Set gain 0=1x, 1=16x tsl.gain = 0 # Set integration time (0=13.7ms, 1=101ms, 2=402ms, or 3=manual) tsl.integration_time = 1 print("Getting readings...") # Get raw (luminosity) readings individually broadband = tsl.broadband infrared = tsl.infrared x = 0 initial_time = 0 Final_time = 0 i = 0 j = 0 while True: time.sleep(.5) if (GPIO.input(PIR_input) == 1): j = 0 print("Motion detected") if (i == 0): x = datetime.datetime.now() initial_time = x.second i = 1 elif (i == 1): x = datetime.datetime.now() Final_time = x.second if (Final_time - initial_time >= 2): print("long motion detected") lux = tsl.lux humidity = 50 #simulated values temperature = 25 #simulated values print("\nTemperature: %0.1f C" % temperature) print("Humidity: %0.1f %%" % humidity) if lux is not None: print("Lux = {}".format(lux)) else: print( "Lux value is None. Possible sensor underrange or overrange." ) lux = 0 a = { "awakenTime": str(int(math.ceil(timestamp()))), "humidity": str(int(math.ceil(humidity))), "lightValue": str(int(math.ceil(lux))), "temperature": str(int(math.ceil(temperature))) } try: db.child("userdata/" + login['localId']).push(a) print("Database writing success!") time.sleep(10.0) except: print( "Database cannot be written, check the permission on database" ) if (GPIO.input(PIR_input) == 0): i = 0 print("No motion detected") if (j == 0): x = datetime.datetime.now() initial_time = x.second j = 1 elif (j == 1): x = datetime.datetime.now() Final_time = x.second if (Final_time - initial_time >= 6): print("small motion detected")
def __init__(self): i2c = busio.I2C(board.SCL, board.SDA) self.tsl = adafruit_tsl2561.TSL2561(i2c) self.tsl.enabled = True self.tsl.gain = 0 self.tsl.integration_time = 1
def get_devices(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) return [{'_id': 'tsl2561-1', 'sensor': sensor}]
import cv2 import requests import base64 import schedule import os import subprocess from getmac import get_mac_address URL = "insert url to the server here!" code_version = "2" framecounter = 0 GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) input = GPIO.input(16) i2c = I2C(board.SCL, board.SDA) bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, 0x76, debug=False) tsl2561 = adafruit_tsl2561.TSL2561(i2c, 0x29) tsl2561.gain = 1 # 1 = 16x tsl2561.integration_time = 2 # 2 = 402ms, 1 = 101ms, 0 = 13.7ms wlan_check_flag = False mac_address = get_mac_address(interface="wlan0") def wlan_check(): global wlan_check_flag if wlan_check_flag: print("long term issue, reboot") subprocess.call(['logger "wlan down, forcing a reboot"'], shell=True) wlan_check_flag = False subprocess.call(['sudo reboot'], shell=True) else: print("trying to recover connection") subprocess.call(['logger "Wlan is down, try a reconnect"'], shell=True)
import redis redis_host = "localhost" redis_port = 6379 redis_password = "" try: r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True) # Create the I2C bus i2c = busio.I2C(board.SCL, board.SDA) # Create the TSL2561 instance, passing in the I2C bus tsl = adafruit_tsl2561.TSL2561(i2c) tsl.enabled = True # Print chip info # print("Chip ID = {}".format(tsl.chip_id)) # print("Enabled = {}".format(tsl.enabled)) # print("Gain = {}".format(tsl.gain)) # print("Integration time = {}".format(tsl.integration_time)) # print("Configuring TSL2561...") # Enable the light sensor # sleep(100) # Set gain 0=1x, 1=16x tsl.gain = 0
def get_sensor(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) return sensor
import board import busio import adafruit_tsl2561 i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tsl2561.TSL2561(i2c) print('Lux: {}'.format(sensor.lux)) print('Broadband: {}'.format(sensor.broadband)) print('Infrared: {}'.format(sensor.infrared)) print('Luminosity: {}'.format(sensor.luminosity))
def connect(self): i2c = busio.I2C(board.SCL, board.SDA) tsl = adafruit_tsl2561.TSL2561(i2c) return tsl