ModularSensors > Modules > Supported Modems and Communication Modules > Espressif ESP8266 and ESP32

Espressif ESP8266 and ESP32 module

Introduction

These are handy, cheap, and very widely available WiFi and WiFi/Bluetooth modules. They use relatively little power amd can be put in a deep sleep mode to greatly reduce power draw. The types of sleep they can use are dependent on the breakout you have. If you're shopping for boards, I strongly recommend getting a breakout that exposes the deep sleep wake up (RSTB/DIO16) pin so that you can put the chip in its lowest power mode. When using the AT command firmware, it is not possible to purposefully enter light sleep mode. The module will enter light sleep on its own after some (undocumented) idle period. In my testing, I've never seen the module drop to the documented light sleep power levels regardless of the length of time idle.

DFRobot ESPBee

I don't actually recommend this module. It gets hot and eats power for no apparent reason. And the pin connections are a bit strange. But it is cheap and available. The pins are technically available to use the ESP's "light sleep" but I've never successfully gotten the module to actually enter light sleep mode. It always runs at full power draw. It's not possible to use deep sleep on the DFRobot bee.

Manufacturer Documentation

More information on the ESP8266 is here: https://www.espressif.com/en/products/socs/esp8266 And the page for the ESP32 is here: https://www.espressif.com/en/products/socs/esp32

Modem Constructor

EspressifESP8266(Stream* modemStream, int8_t powerPin, int8_t statusPin, int8_t modemResetPin, int8_t modemSleepRqPin, const char* ssid, const char* pwd, int8_t espSleepRqPin = -1, int8_t espStatusPin = -1)

Construct a new Espressif ESP8266 object.

Parameters
modemStream The Arduino stream instance for serial communication.
powerPin The digital pin number of the mcu pin controlling power to the modem (active HIGH). Should be set to a negative number if the modem should be continuously powered or the power cannot be controlled by the MCU.
statusPin The digital pin number of the mcu pin connected to the modem status output pin. Should be set to a negative number if the modem status pin cannot be read. This can be any DIO pin on the esp. It is only used in light sleep.
modemResetPin The digital pin number of the pin on the mcu attached the the hard or panic reset pin of the modem. Should be set to a negative number if the modem reset pin is not connected to the MCU. This is the ESP's RSTB/DIO16 pin.
modemSleepRqPin The digital pin number of a pin on the mcu used to request the modem enter its lowest possible power state. Should be set to a negative number if there is no pin usable for deep sleep modes or it is not accessible to the MCU. This can be any DIO pin on the esp. It is only used in light sleep.
ssid The wifi network ID.
pwd The wifi network password, assuming WPA2.
espSleepRqPin The DIO pin on the ESP8266 assigned to light sleep wake. This can be any DIO pin on the esp.
espStatusPin The DIO pin on the ESP8566 assigned to status indication. This can be any DIO pin on the esp.

The constuctor initializes all of the provided member variables, constructs a loggerModem parent class with the appropriate timing for the module, calls the constructor for a TinyGSM modem on the provided modemStream, and creates a TinyGSM Client linked to the modem.



Example Code

The ESP8266 is used in the menu a la carte example and the logging to ThingSpeak example.

// For almost anything based on the Espressif ESP8266 using the
// AT command firmware
#include <modems/EspressifESP8266.h>

// NOTE: Extra hardware and software serial ports are created in the "Settings
// for Additional Serial Ports" section
const int32_t modemBaud = 115200;  // Communication speed of the modem
// NOTE:  This baud rate too fast for an 8MHz board, like the Mayfly!  The
// module should be programmed to a slower baud rate or set to auto-baud using
// the AT+UART_CUR or AT+UART_DEF command.

// Modem Pins - Describe the physical pin connection of your modem to your board
// NOTE:  Use -1 for pins that do not apply
// Example pins here are for a DFRobot ESP8266 Bee with Mayfly
const int8_t modemVccPin     = -2;  // MCU pin controlling modem power
const int8_t modemStatusPin  = -1;  // MCU pin used to read modem status
const int8_t modemResetPin   = -1;  // MCU pin connected to modem reset pin
const int8_t modemSleepRqPin = 19;  // MCU pin for wake from light sleep
const int8_t modemLEDPin = redLED;  // MCU pin connected an LED to show modem
                                    // status
// Pins for light sleep on the ESP8266. For power savings, I recommend
// NOT using these if it's possible to use deep sleep.
const int8_t espSleepRqPin = 13;  // GPIO# ON THE ESP8266 to assign for light
                                  // sleep request
const int8_t espStatusPin = -1;   // GPIO# ON THE ESP8266 to assign for light
                                  // sleep status

// Network connection information
const char* wifiId  = "xxxxx";  // WiFi access point name
const char* wifiPwd = "xxxxx";  // WiFi password (WPA2)

// Create the modem object
EspressifESP8266 modemESP(&modemSerial, modemVccPin, modemStatusPin,
                          modemResetPin, modemSleepRqPin, wifiId, wifiPwd,
                          espSleepRqPin,
                          espStatusPin  // Optional arguments
);
// Create an extra reference to the modem by a generic name
EspressifESP8266 modem = modemESP;

Classes

class EspressifESP8266
The loggerModem subclass for any breakout of the Espressif ESP8266 wifi chip or ESP32 wifi/bluetooth chip that has been flashed with Espressif's AT command firmware.

Defines

#define TINY_GSM_MODEM_ESP8266
The modem type for the underlying TinyGSM library.
#define ESP8266_STATUS_LEVEL = HIGH
The loggerModem::_statusLevel.

It is not possible to get status from the ESP8266 in deep sleep mode - during deep sleep the pin state is undefined.

For cases where a pin is defined for light sleep mode, the Espressif documentation states:

since the system needs some time to wake up from light sleep, it is suggested that wait at least 5ms before sending next AT command.

The documentation doesn't say anything about the time before the pin reaches the expected level. The status level during light sleep is user selectable, this library sets it low for wake and high for sleep. Of course, despite being able to configure light sleep mode for the module, it's not actually possible to purposefully enter light sleep via AT commands, so we are dependent on the module deciding it's been idle long enough and entering sleep on its own. It is a terrible system. Use a deep-sleep with reset if possible.

#define ESP8266_STATUS_TIME_MS = 350
The loggerModem::_statusTime_ms.

It is not possible to get status from the ESP8266 in deep sleep mode - during deep sleep the pin state is undefined.

For cases where a pin is defined for light sleep mode, the Espressif documentation states:

since the system needs some time to wake up from light sleep, it is suggested that wait at least 5ms before sending next AT command.

The documentation doesn't say anything about the time before the pin reaches the expected level. The status level during light sleep is user selectable, this library sets it low for wake and high for sleep. Of course, despite being able to configure light sleep mode for the module, it's not actually possible to purposefully enter light sleep via AT commands, so we are dependent on the module deciding it's been idle long enough and entering sleep on its own. It is a terrible system. Use a deep-sleep with reset if possible.

#define ESP8266_RESET_LEVEL = LOW
The loggerModem::_resetLevel.

The ESP8266 is reset with a low pulse on pin 32. The reset time is undocumented but very fast - 1ms seems to be sufficient

#define ESP8266_RESET_PULSE_MS = 1
The loggerModem::_resetPulse_ms.

The ESP8266 is reset with a low pulse on pin 32. The reset time is undocumented but very fast - 1ms seems to be sufficient

#define ESP8266_WAKE_LEVEL = LOW
The loggerModem::_wakeLevel.

This light sleep wake level is user configurable on the ESP8266. This library uses a LOW level for wake.

#define ESP8266_WAKE_PULSE_MS = 0
The loggerModem::_wakePulse_ms.

This light sleep wake level is user configurable on the ESP8266. This library uses a LOW level for wake.

#define ESP8266_WAKE_DELAY_MS = 0
The loggerModem::_wakeDelayTime_ms.

The ESP8266 turns on when power is applied regardless of pin states. No further wake command is needed.

#define ESP8266_ATRESPONSE_TIME_MS = 350
The loggerModem::_max_atresponse_time_ms.

The serial response time after boot (via power on or reset) is undocumented for the ESP8266. Other users online estimate about 350ms.

The serial response time on waking from light sleep is 5ms.

#define ESP8266_DISCONNECT_TIME_MS = 500
The loggerModem::_disconnetTime_ms.

The disconnect time for the ESP8266 is not documented (and the status pin isn't valid) so this time is just an estimate.