Simple test

Ensure your device works with this simple test.

examples/pyportal_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5# NOTE: Make sure you've created your secrets.py file before running this example
 6# https://learn.adafruit.com/adafruit-pyportal/internet-connect#whats-a-secrets-file-17-2
 7import board
 8from displayio import CIRCUITPYTHON_TERMINAL
 9
10from adafruit_pyportal import PyPortal
11
12# Set a data source URL
13TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
14
15# Create the PyPortal object
16pyportal = PyPortal(url=TEXT_URL, status_neopixel=board.NEOPIXEL)
17
18# Set display to show REPL
19board.DISPLAY.root_group = CIRCUITPYTHON_TERMINAL
20
21# Go get that data
22print("Fetching text from", TEXT_URL)
23data = pyportal.fetch()
24
25# Print out what we got
26print("-" * 40)
27print(data)
28print("-" * 40)

Internet test

Example to illustrate the device capability to get json data

examples/pyportal_internet_json_fetching.py
 1# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example to illustrate the device capability to get json data
 6"""
 7
 8# NOTE: Make sure you've created your secrets.py file before running this example
 9# https://learn.adafruit.com/adafruit-pyportal/internet-connect#whats-a-secrets-file-17-2
10import adafruit_connection_manager
11import adafruit_requests
12import board
13from adafruit_esp32spi import adafruit_esp32spi
14from digitalio import DigitalInOut
15
16# Get wifi details and more from a secrets.py file
17try:
18    from secrets import secrets
19except ImportError:
20    print("WiFi secrets are kept in secrets.py, please add them there!")
21    raise
22
23print("ESP32 SPI webclient test")
24
25TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
26JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
27
28
29# ESP32 Pins:
30esp32_cs = DigitalInOut(board.ESP_CS)
31esp32_ready = DigitalInOut(board.ESP_BUSY)
32esp32_reset = DigitalInOut(board.ESP_RESET)
33
34# SPI Configuration
35spi = board.SPI()
36esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
37pool = adafruit_connection_manager.get_radio_socketpool(esp)
38ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
39requests = adafruit_requests.Session(pool, ssl_context)
40
41if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
42    print("ESP32 found and in idle mode")
43print("Firmware vers.", esp.firmware_version)
44print("MAC addr:", [hex(i) for i in esp.MAC_address])
45
46for ap in esp.scan_networks():
47    print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
48
49print("Connecting to AP...")
50while not esp.is_connected:
51    try:
52        esp.connect_AP(secrets["ssid"], secrets["password"])
53    except RuntimeError as e:
54        print("could not connect to AP, retrying: ", e)
55        continue
56print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
57print("My IP address is", esp.pretty_ip(esp.ip_address))
58print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
59print("Ping google.com: %d ms" % esp.ping("google.com"))
60
61# esp._debug = True
62print("Fetching text from", TEXT_URL)
63r = requests.get(TEXT_URL)
64print("-" * 40)
65print(r.text)
66print("-" * 40)
67r.close()
68
69print()
70print("Fetching json from", JSON_URL)
71r = requests.get(JSON_URL)
72print("-" * 40)
73print(r.json())
74print("-" * 40)
75r.close()
76print("Done!")

QR Test

This example shows a web address QR in the display

examples/pyportal_qrcode_generation.py
 1# SPDX-FileCopyrightText: 2021 Jose David M.
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example shows a web address QR in the display
 6"""
 7
 8import board
 9
10from adafruit_pyportal.graphics import Graphics
11
12# Set display to show
13display = board.DISPLAY
14
15# Background Information
16base = Graphics(default_bg=0x990099, debug=True)
17
18# WebPage to show in the QR
19webpage = "http://www.adafruit.com"
20
21# QR size Information
22qr_size = 9  # Pixels
23scale = 3
24
25# Create a barcode
26base.qrcode(
27    webpage,
28    qr_size=scale,
29    x=display.width // 2 - qr_size * scale,
30    y=display.height // 2 - qr_size * scale,
31)
32
33while True:
34    pass