ESP restart and send timing

@eyal Hoping you’ll have the answer to this, since I’m basically just trying to replicate what you’ve already done, however I can’t seem to find an answer in your code on Github.

I’m doing a very basic wake, read DS18b20, sleep/restart loop.

Works 100% when using DHCP (albeit it takes several seconds to get an IP.) Using a static IP I need to put a delay of about 1 sec in before sending the packet for it to work. Anything less and the packet just disappears.

Checking wifi.sta.status() doesn’t seem to produce the required outcome in this case.

Any ideas?

 t = require("ds18b20")
gpio4 = 1

-- Send the packet then go to sleep
local function readAndSend_18B20()
    conn=net.createConnection(net.UDP)
    conn:connect(56000, "192.168.1.6")
    conn:send(temp)
    print(temp)
    tmr.alarm(1,10, tmr.ALARM_SINGLE, function()
        node.dsleep(1000000 * 3)
    end)
    
end


wifi.sta.setip({
    ip="192.168.1.85",
    netmask="255.255.255.0",
    gateway="192.168.1.1"
  })
wifi.sta.status() -- rumoured to kick start WiFi connection
--]]


-- check DS18b20 pin is pulled up as an indicator to start normally
gpio.mode(gpio4, gpio.INPUT)
if(gpio.read(gpio4) == 1) then

    t.setup(gpio4)
    temp = t.read().."'C"

    if(wifi.sta.status() == 5) then
        readAndSend_18B20()
    end

    -- No IP - setup timer to check for it
    tmr.alarm(0, 100, tmr.ALARM_AUTO, function ()
        print(wifi.sta.getrssi())
        if (wifi.sta.status() == 5) then
            tmr.unregister(0)
            readAndSend_18B20()
        end
    end)
    
else
    print("Exit restart loop")
end

My first comment is please use eventmon as my latest app_v3 shows. While not perfect, it is cleaner. It hooks directly into the SDK events.

You need to set the IP asap, which you are doing. Good.
BTW, you have a stray end of comment marker '--]]'.

I see that you use gpio4 for both the abort request and ow. Should be OK.

Can you add current time to messages, e.g.

        print(tmr.now(), wifi.sta.getrssi())

This will show how soon the status (5 is wifi.STA_GOTIP, I need to update my github).

I found that UDP packets can get lost if you sleep too soon. This is the udp_grace_ms and grace_time in my app (save-udp.lua). I delay the sleep until after sleep_time.

There was no way to check when the UDP was sent (need empty send buffer) and the sent event is useless as it fires immediately when you send(). I wonder if the latest SDK 2.0.0 is any better?

HTH

Ok I didn’t know about eventmon, thanks.

I’ve switched to TCP and surprisingly getting much better performance (I guess it’s forcing it to try and connect.) Maybe the root cause is due to poor signal strength where I am using it (RSSI around -60db).

Nice.

I get better numbers using UDP. My app issues the dsleep around 400-450ms after app start. A simple test program (ie.lua) reports wifi available at tmr.now() around 350ms.

As for rssi, some of my devices run reliably with -70 to -75db.

In case it matters, I now updated my git app_v3 to what I am using.

Well that’s a lot worse RSSI that I have, so not sure. Maybe something with my network.

I’m going to look into it further another time, now I’m just getting a RPi server setup to log it all.