MQTT example crashes when trying to get a feed

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
nocom
 
Posts: 9
Joined: Thu Jan 19, 2023 3:37 am

MQTT example crashes when trying to get a feed

Post by nocom »

I try to setup my Raspberry Pico W so that it will pass measurements to adafruit.io by trying to replicate the example found on https://learn.adafruit.com/pico-w-wifi- ... dafruit-io. I adapted the code somewhat and the main portion is:

Code: Select all

wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
#  prints IP address to REPL
print("IP address is", wifi.radio.ipv4_address)
aio_username = os.getenv('aio_username')
aio_key = os.getenv('aio_key')
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
print("connected to io:", io)
# get feed
picowHumid_feed = io.get_feed("distance")
That all works fine until the last line, i get:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
IP address is 192.168.178.58
connected to io: <IO_HTTP object at 0x20017250>
Traceback (most recent call last):
  File "<stdin>", line 58, in <module>
  File "adafruit_io/adafruit_io.py", line 707, in get_feed
  File "adafruit_io/adafruit_io.py", line 555, in _get
  File "adafruit_requests.py", line 730, in get
  File "adafruit_requests.py", line 673, in request
  File "adafruit_requests.py", line 600, in _send_request
AttributeError: 'NoneType' object has no attribute 'encode'
As can be seen from the output is that variable io has a value, so is not None. The error occurs in the requests library where the encoding value is None. Any idea what is the cause of this?

User avatar
barshatriplee
 
Posts: 200
Joined: Wed Mar 22, 2023 10:11 am

Re: MQTT example crashes when trying to get a feed

Post by barshatriplee »

The error message 'NoneType' object has no attribute 'encode' suggests that the requests library is trying to encode a None value. One possible cause of this error is that the io.get_feed() method is returning None instead of a valid response.Verify that the io.get_feed() method is returning a valid response by printing the response before trying to use it.

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”