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

User avatar
By gartin
#63043 Hi there, I am fairly new here. I have a bit of a troubles sending HTTP POST request. It works, but only sometimes.

My setup is following:
Feather HUZZAH ESP 8266 (description)
TP-LINK router
PC Windows 7

The router can see both devices, the Feather HUZZAH has assigned IP 192.168.0.100, the PC has assigned IP 192.168.0.11. The PC is running Hercules with TCP Server listening on port 80 and firewall is disabled. The PC and the ESP8266 can be pinged from router or by independent device on the same network.

The ESP8266 has older firmware 0.9.6 (this one works for me, I have tried scripts searching for wifi, works just fine). It is powered from PC by USB cable. The ESP8266 is running the following .lua script (I have tried both, directly uploaded and compiled as well).

Code: Select all-- variables
wificfg={}
STATUS_REGISTER=0
HTTP_REGISTER=0
SSID_REGISTER=0
GOT_IP_FLAG=3
SSID_RECEIVED_FLAG=4
WIFIPASSWORD_RECEIVED_FLAG=5
CIK_RECEIVED_FLAG=6
HTTP_CONNECTED_FLAG=7
UART_TERMINATOR1='\n'
UART_TERMINATORS='[\r\n]'
CON_LEN_HDR = "Content-Length: <CONLEN>\r\n\r\n"

-- custom HTTP request
ABSOLUTE_URI = "/"
HOST_URI="192.168.0.11"
IP_ADDRESS='192.168.0.11'

-- update status register periodicaly
tmr.alarm(1, 333, 1, function()
    STATUS_REGISTER=wifi.sta.status()
    STATUS_REGISTER=bit.bor(STATUS_REGISTER,HTTP_REGISTER,SSID_REGISTER)
end)

send_data = function(BODY)
   HTTP_WR_HDR = "POST "..ABSOLUTE_URI.." HTTP/1.1\r\nHost: "..HOST_URI.."\r\nContent-Type: text/cml; charset=utf-8\r\nContent-Length: <CONLEN>\r\n\r\n"
    CONLEN = string.len(BODY)
    HTTP_WR_HDR = string.gsub(HTTP_WR_HDR,"<CONLEN>",CONLEN)
    REQ = HTTP_WR_HDR .. BODY
    push_request(REQ)
end

--read_data = function(dataz)
    --HTTP_RD_HDR = "GET /onep:v1/stack/alias?" .. dataz .. " HTTP/1.1\r\nHost: m2.exosite.com\r\nX-Exosite-CIK: " .. CIK .. "\r\nAccept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n"
    --REQ = HTTP_RD_HDR
    --push_request(REQ)
--end

push_request = function(dataz)
    -- if connected then send request to server
    if bit.isset(HTTP_REGISTER,HTTP_CONNECTED_FLAG) then
        hs:send(dataz)
    -- else connect and then send, also handle events
    else
        hs = net.createConnection(net.TCP, 0)
        hs:on("connection", function (skt)
            HTTP_REGISTER=bit.set(HTTP_REGISTER,HTTP_CONNECTED_FLAG)
            -- this is dangerous async stuff
            hs:send(dataz)
        end)
        hs:on("disconnection", function (skt)
            HTTP_REGISTER=bit.clear(HTTP_REGISTER,HTTP_CONNECTED_FLAG)
        end)
        hs:on("receive", function (skt, RESP)
            -- TODO: handle HTTP response body properly
            httpcode = string.sub(RESP,10,12)
            a,b = string.find(RESP,"\r\n\r\n")
            resplen = string.len(RESP)
            uartresp = string.sub(RESP,b+1,resplen)
            uart.write(0, httpcode .. UART_TERMINATOR1)
            if string.len(uartresp) > 2 then
                uart.write(0, uartresp .. UART_TERMINATOR1)
            end
        end)
        hs:connect(80, IP_ADDRESS)
    end
end

function sendhttp()
     uart.write(0, STATUS_REGISTER, UART_TERMINATOR1)
    local fake_data ="Wsomedata"
    local s = string.gsub(fake_data, UART_TERMINATORS, "")
    local slen = string.len(s)
    local b = string.byte(s,1)
    local c = string.sub(s,1,1)
    print("here11")
    for i = 1, 3, 1 do       -- blink LED 50 times
        if s == 'uartstop' then
            uart.on('data')
            if bit.isset(HTTP_REGISTER, HTTP_CONNECTED_FLAG) then
                hs:close()
            end
        end
       
        if b == 0xf0 then
        elseif b == 0xf3 then
            uart.on('data')
            if bit.isset(HTTP_REGISTER, HTTP_CONNECTED_FLAG) then
                hs:close()
            end
            node.restart()
        end
       
        if bit.isclear(SSID_REGISTER, SSID_RECEIVED_FLAG) then
            wificfg.ssid="nodemcu"
            SSID_REGISTER=bit.set(SSID_REGISTER, SSID_RECEIVED_FLAG)
            print("here2")
        end
        if bit.isclear(SSID_REGISTER, WIFIPASSWORD_RECEIVED_FLAG) then
            print("here3")
            wificfg.pwd="nodemcutesting"
            SSID_REGISTER=bit.set(SSID_REGISTER, WIFIPASSWORD_RECEIVED_FLAG)
          wifi.setmode(wifi.STATION)
            wifi.sta.config(wificfg.ssid, wificfg.pwd)
            wifi.sta.autoconnect(1)
            print("here4")
            tmr.alarm(3, 200, 1, function()
                if wifi.sta.getip()== nil then
                else
                    tmr.stop(3)
                    SSID_REGISTER=bit.set(SSID_REGISTER, GOT_IP_FLAG)
                end
            end)
            print("here5")
        end
       
        if c == 'W' and slen > 3 then
            dataz=string.sub(s,2,slen)
            send_data(dataz .. "\r\n")
            tmr.delay(1000000)  -- wait for 0.5 sec = 500 000 micro-sec
            print("here6")
        end
        print("here7")
    end
end

sendhttp()


ESP8266 is programmed from ESPlorer, after executing the script, I have following output :

Code: Select all> dofile("UART2http_post.lc")

here11
here2
here3
here4
here5
here6
here7
here6
here7
here6
here7


The Hercules on my Windows 7 PC registers client just sparsely (sometimes it registers client with 192.162.0.100 IP), the whole HTTP POST request is delivered even more sparsely.

I am sorry for long code, I hope it is easy to read (it is quite simple). Is there any obvious problem ? What else should I test and how ?