Adafruit IO not showing image from Arduino Portenta

I’ve been working on a project that sends different values to Adafruit IO from an Arduino Portenta using WiFi connection.

Code runs perfectly for number values and strings but somehow fails to show converted images sent directly from the OpenMV IDE.

Curious discoveries:

  1. If I print the image string in the OpenMV Serial monitor and then use that same string to publish the image into the Dashboard it works fine. Therefore image string shouldn’t be the problem

  2. If I take the string published into the Image Feed and then use that same string to publish the image into the Dashboard it also works fine. Therefore Adafruit IO receives the string properly and shouldn’t be the problem

Any clues?

Please see code below:

import time, network
from mqtt import MQTTClient
import sensor, image, ubinascii

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Portenta only supportsGRAYSCALE
sensor.set_framesize(sensor.QQVGA)   # Set frame size to QQVGA
sensor.skip_frames(time = 2000)     # Wait for settings take effect.

SSID='' # WiFi Network ID
KEY=''  # WiFi Network password

print("Trying to connect. Note this may take a while...") # Init wlan module and connect to network

wlan = network.WLAN(network.STA_IF)
wlan.deinit()
wlan.active(True)
wlan.connect(SSID, KEY, timeout=30000)

print("WiFi Connected ", wlan.ifconfig()) # We should have a valid IP now via DHCP

client = MQTTClient("openmv", "io.adafruit.com", user="Adafruit IO user", password="Adafruit IO key", port=1883)
client.connect()

i = 20
bt = "Time passed "

while (True):
    i+=1
    text = bt + str(i) + "sec"

    if (i % 2) == 0:
        s_t="0"
    else:
        s_t="1"

    i_t = str(i)

    img = sensor.snapshot()
    img.compress(quality = 90)
    img_bytes = ubinascii.b2a_base64(img)
    img_t = img_bytes.decode("utf-8") # img_t is a string
   
    client.publish("Adafuit IO user/feeds/temperature", i_t) # replace Adafruit IO user with your Adafruit IO user
    client.publish("Adafuit IO user/feeds/text", text)
    client.publish("Adafuit IO user/feeds/ledstatus", s_t)
    client.publish("Adafuit IO user/feeds/image", img_t)
    print(img_t)
    time.sleep(10) # lower values will exceed Adafruit IO requirements

There’s no way I can general purpose debug this…

Try:

img = img.compress(quality = 90)

Always assign the result.

Not sure if this is needed: img_bytes.decode(“utf-8”)

Hi! Thank you for your reply.

I figured out that the decode function creates a string that starts with a " ".
This single space goes unnoticed when printed and therefore is difficult to spot, but was the source of my problem.
To solve it, I added the strip function that removes the spaces from the string.

I’ll paste the working code below in case that any other person ends with the same problem in the future:

import time, network
from mqtt import MQTTClient
import sensor, image, ubinascii

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Portenta only supportsGRAYSCALE
sensor.set_framesize(sensor.QQVGA)   # Set frame size to QQVGA
sensor.skip_frames(time = 2000)     # Wait for settings take effect.

SSID='' # WiFi Network ID
KEY=''  # WiFi Network password

print("Trying to connect. Note this may take a while...") # Init wlan module and connect to network

wlan = network.WLAN(network.STA_IF)
wlan.deinit()
wlan.active(True)
wlan.connect(SSID, KEY, timeout=30000)

print("WiFi Connected ", wlan.ifconfig()) # We should have a valid IP now via DHCP

client = MQTTClient("openmv", "io.adafruit.com", user="Adafruit IO user", password="Adafruit IO key", port=1883)
client.connect()

i = 20
bt = "Time passed "

while (True):
    i+=1
    text = bt + str(i) + "sec"

    if (i % 2) == 0:
        s_t="0"
    else:
        s_t="1"

    i_t = str(i)

    img = sensor.snapshot()
    img.compress(quality = 90)
    img_bytes = ubinascii.b2a_base64(img)
    img_t = img_bytes.decode("utf-8") # img_t is a string
    img_t = img_t.strip()

    client.publish("Adafuit IO user/feeds/temperature", i_t) # replace Adafruit IO user with your Adafruit IO user
    client.publish("Adafuit IO user/feeds/text", text)
    client.publish("Adafuit IO user/feeds/ledstatus", s_t)
    client.publish("Adafuit IO user/feeds/image", img_t)
  
    time.sleep(10) # lower values will exceed Adafruit IO requirements