ESP32Cam crashes on any WiFi method?

Hello all. I’m using platformio for an ESP32Cam project, and so far it’s been working well. I’ve gotten a motion sensor hooked up to it and working properly, but when I tried to start an AP (or do anything involving WiFi, for that matter) the program crashes, saying there’s an invalid opcode. Seems to me like it’s a compatibility issue or a broken library, but here’s my code:

#include <Arduino.h>
#include <WiFi.h>

const int ledPin = 4; // the number of the LED pin
const int motionPin = 12; // the number of the motion sensor pin
bool initialized = false;
bool motionDetected = false;



bool checkInit() {
  if (!initialized && digitalRead(motionPin) == LOW) {
    initialized = true;
    Serial.println("\nMotion sensor initialized.");
  }
  if (!initialized) {
    Serial.print('.');
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  }
  return initialized;
}

void checkMotion() {
  if (!motionDetected && digitalRead(motionPin) == HIGH) {
    motionDetected = true;
    Serial.println("Motion detected!");
    digitalWrite(ledPin, HIGH);
  }
  if (motionDetected && digitalRead(motionPin) == LOW) {
    motionDetected = false;
    Serial.println("Motion stopped.");
    digitalWrite(ledPin, LOW);
  }
}



void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(motionPin, INPUT);
  Serial.begin(9600);


  Serial.print("Starting AP");
  WiFi.enableAP(true);
  WiFi.mode(WIFI_AP);
  delay(1000);
  WiFi.softAP("ESP32-AP", "12345678");
  delay(1000);

  // Serial.print("Initializing motion sensor...");
}

void loop() {
  // if (!checkInit()) return;
  // checkMotion();
  
  delay(10);
}

Here’s the serial output:

␀␀Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d7dfc: 0000ffe6 00000000 00000000
Core  0 register dump:
PC      : 0x400d7e00  PS      : 0x00050031  A0      : 0x4014a176  A1      : 0x3ffbed1c  
A2      : 0x007bf568  A3      : 0x3ffbe0e8  A4      : 0x40085de8  A5      : 0x80000000  
A6      : 0x00000000  A7      : 0x003fffff  A8      : 0x800dec6e  A9      : 0x4008d07a  
A10     : 0x00000000  A11     : 0x80000001  A12     : 0x00000000  A13     : 0x3ffbc660  
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x0000001d  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  


Backtrace: 0x400d7dfd:0x3ffbed1c |<-CORRUPTED




ELF file SHA256: 2bf489710000ab24

Rebooting...

[ continues in reboot loop ]

I’m not quite sure exactly what’s causing this, but I was wondering if someone in the community could help me out here. Everything works fine without trying anything WiFi-related, but when I use the WiFi library’s methods, it crashes like this.

Does anybody know how I could go about troubleshooting / fixing this? Thanks in advance!

Try to run a WiFi-only sketch:

https://github.com/espressif/arduino-esp32/blob/2.0.16/libraries/WiFi/examples/WiFiScan/WiFiScan.ino

What’s the output?

I actually get a Brownout detector trip (which was another problem I was experiencing), and some other info:

Reconnecting to /dev/ttyUSB0     Connected!

Brownout detector was triggered

ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4

This is a bit odd, since there’s nothing else actually connected to the circuit (save the motion sensor, but this persists even when the rest of the circuit is disconnected).

When I disable the brownout trigger, I get this output:

ets Jul 29 2019 12:21:46

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
ets Jul 29 2019 12:21:46

Basically the same, minus the brownout error. Like I said before, it could be some sort of compatibility issue. Here’s my .ini file, if this helps:

[env:esp32]
platform = espressif32
board = esp32cam
framework = arduino
upload_resetmethod = nodemcu
monitor_speed = 115200

As I’m writing this, it occurs to me that the nodemcu config is left over from when I was going to use an ESP01-S. Removing that option (keeping brownout disabled) yields this:

Reconnecting to /dev/ttyUSB0 .   Connected!
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x4013717c: f03df03d 00000000 00000000
Core  0 register dump:
PC      : 0x40137180  PS      : 0x00060a30  A0      : 0x80133f9d  A1      : 0x3ffca0e0  
A2      : 0x00000000  A3      : 0x3ffc8388  A4      : 0x00100000  A5      : 0x0b210067  
A6      : 0x00ad2018  A7      : 0x00000003  A8      : 0x3ffc83fa  A9      : 0x00000006  
A10     : 0x00000014  A11     : 0x0000003f  A12     : 0x3ffc8399  A13     : 0x3ffc06ba  
A14     : 0x00000040  A15     : 0x00000000  SAR     : 0x00000007  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x40137165  LEND    : 0x40137178  LCOUNT  : 0x00000000  


