Post your best Lua script examples here

User avatar
By rsuinux
#47193 Hello;
I'm new with LUA and nodemcu. I test with my esp-01, simple button and del.
My button ( integrated DF-Robot button: +V -V and data) is in GPIO2
DEL is in GPIO0
First test: gpio.HIGH or gpio.LOH for DEL is ok
Second test :
Code: Select allbouton = 4  --> GPIO2

function onChangeup ()
    print("pin moves high")
end

gpio.mode(bouton, gpio.INT)
gpio.trig(bouton, 'up', onChangeup) -- front montant

This work fine, the text is displayed.
But, in association, my lua code does not work. No text display :
Code: Select allbouton = 4  --> GPIO2
led = 3     --> GPIO0
etat = 0
gpio.mode(led, gpio.OUTPUT)
gpio.write(led, gpio.LOW)

function onChangeup ()
    print("Changement d'etat du bouton - on attent le front montant")
    if ( etat == 0 ) then
        gpio.write(led, gpio.HIGH)
        etat=1
    else if ( etat ==1 ) then
        gpio.write(led, gpio.LOW)
        etat=0
    end
end

gpio.mode(bouton, gpio.INT)
gpio.trig(bouton, 'up', onChangeup) -- front montant


the error is probably minimal, but I can not find :oops: .
Can you help me please?

R. Suinot
User avatar
By rsuinux
#47286 After multiple tests, it seems that switch off the ESP before testing a new script.
This morning I left and it worked the first time:
Code: Select allbouton = 4  --> GPIO2
led = 3     --> GPIO0
etat = 0
gpio.mode(led, gpio.OUTPUT)
gpio.write(led, gpio.LOW)

function onChangeup ()
    print("Changement d'etat du bouton")
    if ( etat == 0 ) then
        gpio.write(led, gpio.HIGH)
        etat=1
    else if ( etat ==1 ) then
        gpio.write(led, gpio.LOW)
        etat=0
    end
end

gpio.mode(bouton, gpio.INT)
gpio.trig(bouton, 'up', onChangeup)


Now I have to write the code to send a button state to my server!

You can find (some) of my work here: https://plus.google.com/u/0/+r%C3%A9misuinot/posts/6ma4SPmhbZW

See you soon
User avatar
By RaspberryPiThree
#51073 When I saved these instructions in a .lua file and tried to run with dofile("file.lua") nothing happens. Please help.

gpio.mode(4,gpio.OUTPUT)
gpio.write(4,gpio.HIGH)
gpio.mode(3,gpio.OUTPUT)
gpio.write(3,gpio.LOW)
gpio.mode(3,0)