The use of the ESP8266 in the world of IoT

User avatar
By joefly888
#19801 hello, I am using the example from herehttp://randomnerdtutorials.com/esp8266-web-server/ and altered the code to allow me to query the status of the pin to be
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("mheadhq","mooringroad")
print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
          elseif(_GET.pin == "Status")then
              buf=gpio.read(led1);   
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


if I type in address of my led esp8266 server 192.168.1.137 and the parameters into my browser as http://192.168.1.137/?pin=Status" it correctly responds with the state of the pin as either 1 or 0, same thing as I get if I use the POSTMAN to issue the GET request

However, if I use the program below altered from an example to issue a get request to thingspeak, it responds with as if I just issue the GET request to the root URL and not with the params /?pin=Status

anybody know what is wrong with my code here
Code: Select all
function readTSfield()
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("received "..payload) end)
conn:on("connection",
function(conn, payload)
print("Connected TS")
conn:send('GET /?pin=Status'..'\r\n\r\n')  -- it does not seem to detect the params that I am entering here
end)
conn:on("disconnection", function(conn, payload) print('Disconnected') end)
conn:connect(80,'192.168.1.137')
end
tmr.alarm(0, 5000, 1, function() readTSfield() end)
?
User avatar
By joefly888
#19804 updated my client code to this
Code: Select all
function readTSfield()
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("received "..payload) end)
conn:on("connection",
function(conn, payload)
print("Connected TS")
conn:send("GET /?pin=Status' HTTP/1.0\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; ESP8266;)\r\n")
conn:send("\r\n")
end)
conn:on("disconnection", function(conn, payload) print('Disconnected') end)
conn:connect(80,'192.168.1.137')
end
tmr.alarm(0, 5000, 1, function() readTSfield() end)


But don't really understand it enough to explain it as I mashed some examples together. I would guess that the GET request needs to be in a certain format.