Current Lua downloadable firmware will be posted here

User avatar
By bernardo rodrigues
#16740 Hello.

This is the code I'm trying to run on my ESP8266.
When I try to send packets outside the while loop, it works fine.
But the packets inside the loop are not being sent.
I know I'm supposed to use tmr.alarm() instead of tmr.delay(), but still not sure why and how.

Anyone has any tip on this issue?

Code: Select all-- Reading liquid flow rate using an ESP8266
-- Code adapted by Bernardo Rodrigues
--from PC Fan RPM code written by
--Crenn@thebestcasescenario.com

count = 0
conn=net.createConnection(net.TCP, false)
conn:connect(8888,"192.168.0.103")

--count interrupts
function rpm()
     count = count + 1
end

--enable interrupts
function enInt()     
     gpio.mode(6, gpio.INT)
     gpio.trig(6, "up", rpm)
end

--disable interrupts
function disInt()
     gpio.mode(6, gpio.INPUT)
end

consumption = 0

--infinite loop
while 1 do     
     count = 0       --Set NbTops to 0 ready for calculations
     enInt()             --Enable interrupts
     tmr.delay(1000000)  --Wait 1 second
     disInt()            --Disable interrupts
     flux = (count * 60 / 7.5)
     consumption = consumption + flux/36000

     conn:send(consumption)

     print(flux .. " L/hour")
     print(consumption .. " L consumed\r\n")
end
User avatar
By TerryE
#16783 There are two separate issues to be addressed here and the first is to do with how you structure your code to do the TCP comms. With TCP clients there are two separate sockets involved: the first is returned by the createConnection and the second returned to the callback from the on("connection") event. It's better to use the second. If you want to send to it repeatedly then you need to ensure that it's scope is passed to the send (e.g. copy it to a global), so this is how I suggest that you organise your code:
Code: Select allcount, time_stamp = 0, tmr.now()
gpio.mode(6, gpio.INT)
gpio.trig(6, "up", function() count = count + 1 end)

function sender()
  -- code to calculate flux and consumption goes here
  count = 0
  connect_sk:send(("%.4f"):format(consumption))
end

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:on("connection", function(sck)
  connect_sk = sck
  tmr.alarm(1,1000,1,sender)
  end)
sk:connect(8888,"192.168.0.103")


The second is how you get an accurate flow rate given the timing jitter on your sampling. tmr.delay() is picked up by the tmr.c routine within the runtime and this calls an os_timer_delay(), which isn't even an ESP 8266 SDK function. It's a low level wait loop in the xtensa GCC runtime library, and it does just that loops for this delay with interrupts disabled. A sensible is use is if you want to toggle a GPIO pin for 1mSec, say, but is really isn't meant to be used as you are using it here and doing so will cause you a LOT of problems. If you think that the alarm timing isn't accurate enough, then why not just take the tmr.now() realtime clock at each count reset and use its delta to compute the true elapsed time? (Remember it wraps every 2^31 usec ~ 40mins, so you will need to debounce this wrap).

Also as the poster said on the issue list, if you have a concern with the server keeping up with this send rate, then you may need to include an on("send") limiter.