def __init__(self, port: int): """ Wrapper for Grove Moisture sensor. :param port: port to which the sensor is connected """ super().__init__() self.sensor = GroveMoistureSensor(port)
def main(): period = 60 wait = 59.99 / period sensor = GroveMoistureSensor(0) with open('moisture_2.txt', 'w+') as f: f.write('Timestamp, Moisture\n') while True: moisture_temporary = [] for _ in range(period): try: moisture_temporary.append(sensor.moisture) except Exception as e: logging.error(e) finally: time.sleep(wait) moisture = sum(moisture_temporary) / len(moisture_temporary) now = datetime.now() with open('moisture_2.txt', 'a+') as f: try: # print('Measurement: {}'.format(moisture)) f.write('{}, {}\n'.format(now.strftime("%Y-%m-%d %H:%M:%S"), moisture)) except Exception as e: logging.error(e)
def main(): # Grove - 16x2 LCD(White on Blue) connected to I2C port lcd = JHD1802() # Grove - Moisture Sensor connected to port A0 sensor = GroveMoistureSensor(0) # Grove - Buzzer connected to port PWM buzzer = upmBuzzer.Buzzer(getGpioLookup('GPIO12')) while True: mois = sensor.moisture if 0 <= mois and mois < 300: level = 'dry' elif 300 <= mois and mois < 600: level = 'moist' else: level = 'wet' buzzer.playSound(upmBuzzer.BUZZER_DO, 200000) print('moisture: {}, {}'.format(mois, level)) lcd.setCursor(0, 0) lcd.write('moisture: {0:>6}'.format(mois)) lcd.setCursor(1, 0) lcd.write('{0:>16}'.format(level)) time.sleep(1)
def main(): sensor_moist = GroveMoistureSensor(0) sensor_tmp = DHT("11", 12) lcd = JHD1802() lcd.home() lcd.write("LCD model:".format(lcd.name)) lcd.setCursor(1, 0) lcd.write("{}".format(lcd.name)) time.sleep(5) lcd.clear() while True: for i in range(10): humi, temp = sensor_tmp.read() lcd.setCursor(0, 0) lcd.write('Temp.: {0:.1f}C'.format(temp)) lcd.setCursor(1, 0) lcd.write('Humi.: {0:.1f}%'.format(humi)) time.sleep(5) # lcd.clear() lcd.clear() mois = sensor_moist.moisture lcd.setCursor(0, 0) lcd.write('moisture: {0:>6}'.format(mois)) time.sleep(5) lcd.clear()
def __init__(self): # Define sensor channel self.moisture_channel = int( os.environ.get("analogue_channel_moisture_sensor")) self.max_humidity_reading = 610 # Dry in air self.min_humidity_reading = 385 # In water self.moisture_sensor = GroveMoistureSensor(self.moisture_channel)
def main(): sensor_temperature = DHT("11", 12) sensor_moisture = GroveMoistureSensor(0) with open('measurements.txt', 'w+') as f: f.write('Timestamp, Humidity, Temperature, Moisture\n') while True: try: with open('measurements.txt', 'a+') as f: humi, temp = sensor_temperature.read() mois = sensor_moisture.moisture now = datetime.now() f.write('{}, {}, {}, {}\n'.format(now.strftime("%H:%M:%S"), humi, temp, mois)) time.sleep(15) except Exception as err: debug(err)
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 grove.grove_moisture_sensor import GroveMoistureSensor from grove.grove_led import GroveLed from azure.iot.device.aio import IoTHubDeviceClient, ProvisioningDeviceClient from azure.iot.device import MethodResponse # Configuration parameters bme_pin = 1 bme_address = 0x76 moisture_pin = 2 led_pin = 16 # Create the sensors bus = smbus2.SMBus(bme_pin) calibration_params = bme280.load_calibration_params(bus, bme_address) moisture_sensor = GroveMoistureSensor(moisture_pin) # Create the LED led = GroveLed(led_pin) # Load the IoT Central connection parameters 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 moisture_sensor.moisture
import time from grove.grove_moisture_sensor import GroveMoistureSensor # connect to alalog pin 2(slot A2) PIN = 0 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)
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()
import socket import tempfile import sys from Logger import Logger from grove.grove_moisture_sensor import GroveMoistureSensor from grove.gpio import GPIO S1_IDLE_FREQUENCY = 3600 S1_WATERING_FREQUENCY = 30 logger = Logger(0) # Define Variables for Raspberry Pi sensor = GroveMoistureSensor(0) pump = GPIO(5, GPIO.OUT) # Watering Flag pumpWatering = False BROKER_URL = "mqtt.autonomous-gardener.tech" def on_connect(client, userdata, flags, rc): """ Called when the broker responds to our connection request. client: The client instance for this callback userdata: The private user data as set in Client() or user_data_set() flags: Response flags sent by the broker rc: The connection result