Backtrace: 0x4013717d:0x3ffca0e0 0x40133f9a:0x3ffca100 0x40134ace:0x3ffca120 0x40100ac6:0x3ffca150 0x40100b49:0x3ffca180 0x400e829b:0x3ffca1a0 0x40118e26:0x3ffca1c0 0x40119635:0x3ffca1e0 0x40117a58:0x3ffca200 0x4014cbf9:0x3ffca220




ELF file SHA256: 4e4cb089b0fce1d3

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
ets Jul 29 2019 12:21:46

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered () 
Core  1 register dump:
PC      : 0x40091263  PS      : 0x00060036  A0      : 0x800df062  A1      : 0x3ffbc9a0  
A2      : 0x9c400000  A3      : 0x00038000  A4      : 0x9c400000  A5      : 0x00000008  
A6      : 0x007bf568  A7      : 0x00000000  A8      : 0x3ff5f000  A9      : 0x00000008  
A10     : 0x3ffbc9d0  A11     : 0x00000000  A12     : 0x00000008  A13     : 0x3ffbc9d8  
A14     : 0x3ffbcdb0  A15     : 0x00000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000001  
EXCVADDR: 0x800838b0  LBEG    : 0x4008acc8  LEND    : 0x4008acd3  LCOUNT  : 0xffffffff  


Backtrace: 0x40091260:0x3ffbc9a0 0x400df05f:0x3ffbc9d0 0x400df0a5:0x3ffbca00 0x400df4e7:0x3ffbca70 0x400838ad:0x3ffbcac0 0x40085b68:0x3ffbcae0 0x400838ad:0x3ffbcba0 0x4008e9fc:0x3ffbcbc0 0x4008d3e7:0x3ffbcbe0 0x4008d12d:0x3ffbcc00 0x40085ded:0x3ffbcc10 0x40149d9b:0x3ffbccd0 0x400ded7e:0x3ffbccf0 0x4008e500:0x3ffbcd10




ELF file SHA256: 000000007c380880

E (5612) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0xbd8c8402 instead of 0x3ffc5500


[ etc. ]

I think it’s most likely a configuration mistake on my part, but I’m not exactly sure on where to go from here. I’ve scoured the internet looking for solutions, to no avail.

EDIT: It seems like the brownout error was a result of the nodemcu option being in my config file, it doesn’t occur anymore after removing it.

If it’s a brownout then all the fingers are pointing to the fact that your power supply is not stable.

How do you generate the needed 3.3V? Are the power supply cables as short as they can be? Do you use capacitors to smooth and stablize the supply voltage? How many and what capacitance do they have? Did you try shorter USB cables if they’re used for power? etc. etc.

It seems that the brownout resolved itself, or was tied to the nodemcu option. I’ve tried switching power supplies from my serial adapter (connected to my laptop) to a 5v 0.6a power supply designed specifically for breadboards, and neither had any effect. I’ll continue to tinker around in the meantime.

Here’s another cleaner serial capture I got, in case it’s useful (I added a 5s delay between booting and continuing the setup() function):

Reconnecting to /dev/ttyUSB0     Connected!
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d9fac: ffffffff ffffffff ffffffff
Core  0 register dump:
PC      : 0x400d9fb0  PS      : 0x00050031  A0      : 0x4008429c  A1      : 0x3ffbed1c  
A2      : 0x24221f1c  A3      : 0x3ffbe0e8  A4      : 0x40085de8  A5      : 0x3ffc9fd0  
A6      : 0x00000000  A7      : 0x2e2c2927  A8      : 0x15122500  A9      : 0x4008d07a  
A10     : 0x00000963  A11     : 0x3ff96964  A12     : 0x00000000  A13     : 0x3ffc9fc0  
A14     : 0x24221f1c  A15     : 0x2e2c2927  SAR     : 0x00000007  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  


Backtrace: 0x400d9fad:0x3ffbed1c |<-CORRUPTED




ELF file SHA256: c1d0c92cbe3f06db

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4

“Illegal instruction” and all 0xfffff either means your flash is dying and returning the bits all-unprogrammed or it’s not been flashed correctly at all (flash mode? flash frequency? flash size?) or again, your flash chip not getting a stable supply voltage.

I don’t have any shorter cables of the correct type, but I’m going to borrow a 4 inch one (as opposed to an 18 inch one) from a friend, and see if that will fix things. It did seem a little suspicious that the flash was returning tons of 0xff’s, so I tried toying around with various flash configurations and such.