Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Pyportal Pynt IO and SSL
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pyportal Pynt IO and SSL

by oldblackcrow on Mon Jan 25, 2021 5:15 pm

Good afternoon!

I am using the PyPortal Pynt and am trying to connect to Adafruit IO. I have the correct information in the 'secrets.py' file, but am getting the following error:

File "code.py", line 5, in <module>
ImportError: no module named 'ssl'

The code is as follows:

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# adafruit_circuitpython_adafruitio usage with native wifi networking
import ssl
from random import randint
import adafruit_requests
import socketpool
import wifi
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])


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)

try:
    # Get the 'temperature' feed from Adafruit IO
    temperature_feed = io.get_feed("temperature")
except AdafruitIO_RequestError:
    # If no 'temperature' feed exists, create one
    temperature_feed = io.create_new_feed("temperature")

# Send random integer values to the feed
random_value = randint(0, 50)
print("Sending {0} to temperature feed...".format(random_value))
io.send_data(temperature_feed["key"], random_value)
print("Data sent!")

# Retrieve data value from the feed
print("Retrieving data from temperature feed...")
received_data = io.receive_data(temperature_feed["key"])
print("Data from temperature feed: ", received_data["value"])


I can't find any library files with "ssl". Am I using the wrong code and/or library?

Thanks!

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

Re: Pyportal Pynt IO and SSL

by brubell on Thu Jan 28, 2021 6:26 pm

Hi, this is code for the ESP32-S2.

Try this code:
https://github.com/adafruit/Adafruit_Ci ... io_post.py

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Thu Jan 28, 2021 6:34 pm

Thank you! I'll give it a try.

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Sat Jan 30, 2021 11:29 am

Ok... it's working for the most part.

I'm getting this error: Posting data...Failed to get data, retrying
syntax error in JSON

The AdafruitIO page I'm trying to display is https://io.adafruit.com/oldblackcrow/dashboards/sgp30
It's connected in that the light is flashing green on the PyPortal but it's just giving me the error above. I'm thinking I just have the incorrect URL information. Below is the actual code I'm using. Any help on this is greatly appreciated!

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

print("ESP32 SPI webclient test")

# 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)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
)  # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

counter = 0

while True:
    try:
        print("Posting data...", end="")
        data = counter
        feed = "rx"
        payload = {"value": data}
        response = wifi.post(
            "https://io.adafruit.com/oldblackcrow/dashboards/sgp30"
            + secrets["aio_username"]
            + "/feeds/"
            + feed
            + "/rx",
            json=payload,
            headers={"X-AIO-KEY": secrets["aio_key"]},
        )
        print(response.json())
        response.close()
        counter = counter + 1
        print("OK")
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
        wifi.reset()
        continue
    response = None
    time.sleep(15)

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Sat Jan 30, 2021 11:31 am


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

Re: Pyportal Pynt IO and SSL

by brubell on Mon Feb 01, 2021 10:54 am

You may want to try updating the ESP32's firmware first - https://learn.adafruit.com/upgrading-es ... -one-board

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Mon Feb 01, 2021 6:33 pm

brubell wrote:You may want to try updating the ESP32's firmware first - https://learn.adafruit.com/upgrading-es ... -one-board


I followed the instructions and now I'm getting the following:

Failed to get data, retrying
syntax error in JSON
Posting data...Failed to connect, retrying
('Failed to connect to ssid', b'Computer')

I did not change anything in my secrets file and was connected to the internet before I updated the firmware. IDK LOL.

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

Re: Pyportal Pynt IO and SSL

by brubell on Mon Feb 01, 2021 6:40 pm

Which CircuitPython firmware are you running on the Pynt?

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Mon Feb 01, 2021 7:39 pm

Version 6.1.0

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

Re: Pyportal Pynt IO and SSL

by brubell on Wed Feb 03, 2021 11:51 am

Are you able to run this code: https://github.com/adafruit/Adafruit_Ci ... pletest.py

Could you post the output from the code (make sure to remove network ID/credentials from the log)?

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Wed Feb 03, 2021 4:01 pm

Well, it's connecting with this code:

