The use of the ESP8266 in the world of IoT

User avatar
By andylynch
#13242 hey, trying to figure out why this is returned out of order?
init.lua
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
print(wifi.sta.getip())
tmr.alarm(0, 30000, 1, function() dofile("s.lua") end )

s.lua
Code: Select all-- ft
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) success=true
ft = tonumber(string.sub(payload, string.find(payload, "<field1>") + 8, string.find(payload, "</field1>") - 1))
print("f= 1")
print("ft= "..ft)
end)
conn:on("disconnection", function(conn, payload) print("disconnect") conn:close() end)
conn:on("connection", function(conn, payload) print("sending...")
conn:send("GET /channels/CHANNEL/fields/1/last.xml HTTP/1.0\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n") 
end)
conn:connect(80,'184.106.153.149')
-- fv
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) success=true
fv = tonumber(string.sub(payload, string.find(payload, "<field5>") + 8, string.find(payload, "</field5>") - 1))
print("f= 5")
print("fv= "..fv)
end)
conn:on("disconnection", function(conn, payload) print("disconnect") conn:close() end)
conn:on("connection", function(conn, payload) print("sending...")
conn:send("GET /channels/CHANNEL/fields/5/last.xml HTTP/1.0\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n") 
end)
conn:connect(80,'184.106.153.149')

this is returned via lualoader console
Code: Select allnil
> sending...
sending...
f= 1
ft= 70.925
f= 5
fv= 0
disconnect
disconnect

then other times, this
Code: Select allnil
> sending...
sending...
f= 5
fv= 0
f= 1
ft= 70.925
disconnect
disconnect


also if i place an if/then statement that refers to fv and/or ft at the end of s.lua it will process the if/then before the http requests even though it is placed at the end.

thanks in advance and sorry if i'm incorrect with my terminology.

-Andy
User avatar
By andylynch
#13309 Went ahead and cleaned up the code a bit and added print commands so i can see the order the lines are being called. they are sent in order 1-12 but the console shows they are executing out of order.
s.lua
Code: Select allprint 'Start'
print '1 conn netcreateConnection'
conn=net.createConnection(net.TCP, 0)
print '2 conn on receive'
conn:on("receive", function(conn, payload)
ft = tonumber(string.sub(payload, string.find(payload, "<field1>") + 8, string.find(payload, "</field1>") - 1))
-- print("f= 1")
-- print("ft= "..ft)
end)
print '3 conn on disconnecttion'
conn:on("disconnection", function(conn, payload) conn:close() end)
print '4 conn on connection'
conn:on("connection", function(conn, payload)
print '5 conn send'
conn:send("GET /channels/CHANNEL/fields/1/last.xml HTTP/1.0\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n") 
end)
print '6 conn connect'
conn:connect(80,'184.106.153.149')
print '7 conn netrcreateConnection'
conn=net.createConnection(net.TCP, 0)
print '8 conn on receive'
conn:on("receive", function(conn, payload)
fv = tonumber(string.sub(payload, string.find(payload, "<field5>") + 8, string.find(payload, "</field5>") - 1))
-- print("f= 5")
-- print("fv= "..fv)
end)
print '9 conn on discconection'
conn:on("disconnection", function(conn, payload) conn:close() end)
print '10 conn on connection'
conn:on("connection", function(conn, payload)
print '11 conn send'
conn:send("GET /channels/CHANNEL/fields/5/last.xml HTTP/1.0\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n") 
end)
print '12 conn connect'
conn:connect(80,'184.106.153.149')
print 'End'


this is what is returned in lualoader console
Code: Select allNodeMCU 0.9.5 build 20150214  powered by Lua 5.1.4
nil
> Start
1 conn netcreateConnection
2 conn on receive
3 conn on disconnecttion
4 conn on connection
6 conn connect
7 conn netrcreateConnection
8 conn on receive
9 conn on discconection
10 conn on connection
12 conn connect
End
5 conn send
11 conn send

Start
1 conn netcreateConnection
2 conn on receive
3 conn on disconnecttion
4 conn on connection
6 conn connect
7 conn netrcreateConnection
8 conn on receive
9 conn on discconection
10 conn on connection
12 conn connect
End
11 conn send
5 conn send

Start
1 conn netcreateConnection
2 conn on receive
3 conn on disconnecttion
4 conn on connection
6 conn connect
7 conn netrcreateConnection
8 conn on receive
9 conn on discconection
10 conn on connection
12 conn connect
End
11 conn send
5 conn send

i can't figure out why they are executed/returned out of order.

thanks,
- Andy
User avatar
By cal
#14226 Moin,

I suggest to read up a about functions and callbacks in lua (start with http://www.lua.org/pil/6.html)
and get used to proper indenting of code to see the block structure the code has.
Some call it a nodejs style of programming. Its much older than that but if you read about that it may help you.

Code: Select allprint '3 conn on disconnecttion'
-- this installs a function that will be executed when disconnection event happens on connection.
conn:on("disconnection", function(conn, payload) conn:close() end)
print '4 conn on connection'
-- this installs a function that will we executed when connection event happens on connection.
conn:on("connection", function(conn, payload)
   - we are INSIDE that function now, so no relation to print"4" in terms of "order"
   print '5 conn send'
   conn:send("GET /channels/CHANNEL/fields/1/last.xml HTTP/1.0\r\n")
...
-- this ends the function
end)


Hope that helps,
Carsten