As the title says... Chat on...

User avatar
By zheron
#17774 I have unfortunately hit a brick wall. Any help would be appreciated. I've googled my brains out and found nothing that worked for me. I am trying to get my ESP8266 to send an HTTP POST request to a web server and essentially upload some form data to a database.

Here is a code snip from my test.lua script I am running:

Code: Select allconn=net.createConnection(net.TCP, 0)
conn:connect(server_port,server_address)
conn:send(header)


Now, if I enter each command individually one at a time into the console, it works... but if I hit 'Run' in the ESPlorer IDE it doesn't work.

I've tried a variety of different iterations from using:
Code: Select allconn:on ("connection", function (sck)
     sck:send(header) end)


to putting:
Code: Select alltmr.delay(10000)


in between each command. As I understand it's possible for the 'send' to try and kick off before the connection has completed. I've even tried to put a timer delay in front of the connection itself thinking maybe it was kicking off before I had an IP address. Still no go. It only works if I put the commands in manually one at a time on the console screen.

Here's my full code. I know it's probably not great, as I'm not really a programmed by trade, just tinkering :)

Code: Select all-- Connect to wifi
wifi.setmode(wifi.STATION)
wifi.sta.config("Blacksun","password")
wifi.sta.autoconnect(1)

-- Store IP info for later use
ip,nm,gw = wifi.sta.getip()

-- Prep Variables
server_address="1.1.1.1" --  Edited out for security.
server_port=80
sensor_name="Lot+1+North"
status_period=4
sensor_address="1.1.1.1" -- Edited out for security.  Would use ip variable above but testing for now.
car_state=1
body="sensor_name="..sensor_name.."&"
    .."status_period="..status_period.."&"
    .."sensor_address="..sensor_address.."&"
    .."car_state="..car_state.."&"
    .."action=".."Submit"
length=string.len(body)
header="POST http://www.thisisatest.com/cars/sensor_backend.php HTTP/1.1\r\n"
    .."Host: www.thisisatest.com\r\n"
    .."Connection: keep-alive\r\n"
    .."Content-Type: application/x-www-form-urlencoded\r\n"
    .."Content-Length: "..length.."\r\n"
    .."\r\n"
    ..body.."\r\n\r\n"

-- Connect to HTTP Server and Send Data
conn=net.createConnection(net.TCP, 0)
conn:connect(server_port,server_address)
conn:on("connection", function(conn)
     conn:send(header)
end)