Left here for archival purposes.

User avatar
By cjgarcia
#29569 Hello,

I'm playing with the ESP8266 and nodeMCU to use it in reading water consumption of my house irrigation system. The flow sensor used has a frequency output in function of water flow (frequency = 4.5 * flow). So I have to read the frequency using a input GPIO. To test the system I have used one pwm output of the ESP8266, connecting D1 and D2 with a wire. The script is attached to the message.

The problem is that, after a while, I get PANIC errors and crashes. Sometimes, after one minute, other times after 30 minutes. Heap is always around 29000.

I'm using a nodeMCU 0.9 hardware, but I've tried in others configurations with ESP-07 and ESP-12 modules, and always happens the same. I'm using the build 20150704 of nodeMCU (with float support), but I've tried the a custom build (using cloud service) and happens the same.

I have seen that if pwm frequency is high the PANIC or reset happens before.
To discard hardware problem, I have made the same with Arduido IDE and it works overnight without problem.

Please, could somebody tell me if there are something bad in my script?

Many thanks for your time and best regards,

Carlos.


Code: Select allfunction pulse_up(level)
    pulses = pulses +1 
end
   
function timer_func()
    us=tmr.now()
    elapus=us-oldus
    oldus=us
    --print("Elap: "..elapus)
    --print("Pulses: ".. pulses)
    elap=elapus/1000000.0
    freq=pulses/elap
    lmin=freq/4.5
    m3total=m3total+lmin*elap/60000.0
    pulses=0
end

function init(pin,period,oldm3)
    pulses = 0
    freq = 0
    lmin=0
    m3total=oldm3
    oldus=tmr.now()
       
    gpio.mode(pin, gpio.INT,gpio.PULLUP)
    gpio.trig(pin,"up",pulse_up)

    tmr.alarm(0,period,1,timer_func)
end

starttime=tmr.time()

init(1,1000,0)

pwm.setup(2,50,512)
pwm.start(2)

tmr.alarm(1,1500,1,function()
    print("\nTime from start: " .. tmr.time()-starttime)
    print("Freq: " .. freq)
    print("l/m: " .. lmin)
    print("M3: " .. m3total)
    print("Heap: " .. node.heap())
    end )