Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix publishing large payloads (>127 bytes) #42

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 16 commits into from
Jul 3, 2020
Merged

Fix publishing large payloads (>127 bytes) #42

merged 16 commits into from
Jul 3, 2020

Conversation

brentru
Copy link
Member

@brentru brentru commented Jun 25, 2020

Related issue: #31

Previously, the library would throw an IndexError if the combined length of a topic and payload was > 127 bytes.

This pull request:

Tests

Note: tests performed on Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Feather M4 Express with samd51j19 with an Ethernet FeatherWing.

PUBLISH'ing a larger payload (image, 557 bytes) to Adafruit IO

with open("squares.jpg", "rb") as imgfile:
    img64 = b2a_base64(imgfile.read())
    print(len(img64.decode("utf-8")))
    print(type(img64.decode("utf-8")))
    print(img64.decode("utf-8"))

    mqtt_client.publish(test_feed, img64.decode("utf-8"), retain=False, qos=0)

print("Published")

image

Publishing a small packet with a QoS of 0 and retain flag un-set.
mqtt_client.publish(test_feed, "adafruit")
image

Publishing with a QoS of 0 and retain flag set.
mqtt_client.publish(test_feed, img64.decode("utf-8"), retain=True, qos=0)

Capturing_from_USB_10_100_1000_LAN__en7_and_Mu_1_0_2_-_code_py

Repeated Publishing with a QoS of 1 and a large payload

Pasted_Image_6_25_20__1_14_PM

@brentru brentru added the bug Something isn't working label Jun 25, 2020
@brentru brentru requested a review from a team June 25, 2020 17:11
@brentru
Copy link
Member Author

brentru commented Jun 25, 2020

@caternuson @seantibor this is ready for review (or testing if you are set up to test)

@caternuson
Copy link

I'm not familiar enough with the code level details to do a code review. But ran a simple test, which seems to work.

Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit PyPortal with samd51j20
>>> import pp_mqtt_send
ESP firmware: bytearray(b'1.2.2\x00')
Set background to  0
No SD card found: no SD card
Connecting to WiFi...
Connected!
Connecting to Adafruit IO...
Subscribing to feed.
Connected.
Sending image...
Published
>>> 

Here's the code:

from adafruit_binascii import b2a_base64
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
#https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import adafruit_pyportal

FILENAME = 'squares.jpg'
FEED = 'image_test'

from secrets import secrets

pyportal = adafruit_pyportal.PyPortal()

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(pyportal._esp, secrets, None)

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

# Callbacks
def connected(client, userdata, flags, rc):
    print("Subscribing to feed.")
    client.subscribe(mqtt_topic)

def disconnected(client, userdata, rc):
    print("Disconnected from MQTT Broker!")

def message(client, topic, message):
    print("New message on topic {0}: {1}".format(topic, message))

mqtt_topic = "{}/feeds/{}".format(secrets['aio_username'], FEED)

MQTT.set_socket(socket, pyportal._esp)

mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    username=secrets["aio_username"],
    password=secrets["aio_key"],
)

# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message

# Connect the client to the MQTT broker.
print("Connecting to Adafruit IO...")
mqtt_client.connect()
print("Connected.")

print("Sending image...")
# pre-send loop pump
mqtt_client.loop()
with open(FILENAME, "rb") as imgfile:
    img64 = b2a_base64(imgfile.read())
    mqtt_client.publish(mqtt_topic, img64.decode("utf-8"), retain=False, qos=0)
#post-send loop pump
mqtt_client.loop()
print("Published")

Here's the test image:
squares

And the end results in the AIO feed:
aio_pp_test

@brentru
Copy link
Member Author

brentru commented Jul 3, 2020

@caternuson Thanks for testing.

@brentru brentru merged commit eb07565 into adafruit:master Jul 3, 2020
@brentru brentru deleted the variable-headers-large-payload branch July 3, 2020 14:13
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jul 4, 2020
rtwfroody pushed a commit to rtwfroody/Adafruit_CircuitPython_MiniMQTT that referenced this pull request Sep 18, 2022
…yload

Fix publishing large payloads (>127 bytes)
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