Recently I have been working on some deepsleep projects and I needed to get some feedback on the actual sleeptime compared to the programmed sleeptime. As this was in an ESP8266 that was not connected to any terminal, i needed another way to check on how long the sleeptime actually was. Now there are various ways to do it: I could for instance send a value to thingspeak on waking up and see the time difference between two values, but that would require adding quite some code that I did not really need.
As, my project on wake up does already a check on possible new software for an OTA, it already makes an HTTP call, so i just wanted to add another http call that just stores the time. so I simply added:
httpClient.begin(client,"http://192.168.1.128/store_time.php"); // Specify the URL
int httpResponseCode = httpClient.GET(); // Make the GET request
and on my server I added a piece of php script:
<?php
// File path to store the CSV
define('CSV_FILE_PATH', 'timestamps.csv');
// Function to capture the current time and store it in a CSV file
function storeCurrentTimeInCSV() {
// Capture the current date and time
$currentTime = date('Y-m-d H:i:s');
// Open the CSV file in append mode
$fileHandle = fopen(CSV_FILE_PATH, 'a');
// Check if the file is writable
if ($fileHandle !== false) {
// Write the current time to the CSV file
fputcsv($fileHandle, [$currentTime]);
// Close the file
fclose($fileHandle);
// Return a success response
return json_encode(['status' => 'success', 'message' => 'Time stored successfully']);
} else {
// Return an error response if the file is not writable
return json_encode(['status' => 'error', 'message' => 'Unable to write to the file']);
}
}
// Call the function and output the result as a JSON response
header('Content-Type: application/json');
echo storeCurrentTimeInCSV();
?>
So, now every time my ESP8266 wakes up, before it checks for updates, it first makes a call to the “store_time.php” routine. the results look like this:
"2024-05-31 22:59:34"
"2024-05-31 23:32:10"
"2024-06-01 00:05:51"
"2024-06-01 00:39:30"
"2024-06-01 01:13:16"
"2024-06-01 01:47:01"
"2024-06-01 02:20:43"
"2024-06-01 02:54:29"
"2024-06-01 03:28:09"
"2024-06-01 04:01:43"
"2024-06-01 04:35:26"
"2024-06-01 05:09:04"
"2024-06-01 05:42:47"
"2024-06-01 06:16:31"
"2024-06-01 06:50:17"
"2024-06-01 07:23:57"
"2024-06-01 07:57:35"
"2024-06-01 08:31:17"
"2024-06-01 09:05:11"
"2024-06-01 09:38:58"
"2024-06-01 09:39:45"
Anyway, it allows me to try several different settings and once I am happy with what i have it is a matter of commenting out 2 lines and put that new program ready for an automated OTA