-->
Page 1 of 2

File IO write causes reset

PostPosted: Sun Nov 04, 2018 11:04 am
by SuperDave
Hi all I am have an issue with a reset when the file IO write command is issued. I am using an ESP-07 ( mostly because of the external antenna connection ) for an engine driven off grid solar backup charger. I like to keep track of engine hours so I use the micro controller to add a time constant each time the engine running is detected. All of this works great, my problem is I like to store the accumulated engine run time in a file called hours.dat. This allows the engine hours to be retained in the case of a power down reset of the controller. The file write command writes the engine hours multiplied by 10 ( this allows me to preserve the hours down to tenths ). About half of the time when the file write command is issued it resets to ESP-07. This causes the loss of the accumulated engine run time and stops the execution of the program short of the auto oil change cycle. I will post my code so you can review, I am using version 3 alpha 69. BTW Mike great job on this firmware!


Code: Select allmemclear
udpbegin 7531
io(po,16,0) 'Gas Valve & Ignition
io(po,5,0)   'Starter Solenoid
io(po,4,0)   'Alternator Control
io(po,14,0) 'Alternator High Charge
io(po,12,0) 'Oil Change Pump Fill
io(po,13,0) 'Oil Change Pump Empty
io(pi,3)       ' Engine Running Input
delay 100


overcrank = 0
normal = 0
high = 0
starttimer = 0
stoptimer = 0
starthour = 0
oilchange = 0
R = 0
T = 0
reset = " "
system = "Auto"
hourmeter = read.val(hours)
hourmeter = hourmeter / 10
starthour = hourmeter



time.setup(-6,-5)
[status]
cls
R = io(pi,3)
analog = io(ai)
voltage = analog * .029
udpwrite "192.168.10.240", 10000, str(voltage * 10)
celsius = temp(0)
if celcius > -60 then oiltemperature = (celsius * 1.8) + 32
round(oiltemperature)
celsius1 = temp(1)
if celcius1 > -60 then alternatortemperature = (celsius1 * 1.8) + 32
round(alternatortemperature)
wprint "Daveco Engine Charge Controller APU#1  "
wprint "<br>"
wprint "System Control  "
dropdown system,  "Auto,Manual Start,Off"
wprint "<br>"
wprint "OverCrank Reset "
dropdown reset,  "Reset"
if reset = "Reset" then gosub [clearovercrank]
wprint "<br>"
wprint "<hr>"
wprint "Update Time = "
wprint time("hour")
wprint ":"
wprint time("min")
wprint "<br>"
wprint "Engine Total Hours = "
wprint  hourmeter
wprint "<br>"
wprint "Battery Bank Voltage = "
wprint  voltage
wprint "<br>"
wprint "<br>"
wprint "Crankcase Temperature = "
wprint  oiltemperature
wprint " F "
wprint "<br>"
wprint "<br>"
wprint "Alternator Temperature = "
wprint  alternatortemperature
wprint " F "
wprint "<br>"
wprint "<br>"
if R = 0 then wprint "Engine is Stopped "
if R = 1 then wprint "Engine is Running "
wprint "<br>"
wprint "<br>"
if R = 0 and overcrank = 1 then wprint "Engine Overcrank "
if R = 1 and normal = 0 and high = 0 then wprint "Cold Engine Delay "
if R = 1 and normal = 1 and high = 0 then wprint "Bank is Charging at Normal Rate "
if R = 1 and normal = 0 and high = 1 then wprint "Bank is Charging at High Rate "
wprint "<br>"
if starttimer > 0 then wprint "Timing for Start "
if stoptimer > 0 then wprint "Timing for Stop "
if oilchange > 0 then wprint "Engine in Maintenance Cycle "
wprint "<br>"
wprint "<br>"
wprint "<hr>"
wprint "Last / Current Cycle Time = "
wprint  (hourmeter - starthour)

if voltage < 11.8 and R= 0 and overcrank = 0 and system = "Auto" then starttimer = starttimer + 1
If voltage < 11.8 and overcrank=0 and R = 0 and starttimer > 10 and system = "Auto" and oilchange = 0 then gosub [start]
if R = 0 and system = "Manual Start" and overcrank = 0 then gosub [start]
If R = 1 and oiltemperature > 55 and normal = 0 and high = 0 then gosub [normalcharge]
if voltage > 12.8 and R = 0 then starttimer = 0
if voltage > 14.0 and R = 1 and normal = 1 and (hourmeter - starthour) > 1.0 then gosub [highcharge]
if voltage < 14.0 and R = 1 then stoptimer = 0
if voltage > 14.4 and R = 1 and (hourmeter - starthour) > 3.0 then stoptimer = stoptimer + 1
if voltage > 14.4 and R = 1 and stoptimer > 20 and (hourmeter - starthour) > 3.0 then gosub [shutdown]
if voltage > 14.7 and R = 1 and (hourmeter - starthour) > 2.0 then stoptimer = stoptimer + 1
if voltage > 14.7 and R = 1 and stoptimer > 20 and (hourmeter - starthour) > 2.0 then gosub [shutdown]
If R = 0 and normal = 1 then gosub [shutdown]
if R = 0 and high = 1 then gosub [shutdown]
if R = 1 and system = "Off" then gosub [shutdown]
if R = 1 then hourmeter = hourmeter + .0038
if oilchange = 1 and oiltemperature < 125 then gosub [drain]
if oilchange = 1 and voltage < 12 then gosub [drain]

