Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

ESP8266 multiple pages access using net.createConnection(net.TCP, 0)

Open msbrigittte opened this issue 9 years ago • 1 comments

I would like to read time from google and sand data to my thingspeak channel all in one lua program.

First program:

connout = nil
connout = net.createConnection(net.TCP, 0)
connout:on("receive", function(connout, payloadout)
if (string.find(payloadout, "Status: 200 OK") ~= nil) then
 end
end)

connout:on("connection", function(connout, payloadout)   
    connout:send("GET /update?api_key="..CHANNEL_API_KEY.."&field1=" .. humi   
    .. " HTTP/1.1\r\n"
    .. "Host: api.thingspeak.com\r\n"
    .. "Connection: close\r\n"
    .. "Accept: */*\r\n"
    .. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
    .. "\r\n")
    end)

    connout:on("disconnection", function(connout, payloadout)
    connout:close();
    collectgarbage();
end)
connout:connect(80,'api.thingspeak.com')
gpio.write(pinled,gpio.LOW)
 end)

Second program:

  conn=net.createConnection(net.TCP, 0) 
  conn:on("connection",function(conn, payloadout)
    conn:send("HEAD / HTTP/1.1\r\n".. 
              "Host: google.com\r\n"..
              "Accept: */*\r\n"..
              "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
              "\r\n\r\n") 
    end)

conn:on("receive", function(conn, payload)
tmp = string.find(payload,"Date: ")
print(tmp)
conn:close()
end) 
t = tmr.now()    
conn:connect(80,'google.com')

The programs work fine alone, but I want them both in one lua file. Should I create two tcp connections? or arrange them in some other way?

msbrigittte avatar Dec 18 '16 20:12 msbrigittte

@msbrigittte wrong repo, this is the Arduino C++ firmware, not the Nodemcu Lua firmware. Closing.

devyte avatar Oct 05 '17 06:10 devyte