Reputation: 85
I am trying to save the current time & date into SPIFFS from a web page hosted on ESP32 but for some reason, the ESP32 reboots when the button <button type="button" onclick="sendTime()">Record Time&Date</button>
is pressed.
JavaScript:
<script>
function sendTime() {
// Get the current time and date
const now = new Date();
const dateStr = now.toLocaleDateString();
const timeStr = now.toLocaleTimeString();
// Create the request body
const body = {
date: dateStr,
time: timeStr
};
// Send the HTTP POST request to the server
fetch('/save-time', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(response => {
if (!response.ok) {
throw new Error('Error saving time to server');
}
})
.catch(error => {
console.error(error);
});
}
</script>
And the ESP32 server-side code:
Libraries used: WiFi.h, FS.h, SPIFFS.h, Arduino.h, ArduinoJson.h and ESPAsyncWebServer.h
setup() code snippet:
// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("Failed to mount file system");
return;
}
Serial.println("SPIFFS mounted");
// Set up routes
server.on("/save-time", HTTP_POST, [](AsyncWebServerRequest *request){
// Get the request body as a string
String body = request->getParam("plain")->value();
// Parse the request body as JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, body);
// Get the date and time from the request body
String dateStr = doc["date"].as<String>();
String timeStr = doc["time"].as<String>();
// Create the file name using the date
String fileName = "/watering-" + dateStr + ".txt";
// Open the file for writing
File file = SPIFFS.open(fileName, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for writing");
request->send(500, "text/plain", "Failed to open file for writing");
return;
}
// Write the time to the file
file.print(timeStr);
file.print("\n");
// Close the file
file.close();
// Send a success response
request->send(200, "text/plain", "Time saved successfully");
});
// Route for retrieving watering times
server.on("/watering-times", HTTP_GET, [](AsyncWebServerRequest *request){
String response = "";
// Get a list of all the files in the / directory
File root = SPIFFS.open("/");
File file = root.openNextFile();
while(file){
// Check if the file name starts with "watering-"
String fileName = file.name();
if (fileName.startsWith("/watering-")) {
// Read the file contents and add them to the response string
response += fileName.substring(10, 20) + " " + file.readString() + "<br>";
}
file = root.openNextFile();
}
// Send the response with the list of watering times
request->send(200, "text/html", response);
});
// Start the server
server.begin();
}
as soon as the button is pressed ESP32 reboots and prints this in Serial:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400dec30 PS : 0x00060e30 A0 : 0x800dec6e A1 : 0x3ffd2280
A2 : 0x3ffd230c A3 : 0x00000010 A4 : 0x00000005 A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x800de95c A9 : 0x3ffd2260
A10 : 0x00000010 A11 : 0x00000000 A12 : 0x00000006 A13 : 0x3ffce334
A14 : 0x00ff0000 A15 : 0x00000002 SAR : 0x0000001b EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000001f LBEG : 0x40089db9 LEND : 0x40089dc9 LCOUNT : 0xfffffffe
Backtrace: 0x400dec2d:0x3ffd2280 0x400dec6b:0x3ffd22a0 0x400d45a2:0x3ffd22c0 0x400d48a5:0x3ffd2390 0x400dbbf9:0x3ffd23b0 0x400d9fbd:0x3ffd2400 0x400da02d:0x3ffd2450 0x400dcca5:0x3ffd2470 0x400dcd21:0x3ffd24a0 0x400dd32a:0x3ffd24c0
ELF file SHA256: 78ff020431f74c0c
Rebooting...
Upvotes: 0
Views: 89