I’ve been trying to hunt down this problem for a while now and I am at this point convinced there is a bug somewhere in Espressif’s wifi code, and would love to get some ideas on how to debug this. Here is the issue:
- ESP8266 has issues receiving an IP address from a DHCP server in some (unknown) circumstances.
- You find not a ton but a continuous stream of posts e.g. on reddit with people describing an issue that sounds very similar to this.
- I have two Wemos D1 Mini ESP8266 dev boards showing this issue as well as my own custom PCB using the wroom2 module.
- The issue reproduces with my own, very basic demo (see below) as well as when putting a prebuilt version of e.g. WLED on the board.
- Given enough time, eventually the ESP will connect to wifi, if it doesn’t give up first and assign itself an ip. It can take minutes. Restarting the ESP after some amount of time helps here.
- Sniffing the WiFi traffic from my Mac, as well as checking the logs on the DHCP server, I see a pretty constant ping pong of the ESP sending a DHCP discover message, followed by an immediate offer from my DHCP server. Then after about 2 seconds, the ESP repeats the discover broadcast and the cycle repeats.
- The issue does not reproduce on an ESP32 and it also clearly does not reproduce on all WiFis. In fact setting up a quick test WiFi, the ESP connects without any issues. I still believe it’s an issue with the ESP because no other device in my network has this issue.
Here’s the basic example that reproduces the issue:
// main.c
#include <Arduino.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.setDebugOutput(true);
// commenting in the following line fixes the issue:
// WiFi.config(IPAddress(192,168,2,101), IPAddress(192,168,1,1), IPAddress(255,255,128,0), IPAddress(1,1,1,1), IPAddress(2,2,2,2));
WiFi.begin("MyWiFiSSID", "MyWiFiPassword");
}
The serial output this logs is:
fpm close 3
mode : sta(48:e7:29:4a:77:3c)
add if0
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 27
cnt
connected with MyWiFiSSID, channel 9
dhcp client start...
pm open,type:2 0 // this takes about 10 seconds to show up
ip:169.254.61.119,mask:255.255.0.0,gw:0.0.0.0 // after 5 minutes, it gave up and assign itself an IP
The only thing ‘unusual’ about my network is that I maybe have a slightly larger than normal net mask of 255.255.128.0 and my DHCP server is not the gateway. My local IPs are in the 192.168.0.0 network.
I have my ESP8266 connected via the Espressif Prog Board and afaik PlatformIO does not support debugging via the serial connection, so I have to basically revert back to printf debugging. I would love to see if the wifi module ever receives those offer replies from the DHCP server and if so what it does with them, but it looks like that part is not compiled from source, which makes this hard. I guess one way to get to the bottom of this is to compile espressif’s wifi library from scratch, but I’m not sure how to hook that into a PIO project. Are there any instructions on that anywhere?
Obviously for an at-home project I could just keep rebooting the ESP until this works. But this is not really a shippable solution for any project you might want to market. So I would really like to get to the bottom of this. The fact that this sometimes works in my network tells me it’s not a hardware issue per se, but instead a software issue in the 8266’s specific wifi code somewhere.
Any ideas or help in how to get to the bottom of this would be greatly appreciated.