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