ESP32-WROVER-E Crashing on example code

Hi there,

I am new to the artduino-world, but have some background on Atmels AVRs and Linux development.

I got some ESP32-WROVER-E (named DASDUINO ConnectPlus) boards and try to read a shelly 3em through WiFi. I used the BasicHTTPClient example to receive the response from the shelly and print out the reading to serial. I added code from the JSON example to beatify the reading.

After a short while, the ESP crashes and resets, printing the following:

09:14:41.429 -> [SETUP] WAIT 4...
09:14:42.437 -> [SETUP] WAIT 3...
09:14:43.464 -> [SETUP] WAIT 2...
09:14:44.442 -> [SETUP] WAIT 1...
09:14:48.498 -> Voltage : 235.11V
09:14:48.498 -> Current : 1.63A
09:14:49.535 -> Voltage : 235.11V
09:14:49.535 -> Current : 1.63A
09:14:50.576 -> Voltage : 235.42V
09:14:50.576 -> Current : 1.59A
09:14:51.723 -> Voltage : 235.27V
09:14:51.723 -> Current : 1.62A
09:14:52.792 -> Voltage : 235.25V
09:14:52.792 -> Current : 1.61A
09:14:52.792 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
09:14:52.792 -> Core 1 register dump:
09:14:52.792 -> PC      : 0x401667f4  PS      : 0x00060a30  A0      : 0x800d56e2  A1      : 0x3ffc9be0  
09:14:52.792 -> A2      : 0x3ffc9c6c  A3      : 0x00000000  A4      : 0xffffffff  A5      : 0x00000001  
09:14:52.826 -> A6      : 0x00000000  A7      : 0x00000001  A8      : 0x00000000  A9      : 0x3ffc9bc0  
09:14:52.826 -> A10     : 0x3ffd51f8  A11     : 0x00000000  A12     : 0x00000001  A13     : 0x0000ff00  
09:14:52.826 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000001f  EXCCAUSE: 0x0000001c  
09:14:52.826 -> EXCVADDR: 0x00000010  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
09:14:52.826 -> 
09:14:52.826 -> ELF file SHA256: 0000000000000000
09:14:52.857 -> 
09:14:52.857 -> Backtrace: 0x401667f4:0x3ffc9be0 0x400d56df:0x3ffc9c00 0x400d575d:0x3ffc9c20 0x400d355a:0x3ffc9c40 0x400d9da9:0x3ffc9d20 0x4008b296:0x3ffc9d40
09:14:52.857 -> 
09:14:52.857 -> Rebooting...
09:14:52.857 -> ets Jul 29 2019 12:21:46
09:14:52.857 -> 
09:14:52.857 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
09:14:52.857 -> configsip: 0, SPIWP:0xee
09:14:52.857 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
09:14:52.897 -> mode:DIO, clock div:2
09:14:52.897 -> load:0x3fff0018,len:4
09:14:52.897 -> load:0x3fff001c,len:1100
09:14:52.897 -> load:0x40078000,len:10900
09:14:52.897 -> load:0x40080400,len:6360

Here is my code:

/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */

#include <Arduino.h>

#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>
#include "wifi-credentials.h"

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

/*
const char* ca = \ 
"-----BEGIN CERTIFICATE-----\n" \  
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \  
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \  
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \  
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \  
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \  
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \  
"q6meNQhY7LEqxGiHC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2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \  
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \  
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \  
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \  
"-----END CERTIFICATE-----\n";
*/

float power;
float voltage;
float current;
float total_power;

void readJson(String json){
// Allocate the JSON document
  JsonDocument doc;

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, json);

  // Test if parsing succeeds
  if (error) {
    USE_SERIAL.print(F("deserializeJson() failed: "));
    USE_SERIAL.println(error.f_str());
    return;
  }
    power = doc["power"];
    voltage = doc["voltage"];
    current= doc["current"];

    USE_SERIAL.print("Voltage ");
    USE_SERIAL.print(": ");
    USE_SERIAL.print(voltage);
    USE_SERIAL.println("V");
    USE_SERIAL.print("Current ");
    USE_SERIAL.print(": ");
    USE_SERIAL.print(current);
    USE_SERIAL.println("A");
}


void setup() {

    USE_SERIAL.begin(115200);

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    wifiMulti.addAP(ssid, password);

}

void loop() {
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        //USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
        http.begin("http://192.168.178.30/emeter/0"); //HTTP

        //USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            //USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                readJson(payload);
                //USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }
    USE_SERIAL.flush();
    delay(1000);
}

What is the problem? I didn't expect the examples to cause errors so quickly. If I strip down the code, the crashes need more time to occur, but they do.

If anyone can help me,that would be nice. I didn't want to use another raspberrypi for such a simple task. Thanks.

Here is how I got Shelly EM data (several years ago), but I was not using wifimulti.

  if (WiFi.status() == WL_CONNECTED) 
  {
    WiFiClient client;
    HTTPClient http; //Object of class HTTPClient
    http.begin(client,"http://192.168.0.240/status");
    int httpCode = http.GET();

    if (httpCode > 0) 
    {
      
      StaticJsonDocument<1024> doc;
      deserializeJson(doc, http.getString());
 
      Spower = doc["emeters"][1]["power"];
      voltage = doc["emeters"][1]["voltage"]; 
      relay = doc["relays"][0]["ison"];

I suspect a memory fragmentation issue. Where is the JsonDocument allocated ? on the heap ?

Because in that case the String that you use to receive the http data is also declared there but doesn't go out of scope before the declaration of the JsonDocument, and things can go pretty quick from there. Still somehow the whole thing should be cleaned up at the end of that process. Any chance the JsonDocument doesn't have sufficient buffer ?

That looks better.