ESP32-C3 continuously reset

Hi,

I’m trying to connect the Adafruit QtPy ESP32-C3 to AWS services through the MQTT protocol
It’s the first time I have used an ESP32 but I have plenty of experience with PlatformIO and Atmel AVR.
I can connect to WiFi and I can also establish a connection with the server and send the first message. However, after these actions, the board reset and starts again.
I’m powering through USB C charger so I don’t think it’s a power issue. I tried to change platformio.ini configuration with various suggestions from the web with no luck. For example filesystem, flash mode, and partition. This last one has some effect on the behaviour but only worse.

An interesting fact is that if I use the example from AWS the code works. I only added a sleep condition and wrapped all connectivity instructions within a class to have a more organised code.

This is the output I get:

Connecting to AWS IOT
Connection Established
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x40381846
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x38c
load:0x403ce000,len:0x6ac
load:0x403d0000,len:0x2464
SHA-256 comparison failed:
Calculated: b080eacadd0b63e0211a4f0e60905e026699184995bb6e1528c6f73d2bd5875b
Expected: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Attempting to boot anyway...
entry 0x403ce000
Connecting to Wi-Fi
....
Connecting to AWS IOT
Connection Established
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x40381846
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x38c
load:0x403ce000,len:0x6ac
load:0x403d0000,len:0x2464
SHA-256 comparison failed:
Calculated: b080eacadd0b63e0211a4f0e60905e026699184995bb6e1528c6f73d2bd5875b
Expected: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Attempting to boot anyway...
entry 0x403ce000
Connecting to Wi-Fi

The code never goes after the first message to the server and reset (RTC_SW_SYS_RST)
Sometimes it does not even connect to WiFi for minutes and generally, it takes 10+seconds which I think it’s quite a lot. The board is 2 metres away from the router.
This is my code:

#include <Arduino.h>
// #include "secrets.h"
#include <IoTConnection.h>

#define SLEEPTIME_MINUTES 1
#define SLEEPTIME_MICROSECOND SLEEPTIME_MINUTES*60*1000000

IoTConnection iot_conn = IoTConnection();


void setup() {
  Serial.begin(11520);
  iot_conn.startConnection();
  if(iot_conn.connected()) Serial.print("Connection Established\n");
}

void loop() {
  // iot_conn.startConnection();

  iot_conn.publishLog("ESP32 Connected");

  iot_conn.transmit();
  delay(5000);
  Serial.print("got here\n");
  iot_conn.publishLog("got here");

  // Power Down
  iot_conn.closeConnection();
  Serial.print("Connection Closed\n");
  Serial.print("---> GOING to SLEEP\n");
  esp_deep_sleep(SLEEPTIME_MICROSECOND);
}

// IoTConnection.cpp

IoTConnection::IoTConnection(/* args */)
{
   net = WiFiClientSecure();
   client = MQTTClient(256);
}



bool IoTConnection::startConnection()
{

   WiFi.mode(WIFI_STA);
   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

   Serial.println("Connecting to Wi-Fi");

   while (WiFi.status() != WL_CONNECTED){
      delay(500);
      Serial.print(".");
   }
   Serial.print("\n");
   

   // Configure WiFiClientSecure to use the AWS IoT device credentials
   net.setCACert(AWS_CERT_CA);
   net.setCertificate(AWS_CERT_CRT);
   net.setPrivateKey(AWS_CERT_PRIVATE);


   // Connect to the MQTT broker on the AWS endpoint we defined earlier
   client.begin(AWS_IOT_ENDPOINT, 8883, net);

   // Create a message handler
   // client.onMessage(messageHandler);

   Serial.print("Connecting to AWS IOT\n");

   while (!client.connect(THINGNAME)) {
      Serial.print(".");
      delay(100);
   }

   if(!client.connected()){
      Serial.println("AWS IoT Timeout!");
      return false;
   }

   // Retain the session

   // client.setCleanSession(false);

   // Subscribe to a topic with Quality of Service 1 (QoS)
   // client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC, 1);
   client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

   // Serial.println("AWS IoT Connected!");

   return true;
}

void IoTConnection::closeConnection()
{
   client.disconnect();
   
   net.stop();

   WiFi.disconnect(true);

}

I tried compiling with Arduino IDE and removing every piece of code but starts working properly only when there is nothing useful left but only print to serial.

I probably spent 10+ hours researching on the web and testing and I think now I need some help.
Thanks a lot

You may be running in ESP32-C3 Preferences and NVS storage not work - ESP32 Forum?

Have you tried adding board_build.flash_mode = qio to the platformio.ini per docs?

Yes I did. Same result
Also tried other flash modes.
The SHA-256 comparison fail is solved using a different partition table (I tried multiple csv files) but this doesn’t solve the reset problem (maybe I’m not using the partition correctly)

This is what I get from your suggestion:

Connecting to Wi-Fi
..
Connecting to AWS IOT
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x40381846
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x438
load:0x403ce000,len:0x90c
load:0x403d0000,len:0x2358
SHA-256 comparison failed:
Calculated: a9753a4fc647c6545c1b919ef08db429130a48592727edca270f1e5a3da0d0a9
Expected: 3bf6ef2cf3b9eefcd4b3c70cc5d1ce5138292d101a5cb1d5db6fbebf081b0a19
Attempting to boot anyway...
entry 0x403ce000

I just noted that the reset message I reported above says mode:DIO despite the flash_mode being defined as qio.
Is that ok?

Actually even DOUT should be used, not dio or qout

https://github.com/espressif/arduino-esp32/blob/master/boards.txt#L7407-L7414

which is also the board default

, so please remove any previous board_build... lines in the platformio.ini.

And the logic at

is supposed to burn the right bootloader.

Can you post the logs of a Advanced → Verbose Upload so that we can have a look at what it flashes?

I removed the flash mode parameter from platformio.ini
here it is verbose upload:

> Executing task: platformio run --verbose --target upload --environment adafruit_qtpy_esp32c3 <

Processing adafruit_qtpy_esp32c3 (platform: espressif32; board: adafruit_qtpy_esp32c3; framework: arduino; monitor_speed: 11520; lib_deps: 256dpi/MQTT@^2.5.0, bblanchon/ArduinoJson@^6.19.4)
--------------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/adafruit_qtpy_esp32c3.html
PLATFORM: Espressif 32 (4.4.0) > Adafruit QT Py ESP32-C3
HARDWARE: ESP32C3 160MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 @ 3.20003.220626 (2.0.3) 
 - tool-esptoolpy @ 1.30300.0 (3.3.0) 
 - tool-mkfatfs @ 2.0.1 
 - tool-mklittlefs @ 1.203.210628 (2.3) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 42 compatible libraries
Scanning dependencies...
Dependency Graph
|-- MQTT @ 2.5.0 (License: Unknown, Path: /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/.pio/libdeps/adafruit_qtpy_esp32c3/MQTT)
|-- ArduinoJson @ 6.19.4 (License: Unknown, Path: /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/.pio/libdeps/adafruit_qtpy_esp32c3/ArduinoJson)
|-- IoTConnection (License: Unknown, Path: /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/lib/IoTConnection)
|   |-- WiFi @ 2.0.0 (License: Unknown, Path: /Users/ridoluc/.platformio/packages/framework-arduinoespressif32/libraries/WiFi)
|   |-- ArduinoJson @ 6.19.4 (License: Unknown, Path: /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/.pio/libdeps/adafruit_qtpy_esp32c3/ArduinoJson)
|   |-- MQTT @ 2.5.0 (License: Unknown, Path: /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/.pio/libdeps/adafruit_qtpy_esp32c3/MQTT)
|   |-- WiFiClientSecure @ 2.0.0 (License: Unknown, Path: /Users/ridoluc/.platformio/packages/framework-arduinoespressif32/libraries/WiFiClientSecure)
|   |   |-- WiFi @ 2.0.0 (License: Unknown, Path: /Users/ridoluc/.platformio/packages/framework-arduinoespressif32/libraries/WiFi)
Building in release mode
<lambda>(["checkprogsize"], [".pio/build/adafruit_qtpy_esp32c3/firmware.elf"])
MethodWrapper(["checkprogsize"], [".pio/build/adafruit_qtpy_esp32c3/firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  10.5% (used 34452 bytes from 327680 bytes)
Flash: [======    ]  59.9% (used 785316 bytes from 1310720 bytes)
.pio/build/adafruit_qtpy_esp32c3/firmware.elf  :
section                    size         addr
.rtc.text                    16   1342177280
.rtc.force_fast               0   1342177296
.rtc.data                    16   1342177296
.rtc_noinit                   0   1342177312
.rtc.force_slow               0   1342177312
.iram0.text               55064   1077411840
.dram0.dummy              55296   1070071808
.dram0.data               12964   1070127104
.noinit                       0   1070140068
.dram0.bss                21488   1070140080
.flash.text              597480   1107296288
.flash_rodata_dummy      655360   1006632992
.flash.appdesc              256   1007288352
.flash.rodata            119808   1007288608
.eh_frame                 19688   1007408416
.flash.rodata_noload      14859   1007428104
.iram0.text_end             232   1077466904
.iram0.data                   0   1077467136
.iram0.bss                    0   1077467136
.dram0.heap_start             0   1070161568
.debug_info             4849304            0
.debug_abbrev            458970            0
.debug_loc              1258170            0
.debug_aranges            53104            0
.debug_ranges            137576            0
.debug_line             2257817            0
.debug_str               588391            0
.comment                     93            0
.riscv.attributes            40            0
.debug_frame             150036            0
Total                  11306028
<lambda>(["upload"], [".pio/build/adafruit_qtpy_esp32c3/firmware.bin"])
AVAILABLE: cmsis-dap, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = esptool
BeforeUpload(["upload"], [".pio/build/adafruit_qtpy_esp32c3/firmware.bin"])
Auto-detected: /dev/cu.usbmodem14101
"/Users/ridoluc/.platformio/penv/bin/python" "/Users/ridoluc/.platformio/packages/tool-esptoolpy/esptool.py" --chip esp32c3 --port "/dev/cu.usbmodem14101" --baud 460800 --before default_reset --after hard_reset write_flash -z --flash_mode dout --flash_freq 80m --flash_size 4MB 0x0000 /Users/ridoluc/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32c3/bin/bootloader_dout_80m.bin 0x8000 /Users/ridoluc/Documents/Projects/Plant_water/hardware/watering_can/.pio/build/adafruit_qtpy_esp32c3/partitions.bin 0xe000 /Users/ridoluc/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin 0x10000 .pio/build/adafruit_qtpy_esp32c3/firmware.bin
esptool.py v3.3
Serial port /dev/cu.usbmodem14101
Connecting...
Chip is ESP32-C3 (revision 3)
Features: Wi-Fi
Crystal is 40MHz
MAC: 34:b4:72:e9:20:78
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00002fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000d4fff...
Flash params set to 0x032f
Compressed 11744 bytes to 8492...
Writing at 0x00000000... (100 %)
Wrote 11744 bytes (8492 compressed) at 0x00000000 in 0.3 seconds (effective 297.7 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.1 seconds (effective 358.3 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 454.8 kbit/s)...
Hash of data verified.
Compressed 805392 bytes to 485491...
Writing at 0x00010000... (3 %)
Writing at 0x00019c2c... (6 %)
Writing at 0x00024994... (10 %)
Writing at 0x0003019d... (13 %)
Writing at 0x00037f1d... (16 %)
Writing at 0x0003dfa3... (20 %)
Writing at 0x0004414e... (23 %)
Writing at 0x0004a6ff... (26 %)
Writing at 0x00050910... (30 %)
Writing at 0x00056dbb... (33 %)
Writing at 0x0005ca19... (36 %)
Writing at 0x00062ba0... (40 %)
Writing at 0x00068f1f... (43 %)
Writing at 0x0006f449... (46 %)
Writing at 0x00075abb... (50 %)
Writing at 0x0007b794... (53 %)
Writing at 0x00081212... (56 %)
Writing at 0x00086df5... (60 %)
Writing at 0x0008cf96... (63 %)
Writing at 0x00092d12... (66 %)
Writing at 0x00098cc0... (70 %)
Writing at 0x0009e2a9... (73 %)
Writing at 0x000a3a88... (76 %)
Writing at 0x000a9e26... (80 %)
Writing at 0x000af6e8... (83 %)
Writing at 0x000b5181... (86 %)
Writing at 0x000bafe3... (90 %)
Writing at 0x000c15a2... (93 %)
Writing at 0x000c8d9a... (96 %)
Writing at 0x000d0a84... (100 %)
Wrote 805392 bytes (485491 compressed) at 0x00010000 in 11.1 seconds (effective 581.0 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
======================================== [SUCCESS] Took 20.09 seconds ========================================

Terminal will be reused by tasks, press any key to close it.

Yeah it totally flashes with --flash_mode dout --flash_freq 80m, esp32c3/bin/bootloader_dout_80m.bin et cetara. I don’t see a fault here.

Can you install the Arduino IDE + Arduino-ESP32 regularly and check if the bootloop also occurrs there?

Yes, the bootloop occurs with the Arduino IDE as well.
I installed it just yesterday to try it and tried again now.
The reset keeps happening at a random position…
The program I’m using now is just connection setup and serial print every 10 seconds inside the main loop but hardly reaches the first print and never the second time

And with esp_deep_sleep(SLEEPTIME_MICROSECOND); exchanged with a delay(1000) it’s the same behavior?

Yes I removed everything from the main loop to test.

Now it’s just these few lines

void loop() {
  if((millis() - sys_time )> 10000){
    Serial.println("got here");
    sys_time = millis();
  }
  delay(100);
}

If this simplistic piece of code still crashes in the Arduino IDE, please file an issue at Issues · espressif/arduino-esp32 · GitHub so that the Espressif people can look at it.

Yes besides this there is only the connection to AWS services.
I will post it as you suggested and report back here if a solution is found.
Thanks a lot

Hi,

Have you resolved this problem?
I’m getting the same issue with a custom made board. Not using AWS though…

Unfortunately, I lost access to that specific hardware and could never investigate further.
I recently repeated the application on a different board (ESP32­-C3­-WROOM-­02) and it works properly.
I suggest trying a different board if you have access to it.