0

FeatherWing Huzzah Restarts
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

FeatherWing Huzzah Restarts

by loraan on Tue Dec 13, 2016 5:47 pm

I've started experimenting with MicroPython on the Feather Huzzah. I've been working on a project to control a headboard mounted DotStar strip through MQTT and have banged together some Python scripts to do it. For the most part they are working fine, but when I first run the script to start the MQTT client, I get random restarts. Sometimes the client starts fine, sometimes it the board will restart. When the board does restart, it restarts at different places in my code starting from the point where I connect to the MQTT broker (io.adafruit.com in this case) to the point where I am subscribing to the feeds.

It appears that the restarts are being triggered by the ESP's watchdog timer, and seems to be happening somewhere inside the umqtt library. I am beginning to suspect the issue may be happening due to some sort of blocking in the internal socket code umqtt is using... but I don't know for sure. Has anyone else run into this? I've done some general searches and haven't seen anything about this on other forums generally.

This is on MicroPython version v1.8.6-7-gefd0927.

Here's the reboot message along with some of my debugging messages at the top (here the restart happened while I was trying to subscribe to my first feed and before I could subscribe to the second):

Code: Select all | TOGGLE FULL SIZE
>>> c.run()
Connecting
Subscribing power

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x40100000, len 32024, room 16
tail 8
chksum 0x37
load 0x3ffe8000, len 1092, room 0
tail 4
chksum 0xce
load 0x3ffe8450, len 3000, room 4
tail 4
chksum 0x16
csum 0x16


And here is my MQTT client code ("bedlightcontroller" is the Python class that manages the DotStar strip and knows nothing about the network or MQTT):

Code: Select all | TOGGLE FULL SIZE
from umqtt import robust
import bedlightcontroller
import machine
import time

class BedLightMQTTClient:
    """An MQTT client to listen to bedlight topics and trigger light effects."""

    def __init__(self, spi, config):
        self.controller = bedlightcontroller.BedLightController(config["led_count"], spi)
        self.config = config

        self.client = robust.MQTTClient(config["client_id"],
                                        config["mqtt_server"],
                                        config["mqtt_port"],
                                        config["mqtt_username"],
                                        config["mqtt_password"],
                                        ssl=config["mqtt_ssl"])
        self.client.set_callback(self.on_message)
       
        self.power_feed = config["power-feed"]
        self.mode_feed = config["mode-feed"]
        self.color_feed = config["color-feed"]
       
    def on_message(self, topic, message):
        """Respond to a message on any of the subscribed feeds.

Dispatch the message to the appropriate handler, based on the topic name."""
        if topic == self.power_feed:
            self.on_power(message)
        elif topic == self.mode_feed:
            self.on_mode(message)
        elif topic == self.color_feed:
            self.on_color(message)

    def on_power(self, message):
        """Respond to a change in the power feed."""
        self.controller.set_is_on(message == b"ON")

    def on_mode(self, message):
        """Respond to a change in the mode feed."""
        self.controller.set_mode(int(message))

        (red, green, blue) = self.controller.get_mode_color()
        color_code = "#{0:02x}{1:02x}{2:02x}".format(red, green, blue)
        self.client.publish(self.color_feed, color_code)
                           

    def on_color(self, message):
        print("on_color: {0}".format(message))
        """Respond to a change in the color feed."""

        if message[0:1] == b"#":
            raw = int(message[1:], 16)
            red = (raw >> 16) & 0xff
            green = (raw >> 8) & 0xff
            blue = raw & 0xff
            self.controller.set_mode_color((red, green, blue))

    def run(self):
        """Connect to the MQTT broker, wait for and process messages."""
        print("Connecting")
        self.client.connect()
        print("Subscribing power")
        self.client.subscribe(self.power_feed)
        print("Subscribing mode")
        self.client.subscribe(self.mode_feed)
        print("Subscribing color")
        self.client.subscribe(self.color_feed)

        while True:
            self.client.wait_msg()

        self.client.disconnect()

def make_client():
    spi = machine.SPI(1, baudrate=8000000, polarity=0, phase=0)
    config = {
        "led_count": 30,
        "client_id": "bedlight",
        "mqtt_server": "io.adafruit.com",
        "mqtt_port": 8883,
        "mqtt_username": "loraan",
        "mqtt_password": .....,
        "mqtt_ssl": True,
        "power-feed": b"loraan/feeds/bedlight-power",
        "mode-feed": b"loraan/feeds/bedlight-mode",
        "color-feed": b"loraan/feeds/bedlight-color"
    }

    return BedLightMQTTClient(spi, config)

loraan
 
Posts: 7
Joined: Sun Mar 24, 2013 1:31 pm

Re: FeatherWing Huzzah Restarts

by adafruit2 on Mon Dec 26, 2016 3:12 pm

oof we haven't done anything with micropython+mqtt in depth but 99% of wdt resets are due to a busy wait loop that doesnt call yield()
only thing is, we dont know where that busywait loop is :/ you'll have to find it and put in a yield()

adafruit2
Site Admin
 
Posts: 20107
Joined: Fri Mar 11, 2005 7:36 pm

Please be positive and constructive with your questions and comments.