← All writing

The ESP32 that lost WiFi packets five minutes after every boot

An EleksWFD desk clock kept flipping its display because its WiFi link went bad 4 to 7 minutes after each boot. The cause was the radio's default transmit power, and finding it took several wrong answers.

My EleksWFD desk clock runs custom firmware on an ESP32. A Windows app sends it display frames over WiFi, 30 UDP packets a second. When the clock hears nothing from the app for 2 seconds, it assumes the app is gone and draws its own display.

One day the display started flipping. App display, clock display, app display again, every few seconds. The fix turned out to be one line that lowers the radio's transmit power. Getting there took most of a day and at least six wrong answers, and the wrong answers are the useful part of this story.

What the fault looked like

The clock sat in the same room as its access point, with a signal around -30 dBm. That is about as strong as WiFi gets. The firmware's main loop was running about 800 passes a second, so the clock was not stalling.

The network numbers were bad anyway. Under load, the clock lost 10 to 50 percent of its packets and took several seconds to answer a status request. A restart cured it. Then it came back.

The timing was the strange part. The link was clean after every boot and went bad between minute 4 and minute 7. It stayed bad until the next restart.

Six wrong answers

I blamed these, in this order. Each one was tested and cleared.

  • The USB port. The clock had just moved to a USB 3 port, which already breaks flashing on this board. It made no difference here.
  • The newest firmware build. Rolling back did not help.
  • The microphone task, which samples at 8 kHz on the other core.
  • A slow main loop. I added timing to every part of the loop and found nothing slow.
  • The 40 MHz channel width and WiFi power saving.
  • The access point. The network is a two-node mesh with one name, and the clock had once joined the far node at -70 dBm.

The access point theory is the one I got badly wrong. I changed the firmware to scan every channel and join the strongest access point, ran the test, and the link stayed clean for 40 minutes. I called it confirmed and wrote that explanation into the code comments and the docs.

It was not confirmed. That test build also carried a diagnostic I had added earlier, which re-applied the radio's transmit power every few seconds. Re-applying it had quietly lowered the power from 19.5 dBm to 18 dBm. My comparison changed two things, and I credited the wrong one.

The comparison that settled it

I built the strongest-access-point firmware without the diagnostic and left the radio at its boot default. The link went bad on schedule.

Then I ran the same code with the power set to 15 dBm. Clean, for more than 16 minutes, past the point where it had always failed.

The ESP32 reports transmit power in steps of 0.25 dBm. The boot default on this board is 78, which is 19.5 dBm and the maximum. At 72, which is 18 dBm, and at every lower setting I tried, the link stayed clean. Only the top step fails.

The fix

Once the clock has joined the network, the firmware sets the power once:

static const wifi_power_t WIFI_TX_POWER = WIFI_POWER_15dBm;

static bool powerSet = false;
if (!powerSet) powerSet = WiFi.setTxPower(WIFI_TX_POWER);

15 dBm leaves a margin below the step that fails, and it is plenty for a clock in the same house as its router. After the change the clock lost 2 of 962 pings over 17 minutes from boot.

I kept the strongest-access-point change too. It was not the cause, but joining the far node of a mesh is its own problem.

I do not know why the top power step does this. My guesses are the board's power supply or the antenna match, and I have tested neither. The firmware uses Arduino core 2.0.17, and I have not tried another version.

What I would do differently

Run every test past the point where the fault appears. This one needed 4 to 7 minutes. A two-minute test passes every time and proves nothing. My tests ran 15 minutes or more once I understood that.

Change one thing per comparison. The false confirmation cost hours, and the wrong explanation made it into the documentation before I caught it.

Make the device report its own state. I added two blocks to the clock's status page. One shows the radio: access point, channel, signal, power save mode and transmit power. The other shows how long each part of the main loop takes. The transmit power number in that block is what exposed the bad comparison. It read 72 when I expected 78.

Test under the real load. A ping alone looked fine for long stretches. The fault showed with the 30 packets a second display stream, a status request every second, and a ping, all running together.

One more flip

With the radio fixed, I saw a single flip during testing that had a different cause. The clock's web server answers one client at a time. The app asked for the clock's status every 5 seconds on the same thread that sends the display, so a slow answer paused the stream. If another client held the web server for more than 2 seconds, the clock flipped.

The app now asks for status on its own thread. I tested it by holding the clock's web server busy for 4 seconds at a time. The old app delivered between 8 and 20 packets a second during those holds. The new one delivered 30.

The radio fix is in release 1.5.1 and the app fix in 1.5.2. The code is in wifi_link.cpp, and the firmware notes describe the radio settings and the diagnostics. The EleksWFD project page covers the rest of the firmware and the companion app.