Skip to content

no need to pass self to _handle_on_message() #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2023

Conversation

vladak
Copy link
Contributor

@vladak vladak commented Oct 25, 2023

Found when evaluating #178.

@brentru brentru self-requested a review October 26, 2023 13:54
Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks OK and removing client makes sense to me, have you tested this on hardware yet?

@vladak
Copy link
Contributor Author

vladak commented Oct 26, 2023

Tested on Adafruit CircuitPython 8.2.6 on 2023-09-12; Adafruit QT Py ESP32-S3 no psram with ESP32S3 with the following code:

import adafruit_logging as logging
import socketpool
import ssl
import sys
import time
import wifi

from secrets import secrets

import adafruit_minimqtt.adafruit_minimqtt as MQTT


# pylint: disable=unused-argument, redefined-outer-name
def connect(mqtt_client, userdata, flags, rc):
    logger = logging.getLogger(__name__)
    logger.debug("Connected to MQTT Broker!")
    logger.debug("Flags: {0}\n RC: {1}".format(flags, rc))


def subscribe(mqtt_client, userdata, topic, granted_qos):
    logger = logging.getLogger(__name__)
    logger.debug("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def message(client, topic, message):
    logger = logging.getLogger(__name__)
    logger.debug("New message on topic {0}: {1}".format(topic, message))

    logger.debug("Checking client")
    assert isinstance(client, MQTT.MQTT)
    logger.debug("client check done")


# pylint: disable=too-many-statements,too-many-locals
def main():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    # Connect to Wi-Fi
    logger.info("Connecting to wifi")
    wifi.radio.connect(secrets["SSID"], secrets["password"], timeout=10)
    logger.info(f"Connected to {secrets['SSID']}")
    logger.debug(f"IP: {wifi.radio.ipv4_address}")

    pool = socketpool.SocketPool(wifi.radio)

    # connect to MQTT broker
    mqtt = MQTT.MQTT(
        broker="172.40.0.3",
        port=1883,
        socket_pool=pool,
        ssl_context=ssl.create_default_context(),
    )

    mqtt.on_connect = connect
    mqtt.on_subscribe = subscribe
    mqtt.on_message = message

    logger.info("Connecting to MQTT broker")
    mqtt.connect()
    mqtt.subscribe("devices/#", qos=0)

    i = 0
    while True:
        # Make sure to stay connected to the broker e.g. in case of keep alive.
        i += 1
        logger.debug(f"Loop {i}")
        mqtt.loop(1)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(0)

@brentru brentru merged commit 926846c into adafruit:main Oct 27, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 31, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 2.2.24 from 2.2.23:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#93 from ilario/Pin

Updating https://github.com/adafruit/Adafruit_CircuitPython_BNO055 to 5.4.12 from 5.4.11:
  > Merge pull request adafruit/Adafruit_CircuitPython_BNO055#119 from CedarGroveStudios/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_PN532 to 2.4.0 from 2.3.20:
  > Merge pull request adafruit/Adafruit_CircuitPython_PN532#66 from symm/mifare-value-block

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.4.3 from 7.4.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#179 from vladak/handle_on_message_vs_self

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants