Saving data on ramdisk before a WDT event
Posted: Sun Jun 13, 2021 10:55 pm
I modified this snippet found elsewhere on the forum so that I could log the time of random timeouts (freezes) on a ESP32. Then I thought I would try to save what was in /ramdisk before I did a machine.reset()
However, what seems to happen when the callback occurs is that the contents of /ramdisk disappear. I have tested the copyfileobj part in a stand-alone program. The write to errors.txt continues to work after the callback.
Is this expected behaviour? Any thoughts on how to save the data other than periodic saves to flash?
Thanks.
Code: Select all
## Simple software WDT implementation
import machine
import utime
from shutil import copyfileobj
wdt_counter = 0
def wdt_callback():
global wdt_counter
wdt_counter += 1
if (wdt_counter >= 70): # 70 seconds
epoch = utime.time()
local_time = utime.localtime(epoch)
seconds = local_time[5]
minutes = local_time[4]
hours = local_time[3]
day = local_time[2]
time = str(hours) + ':' + str(minutes) + ':' + str(seconds)
try:
with open('errors.txt', 'a') as outfile:
outfile.write(str(day) + ' ' + time + '\n')
outfile.write('had a wdt event' + '\n')
except OSError:
pass
# save sbms_datalog.csv
src_file = open('/ramdisk/sbms_datalog.csv', 'rb')
dest_file = open('sbms_datalog.csv', 'wb')
copyfileobj(src_file, dest_file, length=512)
print('Contents of src copied to dest')
src_file.close()
dest_file.close()
machine.reset()
def wdt_feed():
global wdt_counter
wdt_counter = 0
wdt_timer = machine.Timer(3) # hardware timer for ESP32
wdt_timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=lambda t:wdt_callback())
## END Simple software WDT implementation
Is this expected behaviour? Any thoughts on how to save the data other than periodic saves to flash?
Thanks.