Embedded and IoT

Lua on microcontrollers: NodeMCU firmware, eLua, Whitecat, Lua RTOS (ESP32), embedded scripting in OpenWrt (LuCI web UI), set-top boxes, and printers.

Tips for constrained systems: use <const> and locals, reuse tables, compile to bytecode (luac) to save RAM, use emergency GC settings, and tune LUAI_MAXCCALLS and number types (LUA_32BITS build option for 32-bit integers/floats).

-- NodeMCU (ESP8266/ESP32) example: connect to Wi-Fi and blink an LED
wifi.setmode(wifi.STATION)
wifi.sta.config({ ssid = "MyNetwork", pwd = "password" })

local LED_PIN = 4
gpio.mode(LED_PIN, gpio.OUTPUT)
local state = false
local blink = tmr.create()
blink:register(500, tmr.ALARM_AUTO, function()
    state = not state
    gpio.write(LED_PIN, state and gpio.HIGH or gpio.LOW)
end)
blink:start()

-- Simple HTTP server
local srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on("receive", function(sck, req)
        sck:send("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello from NodeMCU")
    end)
    conn:on("sent", function(sck) sck:close() end)
end)