ESP32-C3 DHCP Server only works on first boot #17872
Replies: 4 comments 1 reply
-
As you know the company pycom does not exist any more, and therefore there is no maintenance to pymakr anymore. I used to contribute to the serial interface, and afaik there is nothing special about Pymakr uploads. Mpremote nowadays uses the same serial settings. You can enable logging using
To reduce the number of moving components, what happens is you create a main.py , upload the files, then just turn off, and turn back on ? |
Beta Was this translation helpful? Give feedback.
-
I tried your code on an ESP32-C3 and was not able to reproduce the issue. I did modify the code a bit, hard-coding some values. Here what I ran as boot.py: import time
import network
import urandom
def start_ap():
print("Starting Access Point...")
ap = network.WLAN(network.AP_IF)
ap.active(False)
time.sleep(3)
ap.active(True)
time.sleep(0.5)
ap.config(channel=6)
# Configure IP
ap.ifconfig(("10.10.10.1", "255.255.255.0", "10.10.10.1", "10.10.10.1"))
# Generate random suffix to avoid SSID cache issues
random_suffix = urandom.getrandbits(12) % 10000
# Concat
ssid = f"esp32c3-{random_suffix:04d}"
ap.config(essid=ssid, authmode=network.AUTH_OPEN)
print(f"Access Point: {ap.ifconfig()} started with SSID: {ssid}")
start_ap() I was able to attach to the original SSID, and the new SSID after reset, and I got the same IP address both times.
I used VS Code, but with mpremote, not pymakr. The board I'm using is some ESP32-C3 Mini clone from Amazon. The DHCP client is Windows 11. Sorry to say I can't reproduce the problem, but maybe this will help you rule some things out??? |
Beta Was this translation helpful? Give feedback.
-
Hey @Josverl , @DavesCodeMusings huge thanks to you both for helping with this. It seems like the issue ended up being that my _get_mac_suffix() function got the mac address by activating STA, which seems like it was giving me grief during AP bringup:
I changed it over to use AP for retrieving the mac, and now we're all good:
Now, I can reliably connect to the AP on every power-cycle of the board, DHCP works, etc. But now I've got an entirely different problem that is just as head-scratching... I've probably spent a good 15 hours on it so far. If either of you have any ideas, I'm all ears. I'm running a captive portal that serves a 71Kb index.html page when a client connects. I run a DNS task to hijack any DNS requests and point them to the server IP (10.0.0.1) and an HTTP server task (using asyncio.start_server) that calls an HTTP request handler. In that handler, I serve any request with my index.html; since it's so big, though, I write it in 512 byte chunks. The strange thing - this works perfectly on first boot after upload, but then if I restart, it never work again. My HTTP request handler gets called (so we're getting a request) but it seems like the sending of the page "locks up" and we get stuck responding to that first request, which we don't even respond to fully. Compare that to the first boot, where we serve about a dozen requests successfully. Function for sending the 71Kb index.html in 512 byte chunks:
Function for writing safely and closing writer:
This is the log from a successful first boot, where we serve up the full index.html (and then the client requests other assets used in index.html, like logo.svg, etc):
And then a failed second boot, where we just get one request and "hang" here - no more requests are ever received.
There's obviously more code here (DNS server, HTTP server, etc) - but I'm omitting them because, to me, this is all just some kind of weird issue with the ESP32/micropython network stack. Why else would this work perfectly without issue on every firmware reupload and first boot, and then fail subsequently every single other boot? It points to some persistent state issue after the first boot. I'm not sure if it's something being written to NVS (though I don't think PyMakr clears NVS?) but somehow something in the stack seems to "break" on every other boot, and the only fix is reuploading the firmware via PyMakr. I feel like the stack is kind of brittle and you need to be a bit of an ESP32 whisperer to coax it into behaving properly and there's some tribal/anecdotal wisdom here... just wondering if you guys have any insights. From what I can tell it seems to be related to uasyncio Writer; maybe the buffer is filling up and not draining? |
Beta Was this translation helpful? Give feedback.
-
I don't think there's any secret sauce to the ESP32s. I've been using them for a few years and any problems I run into are always of my own making. If you want to have a look at some HTTP server code for comparison, I have a project called thimble that may be useful. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I have a very strange and major issue. I'm using the ESP32-C3 in AP mode for a captive portal (start and stop of AP shown below).
The extremely strange behavior I'm getting is that :
Has anyone experienced this before? It looks like there's some sort of cache or config of some kind that causes DHCP to fail every boot after the first. Whatever PyMakr is doing seems to clear it - not sure what REPL commands it sends.
Note - this issue happens whether or not there's even a client connection on the first boot! If I connect and reboot - DHCP fails. If I don't even try connecting and reboot - DHCP still fails.
Things I've tried unsuccessfully:
#4 in particular seems to suggest this is something that's persistent, otherwise a restart should clear it - which makes me suspect NVS since Micropython seems to store WiFi configs in NVS (and perhaps PyMakr clears NVS). But I don't know of any way to do that from within the app.
This is a pretty big problem - a user can connect to the AP on the first boot only. If they reboot - well, you'll never see the AP again.
AP Start Code:
Also, logs below from MacOS console.app showing the clean DHCP when connecting to the AP on first boot, and a bad DHCP on second boot.
Beta Was this translation helpful? Give feedback.
All reactions