-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_client.py
74 lines (63 loc) · 2.2 KB
/
mqtt_client.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
import os
import sys
import time
from Adafruit_IO import MQTTClient
from dotenv import load_dotenv
from api_call import get_api
from fruit_detector import detect_fruit
from image_processor import image_publisher
load_dotenv()
# Load AIO_USERNAME and AIO_KEY from environment variables
AIO_USERNAME = os.getenv('AIO_USERNAME')
AIO_KEY = os.getenv('AIO_KEY')
# Define feed IDs
CONFIDENCE_FEED_ID = "confidence-score"
DETECTED_OBJ_FEED_ID = "detected-obj"
NUTRITION_FEED_ID = "nutrition-values"
IMAGE_FEED_ID = "captured-image"
def connected(client):
# Callback function when connected to the AIO server
print("Connected to the AIO server!!!!")
def disconnected(client):
# Callback function when disconnected from the AIO server
print("Disconnected from the AIO server!!!")
sys.exit(1)
def message(client, feed_id, payload):
# Callback function when a message is received
print("Received: " + payload)
client = MQTTClient(AIO_USERNAME, AIO_KEY)
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
def main():
scan_count = 0
fruits_detected = []
while True:
# Call the program's primary functions
detected_fruit = detect_fruit(client, DETECTED_OBJ_FEED_ID, CONFIDENCE_FEED_ID)
time.sleep(0.5)
image_publisher(client, IMAGE_FEED_ID, detected_fruit.image)
fruit_nutrition = get_api(client, NUTRITION_FEED_ID, detected_fruit.name)
if fruit_nutrition is None:
print("WARNING :: Unable to get data.")
else:
scan_count += 1
fruits_detected.append(fruit_nutrition.name)
print(fruit_nutrition)
# Handle retries and exit
prompt = input("INFO :: Input 'e' for rescan, anything else to stop the program: ")
if prompt == 'e':
print("__RESTARTING__")
time.sleep(0.5)
else:
client.disconnect()
time.sleep(0.01)
print("INFO :: User timed out.")
print(f"Successful scan count: {scan_count}")
for fruit in fruits_detected:
print(f"Name: {fruit}")
break
if __name__ == '__main__':
client.connect()
client.loop_background()
main()