0

adafruit-io library not working in Python 3.9?
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.
Please be positive and constructive with your questions and comments.

adafruit-io library not working in Python 3.9?

by ryanlogsdon on Sat Feb 27, 2021 6:13 pm

Hi Everyone,

Could anyone help me find what I need to change to make my Python script work in Python 3.9? I've tested the exact same code in Python 3.6, 3.7, and 3.8, where it works just fine, but I get an SSL error in my newly upgraded 3.9 environment.

I'm just trying to connect to Adafruit IO, so my IoT device can subscribe to the MQTT service.

Code: Select all | TOGGLE FULL SIZE
import sys
import time
from Adafruit_IO import MQTTClient      # pip install adafruit-io

ADAFRUIT_IO_KEY = 'MY_ADAFRUIT_KEY'
ADAFRUIT_IO_USERNAME = 'MY_ADAFRUIT_NAME'
IO_FEED = 'MY_FEED'

def connected(client):
    print ('Connected to Adafruit IO! Listening for feed changes...')
    client.subscribe(IO_FEED)

def disconnected(client):
    print ('Disconnect from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # feed_id or {0} represents the name of the feed in the message to the Adafruit IO service.
    # Payload or {1} represents the value being sent.
    print ('Feed {0} recieved new vaule: {1}'.format(feed_id, payload))

    if '{1}'.format(feed_id, payload) == "ON":
        print("ON Worked ")
    if '{1}'.format(feed_id, payload) == "OFF":
        print("OFF worked ")

client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message
client.loop_background()
client.connect()

while True:
    # publish data to the feed here
    time.sleep(10)


Error in Python 3.9:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "/Users/ryan/Code/gdgIOT/subscribe.py", line 33, in <module>
    client.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/Adafruit_IO/mqtt_client.py", line 150, in connect
    self._client.connect(self._service_host, port=self._service_port,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py", line 1104, in reconnect
    sock.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123)


Direct link to my SO post: https://stackoverflow.com/questions/663 ... 5_66362762

Thanks very much,
Ryan

ryanlogsdon
 
Posts: 3
Joined: Thu Feb 04, 2021 1:34 pm

Re: adafruit-io library not working in Python 3.9?

by brubell on Mon Mar 01, 2021 11:08 am

Are you using macOS or Windows? How did you install Python?

In the terminal, could you run:

Code: Select all | TOGGLE FULL SIZE
pip3 freeze


and paste the output?

brubell
 
Posts: 1383
Joined: Fri Jul 17, 2015 10:33 pm

Re: adafruit-io library not working in Python 3.9?

by ryanlogsdon on Tue Mar 02, 2021 12:40 am

Hi Brubell,

Code: Select all | TOGGLE FULL SIZE
adafruit-io==2.5.0
beautifulsoup4==4.9.3
bs4==0.0.1
certifi==2020.12.5
chardet==4.0.0
idna==2.10
paho-mqtt==1.5.1
requests==2.25.1
soupsieve==2.2
urllib3==1.26.3


I'm using a Mac, and I upgraded by grabbing a download from Python.org.

Thanks,
Ryan

ryanlogsdon
 
Posts: 3
Joined: Thu Feb 04, 2021 1:34 pm

Re: adafruit-io library not working in Python 3.9?

by brubell on Fri Mar 05, 2021 11:07 am

I'm not sure what's causing the issue.. I just tested and I'm able to run Adafruit IO Python on macOS w/Python 3.9.1.

Could you try a different computer or Wireless connection (maybe try plugging into ethernet)?

brubell
 
Posts: 1383
Joined: Fri Jul 17, 2015 10:33 pm

Re: adafruit-io library not working in Python 3.9?

by ryanlogsdon on Fri Mar 05, 2021 12:52 pm

Sorry, no I can't :(

ryanlogsdon
 
Posts: 3
Joined: Thu Feb 04, 2021 1:34 pm

Please be positive and constructive with your questions and comments.