Current Lua downloadable firmware will be posted here

User avatar
By Luc Volders
#42049 First let me state that I am new to LUA and probably this is some syntax thing which I do not understand.
I really hope someone will dive into this and can provide me with some help, I have been struggling with this for a long time now and surely would appreciate it.

I wrote a program that tests for events.

The first event is a switch being pressed multiple times and at the last time it calls a program that sends a message to IFTTT
The second event is a switch that is pressed and a second program is called when the switch opens and also sends a message (a different one) to IFTTT.

Code: Select all--- Main routine

-- make sure there is no network connection
wifi.sta.disconnect()

-- define the two Switches
Switch1 = 1
Switch2 = 2

-- other variables
test = 1 -- variable that reads Switch1
ftest = 1 -- variable that reads Switch2
ftestrun=0 -- variable that decides wether to run testswitch2()

-- set the pin as input (no Pull-Up)
gpio.mode(Switch1,gpio.INT)
gpio.mode(Switch2,gpio.INT,gpio.PULLUP)

-- first test
-- if Switch1 = not on startpos (Switch1=0)
-- then wait till print start

--- here comes a function for testing Switch2
function testswitch2()
    ftest = gpio.read(Switch2)
        if ftest == 1 then
        print ("Switch2 pressed")
        ftestrun=1
        -- jump to IFTTT routine
        dofile("ifttt2.lua");
        do return end
        error ()
        end
        print ("Switch2 is not pressed")
end
testswitch2()



if gpio.read(Switch1) == 1 then
test = 1
    repeat
      tmr.delay(4000)
      test = gpio.read(Switch1)
    until test == 0
end

-- from here on identical
-- if Switch1 was 0 or is now 0 wait till it lowers
if gpio.read(Switch1) == 0 then
repeat
    tmr.delay(2000)
    test = gpio.read(Switch1)
until test == 1
print ("Process has started")
end

-- Switch1 = now on 1
-- wait till it hits 0 again

if gpio.read(Switch1) == 1 then
test = 1
    repeat
      tmr.delay(4000)
      test = gpio.read(Switch1)
    until test == 0
end

-- second test
-- Switch1 is now at startposition
-- so we wait till the process starts

test=gpio.read(Switch1)
repeat
    tmr.delay(2000)
    test = gpio.read(Switch1)
until test == 1

print ("Process has started now")

-- program is now running (Switch1=1 again)
-- so lets wait till the process is finished
-- when that happens the process presses Switch1 again

test = 1
    repeat
      tmr.delay(2000)
      test = gpio.read(Switch1) 
      if ftestrun == 0 then
        testswitch2()
      end
    until test == 0
   
print ("The process has finished")
dofile("ifttt.lua");


Above is the MAIN routine here comes the first IFTTT routine:

Code: Select all-- First routine to send to IFTTT

wifi.setmode(wifi.STATION)

wifi.sta.config(NETWORKNAME,PASSWORD)

wifi.sta.connect()
print(wifi.sta.getip())

conn = nil
conn=net.createConnection(net.TCP, 0)

conn:on("receive", function(conn, payload)
     print(payload)
     end)
     
conn:on("connection", function(conn, payload)
     print('\nConnected')
     conn:send("GET /trigger/ButtonpressedonESP/with/key/FILLINYOURMAKERKEY"
      .." HTTP/1.1\r\n"
      .."Host: maker.ifttt.com\r\n"
      .."Accept: */*\r\n"
      .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
      .."\r\n")
     end)
     
conn:on("disconnection", function(conn, payload)
      print('\nDisconnected')
      end)
     
print('Posting to ifttt.com') 
                 
tmr.alarm(1, 400, 1, function()
    if wifi.sta.getip()== nil then
        print("Waiting for IP in first routine ...")
    else
        tmr.stop(1)
        print(wifi.sta.getip())
        conn:connect(80,'maker.ifttt.com')
    end
end)     

print ("disconnecting from network in first IFTTT routine")


And the second IFTTT routine:
Code: Select all-- Second routine to send to IFTTT

wifi.setmode(wifi.STATION)

wifi.sta.config(NETWORKNAME,PASSWORD)

wifi.sta.connect()
print(wifi.sta.getip())

conn = nil
conn=net.createConnection(net.TCP, 0)

conn:on("receive", function(conn, payload)
     print(payload)
     end)
     
conn:on("connection", function(conn, payload)
     print('\nConnected')
     conn:send("GET /trigger/toets2/with/key/FILLINYOURMAKERKEY"
      .." HTTP/1.1\r\n"
      .."Host: maker.ifttt.com\r\n"
      .."Accept: */*\r\n"
      .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
      .."\r\n")
     end)
     
conn:on("disconnection", function(conn, payload)
      print('\nDisconnected')
      end)
     
print('Posting to ifttt.com') 
                   
tmr.alarm(1, 400, 1, function()
    if wifi.sta.getip()== nil then
        print("Waiting for IP in second routine ...")
    else
        tmr.stop(1)
        print(wifi.sta.getip())
        conn:connect(80,'maker.ifttt.com')
    end
end)     

print ("disconnecting from network in second IFTTT routine")


Now the problem:

First: both IFTTT routines work flawlessly if I fire them independend directly from Esplorer.
They connect to IFTTT trigger the maker recipe and send a notice by twitter. This should be the case because the two routines are fully identical exept the name of the recipe they call.

Second: If Switch2 is pressed and Switch1 is pressed and released several times like the program asks the program runs to the end and the IFTTT routine is called and fired. So I get a nice twitter message that the process has ended. Just like it should be.

So far so good. However.

If Switch 2 is released when the Main program starts, the program calls IFTTT2 (I know because I get feedack by the print command) but no connection is made with IFTTT. Even worse it looks that no connection is made with the internet. However I get no feedback from the test routine within the timerfunction. So IFTTT is not called.

If the MAIN program runs while Switch2 is pressed it runs through the whole procedure. But if during the process Switch2 is released the test routine (in the last part of the program) indeed again calls the IFTTT2 program dofile("ifttt2.lua"); and this program indeed starts but does not fire the IFTTT event.

So IFTTT works when called from the MAIN program and IFTTT2 which is identical does not work when it is called from the MAIN program.

Anybody ????
Please ????

Luc Volders