def __init__(self, port: int): """ Wrapper for Grove Light sensor. :param port: port to which the sensor is connected """ super().__init__() self.sensor = GroveLightSensor(port)
def main(): value = "Jarvis Focus" # Grove - 16x2 LCD(White on Blue) connected to I2C port lcd = JHD1802() display_in_lcd(lcd, 0, value) time.sleep(2) display_in_lcd(lcd, 0, "Identify") display_in_lcd(lcd, 1, "Temp-Humi") time.sleep(2) # Grove - Light Sensor connected to port A0 light_sensor = GroveLightSensor(0) # Range Sensor - D24 ultrasonic_range_senor = GroveUltrasonicRanger(24) # Motion Sensor - D18 motion_sensor = GroveMiniPIRMotionSensor(18) # Grove - Temperature&Humidity Sensor connected to port D5 climate_sensor = DHT('11', 5) # Grove - LED Button connected to port D16 button = GroveLedButton(16) def on_detect(): print('motion detected') motion_sensor.on_detect = on_detect def on_event(index, event, tm): if event & Button.EV_SINGLE_CLICK: print('single click') button.led.light(True) elif event & Button.EV_LONG_PRESS: print('long press') button.led.light(False) button.on_event = on_event while True: distance = ultrasonic_range_senor.get_distance() print('{} cm'.format(distance)) light_sensor_output = light_sensor.light humi, temp = climate_sensor.read() row_one = f"L:{light_sensor_output}" row_two = f"H:{humi}-T:{temp}" display_in_lcd(lcd, 0, row_one) display_in_lcd(lcd, 1, row_two) time.sleep(2)
def main(): # Grove - Servo connected to PWM port servo = GroveServo(12) # Grove - Light Sensor connected to port A0 sensor = GroveLightSensor(0) while True: angle = sensor.light * 180 / 1000 print('light value {}, turn to {} degree.'.format(sensor.light, angle)) servo.setAngle(angle) time.sleep(1)
def main(): value = "Jarvis Sense" # Grove - 16x2 LCD(White on Blue) connected to I2C port lcd = JHD1802() display_in_lcd(lcd, 0, value) time.sleep(2) display_in_lcd(lcd, 0, "Light-Moisture") display_in_lcd(lcd, 1, "Temp-Humi") time.sleep(2) # Grove - Light Sensor connected to port A0 light_sensor = GroveLightSensor(0) # Grove - Moisture Sensor connected to port A2 moisture_sensor = GroveMoistureSensor(2) # Grove - Temperature&Humidity Sensor connected to port D5 climate_sensor = DHT('11', 5) # Grove - Relay connected to port D16 relay = GroveRelay(16) relay.off( ) # It was supposed to be off. Due to mis-wiring it works the other way while True: light_sensor_output = light_sensor.light humi, temp = climate_sensor.read() moisture = moisture_sensor.moisture turn_on_fan = True if light_sensor_output < 30 else False fan_status = " " if turn_on_fan: relay.off() # Turn of the relay will turn on the Fan fan_status = "Fan" else: relay.on() # Turn off the fan fan_status = "X" row_one = f"L:{light_sensor_output}-M:{moisture} " row_two = f"H:{humi}-T:{temp}C-{fan_status}" display_in_lcd(lcd, 0, row_one) display_in_lcd(lcd, 1, row_two) time.sleep(2)
from dotenv import load_dotenv from grove.grove_light_sensor_v1_2 import GroveLightSensor # Configuration parameters bme_pin = 1 bme_address = 0x76 moisture_pin = 2 light_pin = 0 # Create the sensors bus = smbus2.SMBus(bme_pin) calibration_params = bme280.load_calibration_params(bus, bme_address) moisture_sensor = GroveMoistureSensor(moisture_pin) light_sensor = GroveLightSensor(light_pin) load_dotenv() id_scope = os.getenv('ID_SCOPE') device_id = os.getenv('DEVICE_ID') primary_key = os.getenv('PRIMARY_KEY') def getTemperaturePressureHumidity(): return bme280.sample(bus, bme_address, calibration_params) def getMoisture(): return round(moisture_sensor.moisture, 2)
def main(): # Grove - Servo connected to PWM port servo = GroveServo(12) servo_angle = 90 # Grove - mini PIR motion pir_sensor connected to port D5 pir_sensor = GroveMiniPIRMotionSensor(5) # Grove - Ultrasonic Ranger connected to port D16 ultrasonic_sensor = GroveUltrasonicRanger(16) # Grove - LED Button connected to port D18 button = GroveLedButton(18) # Grove - Moisture Sensor connected to port A0 moisture_sensor = GroveMoistureSensor(0) # Grove - Light Sensor connected to port A2 light_sensor = GroveLightSensor(2) light_state = False # Grove - Temperature&Humidity Sensor connected to port D22 dht_sensor = DHT('11', 22) # Callback for server RPC requests (Used for control servo and led blink) def on_server_side_rpc_request(request_id, request_body): log.info('received rpc: {}, {}'.format(request_id, request_body)) if request_body['method'] == 'getLedState': client.send_rpc_reply(request_id, light_state) elif request_body['method'] == 'setLedState': light_state = request_body['params'] button.led.light(light_state) elif request_body['method'] == 'setServoAngle': servo_angle = float(request_body['params']) servo.setAngle(servo_angle) elif request_body['method'] == 'getServoAngle': client.send_rpc_reply(request_id, servo_angle) # Connecting to ThingsBoard client = TBDeviceMqttClient(thingsboard_server, access_token) client.set_server_side_rpc_request_handler(on_server_side_rpc_request) client.connect() # Callback on detect the motion from motion sensor def on_detect(): log.info('motion detected') telemetry = {"motion": True} client.send_telemetry(telemetry) time.sleep(5) # Deactivating the motion in Dashboard client.send_telemetry({"motion": False}) log.info("Motion alert deactivated") # Callback from button if it was pressed or unpressed def on_event(index, event, tm): if button._GroveLedButton__btn.is_pressed(): log.debug('button: single click') telemetry = {"button_press": True} client.send_telemetry(telemetry) log.info("Pressed") else: log.debug('button: single click') telemetry = {"button_press": False} client.send_telemetry(telemetry) log.info("Unpressed") if event & Button.EV_SINGLE_CLICK: button.led.light(True) elif event & Button.EV_DOUBLE_CLICK: button.led.blink() elif event & Button.EV_LONG_PRESS: button.led.light(False) # Adding the callback to the motion sensor pir_sensor.on_detect = on_detect # Adding the callback to the button button.on_event = on_event try: while True: distance = ultrasonic_sensor.get_distance() log.debug('distance: {} cm'.format(distance)) humidity, temperature = dht_sensor.read() log.debug('temperature: {}C, humidity: {}%'.format( temperature, humidity)) moisture = moisture_sensor.moisture log.debug('moisture: {}'.format(moisture)) log.debug('light: {}'.format(light_sensor.light)) # Formatting the data for sending to ThingsBoard telemetry = { 'distance': distance, 'temperature': temperature, 'humidity': humidity, 'moisture': moisture, 'light': light_sensor.light } # Sending the data client.send_telemetry(telemetry).get() time.sleep(.1) except Exception as e: raise e finally: client.disconnect()
def get_sensor(pin): sensor = GroveLightSensor(pin) return sensor
import time from grove.grove_led import GroveLed from grove.grove_light_sensor_v1_2 import GroveLightSensor ledPin = 5 led2Pin = 16 led3Pin = 18 sensorPin = 0 sensor = GroveLightSensor(sensorPin) led = GroveLed(ledPin) led2 = GroveLed(led2Pin) led3 = GroveLed(led3Pin) led.off() led2.off() led3.off() time.sleep(.1) led.on() led2.on() led3.on() time.sleep(.1) led.off() led2.off() led3.off() time.sleep(.1) led.on() led2.on() led3.on() time.sleep(.1) led.off()