code.py output:
ESP32 SPI webclient test
ESP32 found and in idle mode
Firmware vers. bytearray(b'1.2.2\x00')
MAC addr: ['0xxx', '0xf', '0xxx', '0xax', '0x6x', '0x2x']
Computer RSSI: -26
IHG Connect RSSI: -43
IHG Connect RSSI: -80
IHG Connect RSSI: -85
Connecting to AP...
could not connect to AP, retrying: ('Failed to connect to ssid', b'Computer')
Connected to Computer RSSI: -23
My IP address is *Changed for privacy*
IP lookup adafruit.com: 104.20.38.240
Ping google.com: 290 ms
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)

----------------------------------------

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json
----------------------------------------
{'time': {'updated': 'Feb 3, 2021 19:54:00 UTC', 'updatedISO': '2021-02-03T19:54:00+00:00', 'updateduk': 'Feb 3, 2021 at 19:54 GMT'}, 'disclaimer': 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'description': 'United States Dollar', 'rate_float': 36936.9, 'rate': '36,936.8783'}}}
----------------------------------------
Done!

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Wed Feb 03, 2021 4:04 pm

And I get the same failed result with the previous code...

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Sun Feb 07, 2021 10:41 am

Okay... I know the connection to the internet works, as the example Quote Book code works perfectly.

So, can we assume, then that there is something within the AdafruitIO code that I'm either missing or needs to be changed?

Code: Select all | TOGGLE FULL SIZE
# 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' : '********password',
    'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
    'github_token' : 'fawfj23rakjnfawiefa',
    'hackaday_token' : 'h4xx0rs3kret',
    'aio_username' : 'oldblackcrow',
    'aio_key' : '******removed for security',
    }


And the code for AdafruitIO is as follows:

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

print("ESP32 SPI webclient test")

# 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)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
)  # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

counter = 0

while True:
    try:
        print("Posting data...", end="")
        data = counter
        feed = "test"
        payload = {"value": data}
        response = wifi.post(
            "https://io.adafruit.com/oldblackcrow"
            + secrets["aio_username"]
            + "/feeds"
            + feed
            + "/rx",
            json=payload,
            headers={"X-AIO-KEY": secrets["aio_key"]},
        )
        print(response.json())
        response.close()
        counter = counter + 1
        print("OK")
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
        wifi.reset()
        continue
    response = None
    time.sleep(15)


If I'm getting a JSON error, is there another script that can access AdafruitIO?

Again, thank you for all your help on this!

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

Re: Pyportal Pynt IO and SSL

by brubell on Mon Feb 08, 2021 12:27 pm

No problem - I'd like to get this working for you :)

Let's backtrack a little bit..
Firmware vers. bytearray(b'1.2.2\x00')


Ok, it looks like the nina-fw on the ESP32 was not updated? Latest version is 1.7.1 (https://github.com/adafruit/nina-fw/releases), you're still running the version shipped with the Pynt.

Could you try following the upgrade steps again and then running the simpletest to get the version number?
https://learn.adafruit.com/upgrading-es ... -one-board

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

Re: Pyportal Pynt IO and SSL

by oldblackcrow on Tue Feb 09, 2021 11:35 pm

I appreciate your concern and help on this... you are very patient with me... And I think I found my confusion.

On the website, it states:
"The lights should flash again, BOOT will disappear. It should re-enumerate USB and appear as a COM or Serial port on your computer. Make a note of the serial port by checking the Device Manager (windows) or typing ls /dev/cu* or /dev/tty* (Mac or Linux) in a terminal."

I throw the PyPortal_ESP32_Passthru.uf2 file over to the PyPortal while it's in bootloader mode and it extracts the file. It only appears as a COM6 device in my Device Manager in Windows... the screen is blank, the LED light it white, and it does not appear as a drive in the file manager.

It continues, "If you see your board listed in the terminal: Proceed to the Uploading nina-fw with esptool section of this page."

As you can see, if I'm using a Mac or Linux system, I can enter the Terminal and move on. Unfortunately, it no longer mentions Windows. When I enter Windows Command Prompt, the PIP command obviously doesn't work.

So, I guess the last time I tried it, I ignored that entire part since it didn't mention what to do in Windows any more... my bad. I should have studied further.

Anyway, if anyone has the next step for me, that would be beautimous!

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

Please be positive and constructive with your questions and comments.