Skip to content

Fix LWT Issues #36

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
Jun 4, 2020
Merged

Fix LWT Issues #36

merged 1 commit into from
Jun 4, 2020

Conversation

brentru
Copy link
Member

@brentru brentru commented Jun 4, 2020

This pull request addresses the issues in #35

  • last_will method renamed to will_set, kwargs renamed to mirror Paho-MQTT
  • Call to self.last_will() removed in __init__. Instead, set LWT properties.
  • Payload data converted to bytes for usage in connect

Tested on:
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather Bluefruit Sense with nRF52840

Code Tested:

import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

import adafruit_minimqtt as MQTT

### WiFi ###

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# If you have an externally connected ESP32:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

### Topic Setup ###

# MQTT Topic
# Use this topic if you'd like to connect to a standard MQTT broker
mqtt_topic = "test/topic"

### Code ###

# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connect(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print("Connected to MQTT Broker!")
    client.publish("devices/deviceid", "data")

def disconnect(client, userdata, rc):
    # This method is called when the client disconnects
    # from the broker.
    print("Disconnected from MQTT Broker!")
    client.publish("{}/status".format(client_id), 0) #disconnect from broker


def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def unsubscribe(client, userdata, topic, pid):
    # This method is called when the client unsubscribes from a feed.
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))


def publish(client, userdata, topic, pid):
    # This method is called when the client publishes data to a feed.
    print("Published to {0} with PID {1}".format(topic, pid))


# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Set up a MiniMQTT Client
client_id = "cpyDevice"
client = MQTT.MQTT(broker="SNIP!", port=1884, client_id=client_id)

# Connect callback handlers to client
client.on_connect = connect
client.on_disconnect = disconnect
client.on_subscribe = subscribe
client.on_unsubscribe = unsubscribe
client.on_publish = publish

client.will_set("devices/{}/status".format(client_id), payload=42.5, retain=True)
print("Attempting to connect to %s" % client.broker)
client.connect()

while True:
    client.loop_forever()

REPL Output:

Connecting to WiFi...
...
Connected!
Attempting to connect to 
Connected to MQTT Broker!
Published to devices/deviceid with PID 0

Mosquitto Broker Output:

1591295612: mosquitto version 1.6.10 starting
1591295612: Config loaded from /usr/local/etc/mosquitto/mosquitto.conf.
1591295612: Opening ipv6 listen socket on port 1884.
1591295612: Opening ipv4 listen socket on port 1884.
1591295631: New connection from 192.168.134.211 on port 1884.
1591295631: New client connected from SNIP as cpyDevice (p2, c1, k60).
1591295631: Will message specified (4 bytes) (r1, q0).

@brentru brentru added the bug Something isn't working label Jun 4, 2020
@brentru brentru requested a review from a team June 4, 2020 18:38
@brentru brentru merged commit c546f2e into adafruit:master Jun 4, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 5, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS to 3.0.2 from 3.0.1:
  > removing `boot` that causes issues with the LSM6DS33 on some CLUEs

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1305 to 1.3.0 from 1.2.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1305#9 from makermelissa/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 3.0.0 from 2.3.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#36 from brentru/fix-lwt-issues
rtwfroody pushed a commit to rtwfroody/Adafruit_CircuitPython_MiniMQTT that referenced this pull request Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants