-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT_GAIManual(two-way).py
205 lines (163 loc) · 7.63 KB
/
MQTT_GAIManual(two-way).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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# QCI&AI Learning Tools Software
# Supported by
# National Science Technology Council, Taiwan
# AI-FML Agent with Machine Learning Tools for AloT Future Applications
# MOST 109-2622-E-024-001-CC1, MOST 110-2622-E-024-003-CC1, MOST 111-2622-E-024-001, and NSTC 112-2622-E-024-002
# Copyright (c) 2024
# KWS Center/OASE Lab. of NUTN, Taiwan
# Zsystem Technology Co., Taiwan
# Mediawave Intelligent Communication Limited., Taiwan
# All rights reserved.
from gpb import delay
import pomaspackage as pomaspkg
import time
import const
#inferred result: 17.620(safe), 50(normal), 82.380(dangerous), 64.065(normal), 82.380(dangerous)
input_data = [
['29', '3263', '9', '8.7'],
['95', '76', '7', '7.6'],
['28', '754', '7', '7.6'],
['18', '3046', '8', '8.4'],
['191', '716', '6.5', '7']]
class AILearningPlatform:
def __init__(self):
#Step 1: Initialize value and Hardware
self.myLCD = pomaspkg.LcdModule()
self.mySpeaker = pomaspkg.SpeakerModule()
self.myButton = pomaspkg.ButtonModule(self.myLCD)
self.myLED = pomaspkg.LedModule()
self.myFan = pomaspkg.FanModule()
self.myUltrasound = pomaspkg.UltrasoundModule()
self.myLightSensor = pomaspkg.LightSensorModule()
self.myDHT = pomaspkg.DHTModule()
#ClearLCD, showAI-FML Logo, and play audio
self.myLCD.Clear_LCD()
self.myLCD.ShowImage_LCD('aifml_logo.bmp', const.LCDImgDelayTime)
self.mySpeaker.playAudio('aifml_logo.wav')
#check Wifi and MQTT
self.wifi = self.initialize_wifi()
if self.wifi is None:
raise ValueError("Wifi failed to initialize")
else:
self.MQTT = self.initialize_mqtt()
def initialize_wifi(self):
#Step 2: Check Wifi
wifi = pomaspkg.WifiUartRT10(const.UART_PORTNO, 115200, self.myLCD)
wifi.vccReboot()
wifi.sendAT('VER') # read version
wifi.cmdAT()
WifiConnected = wifi.wifiConap(const.WIFI_NAME, const.WIFI_PASSWORD)
# Check if WIFI connection is success or failure
if not WifiConnected:
self.mySpeaker.playAudio('WiFiFailure.mp3')
return None
self.mySpeaker.playAudio('WiFiConnected.mp3')
wifi.wifiGetip()
return wifi
def initialize_mqtt(self):
#Step 3: Check MQTT Server
self.wifi.mqttSetup(const.MQTT_USERNAME, const.MQTT_PASSWORD)
MQTTConnected = self.wifi.mqttCon(const.MQTT_SERVER, const.MQTT_PORTNO, const.MQTT_CLIENTID)
#Check if MQTT connection is success or failure
if not MQTTConnected:
self.mySpeaker.playAudio('MQTTFailure.mp3')
return False
#Subscribe Topic
self.wifi.mqttSub(const.MQTT_AIFMLTOPIC_Subscriber, const.MQTT_pqos, const.MQTT_WaitTime)
delay(1000)
self.myLCD.ShowImage_LCD('aifml_logo.bmp', const.LCDImgDelayTime)
return True
def organizeMsg(self, outv_msg, msg):
'''
Function: Organize the msg to show it on the LCD
Input:
(1) msg: inferred result
(2) data: input data
Output: message which shows on the LCD and terminal
'''
outv = float(outv_msg)
#show value with 3 decimal
lcdMsg = 'QCI&AI-FML: {:.3f}'.format(outv) + '\r\n{}'.format(msg)
terminalMsg = 'QCI&AI-FML: {:.3f}'.format(outv) + ' ({})'.format(msg)
return lcdMsg, terminalMsg
def Activate_Hardware(self, outv_msg):
'''
Function: Activate Hardware according to outv, and show message on LCD and terminal
Input:
(1) outv: inferred result
(2) display: LCD
(3) lcdMsg: message displayed on LCD
(4) terminalMsg: message displayed on terminal
'''
if (outv_msg == ''):
#if it is an empty string, end the function
return
#light off
self.myLED.TurnOff_ledGREEN()
self.myLED.TurnOff_ledRED()
#According to the parameters of output fuzzy variable to adjust the boundary of if, elseif, and else
#print('outv_msg=', outv_msg)
outv = float(outv_msg)
lcdMsg = ''
terminalMsg = ''
if (outv <= 5.5):
#poor
self.mySpeaker.playAudio('GAIpoorEN.wav')
self.myFan.Control_Fan(0)
self.myLCD.ShowImage_LCD('GAIpoor.bmp', const.LCDImgDelayTime)
lcdMsg, terminalMsg = self.organizeMsg(outv_msg, 'The ability of GAI in\ntravel is \nPoor!')
#control LED to flash
self.myLED.Flash_ledRED(30, 40)
elif (outv > 5.5 and outv <= 7.5):
#medium
self.mySpeaker.playAudio('GAImediumEN.wav')
self.myLCD.ShowImage_LCD('GAImedium.bmp', const.LCDImgDelayTime)
lcdMsg, terminalMsg = self.organizeMsg(outv_msg, 'The ability of GAI to\ntravel is \nCommon!')
self.myLED.Flash_ledGREENledRED(30, 40)
elif (outv > 7.5):
#good
self.mySpeaker.playAudio('GAIgoodEN.wav')
self.myFan.Control_Fan(300)
self.myLCD.ShowImage_LCD('GAIgood.bmp', const.LCDImgDelayTime)
lcdMsg, terminalMsg = self.organizeMsg(outv_msg, 'The ability of GAI in\ntravel is \nGood!')
self.myLED.Flash_ledGREEN(30, 40)
#show message on LCD
if (lcdMsg != ''):
self.myLCD.ShowMessage_LCD(lcdMsg)
#show message on terminal
if (terminalMsg != ''):
print(terminalMsg)
self.myFan.Control_Fan(0)
#light off
self.myLED.TurnOff_ledGREEN()
self.myLED.TurnOff_ledRED()
def getInputData(self, index):
'''
Function: read input data according to index
Output: data that will be sent and use 'underline(_)' to connect the input data
'''
data = input_data[index]
data_str = const.MQTT_Publish_Separator.join(data)
return data_str
def start(self):
if self.MQTT == False:
return
data_index_old = -1
while True:
#check button pressed
data_index_new = self.myButton.getButtonPressed(data_index_old)
#publish the input_msg to AI-FML
if (data_index_new >= 0):
#read data based on data_index
input_msg = self.getInputData(data_index_new)
self.wifi.mqttPub(const.MQTT_pqos, const.MQTT_retain, const.MQTT_AIFMLTOPIC_Publisher, input_msg)
data_index_old = -1
#Check the received message from the subscribed topic of the MQTT Server
topic, outv_msg = self.wifi.mqttRecv(const.MQTT_CHKRECV_COUNT)
#if topic and outv_msg are both correct, then process data
if ((topic == const.MQTT_AIFMLTOPIC_Subscriber) and (outv_msg != '') and (outv_msg.find(const.MQTT_Publish_Separator) == -1)):
self.Activate_Hardware(outv_msg)
self.myLED.Flash_ledGREEN(1, 200)
if __name__ == "__main__":
platform = AILearningPlatform()
platform.start()