Can anybody provide an explanation why I have to do as described below:
I count pulses on GPIO12 using PulseCount.lua called from init.lua shown below. When the ESP is restarted it appears as if the GPIO initialization does not happen correctly as no pulses is registered and reported except if I manually execute dofile('PulseCount.lua') again. The pulse counting then starts as shown.
My work around is to modify the init.lua file and execute the dofile('PulseCount.lua') twice as follows:
--modified init.lua
print("Pulsecounter")
dofile('PulseCount.lua')
dofile('PulseCount.lua')The original init.lua not working is as follows:
--original init.lua
print("Pulsecounter")
dofile('PulseCount.lua')--PulseCount.lua
kWhPulses = 0
TotalPulses = 0
Sample = 120000
gpio.mode(6, gpio.INT,gpio.PULLUP)
gpio.trig(6, "down", kWhPulseInterrupt)
function kWhPulseInterrupt()
kWhPulses = kWhPulses+1
end
function Report()
kWh = 3600 / Sample * kWhPulses
TotalPulses = kWhPulses + TotalPulses
print("kWh: "..kWh .."")
print("Total Pulses: "..TotalPulses .."")
print(" ")
kWhPulses = 0
end
tmr.alarm(1, Sample, 1, function() Report() end)