The code prints time and it crashed at these times:
- 8:50
- 6:19
- 5:19
- 2:19
- 0:19
- 22:49
- 22:8
- 21:49
- 19:49
It might seem like the error could be caused by runnig out of memory? I have no clue.
any help would be appreciated!
Error code: -1
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4008af56 PS : 0x00060d30 A0 : 0x8008b086 A1 : 0x3ffcb0b0
A2 : 0x00000000 A3 : 0x00000000 A4 : 0x00000000 A5 : 0x0000000a
A6 : 0x00000000 A7 : 0x00000008 A8 : 0x8008f7ca A9 : 0x3ffcb0a0
A10 : 0x3ffcb224 A11 : 0x3ff96355 A12 : 0x00001388 A13 : 0x0001d4c0
A14 : 0x00ff0000 A15 : 0xff000000 SAR : 0x0000001d EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400899e8 LEND : 0x400899f3 LCOUNT : 0x00000000
Backtrace: 0x4008af53:0x3ffcb0b0 0x4008b083:0x3ffcb0e0 0x40088891:0x3ffcb100 0x400d379b:0x3ffcb120 0x400db6d5:0x3ffcb190
0x4008af53: strptime_l at /builds/idf/crosstool-NG/.build/HOST-i686-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/time/strptime.c line 238
0x4008b083: strptime_l at /builds/idf/crosstool-NG/.build/HOST-i686-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/time/strptime.c line 323
0x40088891: tx_pwctrl_cal at phy_chip_v7_cal.c line 1879
0x400d379b: loop() at C:\Users\xxx\Documents\Arduino\ESPplay\apireq\LED_to_time/LED_to_time.ino line 112
0x400db6d5: loopTask(void*) at C:\Users\xxx\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\cores\esp32\main.cpp line 42
#include <WiFi.h>
#include <time.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <NeoPixelBus.h>
const char* ssid = "ssid";
const char* password = "pass";
//current time stuff
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
//Your Domain name with URL path or IP address with path
const char* sunEndpoint = "https://api.sunrise-sunset.org/json?lat=51.5073359&lng=-0.12765&date=today";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
unsigned long timerDelay = 6000;
struct tm timeinfo;
bool Increased = false;
String sunRequest;
DynamicJsonDocument sunriseDoc(1024);
// pixel stuff
const uint16_t PixelCount = 4; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 14; // make sure to set this to the correct pin, ignored for Esp8266
#define colorSaturation 128
NeoPixelBus<NeoGrbFeature, NeoWs2812xMethod> strip(PixelCount, PixelPin);
RgbColor white(colorSaturation);
RgbColor black(0);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
pinMode(12, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(12), increase, FALLING);
pinMode(2, OUTPUT);
// pixel stuff
strip.Begin();
strip.Show();
}
// void increase(){
// if (!Increased) {
// currentHour+=1;
// Increased=true;
// digitalWrite(2, HIGH);
// }
// }
void some_call_led(RgbColor colour){
for (int n = 0 ; n < PixelCount; n++) {
strip.SetPixelColor(n,colour);
}
strip.Show();
}
void loop() {
//Send an HTTP POST request every 10 minutes
digitalWrite(2, LOW);
if ((millis() - lastTime) > timerDelay) {
if (digitalRead(12) == 0){
digitalWrite(2, HIGH);
}
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
if (getLocalTime(&timeinfo)) {
int currentHour = timeinfo.tm_hour;
int currentMinute = timeinfo.tm_min;
// parse json for sun request
sunRequest = httpGETRequest(sunEndpoint);
deserializeJson(sunriseDoc, sunRequest);
const char* sunrise = sunriseDoc["results"]["sunrise"];
const char* sunset = sunriseDoc["results"]["sunset"];
// Convert sunrise and sunset times to hours and minutes
int sunriseHour = atoi(sunrise);
int sunriseMinute = atoi(sunrise + 3);
int sunsetHour = atoi(sunset);
int sunsetMinute = atoi(sunset + 3);
// compensation for api being stupid
if (strstr(sunrise, "PM") != NULL && sunriseHour != 12) {
sunriseHour += 12;
}
if (strstr(sunset, "PM") != NULL && sunsetHour != 12) {
sunsetHour += 12;
}
int totalHourSun = sunsetHour - sunriseHour;
int totalMinuteSun = sunsetMinute - sunriseMinute;
int maxHour = 12;
int maxMinute = 0;
int compHour = (totalHourSun - maxHour)/2;
int compMinute = (totalMinuteSun - maxMinute)/2;
int LEDoffHour = sunsetHour-compHour;
int LEDoffMinute;
if (sunsetMinute-compMinute>61){
LEDoffMinute = (sunsetMinute-compMinute)-60;
LEDoffHour += 1;
}
else {
LEDoffMinute = sunsetMinute-compMinute;
}
int LEDonHour = sunriseHour+compHour;
int LEDonMinute;
if (sunriseMinute+compMinute<0){
LEDonMinute = 60+(compMinute+sunriseMinute);
LEDonHour -= 1;
}
else {
LEDonMinute = sunriseMinute+compMinute;
}
Serial.print("sunrise: ");
Serial.print(sunriseHour);
Serial.print(":");
Serial.println(sunriseMinute);
Serial.print("sunset: ");
Serial.print(sunsetHour);
Serial.print(":");
Serial.println(sunsetMinute);
Serial.print("total: ");
Serial.print(totalHourSun);
Serial.print(":");
Serial.println(totalMinuteSun);
Serial.print("comp: ");
Serial.print(compHour);
Serial.print(":");
Serial.println(compMinute);
Serial.print("on: ");
Serial.print(LEDonHour);
Serial.print(":");
Serial.println(LEDonMinute);
Serial.print("off: ");
Serial.print(LEDoffHour);
Serial.print(":");
Serial.println(LEDoffMinute);
Serial.print("Current Time:");
Serial.print(currentHour);
Serial.print(":");
Serial.println(currentMinute);
///to do with debug
// Serial.print("Increased:");
// Serial.println(Increased);
// if (Increased) {
// Increased=false;
// digitalWrite(2, LOW);
// }
// Compare current time with sunrise and sunset times
if ((currentHour > LEDoffHour || (currentHour == LEDoffHour && currentMinute >= LEDoffMinute)) || (currentHour < LEDonHour || (currentHour == LEDonHour && currentMinute < LEDonMinute))) {
// It's past sunset, turn off LEDs
// Code to turn off LEDs
Serial.println("past sunset turn off LEDS");
some_call_led(black);
}
else {
// It's past sunrise, turn on LEDs
// Code to turn on LEDs
Serial.println("past sunrise turn on LEDS");
some_call_led(white);
}
}
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// If you need Node-RED/server authentication, insert user and password below
//http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}