Post your best Lua script examples here

User avatar
By axelk
#7973
iHaveESP wrote:I'm publishing dht temperature data every 45 seconds using a timer. I also have a PIR sensor that is interrupt driven to publish as well. At times they attempt to publish at the same time and I get "unprotected error in call to Lua API (pir.lua:34: sending in process)" and the module reboots.

I've added a flag to skip publishing dht data in favor of PIR motion, it helps, but reboot still happens. Seems like there should be a queue or something.


I ran into exactly the same problem - using some "poor man's semaphore" for now. It works quite ok. I also tried to implement a real queue, but then I get frequent reboots because out-of-heap.

Code: Select all-- publish status
function pub()
  sub=nil -- don't need than any more
  if inpub then
    tmr.alarm(4, 50, 0, pub)
    return
  end
  inpub = true
  m:publish("e44/conf/light/table/status", lsoft, 0, 1, function()
    m:publish("e44/conf/light/room/status", lhard, 0, 1, function()
      m:publish("e44/conf/light/shelf/status", lstudent, 0, 1, function()
        m:publish("e44/conf/temp/status", temp, 0, 0, function()
          print("PUB OK ", node.heap())
          inpub = false
        end) 
      end)
    end)
  end)   
end

-- publish button pressed
function button()
  if inpub then
    tmr.alarm(3, 60, 0, button)
    return
  end
  inpub = true
  m:publish("e44/conf/buttons/0", "1", 0, 0, function()
    inpub = false
  end)
end

inpub = false
tmr.alarm(0,1000,1,pub)

-- int on GPIO0 down
gpio.mode(3, gpio.INPUT, gpio.PULLUP)
gpio.trig(3, "down", function(level)
   button()
end)



I guess what we really need here is
1. some kind of queue in C, using less RAM
2. a more peaceful result of mqtt.publish(), e.g. false/true instead of raising a fatal error

Axel.
User avatar
By ampakinetic
#7978 I'm getting a lot of reboots with the latest MQTT/Lua firmware, especially when the device is acting as a web server.

I have tried several server examples on the MQTT build and they all crash when a connection is made by a client (using the prebuild nodemcu_latest from 5 days ago).

If I revert to the standard pre-build the web server code works well and there are no crashes.
User avatar
By axelk
#7985
ampakinetic wrote:I'm getting a lot of reboots with the latest MQTT/Lua firmware, especially when the device is acting as a web server.


Crashes seem to be heap-related.

Either some corruption occurrs, or the FW simply crashes (and reboots by watchdog) if there is not enough memory available.
Methods I currently use for freeing more heap is removing unused stuff from the FW build (e.g., SSL) and by replacing Lua functions by dofile() calls.

Another thing I've observed with newest NodeMCU MQTT branch is problems with the file system after "big" downloads via http (e.g., 10 files with 1024 bytes each). Although file list, sizes, and file contents look ok, accessing other files - that existed before download - does not work sometimes, but lead to a watchdog reboot.

However, NodeMCU+MQTT is really good stuff even now :-)

Axel.