def add_measurement(json, date): waarde = json["waarde"] comment = json["commentaar"] measureType = json["type"] deviceId = DeviceHelper.get_sensor_for_measureType(measureType) nieuwId = DataRepository.create_meting(deviceId, waarde, comment, date) print("") print(f"Sensor measure") print(f"-deviceId: {deviceId} - {measureType}") print(f"-value: {waarde}") print(f"-comment: {comment}") print(f"-date: {date}") print(f"-result - data ID - {nieuwId}") print("") print(f'DATABASE') if nieuwId is not None: if measureType == "water": global last_water_value last_water_value = waarde if measureType == "light": global last_light_value last_light_value = waarde print(f'- NEW {measureType} measurement-sensor success') print("") return True else: print(f'- NEW {measureType} measurement-sensor failed') print("") return False
def Voeden_loop(gewicht): print("voederloop gestart") SetAngle(servoBakje, pwmBakje, 2) VoederLoop = True while VoederLoop == True: meting = round(hx.get_weight_mean(10), 2) print(meting, 'g') if meting < (gewicht / 1.05): print("servoVoeder") servoVoederBeweging() else: VoederLoop = False print(meting) datum = datetime.now().strftime('%Y-%m-%d %H:%M:%S') DataRepository.create_meting('4', 'PORTIE', '0000000000', meting, datum) SetAngle(servoBakje, pwmBakje, 8) time.sleep(0.5) time.sleep(60)
def set_actuator(measureType, status, message="automatic"): print(f"set_actuator - {status}") if measureType is not None and status is not None: #get data status = actionHelper.set_active(measureType, status) now = datetime.now() date = str(now.strftime("%Y-%m-%d %H:%M:%S")) comment = message deviceId = DeviceHelper.get_actuator_for_measureType(measureType) #print(f"Actuator measure - device ID - {device6Id}") #check state isActive = 0 if (status == True): isActive = 1 #add do database nieuwId = DataRepository.create_meting(deviceId, isActive, comment, date) print("") print(f"Actuator measure") print(f"-deviceId: {deviceId} - {measureType}") print(f"-isActive: {isActive}") print(f"-comment: {comment}") print(f"-date: {date}") print(f"-result - data ID - {nieuwId}") print("") print(f'DATABASE') if nieuwId is not None: print(f"- NEW {measureType} measurement-actuator success") print("") socketio.emit('B2F_actuator_toggled', { 'measureType': measureType, "status": isActive }) return True else: print(f"- NEW {measureType} measurement-actuator failed") print("") actionHelper.set_active(measureType, False) return False else: print("") print('ERROR: Toggle FAILED <==') print("") return False
def Sensoren(): while True: # set Trigger to HIGH GPIO.output(pinTrigger, True) # set Trigger after 0.01ms to LOW time.sleep(0.00001) GPIO.output(pinTrigger, False) startTime = time.time() stopTime = time.time() #Waarden van IR sensoren ophalen IR1_waarde = int(not GPIO.input(IR1)) IR2_waarde = int(not GPIO.input(IR2)) # save start time while 0 == GPIO.input(pinEcho): startTime = time.time() # save time of arrival while 1 == GPIO.input(pinEcho): stopTime = time.time() # time difference between start and arrival TimeElapsed = stopTime - startTime # vermenigvuldigen met Sonic speed (34300 cm/s) # gedeelt door 2, want het signaal gaat door en komt terug distance = TimeElapsed * 34300 / 2 percent = round(100 - ((distance) / 12.5 * 100)) datum = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(f"Distance: {percent}% en de datum is {datum}") time.sleep(0.5) DataRepository.create_meting('1', 'WATER', '0000000000', percent, datum) DataRepository.create_meting('2', 'VOEDING', '0000000000', IR1_waarde, datum) DataRepository.create_meting('3', 'VOEDING', '0000000000', IR2_waarde, datum) print("records were inserted.") time.sleep(900)
def snelheid(): global callibrate global time_for_speed global velocity global active global snelheid_voertuig callibrate = time.time() if (GPIO.input(GPIO_BEGIN_PIN_2) == GPIO.LOW): while GPIO.input(GPIO_END_PIN_2) == GPIO.HIGH: time_for_speed = time.time() - callibrate while GPIO.input(GPIO_END_PIN_2) == GPIO.LOW: velocity = 20 / time_for_speed active = 2 print("binnen") if (GPIO.input(GPIO_BEGIN_PIN) == GPIO.LOW): while GPIO.input(GPIO_END_PIN) == GPIO.HIGH: time_for_speed = time.time() - callibrate while GPIO.input(GPIO_END_PIN) == GPIO.LOW: velocity = 20 / time_for_speed active = 1 if (active == 1): print("speed") snelheid_voertuig = round(velocity, 1) print(f"{snelheid_voertuig} km/h") DataRepository.create_meting(velocity, 1) if (snelheid_voertuig > 90): DataRepository.update_status_licht(1, 2) if (DataRepository.read_status_licht_by_id(1)['verkeerslichtid'] == 2): GPIO.output(RED_LANG, GPIO.LOW) GPIO.output(YELLOW_LANG, GPIO.HIGH) GPIO.output(GREEN_LANG, GPIO.LOW) GPIO.output(buzzer, GPIO.HIGH) time.sleep(3) DataRepository.update_status_licht(1, 1) if (DataRepository.read_status_licht_by_id(1)['verkeerslichtid'] == 1): GPIO.output(RED_LANG, GPIO.HIGH) GPIO.output(YELLOW_LANG, GPIO.LOW) GPIO.output(GREEN_LANG, GPIO.LOW) time.sleep(3) DataRepository.update_status_licht(1, 3) time.sleep(2) # Delay in seconds GPIO.output(buzzer, GPIO.LOW) GPIO.output(RED_LANG, GPIO.LOW) GPIO.output(YELLOW_LANG, GPIO.LOW) GPIO.output(GREEN_LANG, GPIO.HIGH) if (active == 2): print("speed") snelheid_voertuig = round(velocity, 1) print(f"{snelheid_voertuig} km/h") DataRepository.create_meting(velocity, 2) if (snelheid_voertuig > 90): DataRepository.update_status_licht(1, 2) if (DataRepository.read_status_licht_by_id(1)['verkeerslichtid'] == 2): GPIO.output(RED_LANG, GPIO.LOW) GPIO.output(YELLOW_LANG, GPIO.HIGH) GPIO.output(GREEN_LANG, GPIO.LOW) GPIO.output(buzzer, GPIO.HIGH) time.sleep(3) DataRepository.update_status_licht(1, 1) if (DataRepository.read_status_licht_by_id(1)['verkeerslichtid'] == 1): GPIO.output(RED_LANG, GPIO.HIGH) GPIO.output(YELLOW_LANG, GPIO.LOW) GPIO.output(GREEN_LANG, GPIO.LOW) time.sleep(3) DataRepository.update_status_licht(1, 3) time.sleep(2) # Delay in seconds GPIO.output(buzzer, GPIO.LOW) GPIO.output(RED_LANG, GPIO.LOW) GPIO.output(YELLOW_LANG, GPIO.LOW) GPIO.output(GREEN_LANG, GPIO.HIGH) active = 0 Timer(0.1, snelheid).start()