Last active
March 5, 2020 05:39
-
-
Save anecdata/cc4f091cb50fdb665bfe6586dd079999 to your computer and use it in GitHub Desktop.
Feather M4 + Airlift FeatherWing (ESP32SPI) + Ethernet FeatherWing (WIZNET5K)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import busio | |
import digitalio | |
import adafruit_requests as requests | |
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K | |
import adafruit_wiznet5k.adafruit_wiznet5k_socket as wiznet5k_socket | |
from adafruit_esp32spi import adafruit_esp32spi | |
import adafruit_esp32spi.adafruit_esp32spi_socket as esp_socket | |
from secrets import secrets | |
# Feather M4 + Airlift FeatherWing + Ethernet FeatherWing | |
# example customized from https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k | |
print("Wiznet5k + ESP32SPI WebClient Test") | |
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" | |
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" | |
def esp_connect(): | |
esp_status = 255 | |
try: | |
esp_status = esp.status | |
except RuntimeError as e: | |
print("ESP Status Error: ", e) | |
try: | |
esp_is_connected = esp.is_connected | |
except RuntimeError as e: | |
print("ESP Is Connected Error: ", e) | |
# these should never disagree | |
if esp_status == 3 and esp_is_connected: | |
pass # already connected | |
else: | |
try: | |
print("ESP Connecting...", esp_status, esp_status_text(esp_status), "Connected?", esp_is_connected) | |
# "Will retry up to 10 times and return on success | |
# or raise an exception on failure" | |
# [1 second between each retry] | |
esp.connect_AP(secrets['ssid'], secrets['password']) | |
esp_status = esp.status | |
esp_is_connected = esp.is_connected | |
print(" ", esp_status, esp_status_text(esp_status), "Connected?", esp_is_connected) | |
except RuntimeError as e: | |
print("ESP Connection Error: ", e) | |
return esp_status, esp_is_connected | |
def esp_reset(): | |
print("ESP Reset BEGIN") | |
try: | |
esp.reset() | |
except RuntimeError as e: | |
print("ESP Reset Error:", e) | |
time.sleep(1) | |
print("ESP Reset END") | |
def esp_status_text(num): | |
stat = {0: 'WL_IDLE_STATUS', | |
1: 'WL_NO_SSID_AVAIL', | |
2: 'WL_SCAN_COMPLETED', | |
3: 'WL_CONNECTED', | |
4: 'WL_CONNECT_FAILED', | |
5: 'WL_CONNECTION_LOST', | |
6: 'WL_DISCONNECTED', | |
7: 'WL_AP_LISTENING', | |
8: 'WL_AP_CONNECTED', | |
9: 'WL_AP_FAILED', | |
10: 'WL_NO_SHIELD', } | |
if num in stat: | |
return stat[num] | |
else: | |
return 'WL_UNDEFINED' | |
spi = board.SPI() | |
# eth_cs = digitalio.DigitalInOut(board.D10) | |
# customized CS pin b/c ESP32 needs GPIO0/D10 available for reboots: | |
eth_cs = digitalio.DigitalInOut(board.A5) | |
# Initialize ethernet interface with DHCP | |
eth = WIZNET5K(spi, eth_cs, debug=False) | |
esp32_cs = digitalio.DigitalInOut(board.D13) # M4 Red LED | |
esp32_ready = digitalio.DigitalInOut(board.D11) | |
esp32_reset = digitalio.DigitalInOut(board.D12) | |
# esp32_gpio0 = DigitalInOut(board.D10) # optional | |
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
esp_reset() | |
# don't proceed w/o an ESP32 | |
print("Checking ESP32...") | |
while True: | |
try: | |
esp_firmware_version = esp.firmware_version | |
espfw = '' | |
for _ in esp_firmware_version: | |
if _ != 0: | |
espfw += "{:c}".format(_) | |
print("ESP Firmware: ", espfw) | |
esp_MAC_address = esp.MAC_address | |
esp_status = esp.status | |
esp_is_connected = esp.is_connected | |
print("ESP Status: ", esp_status, esp_status_text(esp_status), "Connected?", esp_is_connected) | |
break | |
except RuntimeError as e: | |
print("ESP Access Error:", e) | |
esp_reset() | |
# don't proceed w/o any Wi-Fi APs | |
print("Scanning WiFi...") | |
while True: | |
try: | |
esp_scan_networks = esp.scan_networks() | |
for ap in esp_scan_networks: | |
bssid = ap['bssid'] | |
print(" {5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}".format(*bssid), end=' ') | |
print("{: 3d}".format(ap['channel']), end=' ') | |
print("{: 4d}".format(ap['rssi']), end=' ') | |
print("{: 1d}".format(ap['encryption']), end=' ') | |
print("{:s}".format(str(ap['ssid'], 'utf-8'))) | |
break | |
except (RuntimeError, KeyError) as e: | |
print("ESP AP Scan Error:", e) | |
esp_reset() | |
# don't proceed w/o a Wi-Fi connection | |
while True: | |
try: | |
if esp_connect() == (3, True): | |
print("RSSI: ", esp.rssi) | |
print("SSID: ", str(esp.ssid, 'utf-8')) | |
print("BSSID: {5:02X}:{4:02X}:{3:02X}:{2:02X}:{1:02X}:{0:02X}".format(*esp.bssid)) | |
print("IP: ", esp.pretty_ip(esp.ip_address)) | |
print("Netmask: %d.%d.%d.%d" % (esp.network_data['netmask'][0], esp.network_data['netmask'][1], | |
esp.network_data['netmask'][2], esp.network_data['netmask'][3])) | |
print("Gateway: %d.%d.%d.%d" % (esp.network_data['gateway'][0], esp.network_data['gateway'][1], | |
esp.network_data['gateway'][2], esp.network_data['gateway'][3])) | |
print("LAN ping: %dms" % esp.ping(esp.network_data['gateway'])) | |
print("WAN ping: %dms" % esp.ping('adafruit.com')) | |
break | |
except RuntimeError as e: | |
print("ESP Wi-Fi Error:", e) | |
esp_reset() | |
# Initialize a requests object with a wi-fi socket interface | |
requests.set_socket(esp_socket, esp) | |
print("Fetching text from", TEXT_URL) | |
r = requests.get(TEXT_URL) | |
print('-'*40) | |
print(r.text) | |
print('-'*40) | |
r.close() | |
print() | |
print("Fetching json from", JSON_URL) | |
r = requests.get(JSON_URL) | |
print('-'*40) | |
print(r.json()) | |
print('-'*40) | |
r.close() | |
print("Checking Ethernet...") | |
print("Chip Version:", eth.chip) | |
print("MAC Address:", [hex(i) for i in eth.mac_address]) | |
print("IP Address:", eth.pretty_ip(eth.ip_address)) | |
print("adafruit.com DNS: %s" %eth.pretty_ip(eth.get_host_by_name("adafruit.com"))) | |
# Initialize a requests object with an ethernet socket and interface | |
requests.set_socket(wiznet5k_socket, eth) | |
#eth._debug = True | |
print("Fetching text from", TEXT_URL) | |
r = requests.get(TEXT_URL) | |
print('-'*40) | |
print(r.text) | |
print('-'*40) | |
r.close() | |
print() | |
print("Fetching json from", JSON_URL) | |
r = requests.get(JSON_URL) | |
print('-'*40) | |
print(r.json()) | |
print('-'*40) | |
r.close() | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment