pyportal and mqtt

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.
User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

pyportal and mqtt

Post by oldblackcrow »

Hello... I have a PyPortal Pynt and am trying to connect to AdafruitIO using MQTT. Though there are many examples on the Learning pages, none of them seem to work for various reasons. Based on the error code I get, I'm assuming the issue is with the Secrets file.

There are a menagerie of conflicting and/or incomplete information as to what information one is to include in the Secrets file. Here is what I have in mine (my personal information is not included):

Code: Select all

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'Computer',
    'password' : 'x'
    'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
    'aio_username' : 'oldblackcrow',
    'aio_key' : 'aio_x',
    'broker' : 'https://io.adafruit.com',
    }
I've seen many, many differences in what to put in the 'broker' and some of those are stating to use a numeric IP address. Ok... but if I do a lookup for a numeric IP address for the IO page I want to access (https://io.adafruit.com/oldblackcrow/da ... light-test), all it brings back is the main io.adafruit.com.

Several pages refer to a port address and I can find nothing that states where or how I enter that data... not only that but the example code doesn't even reference a port.

I am using the following example code:

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
 
import time
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_pyportal
 
import adafruit_minimqtt.adafruit_minimqtt as MQTT
 
pyportal = adafruit_pyportal.PyPortal()
 
### WiFi ###
 
# 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
 
# ------------- MQTT Topic Setup ------------- #
mqtt_topic = "test/topic"
 
### Code ###
# 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("Subscribing to %s" % (mqtt_topic))
    client.subscribe(mqtt_topic)
 
 
def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from MQTT Broker!")
 
 
def message(client, topic, message):
    """Method callled when a client's subscribed feed has a new
    value.
    :param str topic: The topic of the feed with a new value.
    :param str message: The new value
    """
    print("New message on topic {0}: {1}".format(topic, message))
 
 
# Connect to WiFi
print("Connecting to WiFi...")
pyportal.network.connect()
print("Connected!")
 
# Initialize MQTT interface with the esp interface
# pylint: disable=protected-access
MQTT.set_socket(socket, pyportal.network._wifi.esp)
 
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    is_ssl=False,
)
 
# 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.
mqtt_client.connect()
 
photocell_val = 0
while True:
    # Poll the message queue
    mqtt_client.loop()
 
    # Send a new message
    print("Sending photocell value: %d" % photocell_val)
    mqtt_client.publish(mqtt_topic, photocell_val)
    photocell_val += 1
    time.sleep(1)
I get the following error:
code.py output:
No SD card found: no SD card
Connecting to WiFi...
Connecting to AP Computer
Connected!
Traceback (most recent call last):
File "code.py", line 71, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
MMQTTException: ('Invalid broker address defined.', RuntimeError('Failed to request hostname',))
Code done running.

All I want to do is show gas sensor data that is sent to AdafruitIO from my ItsyBitsy via BlueFruit Connect App on to my PyPortal Pynt.

Someone please help. Thank you for your time!

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

Re: pyportal and mqtt

Post by brubell »

There are a menagerie of conflicting and/or incomplete information as to what information one is to include in the Secrets file.
do you have links to where you've seen conflicting info so we can change it to be more clear?

Modify your secrets.py file to look like the following:

Code: Select all

secrets = {
    'ssid' : 'Computer',
    'password' : 'x'
    'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
    'aio_username' : 'oldblackcrow',
    'aio_key' : 'aio_x',
    'broker' : 'https://io.adafruit.com',
    'port':8883
    }

In your code.py., change:

Code: Select all

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    is_ssl=False,
)
to:

Code: Select all

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    port=secrets["port"],
    username=secrets["aio_username"],
    password=secrets["aio_key"]
)

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Thank you for your reply!

The sources I've read are:
https://learn.adafruit.com/making-a-pyp ... t-your-own (this is the one I'm trying to use).
https://io.adafruit.com/api/docs/mqtt.h ... o-mqtt-api
https://learn.adafruit.com/adafruit-io/mqtt-api

None of these has a clear instruction for what I need in my secrets file, let alone the format in which it needs to be. Very confusing.

But using your suggestion, I still get:

code.py output:
No SD card found: no SD card
Connecting to WiFi...
Connecting to AP Ross Family
Connected!
Traceback (most recent call last):
File "code.py", line 71, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 304, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 304, in connect
MMQTTException: ('Invalid broker address defined.', RuntimeError('Expected 01 but got 00',))

Code done running.

I've tried changing the line:

Code: Select all

mqtt_topic = "test/topic"
to mqtt_topic = "light-test/topic"
and mqtt_topic = "light-test/photocell"

with no success and same error.

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

So, doing a little research, I found this thread:

https://github.com/adafruit/nina-fw/pull/21

Unfortunately, That information is above my pay grade. Are they talking about the NINA_W102-1.7.1 update? I just updated to that a few weeks ago that solved a problem with the http json error (viewtopic.php?f=60&t=174597).

Anyway, is this the right track or could it be another issue? Thanks!

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

Re: pyportal and mqtt

Post by brubell »

If you're connecting to Adafruit IO, you'll need your MQTT topic formatted to include the correct feed formatting:

