main.py
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import array, utime
from machine import Pin
import rp2
import neopixel
import dht
import bluetoooth as bto
import ultrasonic
import reed
import display4
# --------------------------------------------------- #
# FUNCTIONS
# --------------------------------------------------- #
def _collect_sensor_datas(reed_data:int) -> str:
# Collect Humidity, Temperature
dht_data = dht.work_dht()
if dht_data == False:
dht_data = [0,0]
# Collect Ultrasonic distance
ultrasonic_data = ultrasonic.work_sr04()
# Make data string
send_data_str = str(reed_data) + '/' + str(dht_data[1]) + '/' + str(dht_data[0]) + '/' + str(ultrasonic_data)
return send_data_str
# --------------------------------------------------- #
# LOOP ENTRYPOINT
# --------------------------------------------------- #
def _run():
# INIT REED STATE
reed_data = -1
# LOOP
while True:
# ------------------------------------------- #
# DEFAULT LOOP
# ------------------------------------------- #
# Get data using BT(Standby)
input_data = bto.recv_data_bt()
# Get reed data from reed sensor
current_reed_data = reed.work_reed()
# ------------------------------------------- #
# IF CONDITION MET
# ------------------------------------------- #
if input_data != '' or reed_data != current_reed_data:
# Refine BT data
input_data = input_data.strip()
# Test code
print('INPUT FOUND ', input_data)
# IF INPUT MEANS GET MESSAGE or MEDICINE LID STATUS CHANGED
if input_data == 'REQ' or reed_data != current_reed_data:
# Send data using BT
bto.send_data_bt(_collect_sensor_datas(reed_data))
else:
# Refine BT data
input_data = input_data.strip()
display4.work_tm1637(input_data)
neopixel.work_led()
display4.off_tm1637()
# Send data using BT
bto.send_data_bt(_collect_sensor_datas(reed_data))
# Update reed state
reed_data = current_reed_data
if __name__ == '__main__':
_run()