Skip to content
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

Static IP address for WiFi #6441

Merged
merged 4 commits into from Jun 3, 2022
Merged

Static IP address for WiFi #6441

merged 4 commits into from Jun 3, 2022

Conversation

gamblor21
Copy link
Member

As mentioned in issue #6274 there was a request to add the ability to statically set your IP address. Using @anecdata's suggestions I have added in this functionality. There are three new methods for wifi.Radio:

  1. stop_dhcp - Stop the DHCP client (required to set a static address)
  2. start_dhcp - Start the DHCP client (will automatically grab an IP address if it was stopped)
  3. set_ipv4_address - Provide the static IP, subnet and gateway addresses (all are required). This will stop the DHCP client automatically as it is required.

One thing to note, if you stop DHCP and do not set a static IP address and a call to connect to WiFi will fail.
The set_ipv4_address is a callable function (as opposed to a setter) as it does require all 3 values that the current getters can retrieve.

@dhalbert dhalbert requested a review from tannewt May 29, 2022 16:32
Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for diving in to this!

Using this test code on a
Adafruit CircuitPython 8.0.0-alpha.0-22-g297aa91f2 on 2022-05-27; Adafruit QT Py ESP32S2 with ESP32S2
to set up the static parameters:

import time
import wifi
import ipaddress
from secrets import secrets

print(f"Setting IPv4...")
wifi.radio.set_ipv4_address(ipaddress.ip_address("192.168.6.252"),
                            ipaddress.ip_address("192.168.4.1"),
                            ipaddress.ip_address("255.255.252.0"))
print(f"IPv4    {wifi.radio.ipv4_address}")  # None
print(f"Gateway {wifi.radio.ipv4_gateway}")  # None
print(f"Subnet  {wifi.radio.ipv4_subnet}")   # None

print(f"Connecting...")
wifi.radio.connect(secrets['ssid'], secrets['password'])
print(f"IPv4    {wifi.radio.ipv4_address}")  # 192.168.6.252
print(f"Gateway {wifi.radio.ipv4_gateway}")  # 192.168.4.1
print(f"Subnet  {wifi.radio.ipv4_subnet}")   # 255.255.252.0

print(f"DNS     {wifi.radio.ipv4_dns}")      # 0.0.0.0
print(f"RSSI    {wifi.radio.ap_info.rssi}")  # e.g., -65

Device is able to connect to the AP, and the static parameters are correctly reflected. TCP sockets and Requests work on LAN numeric IPv4 addresses. It's not able to ping LAN or WAN numeric IPv4 addresses for some reason (result always is None) - I may be missing a step or it could be a local network issue. Without DHCP, there is no DNS address. I don't think we have a way to set DNS address(es). So there is no name resolution. But, Requests to an mDNS host does still work, and the mDNS host reflects the static IP :-)

I haven't gotten WAN Requests (v1.11.2) to a numeric IPv4 address to work yet:

Traceback (most recent call last):
  File "code.py", line ..., in <module>
  File "adafruit_requests.py", line 720, in get
  File "adafruit_requests.py", line 661, in request
  File "adafruit_requests.py", line 512, in _get_socket
RuntimeError: Sending request failed

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I can't think of any changes but did see two minor doc issues.

shared-bindings/wifi/Radio.c Outdated Show resolved Hide resolved
shared-bindings/wifi/Radio.c Outdated Show resolved Hide resolved
@gamblor21
Copy link
Member Author

It's not able to ping LAN or WAN numeric IPv4 addresses for some reason (result always is None) - I may be missing a step or it could be a local network issue.

Ping is working for me, so not sure what could be wrong there?

Without DHCP, there is no DNS address. I don't think we have a way to set DNS address(es). So there is no name resolution. But, Requests to an mDNS host does still work, and the mDNS host reflects the static IP :-)

Well that won't do! I added a set method for DNS. Totally skipped my mind as I rarely use DNS for CP projects.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought of one more thing. Thank you for your patience.

shared-bindings/wifi/Radio.c Outdated Show resolved Hide resolved
shared-bindings/wifi/Radio.c Outdated Show resolved Hide resolved
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit a4a744b into adafruit:main Jun 3, 2022
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.

None yet

3 participants