Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Matrixportal MQTT
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Matrixportal MQTT

by kwalkerk on Wed Oct 14, 2020 1:46 pm

How do I get the MatrixPortal to run MQTT. I've tried everything and cannot get it working. Seems to hang up on setting a socket. I'm trying to connect to a broker on my local network.

Thanks,
Ken

kwalkerk
 
Posts: 66
Joined: Wed Jan 11, 2017 11:54 am

Re: Matrixportal MQTT

by dastels on Wed Oct 14, 2020 1:56 pm


dastels
 
Posts: 3767
Joined: Tue Oct 20, 2015 3:22 pm

Re: Matrixportal MQTT

by kwalkerk on Wed Oct 14, 2020 2:13 pm

I have. Can't get past line 226:
# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

kwalkerk
 
Posts: 66
Joined: Wed Jan 11, 2017 11:54 am

Re: Matrixportal MQTT

by dastels on Wed Oct 14, 2020 3:36 pm

Odd there doesn't look to be anything that could get locked up. Can you post yourt code or a link if you're using an example verbatim?

Dave

dastels
 
Posts: 3767
Joined: Tue Oct 20, 2015 3:22 pm

Re: Matrixportal MQTT

by kwalkerk on Wed Oct 14, 2020 3:51 pm

The sample code link you referred to is for the PyPortal, not the MatrixPortal. Far too much modification for me. All of the stuff I do for the MatrixPortal works fine until I add MQTT. Just can't get past the need to initialize MQTT.set_socket(socket, esp).

kwalkerk
 
Posts: 66
Joined: Wed Jan 11, 2017 11:54 am

Re: Matrixportal MQTT

by dastels on Wed Oct 14, 2020 4:35 pm

Hmm.. that shouldn't be different. Let me fiddle with it a bit.

dastels
 
Posts: 3767
Joined: Tue Oct 20, 2015 3:22 pm

Re: Matrixportal MQTT

by kwalkerk on Wed Oct 14, 2020 5:35 pm

Thanks

kwalkerk
 
Posts: 66
Joined: Wed Jan 11, 2017 11:54 am

Re: Matrixportal MQTT

by dastels on Thu Oct 15, 2020 10:12 am

It would help if you could post your code.

dastels
 
Posts: 3767
Joined: Tue Oct 20, 2015 3:22 pm

Re: Matrixportal MQTT

by kwalkerk on Thu Oct 15, 2020 12:37 pm

Below is the code. I get an error when I try to initialize the MatrixPortal on line 93.
Code: Select all | TOGGLE FULL SIZE
# Quote board matrix display


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


import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

import adafruit_minimqtt.adafruit_minimqtt as MQTT

broker_ip = "10.0.1.32"
ourTopic = 'frontporchmusic'
mqtt_message = ""

# 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






# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)

# 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" % ourTopic)
    # Subscribe to all changes on the onoff_feed.
    client.subscribe(ourTopic)
 
 
def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from Adafruit IO!")
 
 
def message(client, topic, message):
    global mqtt_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))
    mqtt_message = message
 

# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")
 
# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
 
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=broker_ip,
    port = 1883
)
 
# 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()
 

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


# Create a new label with the color and text selected
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, (matrixportal.graphics.display.height // 2) - 1),
    scrolling=True,
)

# Static 'Connecting' Text
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(2, (matrixportal.graphics.display.height // 2) - 1),
)


SCROLL_DELAY = 0.04
UPDATE_DELAY = 600




def update_data():
    print("Updating data from Adafruit IO")
    matrixportal.set_text("Connecting", 1)

   


update_data()
last_update = time.monotonic()
matrixportal.set_text(" ", 1)


while True:
   
   

    # Set the quote text
    matrixportal.set_text(mqtt_message)

    # Set the text color
    matrixportal.set_text_color(0xFFFFFF)

    # Scroll it
    matrixportal.scroll_text(SCROLL_DELAY)

    if time.monotonic() > last_update + UPDATE_DELAY:
        update_data()
        last_update = time.monotonic()



kwalkerk
 
Posts: 66
Joined: Wed Jan 11, 2017 11:54 am

Please be positive and constructive with your questions and comments.