Simple test¶
Ensure your device works with this simple test.
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
9from adafruit_pyportal import PyPortal
10
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
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 board
11from digitalio import DigitalInOut
12import adafruit_connection_manager
13import adafruit_requests
14from adafruit_esp32spi import adafruit_esp32spi
15
16
17# Get wifi details and more from a secrets.py file
18try:
19 from secrets import secrets
20except ImportError:
21 print("WiFi secrets are kept in secrets.py, please add them there!")
22 raise
23
24print("ESP32 SPI webclient test")
25
26TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
27JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
28
29
30# ESP32 Pins:
31esp32_cs = DigitalInOut(board.ESP_CS)
32esp32_ready = DigitalInOut(board.ESP_BUSY)
33esp32_reset = DigitalInOut(board.ESP_RESET)
34
35# SPI Configuration
36spi = board.SPI()
37esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
38pool = adafruit_connection_manager.get_radio_socketpool(esp)
39ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
40requests = adafruit_requests.Session(pool, ssl_context)
41
42if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
43 print("ESP32 found and in idle mode")
44print("Firmware vers.", esp.firmware_version)
45print("MAC addr:", [hex(i) for i in esp.MAC_address])
46
47for ap in esp.scan_networks():
48 print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
49
50print("Connecting to AP...")
51while not esp.is_connected:
52 try:
53 esp.connect_AP(secrets["ssid"], secrets["password"])
54 except RuntimeError as e:
55 print("could not connect to AP, retrying: ", e)
56 continue
57print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
58print("My IP address is", esp.pretty_ip(esp.ip_address))
59print(
60 "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
61)
62print("Ping google.com: %d ms" % esp.ping("google.com"))
63
64# esp._debug = True
65print("Fetching text from", TEXT_URL)
66r = requests.get(TEXT_URL)
67print("-" * 40)
68print(r.text)
69print("-" * 40)
70r.close()
71
72print()
73print("Fetching json from", JSON_URL)
74r = requests.get(JSON_URL)
75print("-" * 40)
76print(r.json())
77print("-" * 40)
78r.close()
79print("Done!")
QR Test¶
This example shows a web address QR in the display
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
9from adafruit_pyportal.graphics import Graphics
10
11# Set display to show
12display = board.DISPLAY
13
14# Background Information
15base = Graphics(default_bg=0x990099, debug=True)
16
17# WebPage to show in the QR
18webpage = "http://www.adafruit.com"
19
20# QR size Information
21qr_size = 9 # Pixels
22scale = 3
23
24# Create a barcode
25base.qrcode(
26 webpage,
27 qr_size=scale,
28 x=display.width // 2 - qr_size * scale,
29 y=display.height // 2 - qr_size * scale,
30)
31
32while True:
33 pass