Post your best Lua script examples here
User avatar
By lua2018
#68289 Hello,
I hope I post it in thr right forum.
When I print this code in my ESP8266:
Code: Select alli = 1
switch1 = 4
gpio.mode(switch1, gpio.INPUT)
while i ==1 do
    if gpio.read(switch1) == nil then
       print("Hello")
    end
end

I become this code in the Output:
Code: Select all...
Hello
Hello
Hello
Hello
Hellïc_ÇÏRSöfJSúfJSúfêø

NodeMCU 0.9.5 build 20150318  powered by Lua 5.1.4
Hello
Hello
Hello
...

I hope someone can help me and my English is not so bad:D
Thank you for your help

Felis
User avatar
By jankop
#68347 Hi, Felis

1. You have very old version of Lua firmware. Upload a new one. https://nodemcu-build.com/
2. Lua NodeMCU is RTOS, read NodeMCU documentation. https://nodemcu.readthedocs.io/en/master/
3. Read about tmr.delay() function too. https://nodemcu.readthedocs.io/en/master/en/modules/tmr/#tmrdelay
4. Here is your modified example, But it's not the best way to solve it.
Code: Select alli = 1
switch1 = 4
gpio.mode(switch1, gpio.INPUT)
while i ==1 do
    tmr.delay(5000)
   if gpio.read(switch1) == nil then -- why nil, rather 1 or 0?
       print("Hello")
    end
end
User avatar
By Thomas Jakober
#68444 Hi Felis
You probably have connected the switch to ground, which is correct. In this case, the input has high voltage when the switch is off and low voltage when the switch is closed. That gives a 0 at the gpio pin.
Change
if gpio.read(switch1) == nil then
to
if gpio.read(switch1) == 0 then

Thomas