Left here for archival purposes.

User avatar
By Mikejstb
#10570 I have a few lines of code to post data to Thingspeak.
It works 100% of the time if I just have it in a function which gets called by the tmr.alrm() function that's in all the demos.
But if I put that same code in a block (I'm new to Lua so maybe that's not the right term) like within an if statement - the netword commands stop working.
so this works -
Code: Select allprint ("DHT22 Temp = "..tDh.."\n")
print ("DHT22 Humidity = "..hDh.."\n")
--print("Sending data to thingspeak.com")
--print ("going to send tF, which = "..tF .."and Light = " ..light .."and PIR = "..here .."\n")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=MyKeyValue=" ..tDh .."&field2=" ..light .."&field3=" ..here .."&field4=" ..tDh .."&field5=" ..hDh.." HTTP/1.1\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")
conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
                                print("Got disconnection...")
  end)

end


but just putting it within an if statement like this no longer works -
Code: Select allif true do
print ("DHT22 Temp = "..tDh.."\n")
print ("DHT22 Humidity = "..hDh.."\n")
--print("Sending data to thingspeak.com")
--print ("going to send tF, which = "..tF .."and Light = " ..light .."and PIR = "..here .."\n")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=MyKeyValue=" ..tDh .."&field2=" ..light .."&field3=" ..here .."&field4=" ..tDh .."&field5=" ..hDh.." HTTP/1.1\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")
conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
                                print("Got disconnection...")
  end)

end


I may have a not-matching end in my example because I've been cutting and pasting all day - but honestly the same working code stops working if it's in an if statement, if it's in a function that I call - anything other than "no layer - inline code" the network conn:on("receive"...) never gets any data.

I've been pulling my hair out for two days trying to fudge around this and can't find anything that works.
it is just me?
is it Lua?
Anybody seen this ?
going crazy - first trying to learn Lua on the fly with the "edit-upload-try-crash-repeat" method is really hard. Then seeming to have to program around strange things like this almost has me ready to give up and move to CC32000...
User avatar
By Mikejstb
#10737 Turns out to be all my fault.
I thought that my code would execute serially such as an Arduino.
But it's actually event driven and each function appears to be threaded.
Nice, now the I understand.
Some flags & semaphores and now things are working like I want. :D