def test_lcd(): print('Testing LCD...') lcd = LCD() lcd.text('Works', 1) ans = input('Is Works printed on the LCD? y/n: ') lcd.clear() return ans == 'y'
def customCallback(client, userdata, message): payload = json.loads(message.payload) if payload["deviceID"] == "CA2": lcd = LCD() lcd.clear() takePic() lcd.text('Time to eat', 1) lcd.text('medicine', 2) buzzerOn() lcd.clear()
def lcd_display(text_list): lcd = LCD() print "displaying" print text_list try: if 'clear' in text_list[0]: lcd.clear() else: lcd.text(text_list[0], 1) lcd.text(text_list[1], 2) sleep(1) except Exception: lcd.clear()
class LCDScroller: def __init__(self): self.lcd = LCD(rows = 2) self.lcd.clear() self.internalclear() self.update_time = time.time() def text(self, text, line): print(text) self.lcd.text(text,line) line -= 1 self.index[line] = 0 self.texts[line] = text def update(self): if time.time() - self.update_time < 0.5: return self.update_time = time.time() line = 0 for txt in self.texts: text_len = len(txt) if text_len > 16: self.index[line] += 1 new_text = txt[self.index[line] : self.index[line] + 16] if self.index[line] + 17 > text_len: if len(new_text) <= 0: self.index[line] = 0 new_text = txt[0 : 16 - len(new_text)] else: new_text += ' ' + txt[0 : 16 - len(new_text)] self.lcd.text(new_text,line + 1) line += 1 def clear(self): self.lcd.clear() self.texts self.internalclear() def internalclear(self): self.texts = ['',''] self.index = [0,0]
# If card is the authorised card if uid == [136, 4, 93, 174, 127] or uid == [8, 138, 147, 144, 129]: message = {} message["deviceid"] = "deviceid_1828034" now = datetime.datetime.now() message["datetimeid"] = now.isoformat() message["rfid"] = 1 message["servo"] = 1 message["bot"] = 0 message["camera"] = 0 message["webcontrol"] = 0 my_rpi.publish("lockdata", json.dumps(message), 1) lcd.text('Welcome', 1) lcd.text('Home!', 2) unlockDoor() lcd.clear() message = {} message["deviceid"] = "deviceid_1828034" now = datetime.datetime.now() message["datetimeid"] = now.isoformat() message["rfid"] = 0 message["servo"] = 0 message["bot"] = 0 message["camera"] = 0 message["webcontrol"] = 0 my_rpi.publish("lockdata", json.dumps(message), 1) else: message = {} message["deviceid"] = "deviceid_1828034" now = datetime.datetime.now() message["datetimeid"] = now.isoformat()
def pillSchedule(): try: # Setting up the pin for the dht sensor, the buzzer and motion sensor pin = 4 bz = Buzzer(5) pir = MotionSensor(26, sample_rate=20, queue_len=1) # To setup the certificate etc for mqtt host = "acway7h5aefsa-ats.iot.us-east-1.amazonaws.com" rootCAPath = "getEnvironmentCert/rootca.pem" certificatePath = "getEnvironmentCert/certificate.pem.crt" privateKeyPath = "getEnvironmentCert/private.pem.key" # Set the filename and bucket name BUCKET = 'sp-p1726819-s3-bucket' location = {'LocationConstraint': 'us-east-1'} file_path = "/home/pi/labs/assignment/photo" file_name = "user.jpg" my_rpi = AWSIoTMQTTClient("PubSub-p1726819" + rand_str_gen()) my_rpi.configureEndpoint(host, 8883) my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath) my_rpi.configureOfflinePublishQueueing( -1) # Infinite offline Publish queueing my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz my_rpi.configureConnectDisconnectTimeout(10) # 10 sec my_rpi.configureMQTTOperationTimeout(5) # 5 sec # Connect and subscribe to AWS IoT my_rpi.connect() update = True while update: try: global medicine_time data = get_data_from_dynamodb(1, 'pillSchedule', -1) medicine_time = data global medicine_time_plus medicine_time_plus = datetime.datetime.strptime( medicine_time, "%H:%M:%S") medicine_time_plus += datetime.timedelta(0, 60) medicine_time_plus = medicine_time_plus.strftime("%H:%M:%S") # Get the current time now and check if it is the time specified now = datetime.datetime.now() time_string = now.strftime("%H:%M:%S") if (medicine_time_plus > time_string > medicine_time): # Alert the user by turning on the buzzer bz.on() print("Time to eat medicine") # Print on the LCD to tell the user to take med lcd = LCD() lcd.text('Time to eat', 1) lcd.text('medicine :)', 2) # Checking how long did the user take to take the medicine after buzzer on old_now = datetime.datetime.now().strftime("%H:%M:%S") start_dt = datetime.datetime.strptime(old_now, '%H:%M:%S') pir.wait_for_motion( 10) # Motion sensor will only wait for 10 seconds new_now = datetime.datetime.now() new_time = new_now.strftime("%H:%M:%S") end_dt = datetime.datetime.strptime(new_time, '%H:%M:%S') duration = (end_dt - start_dt) diff = int(duration.seconds) # If the user took more than 10 seconds it means that the user did not take the medicine on time if diff < 8: # Take a photo and store to S3 takePhoto(file_path, file_name) uploadToS3(file_path, file_name, BUCKET, location) ageLow = 0 ageHigh = 0 # Print the deteced face attribute print('Detected faces for') for faceDetail in detect_faces(BUCKET, file_name): ageLow = faceDetail['AgeRange']['Low'] ageHigh = faceDetail['AgeRange']['High'] print('Age between {} and {} years old'.format( ageLow, ageHigh)) print('Here are the other attributes:') print( json.dumps(faceDetail, indent=4, sort_keys=True)) # To ensure that the medicine is taken by elderly and not others (Kids or pets) if (ageLow < 20 < ageHigh): # The medicine is taken in less than 10 seconds, turn off buzzer lcd.text('Medicine taken', 1) lcd.text('successfully :)', 2) print("User ate medicine after {:.2f} seconds". format(diff)) bz.off() # Get the current date and time and insert the data into the database True_string = 'True' message = {} message["deviceID"] = "CA2" message["datetimeID"] = new_now.isoformat() message["takenOnTime"] = True_string my_rpi.publish("sensor/takeMed", json.dumps(message), 1) # Clear LCD print("Uploaded data to database") print("Waiting for next schedule") lcd.clear() sleep(60) # The medicine is not taken by elderly else: bz.off() print("Medicine not taken by patient") # Send a msg to the user telling that the patient did not take medicine on time send_user_Msg("Medicine not taken by patient") # Get the current date and time and insert into the database False_string = 'False' message = {} message["deviceID"] = "CA2" message["datetimeID"] = new_now.isoformat() message["takenOnTime"] = False_string my_rpi.publish("sensor/takeMed", json.dumps(message), 1) # Clear LCD print("Uploaded data to database") print("Waiting for next schedule") lcd.clear() sleep(60) else: # The user did not take medicine within 10 seconds bz.off() print("Patient did not take medicine on time") # Send a msg to the user telling that the patient did not take medicine on time send_user_Msg("User did not eat medicine on time") # Get the current date and time and insert into the database False_string = 'False' message = {} message["deviceID"] = "CA2" message["datetimeID"] = new_now.isoformat() message["takenOnTime"] = False_string my_rpi.publish("sensor/takeMed", json.dumps(message), 1) # Clear LCD print("Uploaded data to database") print("Waiting for next schedule") lcd.clear() sleep(60) except KeyboardInterrupt: update = False except: print("Error while inserting data...") print(sys.exc_info()[0]) print(sys.exc_info()[1]) except: print(sys.exc_info()[0]) print(sys.exc_info()[1])
my_rpi = AWSIoTMQTTClient("basicPubSub") my_rpi.configureEndpoint(host, 8883) my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath) my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz my_rpi.configureConnectDisconnectTimeout(10) # 10 sec my_rpi.configureMQTTOperationTimeout(5) # 5 sec # Connect and subscribe to AWS IoT my_rpi.connect() my_rpi.subscribe("smartgarden/readings", 1, customCallback) lcd.text(" SMART GARDEN ", 1) lcd.text("* 환영합니다 *", 2) sleep(2) lcd.clear() # Publish to the same topic in a loop forever loopCount = 0 while True: temp = float(ser.readline()) hum = float(ser.readline()) soil = int(ser.readline()) light = int(ser.readline()) lcd.text('Humidity: {:.2f}%'.format(hum), 1) lcd.text('Temp: {:.2f} C'.format(temp), 2) sleep(2) lcd.clear() lcd.text('Moisture: {:d}'.format(soil), 1)