0
votes

I'm getting a nil payload while trying to get data from a page using ESP8266 WiFi module using Lua.

Here's my pseudo-code :

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
wifi.sta.connect()

tmr.alarm(1,10000, 1, function()
    if (wifi.sta.getip() == nil) then
        print("IP unavaiable, Waiting...")
    else
        foo()
    end
end)


function foo()
    print("Inside foo function"..node.heap());
        conn = nil
        conn=net.createConnection(net.TCP,0)        -- 30 seconds timeout time of server
        conn:on("receive", function(conn, payload)
            -- local buf = "";
            startRead = false
            gpioData = ""
            print("payload : "..#payload);
            for i = 1, #payload do
                print(i); 
            end     
        end)
        conn:connect(80,"server.co.in")
        conn:on("connection", function(conn, payload)
            print("Server Connected, sending event")
            conn:send("GET /mypage?id=deadbeef HTTP/1.1 200 OK\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)

        conn:on("sent",function(conn)
                print("Closing server connection")
                conn:close()
            end)
end

I'm using NodeMCU Lua, and guess will be same even if i use Arduino framework.

NodeMCU custom build by frightanic.com
    branch: master
    commit: 22e1adc4b06c931797539b986c85e229e5942a5f
    SSL: false
    modules: adc,bit,cjson,file,gpio,http,i2c,mdns,mqtt,net,node,ow,struct,tmr,uart,websocket,wifi
 build  built on: 2017-05-03 11:24
 powered by Lua 5.1.4 on SDK 2.0.0(656edbf)

I'm able to see all requests on my server which means server request code is ok, but payload/response is coming out blank.

Output is complete blank...

Please help.

1

1 Answers

2
votes
       conn:send("GET /mypage?id=deadbeef HTTP/1.1 200 OK\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)

This is not a valid HTTP request. It looks more like a mix of HTTP request and HTTP response. The server might simply close the connection because it does not understand this. A valid HTTP request would look like this:

GET /mypage?id=deadbeef HTTP/1.1\r\n
Host: ...\r\n
\r\n

Apart from that you are using HTTP/1.1 and even explicitly set that you want to use HTTP persistent connections (Connection: keep-alive) although this behavior is implicit with HTTP/1.1 anyway. Because of this you cannot expect that the response will be followed immediately by a connection close like you currently do. Also, because of HTTP/1.1 you need to deal with HTTP chunked encoding.

The easiest way to avoid this complexity is to use HTTP/1.0 instead and not use a Connection header or set it explicitly to close instead. If you instead really want to handle the complexity please study the standard carefully.