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:
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