For example, a topic which looks like:

Code: Select all

mqtt_topic = secrets["aio_username"] + "/feeds/test"
Would be: brubell/feeds/test, where brubell is my IO username and "test" is the Adafruit IO Feed Key I'm trying to access.

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Thank you for checking up on this, however, that still didn't work... I changed the line to:

Code: Select all

mqtt_topic = secrets["aio_username"] + "/feeds/photocell"
and

Code: Select all

mqtt_topic = secrets["aio_username"] + "/dashboards/light-test"
Sill I get the same error:
code.py output:
No SD card found: no SD card
Connecting to WiFi...
Connecting to AP Ross Family
Connected!
Traceback (most recent call last):
File "code.py", line 71, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
MMQTTException: ('Invalid broker address defined.', RuntimeError('Failed to request hostname',))

Code done running.

Just to make sure I'm entering the correct info, here are a couple screen shots of my IO dashboard.
AdafruitIO-001.jpg
AdafruitIO-001.jpg (122.21 KiB) Viewed 497 times
AdafruitIO-002.jpg
AdafruitIO-002.jpg (114.71 KiB) Viewed 497 times

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

Re: pyportal and mqtt

Post by brubell »

mqtt_topic = secrets["aio_username"] + "/dashboards/light-test"
Remove this line, it is not a topic. Topics are prefixed with `feeds/` or `groups`/, dashboards can't be accessed from the MQTT API.

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Yep, same thing:

Code: Select all

mqtt_topic = secrets["aio_username"] + "/feeds/photocell"
MQTT-test.jpg
MQTT-test.jpg (134.42 KiB) Viewed 482 times
code.py output:
No SD card found: no SD card
Connecting to WiFi...
Connecting to AP Ross Family
Connected!
Traceback (most recent call last):
File "code.py", line 71, in <module>
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
MMQTTException: ('Invalid broker address defined.', RuntimeError('Failed to request hostname',))

Code done running.

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

Re: pyportal and mqtt

Post by brubell »

Could you post your updated code and secrets.py (remove credentials please) and I'll try it out on my PyPortal.

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Here ya go...

SECRETS - shhhhhh:

Code: Select all

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'Ross Family',
    'password' : 'NOTAREALPASSWORD',
    'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
    'github_token' : 'fawfj23rakjnfawiefa',
    'hackaday_token' : 'h4xx0rs3kret',
    'aio_username' : 'oldblackcrow',
    'aio_key' : 'aio_notarealkey',
    'broker' : 'https://io.adafruit.com',
    'port':8883
    }

MAIN CODE:

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
 
import time
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_pyportal
 
import adafruit_minimqtt.adafruit_minimqtt as MQTT
 
pyportal = adafruit_pyportal.PyPortal()
 
### WiFi ###
 
# 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
 
# ------------- MQTT Topic Setup ------------- #
mqtt_topic = secrets["aio_username"] + "/feeds/photocell"
 
### Code ###
# 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("Subscribing to %s" % (mqtt_topic))
    client.subscribe(mqtt_topic)
 
 
def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from MQTT Broker!")
 
 
def message(client, topic, message):
    """Method callled when a client's subscribed feed has a new
    value.
    :param str topic: The topic of the feed with a new value.
    :param str message: The new value
    """
    print("New message on topic {0}: {1}".format(topic, message))
 
 
# Connect to WiFi
print("Connecting to WiFi...")
pyportal.network.connect()
print("Connected!")
 
# Initialize MQTT interface with the esp interface
# pylint: disable=protected-access
MQTT.set_socket(socket, pyportal.network._wifi.esp)
 
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    is_ssl=False,
)
 
# 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.
mqtt_client.connect()
 
photocell_val = 0
while True:
    # Poll the message queue
    mqtt_client.loop()
 
    # Send a new message
    print("Sending photocell value: %d" % photocell_val)
    mqtt_client.publish(mqtt_topic, photocell_val)
    photocell_val += 1
    time.sleep(1)

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

Re: pyportal and mqtt

Post by brubell »

@oldblackcrow: I've been following your other post about nina-firmware. Do you still hit the same issue when updated to the latest build?

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Both of my PyPortals are updated to the latest firmware. It took quite a bit longer to get my original one updated, but it worked this past week (that was the last post I made on the Pyportal Pynt IO and SSL thread viewtopic.php?f=60&t=174597).

I've tested all the other WiFi examples and they work with no issue... It's just the MQTT that still does not work on either of them.

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

So, do you think it's the NINA_W102 file that needs to be updated (as stated in my earlier post)?

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

@brubell

Well, I successfully upgraded to NINA 1.7.3, the newest available and I still get the same error as above on Line 71. :shrug:

User avatar
oldblackcrow
 
Posts: 221
Joined: Tue Jun 20, 2017 5:54 pm

Re: pyportal and mqtt

Post by oldblackcrow »

Maybe you can help me with this... On the examples, I choose the MQTT Simplest and in the library list, it refers to:
import ssl
import socketpool

However, I don't find either of those library files. Searching through the interwebs, I find references to them, but I can't find the actual library files. Do you have a direct link or can you post those files here? Thanks so much!

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”