-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·155 lines (113 loc) · 4.38 KB
/
main.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python3
from datetime import datetime
import serial
import os
import sys
import time
import dotenv
import paho.mqtt.client as mqtt
from picamera2 import Picamera2, Preview
from control import at_mqtt
from control import bme280_ctl
from control import i2c_power_controller
dotenv.load_dotenv()
# MQTT settings
CLIENT_ID = os.getenv('CLIENT_ID')
MY_NAME = os.getenv('MY_NAME')
# MQTT Broker settings
BROKER_ADDRESS = os.getenv('BROKER_ADDRESS')
BROKER_PORT = os.getenv('BROKER_PORT')
TEMPERATURE_TOPIC = f"{MY_NAME}/{CLIENT_ID}/temp"
HUMIDITY_TOPIC = f"{MY_NAME}/{CLIENT_ID}/humid"
PRESSURE_TOPIC = f'{MY_NAME}/{CLIENT_ID}/pressure'
YOLO_DATA_TOPIC = f'{MY_NAME}/{CLIENT_ID}/data'
RTC_TOPIC = f'{MY_NAME}/{CLIENT_ID}/rtc'
TIME_INTERVAL_TOPIC = f'{MY_NAME}/{CLIENT_ID}/interval'
TEST_DATA_TOPIC = f'{MY_NAME}/{CLIENT_ID}/test'
MAX_TRIES = 10
DATETIME_FORMAT = '%y/%m/%d,%H:%M:%S'
time_interval = None
last_received_time = None
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
def on_message(client, userdata, msg):
global last_received_time, time_interval
print(f"Received message: {msg.payload.decode()}")
if msg.topic == RTC_TOPIC:
print(f"RTC message received: {msg.payload.decode()}")
last_received_time = msg.payload.decode()
elif msg.topic == TIME_INTERVAL_TOPIC:
print(f"Time interval message received: {msg.payload.decode()}")
time_interval = msg.payload.decode()
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
def all_exit():
sys.exit(1)
if __name__ == '__main__':
at_mqtt.init_module()
num_tries = 0
# Check network reachability
while not at_mqtt.check_network_reachability():
num_tries += 1
if num_tries == MAX_TRIES:
all_exit()
time.sleep(3)
curr_time = at_mqtt.read_clock()
print(f"Current time: {curr_time}")
with open("/home/pi/rtc.txt", "r") as f:
last_received_time = f.read().strip()
with open("/home/pi/time_interval.txt", "r") as f:
time_interval = f.read().strip()
last_sent_time = datetime.strptime(last_received_time, DATETIME_FORMAT)
current_time = datetime.strptime(curr_time, DATETIME_FORMAT)
time_diff = current_time - last_sent_time
time_remaining = int(time_interval) * 3600 - time_diff.total_seconds()
if time_remaining > 20:
i2c_power_controller.set_time_command(int(time_remaining) * 1000000)
all_exit()
time.sleep(10)
# Read current time
mqttc.connect(BROKER_ADDRESS, int(BROKER_PORT), 60)
mqttc.loop_start()
mqttc.subscribe(RTC_TOPIC)
mqttc.subscribe(TIME_INTERVAL_TOPIC)
time.sleep(5)
curr_time = at_mqtt.read_clock()
mqttc.unsubscribe(RTC_TOPIC)
mqttc.unsubscribe(TIME_INTERVAL_TOPIC)
if last_received_time is None:
print("No RTC or time interval received")
with open("/home/pi/rtc.txt", "r") as f:
last_received_time = f.read().strip()
if time_interval is None:
with open("/home/pi/time_interval.txt", "r") as f:
time_interval = f.read().strip()
mqttc.publish(RTC_TOPIC, curr_time, qos=1, retain=True)
mqttc.publish(TIME_INTERVAL_TOPIC, str(time_interval), qos=1, retain=True)
with open("/home/pi/rtc.txt", "w") as f:
f.write(curr_time)
with open("/home/pi/time_interval.txt", "w") as f:
f.write(time_interval)
i2c_power_controller.set_time_command(int(time_interval) * 3600 * 1000000)
picam2 = Picamera2()
preview_config = picam2.create_preview_configuration(main={"size": (1280, 960)})
picam2.configure(preview_config)
picam2.start()
time.sleep(1)
metadata = picam2.capture_file("/tmp/img.jpg")
print(metadata)
picam2.close()
yolo_output = os.popen(f"/home/pi/onnx/build/main").read()
time.sleep(5)
# mqttc.reconnect()
# time.sleep(3)
mqttc.publish(RTC_TOPIC, curr_time, qos=1, retain=True)
mqttc.publish(TEMPERATURE_TOPIC, str(bme280_ctl.get_temperature()), qos=1, retain=True)
mqttc.publish(HUMIDITY_TOPIC, str(bme280_ctl.get_humidity()), qos=1, retain=True)
mqttc.publish(PRESSURE_TOPIC, str(bme280_ctl.get_pressure()), qos=1, retain=True)
# mqttc.publish(TEST_DATA_TOPIC, "Hello World", qos=1, retain=True)
mqttc.publish(YOLO_DATA_TOPIC, yolo_output, qos=1, retain=True)
time.sleep(5)
mqttc.loop_stop()
mqttc.disconnect()