Post your best Lua script examples here

User avatar
By kayakpete
#9607 Hiya -

Thanks, to everyone, for the conversations that got me as far as I have -

Garage Door Opener with ESP01, a relay and a switch.

http://kayakpete.tumblr.com/post/110860 ... 66-running

Image

[2/13/2015 - *** Diagram removed - See responses ***]

[2/15/2015 EDIT - Updated code]
[2/13/2015 EDIT - Updated code]
Code: Select all-- Garage Door controller version 2/15/15 pete@hoffswell.com
-- GPIO2 is connected to switch with internal pulldown enabled 
gpio.write(4,gpio.LOW)
gpio.mode(4,gpio.INPUT,gpio.PULLDOWN) 
--GPIO0 is connected to Relay
gpio.mode(3,gpio.OUTPUT) 
gpio.write(3,gpio.HIGH) 

print("Program Start")

-- Start up mqtt
m = mqtt.Client("ESP1", 120, "user", "password")
m:lwt("/lwt", "offline", 0, 0)
m:connect("192.168.15.22", 1883, 0, function(conn) print("mqtt connected")
   m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1")
   m:publish("openhab/garage/relay1","GO",0,0) -- counter program start
   end)
end)

-- Reconnect to mqtt server if needed
m:on("offline", function(con) print ("reconnecting...")
   tmr.alarm(1, 10000, 0, function()
      m:connect("192.168.15.22", 1883, 0, function(conn) print("mqtt connected")
         m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1")
         end)
      end)
   end)
end)

 -- Switch Trigger
gpio.trig(4, "both",function (level)
   state = gpio.read(4)
   m:publish("openhab/garage/switch1",state,0,0)
   print("Sent openhab/garage/switch1 " .. state )   
end)

-- MQTT Message Processor
m:on("message", function(conn, topic, msg)   
   print("Recieved:" .. topic .. ":" .. msg)   
   if (msg=="GO") then  -- Activate Door Button
      --print("Activating Door")   
      gpio.write(3,gpio.LOW) 
      tmr.delay(1000000) -- wait 1 second
      gpio.write(3,gpio.HIGH) 
   else 
      print("Invalid - Ignoring")   
   end   
end) 



On occasion, I get the following:
PANIC: unprotected error in call to Lua API (door1.lua:24: sending in process)

That's the m:publish within the gpio.trig function.

Also some times it locks up enough to where I must power cycle the ESP01. When I do, sometimes even that won't recover until I disconnect GPIO0

Any thoughts as to how to fix either of these issues?

Thanks!
Last edited by kayakpete on Sun Feb 15, 2015 8:55 pm, edited 3 times in total.
User avatar
By kayakpete
#9639 Hi Arnie -

I'm sorry, you are right. The schematic is incorrect. I'm afraid I'm quite the novice in drawing circuits. Pretty much so in designing them as well.

Image

That's a pull-down resistor for GPIO0. I'm pretty sure that's the correct way of doing it circuit-wise. Not sure I'm representing it schematic-wise
User avatar
By kayakpete
#9669 Another interesting issue with this code:

When you start it up, it runs the commands in the m:on("message",... routine:

NodeMCU 0.9.5 build 20150126 powered by Lua 5.1.4
lua: cannot open init.lua
> dofile("door1.lua")
> mqtt connected
subscribed relay1
Recieved:openhab/garage/relay1:GO
Activate Door


This causes the door to activate upon startup, even though it didn't receive a mqtt message from the broker. I'd rather it not do that.