SOLVED: after may hours of testing i have solved the problem. it turned out to not be a software issue. the power supply i was using did not have a flyback diode and thus caused large voltage spikes that would crash the esp. the solution was to add a diode on the inductive load to remove those voltage spikes.
I have an esp-01 running some code that checks for a rf remote signal using the rc-switch library, it is also running some blynk and ota code.
when i press one of the buttons on my rf remote the buzzer pin keeps being triggered but will not shut off, eventually the esp will crash..
the device is in a permanent location thus the reason for the ota updates and i cant easily hookup a serial monitor to see what the esp crash code is.
the troublesome code is,
void newloop() {
if (mySwitch.available()) {
// int recieved = mySwitch.getReceivedValue();
if (mySwitch.getReceivedValue() == 5099665) {
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 5099666) {
digitalWrite(buzzer, LOW);
delay(2000);
digitalWrite(buzzer, HIGH);
mySwitch.resetAvailable();
}
}
// Blynk.run();
}
the full code, with redacted passwords/keys is,
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#define BLYNK_PRINT Serial
#include <RCSwitch.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define buzzer 2
char auth[] = "<blynk auth>";
RCSwitch mySwitch = RCSwitch();
const char* ssid = "HansenHome";
const char* password = "<wifi password>";
ESP8266WebServer server;
bool ready = false;
bool ota_flag = true;
uint16_t time_elapsed = 0;
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, HIGH);
Blynk.begin(auth, ssid, password);
mySwitch.enableReceive(0);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("lock");
// No authentication by default
ArduinoOTA.setPassword("<uploading password>");
ArduinoOTA.onStart( {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/restart",{
server.send(200,"text/plain", "Restarting...");
delay(1000);
ESP.restart();
});
server.on("/setflag",{
server.send(200,"text/plain", "Setting flag...");
ota_flag = true;
time_elapsed = 0;
});
server.begin();
}
void loop() {
if(ota_flag)
{
uint16_t time_start = millis();
while(time_elapsed < 10000)
{
ArduinoOTA.handle();
time_elapsed = millis()-time_start;
delay(10);
}
ota_flag = false;
}
if(ready == false) {
ready = true;
digitalWrite(buzzer, LOW);
delay(20);
digitalWrite(buzzer, HIGH);
delay(20);
digitalWrite(buzzer, LOW);
delay(20);
digitalWrite(buzzer, HIGH);
delay(20);
digitalWrite(buzzer, LOW);
delay(20);
digitalWrite(buzzer, HIGH);
delay(20);
digitalWrite(buzzer, LOW);
delay(20);
digitalWrite(buzzer, HIGH);
ready = true;
}
server.handleClient();
newloop();
}
BLYNK_WRITE(V1) {
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
}
void newloop() {
if (mySwitch.available()) {
// int recieved = mySwitch.getReceivedValue();
if (mySwitch.getReceivedValue() == 5099665) {
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 5099666) {
digitalWrite(buzzer, LOW);
delay(2000);
digitalWrite(buzzer, HIGH);
mySwitch.resetAvailable();
}
}
// Blynk.run();
}
EDIT:
so after so further testing it appears to be a power supply issue, the power supply i was using was hooked up to a large inductive load. once that load is turned off there is a huge power spike and for some reason causing the esp to crash.
the board the esp is on has 2 voltage regulators and a few input caps
i added a large cap to the main supply and a 100uf to the 3.3v rail directly on the esp. it is still crashing..