The ESP8266 Arduino Core is a software framework that enables developers to program ESP8266 microcontrollers using the Arduino programming language and environment. This core provides a set of libraries and tools that simplify the development process for projects involving the ESP8266 chip. Here will discuss ESP8266 Arduino-Core Basics.
ESP8266 Arduino Core serves as a powerful tool for developing IoT (Internet of Things) applications, home automation systems, sensor networks, and more, using the ESP8266 microcontroller platform with the simplicity and flexibility of the Arduino ecosystem.
Read more: ESP8266 Arduino-Core – Basics
Call SDK Functions
- Required Hardware ESP8266 with Programmer (or) NodeMCU Dev Kit
- Required Software Tools Arduino IDE with ESP8266 Core
Code
/*
https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_arduino-core/
Tested By : Arun(20170219)
Example Name : AEW_CallSDKFunctions.ino
*/
/**
* TestEspApi by Max Vilimpoc
* This code is released to the public domain.
*
* Test out the Expressif ESP8266 Non-OS API, exhaustively trying out
* as many of the built-in functions as possible.
*
* Some of the code is based on examples in:
* "20A-ESP8266__RTOS_SDK__Programming Guide__EN_v1.3.0.pdf"
*/
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
// Set up output serial port (could be a SoftwareSerial
// if really wanted).
Stream& ehConsolePort(Serial);
// Wired to the blue LED on an ESP-01 board, active LOW.
//
// Cannot be used simultaneously with Serial.print/println()
// calls, as TX is wired to the same pin.
//
// UNLESS: You swap the TX pin using the alternate pinout.
const uint8_t LED_PIN = 1;
const char * const RST_REASONS[] =
{
"REASON_DEFAULT_RST",
"REASON_WDT_RST",
"REASON_EXCEPTION_RST",
"REASON_SOFT_WDT_RST",
"REASON_SOFT_RESTART",
"REASON_DEEP_SLEEP_AWAKE",
"REASON_EXT_SYS_RST"
};
const char * const FLASH_SIZE_MAP_NAMES[] =
{
"FLASH_SIZE_4M_MAP_256_256",
"FLASH_SIZE_2M",
"FLASH_SIZE_8M_MAP_512_512",
"FLASH_SIZE_16M_MAP_512_512",
"FLASH_SIZE_32M_MAP_512_512",
"FLASH_SIZE_16M_MAP_1024_1024",
"FLASH_SIZE_32M_MAP_1024_1024"
};
const char * const OP_MODE_NAMES[]
{
"NULL_MODE",
"STATION_MODE",
"SOFTAP_MODE",
"STATIONAP_MODE"
};
const char * const AUTH_MODE_NAMES[]
{
"AUTH_OPEN",
"AUTH_WEP",
"AUTH_WPA_PSK",
"AUTH_WPA2_PSK",
"AUTH_WPA_WPA2_PSK",
"AUTH_MAX"
};
const char * const PHY_MODE_NAMES[]
{
"",
"PHY_MODE_11B",
"PHY_MODE_11G",
"PHY_MODE_11N"
};
const char * const EVENT_NAMES[]
{
"EVENT_STAMODE_CONNECTED",
"EVENT_STAMODE_DISCONNECTED",
"EVENT_STAMODE_AUTHMODE_CHANGE",
"EVENT_STAMODE_GOT_IP",
"EVENT_SOFTAPMODE_STACONNECTED",
"EVENT_SOFTAPMODE_STADISCONNECTED",
"EVENT_MAX"
};
const char * const EVENT_REASONS[]
{
"",
"REASON_UNSPECIFIED",
"REASON_AUTH_EXPIRE",
"REASON_AUTH_LEAVE",
"REASON_ASSOC_EXPIRE",
"REASON_ASSOC_TOOMANY",
"REASON_NOT_AUTHED",
"REASON_NOT_ASSOCED",
"REASON_ASSOC_LEAVE",
"REASON_ASSOC_NOT_AUTHED",
"REASON_DISASSOC_PWRCAP_BAD",
"REASON_DISASSOC_SUPCHAN_BAD",
"REASON_IE_INVALID",
"REASON_MIC_FAILURE",
"REASON_4WAY_HANDSHAKE_TIMEOUT",
"REASON_GROUP_KEY_UPDATE_TIMEOUT",
"REASON_IE_IN_4WAY_DIFFERS",
"REASON_GROUP_CIPHER_INVALID",
"REASON_PAIRWISE_CIPHER_INVALID",
"REASON_AKMP_INVALID",
"REASON_UNSUPP_RSN_IE_VERSION",
"REASON_INVALID_RSN_IE_CAP",
"REASON_802_1X_AUTH_FAILED",
"REASON_CIPHER_SUITE_REJECTED",
};
const char * const EVENT_REASONS_200[]
{
"REASON_BEACON_TIMEOUT",
"REASON_NO_AP_FOUND"
};
void wifi_event_handler_cb(System_Event_t * event)
{
ehConsolePort.print(EVENT_NAMES[event->event]);
ehConsolePort.print(" (");
switch (event->event)
{
case EVENT_STAMODE_CONNECTED:
break;
case EVENT_STAMODE_DISCONNECTED:
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
break;
case EVENT_STAMODE_GOT_IP:
break;
case EVENT_SOFTAPMODE_STACONNECTED:
case EVENT_SOFTAPMODE_STADISCONNECTED:
{
char mac[32] = {0};
snprintf(mac, 32, MACSTR ", aid: %d" , MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid);
ehConsolePort.print(mac);
}
break;
}
ehConsolePort.println(")");
}
void print_softap_config(Stream & consolePort, softap_config const& config)
{
consolePort.println();
consolePort.println(F("SoftAP Configuration"));
consolePort.println(F("--------------------"));
consolePort.print(F("ssid: "));
consolePort.println((char *) config.ssid);
consolePort.print(F("password: "));
consolePort.println((char *) config.password);
consolePort.print(F("ssid_len: "));
consolePort.println(config.ssid_len);
consolePort.print(F("channel: "));
consolePort.println(config.channel);
consolePort.print(F("authmode: "));
consolePort.println(AUTH_MODE_NAMES[config.authmode]);
consolePort.print(F("ssid_hidden: "));
consolePort.println(config.ssid_hidden);
consolePort.print(F("max_connection: "));
consolePort.println(config.max_connection);
consolePort.print(F("beacon_interval: "));
consolePort.print(config.beacon_interval);
consolePort.println("ms");
consolePort.println(F("--------------------"));
consolePort.println();
}
void print_system_info(Stream & consolePort)
{
const rst_info * resetInfo = system_get_rst_info();
consolePort.print(F("system_get_rst_info() reset reason: "));
consolePort.println(RST_REASONS[resetInfo->reason]);
consolePort.print(F("system_get_free_heap_size(): "));
consolePort.println(system_get_free_heap_size());
consolePort.print(F("system_get_os_print(): "));
consolePort.println(system_get_os_print());
system_set_os_print(1);
consolePort.print(F("system_get_os_print(): "));
consolePort.println(system_get_os_print());
system_print_meminfo();
consolePort.print(F("system_get_chip_id(): 0x"));
consolePort.println(system_get_chip_id(), HEX);
consolePort.print(F("system_get_sdk_version(): "));
consolePort.println(system_get_sdk_version());
consolePort.print(F("system_get_boot_version(): "));
consolePort.println(system_get_boot_version());
consolePort.print(F("system_get_userbin_addr(): 0x"));
consolePort.println(system_get_userbin_addr(), HEX);
consolePort.print(F("system_get_boot_mode(): "));
consolePort.println(system_get_boot_mode() == 0 ? F("SYS_BOOT_ENHANCE_MODE") : F("SYS_BOOT_NORMAL_MODE"));
consolePort.print(F("system_get_cpu_freq(): "));
consolePort.println(system_get_cpu_freq());
consolePort.print(F("system_get_flash_size_map(): "));
consolePort.println(FLASH_SIZE_MAP_NAMES[system_get_flash_size_map()]);
}
void print_wifi_general(Stream & consolePort)
{
consolePort.print(F("wifi_get_channel(): "));
consolePort.println(wifi_get_channel());
consolePort.print(F("wifi_get_phy_mode(): "));
consolePort.println(PHY_MODE_NAMES[wifi_get_phy_mode()]);
}
void secure_softap_config(softap_config * config, const char * ssid, const char * password)
{
size_t ssidLen = strlen(ssid) < sizeof(config->ssid) ? strlen(ssid) : sizeof(config->ssid);
size_t passwordLen = strlen(password) < sizeof(config->password) ? strlen(password) : sizeof(config->password);
memset(config->ssid, 0, sizeof(config->ssid));
memcpy(config->ssid, ssid, ssidLen);
memset(config->password, 0, sizeof(config->password));
memcpy(config->password, password, passwordLen);
config->ssid_len = ssidLen;
config->channel = 1;
config->authmode = AUTH_WPA2_PSK;
// config->ssid_hidden = 1;
config->max_connection = 4;
// config->beacon_interval = 1000;
}
void setup()
{
// Reuse default Serial port rate, so the bootloader
// messages are also readable.
Serial.begin(74880);
// Try pushing frequency to 160MHz.
system_update_cpu_freq(SYS_CPU_160MHZ);
Serial.println();
Serial.println(F("ESP starting."));
// System usually boots up in about 200ms.
Serial.print(F("system_get_time(): "));
Serial.println(system_get_time());
// set_event_handler_cb_stream(Serial);
wifi_set_event_handler_cb(wifi_event_handler_cb);
print_system_info(Serial);
Serial.print(F("wifi_get_opmode(): "));
Serial.print(wifi_get_opmode());
Serial.print(F(" - "));
Serial.println(OP_MODE_NAMES[wifi_get_opmode()]);
Serial.print(F("wifi_get_opmode_default(): "));
Serial.print(wifi_get_opmode_default());
Serial.print(F(" - "));
Serial.println(OP_MODE_NAMES[wifi_get_opmode_default()]);
Serial.print(F("wifi_get_broadcast_if(): "));
Serial.println(wifi_get_broadcast_if());
softap_config config;
wifi_softap_get_config(&config);
secure_softap_config(&config, "TestAP", "testtesttest");
wifi_softap_set_config(&config);
print_softap_config(Serial, config);
print_wifi_general(Serial);
// This doesn't work on an ESP-01.
// wifi_set_sleep_type(LIGHT_SLEEP_T);
// Try this dirty little thing.
// Doesn't work because ESP-01 module doesn't link XPD_DCDC -> RST.
// ESP.deepSleep(15000);
}
void loop()
{
Serial.print(F("system_get_time(): "));
Serial.println(system_get_time());
delay(1000);
}
Result
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
wifi_station_get_hostname:
Test ESP8266 API
ESP8266 Arduino-Core Basics
Code
/*
https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_arduino-core/esp8266_arduino-core-basics/
Tested By : Arun(20170219)
Example Name : AEW_TestEspAPI.ino
*/
/**
* TestEspApi by Max Vilimpoc
* This code is released to the public domain.
*
* Test out the Expressif ESP8266 Non-OS API, exhaustively trying out
* as many of the built-in functions as possible.
*
* Some of the code is based on examples in:
* "20A-ESP8266__RTOS_SDK__Programming Guide__EN_v1.3.0.pdf"
*/
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
// Set up output serial port (could be a SoftwareSerial
// if really wanted).
Stream& ehConsolePort(Serial);
// Wired to the blue LED on an ESP-01 board, active LOW.
//
// Cannot be used simultaneously with Serial.print/println()
// calls, as TX is wired to the same pin.
//
// UNLESS: You swap the TX pin using the alternate pinout.
const uint8_t LED_PIN = 1;
const char * const RST_REASONS[] =
{
"REASON_DEFAULT_RST",
"REASON_WDT_RST",
"REASON_EXCEPTION_RST",
"REASON_SOFT_WDT_RST",
"REASON_SOFT_RESTART",
"REASON_DEEP_SLEEP_AWAKE",
"REASON_EXT_SYS_RST"
};
const char * const FLASH_SIZE_MAP_NAMES[] =
{
"FLASH_SIZE_4M_MAP_256_256",
"FLASH_SIZE_2M",
"FLASH_SIZE_8M_MAP_512_512",
"FLASH_SIZE_16M_MAP_512_512",
"FLASH_SIZE_32M_MAP_512_512",
"FLASH_SIZE_16M_MAP_1024_1024",
"FLASH_SIZE_32M_MAP_1024_1024"
};
const char * const OP_MODE_NAMES[]
{
"NULL_MODE",
"STATION_MODE",
"SOFTAP_MODE",
"STATIONAP_MODE"
};
const char * const AUTH_MODE_NAMES[]
{
"AUTH_OPEN",
"AUTH_WEP",
"AUTH_WPA_PSK",
"AUTH_WPA2_PSK",
"AUTH_WPA_WPA2_PSK",
"AUTH_MAX"
};
const char * const PHY_MODE_NAMES[]
{
"",
"PHY_MODE_11B",
"PHY_MODE_11G",
"PHY_MODE_11N"
};
const char * const EVENT_NAMES[]
{
"EVENT_STAMODE_CONNECTED",
"EVENT_STAMODE_DISCONNECTED",
"EVENT_STAMODE_AUTHMODE_CHANGE",
"EVENT_STAMODE_GOT_IP",
"EVENT_SOFTAPMODE_STACONNECTED",
"EVENT_SOFTAPMODE_STADISCONNECTED",
"EVENT_MAX"
};
const char * const EVENT_REASONS[]
{
"",
"REASON_UNSPECIFIED",
"REASON_AUTH_EXPIRE",
"REASON_AUTH_LEAVE",
"REASON_ASSOC_EXPIRE",
"REASON_ASSOC_TOOMANY",
"REASON_NOT_AUTHED",
"REASON_NOT_ASSOCED",
"REASON_ASSOC_LEAVE",
"REASON_ASSOC_NOT_AUTHED",
"REASON_DISASSOC_PWRCAP_BAD",
"REASON_DISASSOC_SUPCHAN_BAD",
"REASON_IE_INVALID",
"REASON_MIC_FAILURE",
"REASON_4WAY_HANDSHAKE_TIMEOUT",
"REASON_GROUP_KEY_UPDATE_TIMEOUT",
"REASON_IE_IN_4WAY_DIFFERS",
"REASON_GROUP_CIPHER_INVALID",
"REASON_PAIRWISE_CIPHER_INVALID",
"REASON_AKMP_INVALID",
"REASON_UNSUPP_RSN_IE_VERSION",
"REASON_INVALID_RSN_IE_CAP",
"REASON_802_1X_AUTH_FAILED",
"REASON_CIPHER_SUITE_REJECTED",
};
const char * const EVENT_REASONS_200[]
{
"REASON_BEACON_TIMEOUT",
"REASON_NO_AP_FOUND"
};
void wifi_event_handler_cb(System_Event_t * event)
{
ehConsolePort.print(EVENT_NAMES[event->event]);
ehConsolePort.print(" (");
switch (event->event)
{
case EVENT_STAMODE_CONNECTED:
break;
case EVENT_STAMODE_DISCONNECTED:
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
break;
case EVENT_STAMODE_GOT_IP:
break;
case EVENT_SOFTAPMODE_STACONNECTED:
case EVENT_SOFTAPMODE_STADISCONNECTED:
{
char mac[32] = {0};
snprintf(mac, 32, MACSTR ", aid: %d" , MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid);
ehConsolePort.print(mac);
}
break;
}
ehConsolePort.println(")");
}
void print_softap_config(Stream & consolePort, softap_config const& config)
{
consolePort.println();
consolePort.println(F("SoftAP Configuration"));
consolePort.println(F("--------------------"));
consolePort.print(F("ssid: "));
consolePort.println((char *) config.ssid);
consolePort.print(F("password: "));
consolePort.println((char *) config.password);
consolePort.print(F("ssid_len: "));
consolePort.println(config.ssid_len);
consolePort.print(F("channel: "));
consolePort.println(config.channel);
consolePort.print(F("authmode: "));
consolePort.println(AUTH_MODE_NAMES[config.authmode]);
consolePort.print(F("ssid_hidden: "));
consolePort.println(config.ssid_hidden);
consolePort.print(F("max_connection: "));
consolePort.println(config.max_connection);
consolePort.print(F("beacon_interval: "));
consolePort.print(config.beacon_interval);
consolePort.println("ms");
consolePort.println(F("--------------------"));
consolePort.println();
}
void print_system_info(Stream & consolePort)
{
const rst_info * resetInfo = system_get_rst_info();
consolePort.print(F("system_get_rst_info() reset reason: "));
consolePort.println(RST_REASONS[resetInfo->reason]);
consolePort.print(F("system_get_free_heap_size(): "));
consolePort.println(system_get_free_heap_size());
consolePort.print(F("system_get_os_print(): "));
consolePort.println(system_get_os_print());
system_set_os_print(1);
consolePort.print(F("system_get_os_print(): "));
consolePort.println(system_get_os_print());
system_print_meminfo();
consolePort.print(F("system_get_chip_id(): 0x"));
consolePort.println(system_get_chip_id(), HEX);
consolePort.print(F("system_get_sdk_version(): "));
consolePort.println(system_get_sdk_version());
consolePort.print(F("system_get_boot_version(): "));
consolePort.println(system_get_boot_version());
consolePort.print(F("system_get_userbin_addr(): 0x"));
consolePort.println(system_get_userbin_addr(), HEX);
consolePort.print(F("system_get_boot_mode(): "));
consolePort.println(system_get_boot_mode() == 0 ? F("SYS_BOOT_ENHANCE_MODE") : F("SYS_BOOT_NORMAL_MODE"));
consolePort.print(F("system_get_cpu_freq(): "));
consolePort.println(system_get_cpu_freq());
consolePort.print(F("system_get_flash_size_map(): "));
consolePort.println(FLASH_SIZE_MAP_NAMES[system_get_flash_size_map()]);
}
void print_wifi_general(Stream & consolePort)
{
consolePort.print(F("wifi_get_channel(): "));
consolePort.println(wifi_get_channel());
consolePort.print(F("wifi_get_phy_mode(): "));
consolePort.println(PHY_MODE_NAMES[wifi_get_phy_mode()]);
}
void secure_softap_config(softap_config * config, const char * ssid, const char * password)
{
size_t ssidLen = strlen(ssid) < sizeof(config->ssid) ? strlen(ssid) : sizeof(config->ssid);
size_t passwordLen = strlen(password) < sizeof(config->password) ? strlen(password) : sizeof(config->password);
memset(config->ssid, 0, sizeof(config->ssid));
memcpy(config->ssid, ssid, ssidLen);
memset(config->password, 0, sizeof(config->password));
memcpy(config->password, password, passwordLen);
config->ssid_len = ssidLen;
config->channel = 1;
config->authmode = AUTH_WPA2_PSK;
// config->ssid_hidden = 1;
config->max_connection = 4;
// config->beacon_interval = 1000;
}
void setup()
{
// Reuse default Serial port rate, so the bootloader
// messages are also readable.
Serial.begin(74880);
// Try pushing frequency to 160MHz.
system_update_cpu_freq(SYS_CPU_160MHZ);
Serial.println();
Serial.println(F("ESP starting."));
// System usually boots up in about 200ms.
Serial.print(F("system_get_time(): "));
Serial.println(system_get_time());
// set_event_handler_cb_stream(Serial);
wifi_set_event_handler_cb(wifi_event_handler_cb);
print_system_info(Serial);
Serial.print(F("wifi_get_opmode(): "));
Serial.print(wifi_get_opmode());
Serial.print(F(" - "));
Serial.println(OP_MODE_NAMES[wifi_get_opmode()]);
Serial.print(F("wifi_get_opmode_default(): "));
Serial.print(wifi_get_opmode_default());
Serial.print(F(" - "));
Serial.println(OP_MODE_NAMES[wifi_get_opmode_default()]);
Serial.print(F("wifi_get_broadcast_if(): "));
Serial.println(wifi_get_broadcast_if());
softap_config config;
wifi_softap_get_config(&config);
secure_softap_config(&config, "TestAP", "testtesttest");
wifi_softap_set_config(&config);
print_softap_config(Serial, config);
print_wifi_general(Serial);
// This doesn't work on an ESP-01.
// wifi_set_sleep_type(LIGHT_SLEEP_T);
// Try this dirty little thing.
// Doesn't work because ESP-01 module doesn't link XPD_DCDC -> RST.
// ESP.deepSleep(15000);
}
void loop()
{
Serial.print(F("system_get_time(): "));
Serial.println(system_get_time());
delay(1000);
Result
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
¡H¨MA�starting.
system_get_time(): 282250
system_get_rst_info() reset reason: REASON_EXT_SYS_RST
system_get_free_heap_size(): 49016
system_get_os_print(): 0
system_get_os_print(): 1
system_get_chip_id(): 0xD98DD7
system_get_sdk_version(): 1.5.3(aec24ac9)
system_get_boot_version(): 5
system_get_userbin_addr(): 0x1000
system_get_boot_mode(): SYS_BOOT_NORMAL_MODE
system_get_cpu_freq(): 160
system_get_flash_size_map(): FLASH_SIZE_4M_MAP_256_256
wifi_get_opmode(): 2 - SOFTAP_MODE
wifi_get_opmode_default(): 2 - SOFTAP_MODE
wifi_get_broadcast_if(): 2
SoftAP Configuration
--------------------
ssid: TestAP
password: testtesttest
ssid_len: 6
channel: 1
authmode: AUTH_WPA2_PSK
ssid_hidden: 0
max_connection: 4
beacon_interval: 11298ms
--------------------
wifi_get_channel(): 1
wifi_get_phy_mode(): PHY_MODE_11N
system_get_time(): 386190
system_get_time(): 1387316
system_get_time(): 2387360
system_get_time(): 3387405
system_get_time(): 4387450
system_get_time(): 5387495
system_get_time(): 6387539
RTC User Memory
Code
/*
https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_arduino-core/esp8266_arduino-core-basics/
Tested By : Arun(20170219)
Example Name : AEW_RTCUserMemory.ino
*/
// Example: Storing struct data in RTC user rtcDataory
//
// Struct data with the maximum size of 512 bytes can be stored
// in the RTC user rtcDataory using the ESP-specifc APIs.
// The stored data can be retained between deep sleep cycles.
// However, the data might be lost after power cycling the ESP8266.
//
// This example uses deep sleep mode, so connect GPIO16 and RST
// pins before running it.
//
// Created Mar 30, 2016 by Macro Yau.
//
// This example code is in the public domain.
// CRC function used to ensure data validity
uint32_t calculateCRC32(const uint8_t *data, size_t length);
// helper function to dump memory contents as hex
void printMemory();
// Structure which will be stored in RTC memory.
// First field is CRC32, which is calculated based on the
// rest of structure contents.
// Any fields can go after CRC32.
// We use byte array as an example.
struct {
uint32_t crc32;
byte data[508];
} rtcData;
void setup() {
Serial.begin(115200);
Serial.println();
delay(1000);
// Read struct from RTC memory
if (ESP.rtcUserMemoryRead(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
Serial.println("Read: ");
printMemory();
Serial.println();
uint32_t crcOfData = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
Serial.print("CRC32 of data: ");
Serial.println(crcOfData, HEX);
Serial.print("CRC32 read from RTC: ");
Serial.println(rtcData.crc32, HEX);
if (crcOfData != rtcData.crc32) {
Serial.println("CRC32 in RTC memory doesn't match CRC32 of data. Data is probably invalid!");
}
else {
Serial.println("CRC32 check ok, data is probably valid.");
}
}
// Generate new data set for the struct
for (int i = 0; i < sizeof(rtcData); i++) {
rtcData.data[i] = random(0, 128);
}
// Update CRC32 of data
rtcData.crc32 = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
// Write struct to RTC memory
if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
Serial.println("Write: ");
printMemory();
Serial.println();
}
Serial.println("Going into deep sleep for 5 seconds");
ESP.deepSleep(5e6);
}
void loop() {
}
uint32_t calculateCRC32(const uint8_t *data, size_t length)
{
uint32_t crc = 0xffffffff;
while (length--) {
uint8_t c = *data++;
for (uint32_t i = 0x80; i > 0; i >>= 1) {
bool bit = crc & 0x80000000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x04c11db7;
}
}
}
return crc;
}
void printMemory() {
char buf[3];
for (int i = 0; i < sizeof(rtcData); i++) {
sprintf(buf, "%02X", rtcData.data[i]);
Serial.print(buf);
if ((i + 1) % 32 == 0) {
Serial.println();
}
else {
Serial.print(" ");
}
}
Serial.println();
}
Result
3F 34 14 6D 36 50 4F 51 7B 57 20 64 6A 4E 20 12 3C 2E 37 1E 23 1E 4F 28 23 16 1D 3F 4E 6F 36 3D
7E 5E 60 05 7D 37 3E 4B 53 40 74 64 09 0A 40 13 3E 05 09 5D 49 63 1C 15 37 50 76 02 5D 75 10 07
4C 2A 73 65 49 78 6E 3C 02 50 1D 6D 0D 16 77 15 10 48 31 02 57 1A 60 3B 41 16 6A 78 3F 0B 1D 2D
39 58 3D 58 5E 72 06 7A 79 52 24 6A 77 68 38 4D 5A 02 43 09 4A 75 4B 04 78 3B 52 7C 20 34 60 1F
51 5F 44 08 26 04 3B 1E 77 73 13 49 4B 11 3A 06 42 1E 59 6D 63 4D 1F 2D 2E 3D 51 00 77 50 28 46
44 78 7A 07 42 21 73 75 44 60 7C 68 23 5B 44 31 47 5E 73 67 69 47 2A 0A 34 05 14 62 61 13 07 02
77 3B 3F 1F 3B 75 77 23 64 34 7D 38 41 00 4E 73 07 5E 7B 02 69 37 65 52 3D 3B 09 10 3B 21 44 36
55 4E 45 20 19 25 0D 38 37 37 66 4A 7B 48 64 45 04 4E 3E 28 55 6F 6E 3F 6F 62 0D 58 71 04 49 53
3F 60 37 22 06 52 4B 5A 56 20 07 22 21 0C 17 32 33 5B 5F 37 06 26 2C 19 6C 25 4E 24 0F 45 0B 3E
5A 4A 03 36 2A 3F 34 78 44 12 42 07 3D 3F 7D 42 1C 73 1F 3D 76 1B 5B 0C 3A 07 1E 64 36 73 6E 77
1A 21 40 76 03 01 16 1F 3E 31 0D 6C 2B 72 7A 24 49 15 50 7C 6A 08 16 72 3F 36 5C 7D 29 15 1B 31
06 31 7A 07 1E 46 28 51 68 4F 5C 1F 1C 33 55 2E 54 0D 24 3F 01 47 32 2B 6B 22 1D 44 2E 2C 65 10
44 37 4A 52 3F 11 7B 3D 68 7B 23 17 42 1B 43 23 57 31 7F 31 7D 21 15 0A 2C 2D 16 21 5C 14 17 16
6E 5E 7D 70 2E 67 79 75 30 34 40 2A 61 3A 74 43 0A 7A 12 7F 7D 48 51 52 22 43 4D 6F 08 4C 65 46
7E 07 76 29 29 59 64 3D 6E 64 5B 61 61 34 7B 0B 02 0D 1B 05 66 49 1D 4A 26 29 1B 4D 7C 03 64 34
0F 2F 73 0D 18 20 74 30 02 00 30 4C 34 62 36 25 42 25 65 52 3E 73 0B 3B 15 31 33 29 00 00 00 00
CRC32 of data: 7927D498
CRC32 read from RTC: 7927D498
CRC32 check ok, data is probably valid.
Write:
4A 64 61 6D 6E 6C 29 0D 59 02 2A 59 0F 3F 76 5E 0F 3E 21 6C 0B 3F 6A 2E 67 4E 6D 05 3F 51 18 50
61 4C 4C 18 0D 0C 0A 07 2D 7E 6D 7F 4E 15 0A 61 15 6D 4F 0E 2B 35 70 45 61 2D 32 48 58 56 60 64
01 7B 3B 45 32 5F 64 7F 71 4C 61 43 00 48 0C 1E 42 76 51 5B 2C 3B 0C 39 68 5F 75 21 25 6C 76 5D
6E 4C 45 3A 12 44 23 25 4E 32 37 4C 44 01 51 62 71 34 53 5F 0C 39 49 67 03 57 13 0F 42 23 45 5C
7B 7F 47 76 73 26 45 65 3E 0C 02 10 20 28 53 68 1B 02 63 3F 26 3D 39 6E 70 09 1C 48 2F 0E 4F 49
1A 04 6F 15 3B 34 5C 76 20 54 02 20 5E 1A 47 38 14 31 69 07 02 60 7E 2F 79 34 43 4B 75 3D 55 3C
41 76 7C 69 3A 74 48 69 75 7B 04 29 71 27 08 04 6D 13 08 70 34 4E 26 43 10 02 1C 0B 57 22 2F 74
0F 0B 59 46 47 35 1B 45 0B 14 32 24 11 29 2E 29 6F 50 4D 75 31 68 4E 3B 00 1D 10 41 0C 2C 55 23
5C 7C 59 25 09 1F 37 56 21 1A 0D 12 56 6B 63 28 2B 75 06 5B 72 56 61 04 68 64 5F 2E 12 13 78 04
3C 43 10 18 63 37 63 74 37 6D 44 22 3D 67 75 68 53 24 03 7F 20 08 7A 59 5B 04 5F 35 4F 42 1E 64
6F 3F 70 54 06 3B 17 4B 1A 70 36 38 38 5D 01 7A 3B 60 02 7D 14 3C 4D 16 3F 30 3A 73 28 1F 3C 0A
0F 00 56 27 73 55 2C 77 21 59 68 0A 68 72 75 76 32 01 2E 0D 04 3E 37 67 75 04 5E 33 13 17 1A 24
1B 59 18 0E 50 41 79 4D 06 7E 4C 70 48 46 37 3D 45 01 6B 0D 3E 64 2E 40 3C 26 7F 04 15 7F 0D 26
66 27 66 53 15 0D 16 6B 02 3E 67 17 60 42 6A 20 78 16 21 75 61 60 50 05 02 6D 4D 1B 1E 50 7B 33
7F 0E 30 0A 6F 16 6E 49 28 53 19 14 11 11 30 56 44 21 3A 55 74 21 47 17 63 1B 28 44 0A 60 53 57
3B 4E 5F 5E 3B 50 04 42 24 4F 31 72 71 35 24 15 2A 4B 18 16 0F 5D 43 5B 60 4F 57 6D 02 69 5D 6C
Going into deep sleep for 5 seconds
Result
Flash real id: 001640E0
Flash real size: 4194304
Flash ide size: 524288
Flash ide speed: 40000000
Flash ide mode: DIO
Flash Chip configuration wrong!
Check Flash Configuration
This Arduino sketch tests whether the ESP8266 microcontroller’s hardware configuration matches the EEPROM settings of the Integrated Development Environment (IDE).
Here’s a breakdown of what the code does:
- Setup Function: The
setup
function is executed once when the microcontroller starts up. In this sketch, it initializes serial communication with a baud rate of 115200.
- Loop Function: The
loop
function is executed repeatedly as long as the microcontroller is powered on. Inside the loop, it performs the following actions:ESP.getFlashChipRealSize()
: Retrieves the actual size of the flash memory chip.ESP.getFlashChipSize()
: Retrieves the size of the flash memory chip as defined in the IDE settings.ESP.getFlashChipMode()
: Retrieves the flash mode of the chip as defined in the IDE settings.
The sketch then prints out the retrieved values via serial communication, along with additional information about the flash chip configuration.
- If the actual size of the flash memory chip (
realSize
) differs from the size defined in the IDE settings (ideSize
), it indicates a configuration mismatch, and a corresponding error message is printed.
- If the sizes match, the sketch prints a message indicating that the flash chip configuration is correct.
- Delay: After printing the flash chip configuration, the loop waits for 5 seconds before repeating the process.
This sketch is useful for verifying that the EEPROM settings in the IDE match the actual hardware configuration of the ESP8266 chip. It can help troubleshoot issues related to flash memory configuration mismatches.
Code
/*
https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_arduino-core/esp8266_arduino-core-basics/
Tested By : Arun(20170219)
Example Name : AEW_CheckFlashConfiguration.ino
ESP8266 CheckFlashConfig by Markus Sattler
This sketch tests if the EEPROM settings of the IDE match to the Hardware
*/
void setup(void) {
Serial.begin(115200);
}
void loop() {
uint32_t realSize = ESP.getFlashChipRealSize();
uint32_t ideSize = ESP.getFlashChipSize();
FlashMode_t ideMode = ESP.getFlashChipMode();
Serial.printf("Flash real id: %08X\n", ESP.getFlashChipId());
Serial.printf("Flash real size: %u\n\n", realSize);
Serial.printf("Flash ide size: %u\n", ideSize);
Serial.printf("Flash ide speed: %u\n", ESP.getFlashChipSpeed());
Serial.printf("Flash ide mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
if(ideSize != realSize) {
Serial.println("Flash Chip configuration wrong!\n");
} else {
Serial.println("Flash Chip configuration ok.\n");
}
delay(5000);
}
Hits (since 2024-Jan-26) - 732
You must be logged in to post a comment.