Sleep with radio disabled

Updated on 3 September 2021
dev board WeMos D1 Mini
chip ESP8266
features sleep wakeup radio
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 no-radio-start-radio.ino
#include <ESP8266WiFi.h>

// Edit "secret" ssid and password below
const char* ssid = "secret";
const char* password = "secret";
int count = 0;

void setup() {
  disableWiFi();

  pinMode(2, OUTPUT);  // GPIO02 on ESP-12 module is linked to on-board LED
  Serial.begin(115200);
  delay(10);

  wakeupWiFiModem();
  connectToWiFi();
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    blink(5);
  }

  sleepAndWakeupWithRadioDisabled();
}

void disableWiFi() {
  WiFi.mode(WIFI_OFF);

  WiFi.forceSleepBegin();
  delay(1);

  return;
}

void wakeupWiFiModem() {
  WiFi.forceSleepWake();  // wakeup WiFi modem
  delay(1);

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  return;
}

void connectToWiFi() {
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    count++;
    Serial.print(".");

    if (count > 20) {
      Serial.println("WiFi connection failed");
      break;
    }
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("WiFi connected. IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void blink(int count) {
  for (int i = 0; i < count; i++) {
    Serial.println("BLINK");
    digitalWrite(2, HIGH);
    delay(1000);
    digitalWrite(2, LOW);
    delay(1000);
  }
}

void sleepAndWakeupWithRadioDisabled() {
  Serial.println("Deep sleeping for 5 seconds...");
  Serial.println("Wakeup without WiFi next time");

  WiFi.disconnect(true);
  delay(1);
  ESP.deepSleep(5000000, WAKE_RF_DISABLED);  // radio disabled after wakeup
}

Makefile

.PHONY: lint compile upload clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-readability/todo *.ino

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

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

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

flash: lint compile upload clean

Description

Sleep to wakeup without radio with ESP.deepSleep(5000000, WAKE_RF_DISABLED);, but on wakeup start the radio manually WiFi.forceSleepWake() after satisfying some conditions. This is done to save more battery as using the radio is expensive.