def __init__(self, port): """ Wrapper for Grove Humidity sensor. :param port: port to which the sensor is connected """ super().__init__() self.sensor = DHT("11", port)
def main(): sensor = 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: humidity = [] temperature = [] for _ in range(60 * 60): humi, temp = sensor.read() humidity.append(humi) temperature.append(temp) lcd.setCursor(0, 0) lcd.write('Temp.: {}C'.format(temp)) lcd.setCursor(1, 0) lcd.write('Humi.: {}%'.format(humi)) time.sleep(1) lcd.clear() temp_avg = round(sum(temperature) / len(temperature), 1) hum_avg = round(sum(humidity) / len(humidity), 1) lcd.setCursor(0, 0) lcd.write('Avg. temp.: {0:.1f}C'.format(temp_avg)) lcd.setCursor(1, 0) lcd.write('Avg. humi.: {0:.1f}%'.format(hum_avg)) time.sleep(20) lcd.clear()
def setup(self): try: self.sensor = DHT(self.dht_type, self.dht_pin) self.init = True except Exception as e: print("AirTemperatureHumiditySensor.setup: " + str(e)) self.init = False
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 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(): 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)
class AirTemperatureHumiditySensor(BaseSensor): def __init__(self): BaseSensor.__init__(self) self.dht_pin = 16 self.dht_type = '11' self.humidity_measurements = [] self.temperature_measurements = [] self.sensor = None self.setup() def setup(self): try: self.sensor = DHT(self.dht_type, self.dht_pin) self.init = True except Exception as e: print("AirTemperatureHumiditySensor.setup: " + str(e)) self.init = False def read(self): air_humidity, air_temperature = self._take_readings() if (air_humidity == 0 or air_temperature == 0): time.sleep(0.1) reH, reT = self._take_readings() if (air_humidity == 0): air_humidity = reH if (air_temperature == 0): air_temperature = reT air_humidity = self.rolling_average(air_humidity, self.humidity_measurements, 10) air_temperature = self.rolling_average(air_temperature, self.temperature_measurements, 10) return air_humidity, air_temperature def _take_readings(self): try: if not self.init: self.setup() air_humidity, air_temperature = self.sensor.read() except Exception as e: print("AirTemperatureHumiditySensor.read: " + str(e)) self.init = False air_humidity, air_temperature = self.null_value, self.null_value finally: return air_humidity, air_temperature
class Humidity(Sensor): def __init__(self, port): """ Wrapper for Grove Humidity sensor. :param port: port to which the sensor is connected """ super().__init__() self.sensor = DHT("11", port) @property def measurement(self): """ Get value of the measurement """ humidity, _ = self.sensor.read() return humidity
class Temperature(Sensor): def __init__(self, port): """ Wrapper for Grove Temperature sensor. :param port: port to which the sensor is connected """ super().__init__() self.sensor = DHT("11", port) @property def measurement(self): """ Get value of the measurement """ _, temperature = self.sensor.read() return temperature
def main(): relay = GroveRelay(18) button = GroveLedButton(5) lcd = JHD1802() temp_humd_sensor = DHT('11',16) classifier = '/home/pi/Desktop/Application/haarcascade_frontalface_default.xml' lcd.setCursor(0,0) lcd.write("System Activated") def doorbell_ring(index,event,tm): if event & Button.EV_SINGLE_CLICK: time_string = datetime.datetime.now().strftime("%H:%M:%S") lcd.setCursor(0,0) lcd.write("Doorbell Pressed") print("[INFO]Doorbell Rung...") button.led.light(True) lcd.setCursor(1,0) lcd.write("Look at Camera ") time.sleep(2) with picamera.PiCamera() as camera: camera.resolution = (320, 240) lcd.setCursor(1,0) lcd.write("3 2 1... ") time.sleep(3) camera.capture(stream,format='jpeg') lcd.setCursor(1,0) lcd.write("Image Captured ") print("[INFO]Image Captured...") data = pickle.loads(open('/home/pi/Desktop/Application/encodings.pickle', "rb").read()) buff = numpy.frombuffer(stream.getvalue(), dtype=numpy.uint8) image = cv2.imdecode(buff, 1) face_cascade = cv2.CascadeClassifier(classifier) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1,minNeighbors=5, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE) boxes = [(y, x + w, y + h, x) for (x, y, w, h) in faces] encodings = face_recognition.face_encodings(rgb, boxes) names = [] face_detected = False for encoding in encodings: matches = face_recognition.compare_faces(data["encodings"],encoding) name = "Unknown" if True in matches: matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 name = max(counts, key=counts.get) names.append(name) for ((top, right, bottom, left), name) in zip(boxes, names): cv2.rectangle(image, (left, top), (right, bottom),(0, 255, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2) face_detected = True if name != 'Unknown': print("[INFO]Face Detected...") print('[INFO] Entry Granted') lcd.setCursor(0,0) lcd.write("Welcome Home ") lcd.setCursor(1,0) lcd.write("{}! ".format(name)) relay.on() print("[INFO] Door Locked") time.sleep(5) relay.off() else: print('[INFO] Entry Denied ') lcd.setCursor(0,0) lcd.write("Face Unknown ") lcd.setCursor(1,0) lcd.write("Contact Owner ") print('[INFO] Door Locked') relay.off() if not face_detected: lcd.setCursor(0,0) lcd.write("No Face Detected ") lcd.setCursor(1,0) lcd.write("Try Again... ") button.led.light(False) cv2.imwrite(path1+'/'+time_string+'.jpg',image) print("[INFO]Image Stored...\n") time.sleep(5) humidity,temperature = temp_humd_sensor.read() lcd.setCursor(0,0) lcd.write("Temperature: {0:2}C".format(temperature)) lcd.setCursor(1,0) lcd.write("Humidity: {0:5}%".format(humidity)) button.on_event = doorbell_ring while True: client_socket, address = server_socket.accept() print("[INFO] Connection Established") full_message = client_socket.recv(8) decoded_message = full_message.decode('utf-8') print('[INCOMING SIGNAL] {}'.format(decoded_message)) if(decoded_message == '1'): relay.on() lcd.setCursor(0,0) lcd.write("Welcome Home! ") lcd.setCursor(1,0) lcd.write("Door Unlocked...") print("[INFO] Door Open") time.sleep(3) elif (decoded_message == '2'): relay.off() lcd.setCursor(0,0) lcd.write("Entry Denied ") lcd.setCursor(1,0) lcd.write("Door Locked... ") print("[INFO] Door Closed") time.sleep(3) elif(decoded_message == '4'): try: with picamera.PiCamera() as camera: print("[INFO] Live Video Stream Started...") while True: client, address = server_socket.accept() message = client.recv(8) try: check = message.decode('utf-8') if(check == '1'): camera.capture('video.jpg') photo = 'video.jpg' file = open(photo,'rb') byte = file.read(1024) while(byte): client.send(byte) byte = file.read(1024) file.close() client.close() elif(check == '0'): client.close() break finally: try: client.close() finally: pass finally: print("[INFO] Live Video Stream Ended...") elif(decoded_message == '5'): humidity,temperature = temp_humd_sensor.read() data = str(humidity) + ' ' + str(temperature) client,address = server_socket.accept() try: client.send(data.encode('utf-8')) client.close() except: try: client.close() except: pass humidity,temperature = temp_humd_sensor.read() lcd.setCursor(0,0) lcd.write("Temperature: {0:2}C".format(temperature)) lcd.setCursor(1,0) lcd.write("Humidity: {0:5}%".format(humidity))