Erase WiFi credentials

Updated on 31 August 2021
dev board WeMos D1 Mini
firmware Arduino
chip ESP8266
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Buy the components

Code

Download code erase-wifi-credentials.ino
#include <ESP8266WiFi.h>

String oldSsid = "";

void setup() {
  // GPIO02 on ESP-12 module is linked to on-board LED
  pinMode(2, OUTPUT);

	Serial.begin(115200);
}

void loop() {
  blinkLED();

  if (hasWiFiCredentials()) {
    storeOldSSID();
    eraseWiFiCredentials();
  } else {
    printOldNewWifi();
  }
}

void blinkLED() {
  digitalWrite(2, HIGH);
  delay(500);

  digitalWrite(2, LOW);
  delay(500);
}

bool hasWiFiCredentials() {
  if (WiFi.SSID() != "") {
    return true;
  } else {
    return false;
  }
}

void storeOldSSID() {
  oldSsid = WiFi.SSID();
  Serial.print("[INFO] WiFi SSID exists: ");
  Serial.println(oldSsid);
}

void eraseWiFiCredentials() {
  WiFi.disconnect(true);
  ESP.eraseConfig();
  Serial.println("[INFO] WiFi credentials are erased.");
}

void printOldNewWifi() {
  Serial.print("[INFO] Old WiFi SSID: ");
  Serial.println(oldSsid);

  Serial.print("[INFO] New WiFi SSID: ");
  Serial.println(WiFi.SSID());
}

Makefile

.PHONY: lint compile upload clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-readability/todo,-readability/casting,-runtime/int,-whitespace/line_length,-runtime/printf *.ino

compile:
	arduino-cli compile --fqbn esp8266com:esp8266:d1_mini ./

upload:
	arduino-cli upload -p /dev/cu.wchusbserial1410 --fqbn esp8266com:esp8266:d1_mini ./

clean:
	rm -f .*.bin
	rm -f .*.elf

flash: compile upload clean

Description

Erase WiFi credentials stored in the WeMos board with WiFi.disconnect().