Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sgbaird committed Oct 18, 2024
2 parents 1466e8e + 78aa3a9 commit 3d45417
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Binary file modified src/public_mqtt_sdl_demo/hivemq-com-chain.der
Binary file not shown.
24 changes: 18 additions & 6 deletions src/public_mqtt_sdl_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import ntptime
from as7341_sensor import Sensor
from data_logging import initialize_sdcard, log_to_mongodb
from machine import PWM, Pin, reset, unique_id
from data_logging import log_to_mongodb
from machine import PWM, WDT, Pin, unique_id # Removed reset, added WDT
from netman import connectWiFi
from sdl_demo_utils import (
Experiment,
Expand Down Expand Up @@ -52,6 +52,10 @@
os.dupterm(logfile)

# https://medium.com/@johnlpage/introduction-to-microcontrollers-and-the-pi-pico-w-f7a2d9ad1394

# Setup watchdog timer with a timeout of 8 seconds (max for rp2040)
wdt = WDT(timeout=8000)

# you can request a MongoDB collection specific to you by emailing
# sterling.baird@utah.edu
mongodb_url = f"https://data.mongodb-api.com/app/{MONGODB_APP_NAME}/endpoint/data/v1/action/insertOne" # noqa: E501
Expand Down Expand Up @@ -81,9 +85,11 @@
try:
connectWiFi(SSID, PASSWORD, country="US")
except RuntimeError as e:
wdt.feed()
print("WiFi connection failed. Retrying in 5 seconds...")
sleep(5.0)
try:
wdt.feed()
connectWiFi(SSID, PASSWORD, country="US")
except RuntimeError as e:
raise RuntimeError(
Expand All @@ -96,11 +102,13 @@
ntptime.timeout = 30 # type: ignore
ntptime.host = "pool.ntp.org"
try:
wdt.feed()
ntptime.settime()
except Exception as e:
print(get_traceback(e))
print("Retrying ntptime.settime(), still with pool.ntp.org")
try:
wdt.feed()
ntptime.settime()
except Exception as e:
print(get_traceback(e))
Expand Down Expand Up @@ -210,12 +218,13 @@ def reset_experiment(parameters, devices=None):

onboard_led = get_onboard_led()
buzzer = PWM(Pin(18))
sdcard_ready = initialize_sdcard()

# MQTT Resources:
# https://gist.github.com/sammachin/b67cc4f395265bccd9b2da5972663e6d
# http://www.steves-internet-guide.com/into-mqtt-python-client/

# sdcard_ready = initialize_sdcard()
sdcard_ready = False # deprecating sdcard functionality to avoid confusion
experiment = Experiment(
run_experiment_fn=run_experiment,
reset_experiment_fn=reset_experiment,
Expand Down Expand Up @@ -243,7 +252,7 @@ def callback(topic, msg, retain=None, dup=None):
payload_data["encrypted_device_id_truncated"] = trunc_device_id
try:
parameters = json.loads(msg)
if parameters.get("mongodb", True):
if parameters.get("mongodb") is True:
log_to_mongodb(
payload_data,
url=mongodb_url,
Expand Down Expand Up @@ -271,7 +280,7 @@ def callback(topic, msg, retain=None, dup=None):
HIVEMQ_HOST,
user=HIVEMQ_USERNAME,
password=HIVEMQ_PASSWORD,
keepalive=30,
keepalive=3600,
ssl=True,
port=port,
ssl_params={
Expand All @@ -285,12 +294,14 @@ def callback(topic, msg, retain=None, dup=None):
)
del cacert
try:
wdt.feed()
client.connect()
except OSError as e:
print(get_traceback(e))
print("Retrying client.connect() in 2 seconds...")
sleep(2.0)
try:
wdt.feed()
client.connect()
except OSError as e:
print(get_traceback(e))
Expand All @@ -313,20 +324,21 @@ def callback(topic, msg, retain=None, dup=None):
client.check_msg()
heartbeat(client, False)
sign_of_life(onboard_led, False)
wdt.feed() # Feed the watchdog timer to prevent reset
except Exception as e:
print(get_traceback(e))
print("Reconnecting to WiFi...")
connectWiFi(SSID, PASSWORD, country="US")
client.connect(clean_session=False)
client.set_callback(callback)
client.subscribe(prefix + "GPIO/#")
wdt.feed() # Ensure watchdog is fed even after reconnecting
except Exception as e:
print(get_traceback(e))
fname = "error.txt"
logfile = open(fname, "w")
logfile.write(get_traceback(e))
logfile.close()
reset()

# %% Code Graveyard

Expand Down

0 comments on commit 3d45417

Please sign in to comment.