Post your best Lua script examples here

User avatar
By rutierut
#12529
pinhead1212 wrote:Hi, i cant get anything on Node-red. How do i have to setup the MQTT Input node in node-red?
I'm using Ubuntu, Mosquitto is running.


I just installed mosquito, launched node-red added a MQTT in node and left everything default(Localhost, port 1883) and it worked instantly. Localhost, port 1883
User avatar
By rutierut
#12536
fluppie007 wrote:I get the following error when pressing the button on D4 / GPIO2

> §„Fêh!9ÿ@h…ÿòÇ!9Œ¦¨Hø
NodeMCU 0.9.5 build 20150311 powered by Lua 5.1.4
> Button pressed
PANIC: unprotected error in call to Lua API (init.lua:49: attempt to index global 'm' (a nil value))
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
PANIC: unprotected error in call to Lua API (attempt to ind?2ˆÈ©™Ž@H6ÎôŒÕÊüŒ¦¨Hø
NodeMCU 0.9.5 build 20150311 powered by Lua 5.1.4
>
Hard Restart maandag 23 maart 2015 21:56:57


I assumed the code suggested by the other forum user was fully tested, i'll test it myself next week, in the meanwhile here's my code:

Code: Select all-- Engeneerd by rutierut, inspired by MikeV

broker = "YOURBROKERSIP"     -- IP or hostname of MQTT broker
mqttport = 1883          -- MQTT port (default 1883)
userID = ""              -- username for authentication if required
userPWD  = ""            -- user password if needed for security
clientID = "ESP1"        -- Device ID
GPIO2 = 4                -- IO Index of GPIO2 which is connected to an LED
ready = 0
previousbuttonpress = 0
gpio.mode(GPIO2, gpio.INPUT)

-- Wifi credentials
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASWORD")

function wifi_connect()
   ip = wifi.sta.getip()
     if ip ~= nill then
          print('Connected, ip is:' .. ip)
          tmr.stop(1)
          ready = 1
     else
          ready = 0
     end
end

function mqtt_do()
if ready == 1 then
  m = mqtt.Client(clientID, 120, userID, userPWD)
  m:connect( broker , mqttport, 0,
  function(conn)
    print("Connected to MQTT:" .. broker .. ":" .. mqttport .." as " .. clientID )
    tmr.stop(0)
    connected = 1;
  end)
end
end

function buttonpres()
  currentime = tmr.now()
  delta = currentime - previousbuttonpress
  previousbuttonpress = tmr.now()
  if delta >= 200000 then
  print('Button pressed')
  publishing = 1;
  m:publish("Frontdoor","Button Pressed", 0, 0,
    function(conn)
      -- Print confirmation of data published
      print(" Message  published!")
      publishing = 0;
      -- Finished publishing - go back to publish state.
    end)
    elseif delta <= 200000 then
    print('Delta was too small')
    else
    print('error')
    end
end
     
gpio.trig(GPIO2, "down", buttonpres)
   
tmr.alarm(0, 1000, 1, function()
     mqtt_do()
     tmr.delay(1000)
     end)
     
tmr.alarm(1, 1111, 1, function()
     wifi_connect()
     end)
User avatar
By bakicc
#12558 which firmware version are you using guy's? I'm having trouble with the round method. Seems that an call within that function is causing issues.
When I remove the round function, it works. But after a few hours, the ESP isn't sensing messages anymore. Didn't ad the chance to debug why, I'll be back ;)

Did you test those sources for a long period of time?