timer 10000,[status]
wait

[start]
starttimer = 0
stoptimer = 0
io(po,16,1)   ' Turn on Gas Valve & Ignition
delay 5000

' Crank #1

R = io(pi,3)
if R = 0 then io(po,5,1)
delay 5000
io(po,5,0)
delay 5000
R = io(pi,3)

' Crank #2

if R = 0 then io(po,5,1)
R = io(pi,3)
if R = 0 then delay 5000
io(po,5,0)
if R = 0 then delay 5000
R = io(pi,3)

' Crank #3

if R = 0 then io(po,5,1)
R = io(pi,3)
if R = 0 then delay 5000
io(po,5,0)
if R = 0 then delay 5000
R = io(pi,3)
if R = 0 then delay 5000
R = io(pi,3)
if R = 0 then overcrank = 1
if R = 1 then overcrank = 0
if R = 1 then starthour = hourmeter
If overcrank = 1 then io(po,16,0)   ' Turn off Gas Valve & Ignition for Overcrank
system = "Auto"
return

[normalcharge]
io(po,4,1)
normal = 1
return

[highcharge]
io(po,14,1)
normal = 0
high = 1
return

[shutdown]
starttimer = 0
stoptimer = 0
io(po,14,0)      ' Turn off High Charge
delay 10000
io(po,4,0)        ' Turn off Normal Charge
delay 10000
io(po,16,0)      ' Turn off Gas Valve & Ignition
overcrank=0
normal = 0
high = 0
write(hours,hourmeter * 10)
R = io(pi,3)
if R = 1 then delay 10000
R = io(pi,3)
if R = 1 then delay 10000
R = io(pi,3)
if R = 1 then delay 10000
R = io(pi,3)
if R = 0 then io(po,12,1)
delay 40000
io(po,12,0)
oilchange = 1
return

[drain]
io(po,13,1)
delay 40000
io(po,13,0)
oilchange = 0
return

[clearovercrank]
overcrank = 0
reset = " "
return

Re: File IO write causes reset

PostPosted: Sun Nov 11, 2018 2:58 am
by Oldbod
Very interesting program. I'm guessing because of the content the answer to this question will be 'yes' but are you confident power supply to esp is ok during shutdown?

Not fully understood your code (tiny phone screen) so please excuse any idiocy.

If you remove the write statement does it eliminate the crash?

How often do you write hours away? I did say I hadn't fully understood code, for which read and I'm not going back and forth between screens - it's horrible reading a program when only a few chars are visible at a time. This is a great forum but imho the mobile implementation of the forum software is not great. How many key presses for next page sorry rant over...

Checked free memory? It doesn't look resource hungry but stuff happens, especially when you have communications... Queued reads and writes etc.

Re: File IO write causes reset

PostPosted: Sun Nov 11, 2018 9:40 pm
by SuperDave
Yes if I remove the write function the unit doesn’t reset. I have tried several different esp-07s and I get the same results. The write function doesn’t always cause a reset, just once in awhile. The hours are only recorded after an engine run cycle, this can very from 1 time a day to once in a few months depending on solar cell and hydroelectric system output. The power supply is a 35ah agm battery with 1 amp voltage regulators and proper filtering/decoupling capacitors. The available flash and ram are good unless the esp has memory leaks. I initially had the flashfree and ramfree commands print to the display but they seemed to be well within limits.

My question to the forum was mostly if my syntax was correct for the write command, or if possibly there was a better way to do it.

Re: File IO write causes reset

PostPosted: Mon Nov 12, 2018 4:52 am
by Oldbod
I would be tempted to calculate the value to be written outside the write statement. Then do a check for reasonableness, or display it.

Presumably you created the initial value of hours.dat separately. Does your previously stored hours value remains unchanged when the program crashes? If you replace the calculated value to be written with an arbitrary numeric value, does that have the same issue?

If the program is expected to sometimes run headless, perhaps a test for wifi connected before generating wprint etc. I also wonder how the udp stack handles any incoming traffic if no handler is defined. I'd assume it just ignores it, but ...

Does the program autorun on boot, and auto reboot ok after a crash?

You could, if the device is always connected, poll the esp for hours periodically.cumbersome, but...