Please take a look into this code. It sends data to Rpi every minute ( sends pulse if pulse available ). I think that something is wrong during\after send . . . As you can see there is a small https server where we can see actual pulse variable.
And strange thing is that PulseGas variable is proper and equal with my gas meter, and on my Rpi there are missed pulses.
--Variables
PulseGas = 0
GasCounterValue = 1
GasDivider = 0.01 -- 0.01 m3 it's pulse divider for my counter
GasPin = 4 -- Gpio02
PinState = 1
LastPinState = 1
FirstSend = 1
Current = 0
SendPulse = 0
LastSentPulse = 0
ServerIP="192.168.1.1"
ServerKey = "123"
SensorType = "gas"
SensorIp = "192.168.1.2"
SensorDevice = "wireless"
function round(what, precision)
return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
end
-- already not used
function SendData()
if(wifi.sta.getip()~=nil) then
t = require("ds18b20")
gpio0 = 3
t.setup(gpio0)
addrs = t.addrs()
if (addrs ~= nil) then
print("Total DS18B20 sensors: "..table.getn(addrs))
-- Just read temperature
temp = round(t.read(),2)
print("Temperature: "..t.read().."")
-- sending Temp Data
SensorType = "temp"
connt=net.createConnection(net.TCP, 0)
connt:connect(80,ServerIP)
connt:send("GET /receiver.php?key=".. ServerKey .. "&type=".. SensorType .. "&value=" .. temp .. "&ip=" .. SensorIp .. "&device=" .. SensorDevice
.. " HTTP/1.1\r\nHost: ServerIP\r\n"
.."Connection: ke ep-alive\r\nAccept: */*\r\n\r\n")
t = nil
ds18b20 = nil
package.loaded["ds18b20"]=nil
end
if (PulseGas ~= LastSentPulse) then
SensorType = "gas"
SendPulse = ((PulseGas - LastSentPulse) * GasDivider)
conn=net.createConnection(net.TCP, 0)
conn:connect(80,ServerIP)
print("Sending Data to Nettemp")
conn:send("GET /receiver.php?key=".. ServerKey .. "&type=".. SensorType .. "&value=" .. SendPulse .. "&ip=" .. SensorIp .. "&device=" .. SensorDevice
.. "¤t=" ..Current .. " HTTP/1.1\r\nHost: ServerIP\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
-- now reading response from server searching for "ok" answer
conn:on("receive", function(conn, payload) print(payload)
if string.find(payload,"ok") ~= nil then
LastSentPulse = PulseGas
--PulseGas=0
print("Response from Server ok ! Data Sent Successfully :... resetting Counter")
--else return
end
end )
end
else
print("Getting IP please wait...")
dofile("init.lua")
end
end
--- MAIN LOOOP
gpio.mode(GasPin, gpio.INPUT)
srv=net.createServer(net.TCP)
srv:listen(80, function(conns)
conns:on("receive", function(conns,payload)
--print(payload)
conns:send("<h1> PulseGas : " .. PulseGas .. " </h1>")
end)
conns:on("sent", function(conns) conns:close() end)
end)
TimerStart = tmr.now()
tmr.alarm(1, 60000, 1, function() SendData() end)
tmr.alarm(2, 100, 1, function()
local pinValue = gpio.read(GasPin)
if pinValue == gpio.LOW then
-- print 'GPIO2 is low'
PinState = 0
if PinState ~= LastPinState then
PulseGas = PulseGas + GasCounterValue
TimerStop = tmr.now()
interval = TimerStop - TimerStart
TimerStart = tmr.now()
Current = round(((360000 / interval) / GasDivider),2)
print("Pulse Detected : " .. PulseGas )
print(" Current is : " .. Current .. " m3/h" )
-- print(PulseGas)
end
LastPinState = 0
else
-- print 'GPIO2 is high'
PinState = 1
LastPinState = 1
end
end)