Skip to content

Commit c13a92b

Browse files
authored
Merge pull request #111 from jposada202020/adding_examples
Adding examples, changing API index, examples.rst
2 parents 2c09cc1 + d29b450 commit c13a92b

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

docs/examples.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/pyportal_simpletest.py
77
:caption: examples/pyportal_simpletest.py
88
:linenos:
9+
10+
Internet test
11+
-------------
12+
13+
Example to illustrate the device capability to get json data
14+
15+
.. literalinclude:: ../examples/pyportal_internet_json_fetching.py
16+
:caption: examples/pyportal_internet_json_fetching.py
17+
:linenos:
18+
19+
QR Test
20+
-------
21+
22+
This example shows a web address QR in the display
23+
24+
.. literalinclude:: ../examples/pyportal_qrcode_generation.py
25+
:caption: examples/pyportal_qrcode_generation.py
26+
:linenos:

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26+
Adafruit PyPortal Learning Guide <https://learn.adafruit.com/adafruit-pyportal>
27+
2628
.. toctree::
2729
:caption: Related Products
2830

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

0 commit comments

Comments
 (0)