station-meteo/pyportal-controller/main.py

47 lines
1.5 KiB
Python
Raw Normal View History

import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
import config
# 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)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
requests.set_socket(socket, esp)
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])
print("Connecting to AP...")
def WifiConnect(ssid, psk):
"""Connect to the wifi with given credentials."""
nets = esp.scan_networks()
for net in nets:
net_ssid = net['ssid']
if net_ssid == bytearray(ssid):
print('Network found!')
esp.connect_AP(net_ssid, psk)
while not esp.is_connected:
machine.idle() # save power while waiting
print('WLAN connection succeeded!')
break
if not esp.is_connected:
print("WLAN not found/not connected")
WifiConnect(config.WIFI_SSID, config.WIFI_PSK)
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))