So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Sharathk
#63831 Hi Everyone,

This is my first post in this forum, pardon me for any errors,

I am working on this project where I need to send sensor data from micro-controller to esp8266 running on NodeMCU firmware. The esp8266 is programmed to be a TCPClient to send data to a webserver(database). Below is the lua code that I wrote to obtain the sensor data from micro-controller via UART.
Code: Select all--init.lua
uart.setup(0, 9600, 8, 0,1,0)
wifi.setmode(wifi.STATION)
wifi.sta.config("XYZ","abc")
wifi.sta.setip({ip="10.196.26.43",netmask="255.255.255.0",gateway="10.196.26.1"})
wifi.sta.connect()
--tmr.delay(10000000)   -- wait 1,000,000 us = 1 second
tmr.alarm(0,1000,1,function()
    if wifi.sta.status() ~= 5 then
        print("Connecting...")
    else       
        tmr.stop(0)
        print(wifi.sta.status())
        print("Connected, IP is "..wifi.sta.getip())                                                                             
            uart.on("data",0,function(buf)                 
                local data
                cl=net.createConnection(net.TCP, 0)
                cl:connect(80,"10.196.26.36")
                print("GET /save.php?"..buf.." HTTP/1.0\r\nHost: 10.196.26.36\r\nConnection: close\r\nAccept: */*\r\n\r\n")           
                cl:on("connection",function(cl,c)                                       
                cl:send("GET /save.php?"..buf.." HTTP/1.0\r\nHost: 10.196.26.36\r\nConnection: close\r\nAccept: */*\r\n\r\n")                                               
                end)   
                cl:on("receive", function(cl, payload)
                print(0,payload)             
                end)                           
                end,0)
                                                     
        end
end)


My issue here is, Since Lua is asynchronous, event based program, even before the data is completely received by the esp8266 the request is being fired rendering it to be bad request to the server. Is there a way to achieve this goal in a proper manner? Can I block other events till the data is completely received? If yes, please enlighten me in this regard.

Thank you,