I am experiencing the "Guru Meditation Error: Core 1 panic'ed (IntegerDivideByZero)" error and I can not find where the error lies so I am hoping someone can come to my rescue.
The code, below, is a fairly straight forward code that controls a fan using pwm on an esp32 and measuring the temperature with a DH22. I also store the data to a sd card so I can evaluate the data at a later stage. The program runs fine until it is stopped and rebooted followin the "guru" error. This can happen after a few minutes or hours. My program itself does not involve any calculation, this is mainly done in the dht file.
I would be grateful if anyone could solve this problem for me.
Thanks
Dave
#include "DHTesp.h"
DHTesp dht;
#include <ESP32Time.h>
#include <Arduino.h>
#include <WiFi.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "time.h"
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define FAN_PWM_PIN 27 // was 17
#define FAN_TACH_PIN 26 // was 2
#define PWM_CHANNEL 0
#define PWM_RESOLUTION 8
#define PWM_FREQUENCY 25000
int dutyCycle = 0;
ESP32Time rtc;
BluetoothSerial SerialBT;
const char* ssid = "xxxxxxx";
const char* password = "yyyyyyyyy";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
char filename[] = "000000.CSV";
char buffer[80];
const char* header = " ";
String headerMessage;
String dataMessage;
char datetime[80];
char dateandtime[40];
char daynumber[10];
unsigned long previousMillis = 0;
const long interval_sec = 60000;
/////////////////////////VOID INITIALISE SD CARD //////////////////////
void initSDCard() {
if (!SD.begin()) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
//////////////////////// VOID INITIALISE WiFi ////////////////////////////////////////
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
// WiFi.setHostname(hostname.c_str()); //define hostname
//wifi_station_set_hostname( hostname.c_str() );
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
long rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
}
/////////////////////////VOID SET LOCAL TIME //////////////////////////////////////
void updateTime() {
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
rtc.setTimeStruct(timeinfo);
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
}
///////////////////////// VOID WRITE FILE ///////////////////////////////////////
void writeFile(fs::FS & fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
///////////////////// VOID APPEND FILE DATA /////////////////////////////
void appendFile(fs::FS & fs, const char * path, const char * message) {
// Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending data");
return;
}
if (file.print(message)) {
Serial.println("Data Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}
void setup() {
Serial.begin(9600);
dht.setup(25, DHTesp::DHT22);
SerialBT.begin("Victron Cooling Fan");
Serial.println("Bluetooth Started! Ready to pair...");
ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttachPin(FAN_PWM_PIN, PWM_CHANNEL);
pinMode(FAN_TACH_PIN, INPUT_PULLUP);
Serial.println("Initiaiase WiFi");
initWiFi();
Serial.println("Initiaiase SDcard");
initSDCard();
Serial.println("Set RTC from NTP server");
updateTime();
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
rtc.setTimeStruct(timeinfo);
Serial.println(&timeinfo, "%W, %w, %A, %B %d %Y %H:%M:%S");
}
headerMessage = String("Date,Time,Duty Cycle,B2B Temp,RPM") + "\r\n";
strftime (buffer, 80, "/B2B_Temp.csv", &timeinfo);
writeFile(SD, buffer, header);
appendFile(SD, buffer, headerMessage.c_str());
}
void loop() {
struct tm timeinfo = rtc.getTimeStruct();
strftime(datetime, 30, "%F, %X" , &timeinfo);
strftime(dateandtime, 30, "%F, %R" , &timeinfo);
strftime (daynumber, 10, "%w", &timeinfo);
// float t = dht.readTemperature();
float t = dht.getTemperature();
if (t < 20) {
dutyCycle = 0;
} else if (t >= 20 && t <= 25) {
dutyCycle = 50;
} else if (t >= 25 && t <= 30) {
dutyCycle = 100;
} else if (t >= 30 && t <= 35) {
dutyCycle = 150;
} else if (t >= 35 && t <= 40) {
dutyCycle = 200;
} else if (t >= 40 ) {
dutyCycle = 250;
}
ledcWrite(PWM_CHANNEL, dutyCycle);
unsigned long pulseWidth = pulseIn(FAN_TACH_PIN, HIGH);
unsigned int rpm = (1000000 * 60) / (pulseWidth * 2);
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S"));
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("C ");
Serial.print(" DutyCycle ");
Serial.print(dutyCycle);
Serial.print(" RPM: ");
Serial.println(rpm);
Serial.println();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval_sec) {
previousMillis = currentMillis;
dataMessage = String(dateandtime) + "," + String(dutyCycle) + "," + String(t) + "," + String(rpm) + "\r\n";
appendFile(SD, buffer, dataMessage.c_str());
readFile(SD, buffer);
}
delay(5000);
// Write to Bluetooth
SerialBT.println(datetime);
SerialBT.print("Temp ");
SerialBT.print(t);
SerialBT.println(" C");
SerialBT.print("DutyCycle ");
SerialBT.println(dutyCycle);
SerialBT.print("RPM ");
SerialBT.println(rpm);
SerialBT.println();
}