MatrixPortal - SSL / WiFi

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
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

MatrixPortal - SSL / WiFi

Post by james_bond »

I've been struggling to get rid of the following error message:

Code: Select all

File "code.py", line 5, in <module>
ImportError: no module named 'socketpool
Does anyone know what library module these would belong to? I also have the same error message with importing wifi and ssl

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal - SSL / WiFi

Post by dastels »

The socketpool module is part of the CircuitPython runtime for some boards. Not, however, the MatrixPortal. It seems to be only on ESP32-S2 boards.

What are you trying to do? Can you post code.py?

Dave

User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »

I'm trying to incorporate MQTT and io.adafruit but all the examples I've seen thus far require those modules. Would it be easier if I used pi zero instead?

Code: Select all

import time
import board
import displayio
import terminalio
import socketpool
import wifi
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font
from adafruit_matrixportal.network import Network
from adafruit_matrixportal.matrix import Matrix

import adafruit_minimqtt.adafruit_minimqtt as MQTT

BLINK = True
DEBUG = False

# 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

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

# Setup a feed named 'onoff' for subscribing to changes
color_feed = secrets["aio_username"] + "/feeds/color-feed"

# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
    # Subscribe to all changes on the onoff_feed.
    client.subscribe(color-feed)


def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from Adafruit IO!")


def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    # has a new message.
    print("New message on topic {0}: {1}".format(topic, message))


# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
        broker='io.adafruit.com',
    port=1883,
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=pool,
    ssl_context=ssl.create_default_context(),
)

# 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("Open/Close SW Init")

# --- Display setup ---
matrix = Matrix()
display = matrix.display
network = Network(status_neopixel=board.NEOPIXEL, debug=False)

# --- Drawing setup ---
group = displayio.Group(max_size=4)  # Create a Group
bitmap = displayio.Bitmap(64, 32, 2)  # Create a bitmap object,width, height, bit depth
color = displayio.Palette(4)  # Create a color palette
color[0] = 0x000000  # black background
color[1] = 0xFF0000  # red
color[2] = 0xCC4000  # amber
color[3] = 0x85FF00  # greenish:w

print(network.get_local_time())

# Create a TileGrid using the Bitmap and Palette
tile_grid = displayio.TileGrid(bitmap, pixel_shader=color)
group.append(tile_grid)  # Add the TileGrid to the Group
display.show(group)

if not DEBUG:
    font = bitmap_font.load_font("/IBMPlexMono-Medium-24_jep.bdf")
else:
    font = terminalio.FONT

clock_label = Label(font, color=color[1], scale=1, text='CLOSED')
clock_label.y = display.height // 2
group.append(clock_label)

while True:
    time.sleep(1)

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal - SSL / WiFi

Post by dastels »

Where are you getting the examples from?

Dave

User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »


User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »

What I've come to notice is that there are some api calls that you expect to work but in reality they don't.

For example: https://learn.adafruit.com/network-conn ... trix-clock

Line 76 of the example code has bbx, bby, bbwidth, bbh = clock_label.bounding_box

I had to remove that line and do the calculations alternatively.

Ben

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: MatrixPortal - SSL / WiFi

Post by dastels »

It's usually not a matter of them "not working". It's usually because a module isn't supported on a particular board (see https://circuitpython.readthedocs.io/en ... atrix.html for a list of what internal modules are on each board), or the module API/code has changed over time and the project code is now out of date.

Odd. The bounding_box call should work.

Dave

User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »

So what about using a raspberry pi? Would that ensure I have the full feature set?

Ben.

User avatar
jliu70
 
Posts: 84
Joined: Wed Oct 21, 2020 9:27 pm

Re: MatrixPortal - SSL / WiFi

Post by jliu70 »

Looking at the tutorial, there are two example code blocks. You seem to have used the code from the first example.
The first example code explicitly states that it is for CircuitPython boards using the ESP32-S2 such as the MagTag or Metro ESP32-S2 which have internet connectivity built into the CircuitPython firmware.
ESP32-S2 Code

You should scroll down a little further and use the code (second example block)
ESP32 AirLift Code

In particular you'll see that the helper library is imported as follows:

Code: Select all

import adafruit_esp32spi.adafruit_esp32spi_socket as socket
Would recommend that you try the second code block.

NOTE: If you haven't done so already, you'll need to copy over the adafruit_esp32spi library to the MatrixPortal (CIRCUITPYTHON/lib/)

I hope that helps.

User avatar
jliu70
 
Posts: 84
Joined: Wed Oct 21, 2020 9:27 pm

Re: MatrixPortal - SSL / WiFi

Post by jliu70 »

Sorry, forgot to reference the specific webpage: https://learn.adafruit.com/mqtt-in-circ ... wifi-usage


And here's another project example (for the PyPortal - but it should be similar to MatrixPortal): https://learn.adafruit.com/pyportal-mqt ... ython-code

User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »

Ok thanks for the update, I appreciate you jumping in to help out. Here's the thing though, I was able to implement the solution in just a few lines of code:

Code: Select all

import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal

# --- Display setup ---
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=False)

feed = matrixportal.get_io_feed("color-feed", detailed=True)
value = feed["details"]["data"]["last"]["value"]
print(value)
Notice the API doesn't reference anything about sockets, chipsets, pin configurations etc. To me it looks like this matixportal object you create at the very beginning can also handle MQTT requests. It might do more but I wasn't able to find the documentation for that api. Seems like something you should have a link for right on the product page?

Ben.

User avatar
jliu70
 
Posts: 84
Joined: Wed Oct 21, 2020 9:27 pm

Re: MatrixPortal - SSL / WiFi

Post by jliu70 »

Yes, it does seem that the example code doesn't take into account the MatrixPortal library (which was probably released after the example code was first written).

https://learn.adafruit.com/adafruit-mat ... y-overview

https://learn.adafruit.com/assets/98460


Thanks,
Jeff

User avatar
james_bond
 
Posts: 8
Joined: Tue Mar 16, 2021 11:28 am

Re: MatrixPortal - SSL / WiFi

Post by james_bond »

Sorry for all the back and forth but do you have anything like a javadoc or api documentation for the MatrixPortal object? I do see the following: https://circuitpython.readthedocs.io/pr ... en/latest/

But searching the doc for "get_io_feed" yields no results.

Ben,

User avatar
jliu70
 
Posts: 84
Joined: Wed Oct 21, 2020 9:27 pm

Re: MatrixPortal - SSL / WiFi

Post by jliu70 »

I don't know if this is what you're looking for?

https://circuitpython.readthedocs.io/pr ... trixportal

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”