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',
}
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)
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!