Skip to content

Added the ability to set static ip, set hostname, and set DNS #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

manpowre
Copy link
Contributor

Added the ability to set static ip, set hostname, and set DNS with the esp32spi interface as the esp32SPI interface supports this

…e esp32spi interface as the esp32SPI interface supports this
@ladyada
Copy link
Member

ladyada commented Oct 23, 2021

do you want to also submit an example showing this in use?

@manpowre
Copy link
Contributor Author

this code recieves udp data on ip: 192.168.2.2:5500

`import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import neopixel
from rainbowio import colorwheel
import gc
from secrets import secrets

IP_ADDRESS = "192.168.2.2"
GATEWAY_ADDRESS = "192.168.2.1"
SUBNET_MASK = "255.255.255.0"

UDP_IN_ADDR="192.168.2.2"
UDP_IN_PORT = 5500

UDP_TIMEOUT = 20

esp32_cs = DigitalInOut(board.CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)

esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
socket.set_interface(esp)

s_in = socket.socket(type=socket.SOCK_DGRAM)
s_in.settimeout(UDP_TIMEOUT)
print("set hostname:")
esp.set_hostname("new_hostname".encode("utf-8"))

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...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
ip1 = esp.ip_address

print("set ip dns")
esp.set_dns_config("192.168.2.1", "8.8.8.8")

print("set ip config")
esp.set_ip_config(IP_ADDRESS, GATEWAY_ADDRESS, SUBNET_MASK)

time.sleep(1)
ip2=esp.ip_address

time.sleep(1)
info = esp.network_data
print("get network_data: ", esp.pretty_ip(info["ip_addr"]), esp.pretty_ip(info["gateway"]), esp.pretty_ip(info["netmask"]))

IP_ADDR = esp.pretty_ip(esp.ip_address)
print("ip:", IP_ADDR)
print("My IP address is", esp.pretty_ip(esp.ip_address))
print("udp in addr: ", UDP_IN_ADDR, UDP_IN_PORT)

socketaddr_udp_in = socket.getaddrinfo(UDP_IN_ADDR, UDP_IN_PORT)[0][4]
s_in.connect(socketaddr_udp_in, conntype=esp.UDP_MODE)
print("connected local UDP")

while True:
data = s_in.recv(1205)
if len(data)>=1:
data=data.decode('utf-8')
print(len(data), data)

`

outputs:

set hostname:
ESP32 found and in idle mode
Firmware vers. bytearray(b'1.4.8\x00')
MAC addr: ['0xd0', '0xb1', '0x78', '0xa8', '0xcc', '0x84']
Connecting to AP...
Connected to zone RSSI: -58
set ip dns
set ip config
get network_data: 192.168.2.2 192.168.2.1 255.255.255.0
ip: 192.168.2.2
My IP address is 192.168.2.2
udp in addr: 192.168.2.2 5500
connected local UDP

@ladyada
Copy link
Member

ladyada commented Oct 24, 2021

ok for the example, add it to the PR :)

@anecdata
Copy link
Member

anecdata commented Nov 7, 2021

@manpowre looks like it's failing on black. Can you repair that?

@FoamyGuy
Copy link
Contributor

Closing this for now.

@manpowre if you are still interested and get back around to this, please run the black code formatter on the code and add an example into the examples/ directory of the repo and add a new commit / push after doing those two things.

We have a guide here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/black that shows the formatting process and you can ask for help in our discord if you want to do this but don't know how.

Feel free to re-open this once that has been done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants