|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +from digitalio import DigitalInOut |
| 5 | +import adafruit_requests as requests |
| 6 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 7 | +from adafruit_esp32spi import adafruit_esp32spi |
| 8 | +import neopixel |
| 9 | +from rainbowio import colorwheel |
| 10 | +import gc |
| 11 | +from secrets import secrets |
| 12 | + |
| 13 | +IP_ADDRESS = "192.168.2.2" |
| 14 | +GATEWAY_ADDRESS = "192.168.2.1" |
| 15 | +SUBNET_MASK = "255.255.255.0" |
| 16 | + |
| 17 | +UDP_IN_ADDR = "192.168.2.2" |
| 18 | +UDP_IN_PORT = 5500 |
| 19 | + |
| 20 | +UDP_TIMEOUT = 20 |
| 21 | + |
| 22 | +esp32_cs = DigitalInOut(board.CS1) |
| 23 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 24 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 25 | + |
| 26 | +spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1) |
| 27 | + |
| 28 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 29 | +socket.set_interface(esp) |
| 30 | + |
| 31 | +s_in = socket.socket(type=socket.SOCK_DGRAM) |
| 32 | +s_in.settimeout(UDP_TIMEOUT) |
| 33 | +print("set hostname:") |
| 34 | +esp.set_hostname("new_hostname".encode("utf-8")) |
| 35 | + |
| 36 | +if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 37 | + print("ESP32 found and in idle mode") |
| 38 | +print("Firmware vers.", esp.firmware_version) |
| 39 | +print("MAC addr:", [hex(i) for i in esp.MAC_address]) |
| 40 | + |
| 41 | +print("Connecting to AP...") |
| 42 | +while not esp.is_connected: |
| 43 | + try: |
| 44 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 45 | + except RuntimeError as e: |
| 46 | + print("could not connect to AP, retrying: ", e) |
| 47 | + continue |
| 48 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 49 | +ip1 = esp.ip_address |
| 50 | + |
| 51 | +print("set ip dns") |
| 52 | +esp.set_dns_config("192.168.2.1", "8.8.8.8") |
| 53 | + |
| 54 | +print("set ip config") |
| 55 | +esp.set_ip_config(IP_ADDRESS, GATEWAY_ADDRESS, SUBNET_MASK) |
| 56 | + |
| 57 | +time.sleep(1) |
| 58 | +ip2 = esp.ip_address |
| 59 | + |
| 60 | +time.sleep(1) |
| 61 | +info = esp.network_data |
| 62 | +print( |
| 63 | + "get network_data: ", |
| 64 | + esp.pretty_ip(info["ip_addr"]), |
| 65 | + esp.pretty_ip(info["gateway"]), |
| 66 | + esp.pretty_ip(info["netmask"]), |
| 67 | +) |
| 68 | + |
| 69 | +IP_ADDR = esp.pretty_ip(esp.ip_address) |
| 70 | +print("ip:", IP_ADDR) |
| 71 | +print("My IP address is", esp.pretty_ip(esp.ip_address)) |
| 72 | +print("udp in addr: ", UDP_IN_ADDR, UDP_IN_PORT) |
| 73 | + |
| 74 | +socketaddr_udp_in = socket.getaddrinfo(UDP_IN_ADDR, UDP_IN_PORT)[0][4] |
| 75 | +s_in.connect(socketaddr_udp_in, conntype=esp.UDP_MODE) |
| 76 | +print("connected local UDP") |
| 77 | + |
| 78 | +while True: |
| 79 | + data = s_in.recv(1205) |
| 80 | + if len(data) >= 1: |
| 81 | + data = data.decode("utf-8") |
| 82 | + print(len(data), data) |
0 commit comments