我已经为我的狗建立了一个自动的,自我填充的水碗,它与水位控制器完美地工作,我现在想添加一些使用ESP8266的监控功能。就像检测水是否在流动,或者是否发生了溢出。
观察水位传感器和溢出传感器是没有问题的,但我被困在检测水流。
我有一个霍尔效应流量传感器,当水通过它时发送1/0脉冲流,我可以用GPIO引脚上的中断来很好地检测它。我的问题是,我无法用一种可靠的方法来检测脉冲何时停止。
到目前为止,我的解决方案是计数脉冲并将值写入计数器,然后设置与其相等的测试计数器。我的想法是,只要水是流动的,计数将继续变化,一旦水停止,计数将保持不变。所有这些都是通过计时器来实现的,当GPIO中断被触发时,计时器就会启动。
它主要是按预期工作,但是当计时器触发时,它会检查两个计数器值,并且有一段时间它们是相同的,因此它表示流已经停止了一个周期,然后再次恢复流。这就是我想要克服的行为。
我的代码如下所示:
flow_sense_pin = 1
flow_counter = 0
test_counter = 0
flow = false
flow_timer = tmr.create()
flow_timer:register(4000, tmr.ALARM_AUTO, function() test_flow() end)
gpio.mode(flow_sense_pin,gpio.INT)
function flow_pin_cb(level)
gpio.trig(flow_sense_pin, level == gpio.HIGH and "down" or "up")
flow_counter = flow_counter + 1
test_counter = flow_counter
if flow == true then else print("Flow Detected") end
flow = true
flow_timer:start()
end
function test_flow()
if test_counter == flow_counter then flow = false end
if flow == false then flow_timer:stop() print("Flow Stopped") end
end
gpio.trig(flow_sense_pin, "down", flow_pin_cb)终端的输出是这样的:

我肯定我忽略了一些显而易见的东西,但我已经用头顶着它好几个小时了,我什么也没做。
发布于 2021-02-09 19:02:32
我会采用这种更简单、可能更健壮的方法:
一样创建flow_timer对象
在
flow_pin_cb),调用flow_timer:alarm(timeout, tmr.ALARM_SINGLE, flow_stop)
其中,timeout仅比连续流的脉冲之间的最大时间长一点,而flow_stop是一个函数,然后当流停止时调用该函数。
flowing在flow_pin_cb中,如果flowing为false,则print("Flow Detected")并设置为flowing true
在flow_stop、print("Flow Stopped")和set flowing false中
发布于 2021-02-10 11:47:01
我仍然需要做一些脉宽测试,但这是完美的,新的代码要简单得多!谢谢!
新代码要简单得多:
flow_sense_pin = 1
timeout = 1000
flow_timer = tmr.create()
flowing = false
gpio.mode(flow_sense_pin,gpio.INT)
function flow_pin_cb(level)
gpio.trig(flow_sense_pin, level == gpio.HIGH and "down" or "up")
if flowing == false then print("Flow Detected") flowing = true end
flow_timer:alarm(timeout, tmr.ALARM_SINGLE, flow_stop)
end
function flow_stop()
print("Flow Stopped")
flowing = false
end
gpio.trig(flow_sense_pin, "down", flow_pin_cb)https://stackoverflow.com/questions/66122512
复制相似问题