Skip to content

Commit e433ef6

Browse files
committed
apply black and add example
1 parent 9a40af7 commit e433ef6

File tree

2 files changed

+104
-8
lines changed

2 files changed

+104
-8
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,35 @@ def scan_networks(self):
410410

411411
def set_ip_config(self, new_ip, new_gw, new_mask="255.255.255.0"):
412412
"""Tells the ESP32 to set ip, gateway and network mask b"\xFF" """
413-
resp = self._send_command_get_response(_SET_IP_CONFIG,
414-
params= [b"\x00",self.unpretty_ip(new_ip),self.unpretty_ip(new_gw), self.unpretty_ip(new_mask)],
415-
sent_param_len_16=False)
413+
resp = self._send_command_get_response(
414+
_SET_IP_CONFIG,
415+
params=[
416+
b"\x00",
417+
self.unpretty_ip(new_ip),
418+
self.unpretty_ip(new_gw),
419+
self.unpretty_ip(new_mask),
420+
],
421+
sent_param_len_16=False,
422+
)
416423
return resp
417424

418425
def set_dns_config(self, dns1, dns2="8.8.8.8"):
419426
"""Tells the ESP32 to set DNS, with dns2 default to google dns=8.8.8.8"""
420-
resp = self._send_command_get_response(_SET_DNS_CONFIG, [b"\x00", self.unpretty_ip(dns1), self.unpretty_ip(dns2)])
427+
resp = self._send_command_get_response(
428+
_SET_DNS_CONFIG, [b"\x00", self.unpretty_ip(dns1), self.unpretty_ip(dns2)]
429+
)
421430
if resp[0][0] != 1:
422431
raise RuntimeError("Failed to set dns with esp32")
423-
432+
424433
def set_hostname(self, hostname):
425-
"""Tells the ESP32 to set hostname"""
426-
resp = self._send_command_get_response(_SET_HOSTNAME, [hostname])
427-
return resp
434+
"""
435+
Tells the ESP32 to set hostname
436+
437+
:params str hostname: Set the host name, used by DHCP to associate a local
438+
domain name like hostname.home for example, depending
439+
on the DHCP server setup.
440+
"""
441+
resp = self._send_command_get_response(_SET_HOSTNAME, [hostname.encode()])
428442
if resp[0][0] != 1:
429443
raise RuntimeError("Failed to set hostname with esp32")
430444

examples/esp32spi_ipconfig.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)