HeapCrash / Challenges / Wi-Fi reconnect handler

Wi-Fi reconnect handler

ESP32 firmware (ESP-IDF) reconnects to Wi-Fi when the access point drops. After a router firmware update that took 10 minutes, some units never came back. Others came back with two MQTT connections, and button and OTA events stall for seconds whenever the link drops.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

static int retries;

static void wifi_event(void *arg, esp_event_base_t base,
                       int32_t id, void *data)
{
    if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
        if (retries < 5) {
            vTaskDelay(pdMS_TO_TICKS(1000));
            esp_wifi_connect();
            retries++;
        }
    } else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
        mqtt_start();
    }
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs