109 lines
2.8 KiB
Python
109 lines
2.8 KiB
Python
import os
|
|
import time
|
|
import network
|
|
import json
|
|
import dht
|
|
from machine import Pin
|
|
from machine import WDT
|
|
import rgb
|
|
import secrets
|
|
import netman
|
|
import mqtt
|
|
import gc
|
|
|
|
gc.enable()
|
|
|
|
#Init WiFi
|
|
country = 'US'
|
|
ssid = secrets.ssid
|
|
password = secrets.password
|
|
sensor = dht.DHT22(machine.Pin(1))
|
|
|
|
#mqtt config
|
|
mqtt_server = secrets.mqtt_server
|
|
client_id = secrets.client_id
|
|
user_t = secrets.user_t
|
|
password_t = secrets.password_t
|
|
topic_pub_temphum = b'enclosure/dht'
|
|
last_will_topic = b'enclosure/lw/topic'
|
|
led_sub_topic = b'/enclosure/led'
|
|
led_sub_status = b'/enclosure/status/led'
|
|
|
|
|
|
last_message = 0
|
|
message_interval = 2
|
|
|
|
|
|
|
|
print("Standing by for 2 seconds")
|
|
time.sleep(2)
|
|
|
|
wdt = WDT(timeout=8388) # enable it with a timeout of 20s
|
|
machine.freq(125000000)
|
|
print("System Clock set to 125MHz")
|
|
wdt.feed()
|
|
|
|
def flash_onboard_led():
|
|
onboard_led = machine.Pin("LED", machine.Pin.OUT)
|
|
wdt.feed()
|
|
onboard_led.on()
|
|
time.sleep_ms(10)
|
|
onboard_led.off()
|
|
|
|
try:
|
|
wifi_connection = netman.connectWiFi(ssid,password,country,client_id)
|
|
except RuntimeError as e:
|
|
machine.reset()
|
|
|
|
client = mqtt.mqtt_connect(client_id,mqtt_server,user_t,password_t,last_will_topic)
|
|
if not client.connect(clean_session=False):
|
|
flash_onboard_led()
|
|
print("New session being set up")
|
|
|
|
mqtt.mqtt_subscribe(client,led_sub_topic)
|
|
rgb.solid(rgb.white)
|
|
machine.freq(65000000)
|
|
print("System Clock set to 65MHz")
|
|
|
|
while True:
|
|
flash_onboard_led()
|
|
time.sleep_ms(500)
|
|
# At this point in the code you must consider how to handle
|
|
# connection errors. And how often to resume the connection.
|
|
if client.is_conn_issue():
|
|
while client.is_conn_issue():
|
|
wdt.feed()
|
|
# If the connection is successful, the is_conn_issue
|
|
# method will not return a connection error.
|
|
client.reconnect()
|
|
client.publish(last_will_topic, 'Disconnected', retain=True)
|
|
else:
|
|
wdt.feed()
|
|
client.resubscribe()
|
|
|
|
if (time.time() - last_message) > message_interval:
|
|
wdt.feed()
|
|
gc.collect()
|
|
temp, hum = mqtt.read_sensor(sensor)
|
|
dict = {}
|
|
dict["temperature"] = temp
|
|
dict["humidity"] = hum
|
|
print(dict)
|
|
data_out = json.dumps(dict)
|
|
flash_onboard_led()
|
|
client.publish(topic_pub_temphum, data_out)
|
|
last_message = time.time()
|
|
|
|
# WARNING!!!
|
|
# The below functions should be run as often as possible.
|
|
# There may be a problem with the connection. (MQTTException(7,), 9)
|
|
# In the following way, we clear the queue.
|
|
for _ in range(500):
|
|
wdt.feed()
|
|
client.check_msg() # needed when publish(qos=1), ping(), subscribe()
|
|
client.send_queue() # needed when using the caching capabilities for unsent messages
|
|
if not client.things_to_do():
|
|
break
|
|
time.sleep_ms(1)
|
|
|
|
client.disconnect() |