-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
257 lines (213 loc) · 7.86 KB
/
Copy pathtest.py
File metadata and controls
257 lines (213 loc) · 7.86 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from mqtt import *
from yolobit import *
from yolobit_wifi import *
from event_manager import *
import dht
import network
##### CONFIG #######
BROKER_ADDR = '192.168.88.74' # broker server address
BROKER_PORT = 1883
# BROKER_ADDR = '0.tcp.ap.ngrok.io'
# BROKER_PORT = 15435
BROKER_USERNAME = "iot_1"
WIFI_SSID = 'TheVanilla.SaiGon'
WIFI_PASS = 'themlynua'
DHT_PIN = Pin(pin0.pin)
WATER_PIN = pin1
dhtSensor = dht.DHT11(Pin(pin0.pin))
# waterSensor = pin0
NUMBER_OF_DATAFIELD = 3
####################
######### ENUM ###########
WIFI_CHECK = 0
WIFI_CONNECT = 1
MQTT_SEND = 2
MQTT_CONNECT = 3
MQTT_FAIL = 4
MQTT_WAIT = 5
MQTT_ACTIVE = 6
MESSAGE_HUM = 7
MESSAGE_TEMP = 8
MESSAGE_RAIN = 9
INIT_STATE = 10
RUN_STATE = 11
##########################
####### WIFI CORE #######
class WifiClient():
def __init__(self, ssid, password):
self._station = network.WLAN(network.STA_IF)
self._station.active(True)
self._ssid = ssid
self._password = password
self._isConnected = False
self._state = WIFI_CHECK
def fsm(self):
try:
if (self._state == WIFI_CHECK):
if (self._station.isconnected() == True):
display.scroll("C")
self._state = WIFI_CHECK
self._isConnected = True
else:
display.scroll("N")
self._state = WIFI_CONNECT
self._isConnected = False
elif (self._state == WIFI_CONNECT):
display.scroll("T")
self._station.connect(self._ssid, self._password)
self._state = WIFI_CHECK
else:
self._state = WIFI_CHECK
except Exception as err:
#print(f"error wifi: {str(err)}")
self._state = WIFI_CHECK
def isConnected(self):
return self._station.isconnected()
#########################
####### MQTT CORE #######
class MqttClient():
state = MQTT_CONNECT
def __init__(self, brokerAddr, brokerPort, username, dhtPin, waterPin, wifiClient):
self._address = brokerAddr
self._port = brokerPort
self._isConnected = False
self._state = MQTT_CONNECT
self._username = username
self._message = -1
self._topic = ""
self._messageState = MESSAGE_HUM
self._waterSensor = waterPin
self._wifiClient = wifiClient
self.receiveTopic = "iot/capture"
self.activeCheckTopic = "iot/active"
self._mqtt_msg = {}
def fsm(self):
print(f'state: {MqttClient.state}')
if (wifiClient.isConnected() == False):
print(f"Wifi not connected")
pass
elif (MqttClient.state == MQTT_CONNECT):
try:
print(f"Connecting mqtt_broker {self._address}:{self._port}")
mqtt.connect_broker(server=self._address, port=self._port)
self._isConnected = True
MqttClient.state = MQTT_WAIT
except Exception as err:
MqttClient.state = MQTT_CONNECT
self._isConnected = False
elif (MqttClient.state == MQTT_WAIT):
pass
# elif (self._state == MQTT_ACTIVE):
# try:
# self._mqtt_msg = {}
# self._mqtt_msg["id"] = self._username
# mqtt.publish("active", self._mqtt_msg)
# print("sent")
# self._state = MQTT_WAIT
# except Exception as err:
# print(f"Error while sending mqtt: {str(err)}")
# self._isConnected = False
# self._state = MQTT_WAIT
elif (MqttClient.state == MQTT_SEND):
try:
dhtSensor.measure()
self._mqtt_msg = {}
self._mqtt_msg["id"] = self._username
for i in range(NUMBER_OF_DATAFIELD):
self._topic = self.generateTopic()
self._message = self.generateMessage()
self._mqtt_msg[self._topic] = self._message
self.updateMessageState()
mqtt.publish("weather_data", self._mqtt_msg)
print("sent")
MqttClient.state = MQTT_WAIT
except Exception as err:
print(f"Error while sending mqtt: {str(err)}")
self._isConnected = False
MqttClient.state = MQTT_WAIT
else:
MqttClient.state = MQTT_CONNECT
def generateMessage(self):
try:
retVal = -1
print(f"message code: {self._messageState}")
if (self._messageState == MESSAGE_HUM):
retVal = dhtSensor.humidity()
elif (self._messageState == MESSAGE_TEMP):
retVal = dhtSensor.temperature()
elif (self._messageState == MESSAGE_RAIN):
retVal = self._waterSensor.read_digital()
return retVal
except Exception as err:
print(f"Unknown exception while generate message of topic code {self._messageState}: {str(err)}")
return -1
def generateTopic(self):
retVal = "Unknown_topic"
# print(f"topic code: {self._messageState}")
if (self._messageState == MESSAGE_HUM):
retVal = "hum"
elif (self._messageState == MESSAGE_TEMP):
retVal = "temp"
elif (self._messageState == MESSAGE_RAIN):
retVal = "rain"
return retVal
def updateMessageState(self):
if (self._messageState == MESSAGE_HUM):
self._messageState = MESSAGE_TEMP
elif (self._messageState == MESSAGE_TEMP):
self._messageState = MESSAGE_RAIN
elif (self._messageState == MESSAGE_RAIN):
self._messageState = MESSAGE_HUM
####### BUTTON HANDLER #######
class ButtonHandler():
def __init__(self, resetButton):
self.resetButton = resetButton
self.resetPressed = False
def handle(self):
self.resetPressed = self.resetButton.is_pressed()
def isResetPressed(self):
return self.resetPressed
# -------------------------- #
def changeToSend(th_C3_B4ng_tin):
print("Change state to send")
MqttClient.state = MQTT_SEND
def changeToActiveSend(th_C3_B4ng_tin):
MqttClient.state = MQTT_ACTIVE
def run(isInit):
global wifiClient
global mqttClient
try:
if (isInit == True):
event_manager.reset()
# Init clients
# WIFI_SSID = input("Enter wifi name: ").strip()
# WIFI_PASS = input("Enter wifi password: ").strip()
wifiClient = WifiClient(WIFI_SSID, WIFI_PASS)
event_manager.add_timer_event(200, wifiClient.fsm)
while (wifiClient.isConnected() == False):
event_manager.run()
# BROKER_ADDR = input("Enter broker address: ").strip()
# BROKER_PORT = int(input("Enter broker port: ").strip())
mqttClient = MqttClient(BROKER_ADDR, BROKER_PORT, BROKER_USERNAME, DHT_PIN, WATER_PIN, wifiClient)
# event_manager.add_timer_event(5000, mqttClient.fsm)
# mqtt.on_receive_message(mqttClient.activeCheckTopic, changeToActiveSend)
mqtt.on_receive_message("iot/capture", changeToSend)
isInit = False
mqttClient.fsm()
event_manager.run()
# if (buttonHandler.isResetPressed() == True):
# isInit = True
except:
return False
return isInit
# global isInit
# global buttonHandler
isInit = True
# print("Creating button handler...")
# buttonHandler = ButtonHandler(button_a)
# print("Button handler created")
# event_manager.add_timer_event(10, buttonHandler.handle)
# print("Button handling added to event")
while True:
isInit = run(isInit)
mqtt.check_message()