-->
Page 1 of 1

Who read a gpio pin

PostPosted: Mon Jul 17, 2017 8:36 am
by lua2018
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

Re: Who read a gpio pin

PostPosted: Wed Jul 19, 2017 10:18 am
by jankop
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

Re: Who read a gpio pin

PostPosted: Fri Jul 21, 2017 2:24 pm
by lua2018
Hello,
thank you for your help. I will try it.

Re: Who read a gpio pin

PostPosted: Sat Jul 22, 2017 11:58 am
by Thomas Jakober
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