change broker, introduce mqtt authentication
This commit is contained in:
14
config.py
14
config.py
@ -1,5 +1,11 @@
|
|||||||
ssid = 'telemetry'
|
import secrets
|
||||||
|
|
||||||
mqtt_server = '172.16.10.47'
|
SSID = 'telemetry'
|
||||||
heartbeat_topic = b'IoT/rpipwThermometer/0/Heartbeat'
|
WPA_KEY = secrets.WPA_KEY
|
||||||
watchdog_topic = b'IoT/Watchdog'
|
MQTT_SERVER = '172.16.10.47'
|
||||||
|
MQTT_LOGIN = 'thermo-anna'
|
||||||
|
MQTT_PASSWORD = secrets.MQTT_PASSWORD
|
||||||
|
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
|
||||||
|
WATCHDOG_TOPIC = b'IoT/Watchdog'
|
||||||
|
|
||||||
|
MAX_CONNECT_RETRY_CNT = 1000000
|
25
main.py
25
main.py
@ -1,13 +1,12 @@
|
|||||||
# main.py -- put your code here!
|
# main.py -- put your code here!
|
||||||
|
|
||||||
from umqttsimple import MQTTClient
|
from umqttsimple import MQTTClient, MQTTException
|
||||||
import time
|
import time
|
||||||
import ubinascii
|
import ubinascii
|
||||||
from machine import WDT
|
from machine import WDT
|
||||||
import network
|
import network
|
||||||
import os
|
import os
|
||||||
import config
|
import config
|
||||||
import secrets
|
|
||||||
|
|
||||||
last_message = 0
|
last_message = 0
|
||||||
message_interval = 5
|
message_interval = 5
|
||||||
@ -20,20 +19,26 @@ client = None
|
|||||||
def init():
|
def init():
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
wlan.connect(config.ssid, secrets.wpakey)
|
wlan.connect(config.SSID, config.WPA_KEY)
|
||||||
|
|
||||||
while (not wlan.isconnected()):
|
connectCnt = 0
|
||||||
print('Not yet connected to WLAN')
|
while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT):
|
||||||
|
connectCnt += 1
|
||||||
|
if (not wlan.isconnected()):
|
||||||
|
print("Failed to connect to wlan, restarting")
|
||||||
|
machine.reset()
|
||||||
|
|
||||||
print('Connected to WLAN')
|
print('Connected to WLAN')
|
||||||
|
|
||||||
global client
|
global client
|
||||||
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
|
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
|
||||||
client = MQTTClient(clientId, config.mqtt_server, keepalive=60, ssl=True)
|
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||||
|
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||||
client.set_callback(mqtt_callback)
|
client.set_callback(mqtt_callback)
|
||||||
client.connect()
|
client.connect()
|
||||||
client.subscribe(config.watchdog_topic)
|
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||||
print(f"Connected to {config.mqtt_server} MQTT Broker")
|
client.subscribe(config.WATCHDOG_TOPIC)
|
||||||
|
print(f"Subscribed to {config.WATCHDOG_TOPIC}")
|
||||||
|
|
||||||
global wdt
|
global wdt
|
||||||
wdt = WDT(timeout=5000)
|
wdt = WDT(timeout=5000)
|
||||||
@ -44,7 +49,7 @@ def init():
|
|||||||
def mqtt_callback(topic, msg):
|
def mqtt_callback(topic, msg):
|
||||||
global wdt, wdtCnt
|
global wdt, wdtCnt
|
||||||
print(f"Received: {topic}: {msg}")
|
print(f"Received: {topic}: {msg}")
|
||||||
if topic == config.watchdog_topic:
|
if topic == config.WATCHDOG_TOPIC:
|
||||||
wdtCnt += 1
|
wdtCnt += 1
|
||||||
print(f"feed watchdog {wdtCnt}")
|
print(f"feed watchdog {wdtCnt}")
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
@ -57,7 +62,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
init()
|
init()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Some error when starting: {e}")
|
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
||||||
time.sleep(15)
|
time.sleep(15)
|
||||||
machine.reset()
|
machine.reset()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user