Skip to content

Web Workflow Impacts MQTT message flow #9123

Open
@niccit

Description

@niccit

CircuitPython version

Adafruit CircuitPython 9.0.2

Code/REPL

import wifi
import adafruit_connection_manager
import adafruit_minimqtt as my_mqtt

cert_file = mqtt_data["cert_file"]
with open(cert_file, 'r') as file:
    cert_data = file.read()

radio = wifi.radio
pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
ssl_context.load_verify_locations(cadata=cert_data)

mqtt_client = my_mqtt.MQTT(
            broker=mqtt_data["server"],
            port=mqtt_data["port"],
            username=mqtt_data["username"],
            password=mqtt_data["key"],
            socket_pool=pool,
            ssl_context=ssl_context,
            is_ssl=true,
        )

def message(client, topic, message):
    # listen for messages from sensors being monitored
    if "pir1" in topic:
        log_message = "Motion detected by PIR sensor 1"
        mqtt_client.publish(topic, log_message)
        trip_zone(relay_pin)

def connected(client, userdata, flags, rc):
    my_mqtt.subscribe(topics)  # subscribe to topics



# Behavior when disconnected from MQTT broker
# Log a warning and try to reconnect
def disconnected(client, userdata, rc):
    mqtt_client.connect()

def trip_zone(pin):
    if pin.value is False:
        pin.value = True
        time.sleep(4)
        pin.value = False

# Set up MQTT for publish/subscribe
mqtt_client.on_message = message
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected

# Subscribe to sensor feeds in MQTT
# All feeds should be in the data.py file
topics = home-monitoring.pir1-logging

while True:
   mqtt_client.loop(timeout=5)
   time.sleep(0.25)

Behavior

No error messages

Description

Once code is running:

  1. In one window subscribe to the MQTT server on the topic in the code
  2. In a second window send a message to the MQTT server on the topic in the code
  3. Confirm message sent, received, and relay tripped - will all happen within 2 seconds
  4. Now, open Web Workflow for the board (in this case a Feather ESP32 V2)
  5. Send another message the MQTT server on the topic in the code
    Nothing happens, no message received
  6. Refresh the Web Workflow page (reload button)
    Message will send, relay will trip - but relay will not reset
  7. Refresh the Web Workflow page (reload button)
    Relay resets

Additional information

Other odd behaviors happen including:

  1. DNS resolution for circuitpython.local will fail temporarily although you can access the WWF via IP
  2. MQTT sending will not work reliably once the WWF is closed and the board will need to be reset

This also happens when I access the WWF for the sensor board (QTY ESP32-S3 - running 9.0.0)
I saw the same behavior on the Feather board using 9.0.0

MQTT server is running locally (Eclipse Mosquitto on a Raspberry Pi 4)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions