Adafruit IO not showing image from Arduino Portenta

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
lesurfe
 
Posts: 3
Joined: Sun May 30, 2021 7:24 pm

Adafruit IO not showing image from Arduino Portenta

Post by lesurfe »

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.
Dashboard showing null image
Dashboard showing null image
dashboard.jpg (103.05 KiB) Viewed 135 times
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
Dashboard showing image properly
Dashboard showing image properly
dashboard2.jpg (556.03 KiB) Viewed 135 times
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
String uploaded to Adafruit IO
String uploaded to Adafruit IO
feed.jpg (140.85 KiB) Viewed 135 times
Any clues?

Please see code below:

Code: Select all

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

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

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

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

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


User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit IO not showing image from Arduino Portenta

Post by brubell »

but somehow fails to show converted images sent directly from the OpenMV IDE.
Do you have an example image string sent from the OpenMV IDE? And to compare - do you have an image string from the openmv serial terminal?

Could you paste both of those strings here?

User avatar
lesurfe
 
Posts: 3
Joined: Sun May 30, 2021 7:24 pm

Re: Adafruit IO not showing image from Arduino Portenta

Post by lesurfe »

Thank you for your reply. I copied both in the following code and I received True as an output of the comparison
image
image
image.jpg (635 Bytes) Viewed 127 times

Code: Select all

openmv = "/9j/4AAQSkZJRgABAQEAAAAAAAD//gAMAAAAAAAAAAAAAP/bAEMADQkKCwoIDQsKCw4ODQ8TIBUTEhITJxweFyAuKTEwLiktLDM6Sj4zNkY3LC1AV0FGTE5SU1IyPlphWlBgSlFST//AAAsIAB4AKAEBEQD/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwU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tl5ufo6ery8/T19vf4+fr/2gAIAQEAAD8A4QCnqKeBUijPWngVWVacBUgWnqKeBVVRUirT1WpAKcBWcLu3z/rf/HTTxe2w/wCWn/jppwv7Uf8ALX/x0/4U8ahad5f/AB0/4Uv9o2f/AD2/8dP+Ff/Z"

print(openmv == adafruitio)

User avatar
lesurfe
 
Posts: 3
Joined: Sun May 30, 2021 7:24 pm

Re: Adafruit IO not showing image from Arduino Portenta

Post by lesurfe »

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:

Code: Select all

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


User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit IO not showing image from Arduino Portenta

Post by brubell »

I figured out that the decode function creates a string that starts with a " ".
Yup, this is why I wanted to compare the strings. Thanks for posting the working code as well as the cause - it'll be useful for others.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”