Since the ESP firmware has a complete IP stack it can also act as a server in an IP network. Of course you will not be able to run a full featured WEB server on such a device. But that is also not necessary to do some useful tasks.
If you want to do some home automation – web services are a simple way to connect things together. In my example I have a little web server which controls a GPIO pin of the ESP. You can turn it on an off and ask for the current state. To add some security also have to submit a PIN as well. If you want to make sure that your home automation is save you should run all components in a separate WLAN. The script will do the following.
If you do not use the right pin the TCP connection is closed without any response. This example can easily extended to control more pins or display values from sensors.
-- script will start a http server on http_port
-- requests do look like
-- http://espmodule.domain/?PIN=1234&switch=ON
-- PIN is just a little bit of security to avoide someone playing around
-- switch=ON will turn switch_pin to high
-- switch=OFF will turn switch_pin to low
-- switch=status will just print ON or OFF
switch_pin = 0 -- ESP pin to be controlled
switch_state = "OFF" -- initial state
PIN = "1234" -- pin code
http_port = 80 -- port of http server
module_name = "kitchen" -- just give the module a name
gpio.mode(switch_pin, gpio.OUTPUT)
gpio.write(switch_pin, gpio.LOW)
-- deliver static content (just to make the code more readable)
function page(what)
local content = ""
if what == "start" then --httppage header
content = content .. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
content = content .. "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
content = content .. "<html>\n<head>\n<title>NODE MCU TEST</title>\n</head>\n<body>\n"
content = content .. "<h1>ESP8266 module "..module_name.."</h1>\n"
end
if what == "end" then --close http page
content = content .. "</body>\n</html>"
end
return content
end
--define what is happening if server is contacted
server = net.createServer(net.TCP)
server:listen(http_port,function(conn)
conn:on("receive", function(client,request)
-- this code block gets the params of the http request und stores that in params
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local params = {}
if (vars ~= nil)then
for param, value in string.gmatch(vars, "(%w+)=(%w+)&*") do
params[param] = value
end
end
-- now the code which is doing something based on the request
if params.pin == PIN then
local message = ""
if(params.switch == "ON")then
message = "<h1>Turning switch ON</h2>\n";
gpio.write(switch_pin, gpio.HIGH);
switch_state = "ON"
elseif(params.switch == "OFF")then
message = "<h1>Turning switch OFF</h2>\n";
gpio.write(switch_pin, gpio.LOW);
switch_state = "OFF"
elseif(params.switch == "STATE")then
message = "<h1>state: "..switch_state.."</h2>\n";
end
client:send(page("start")..message..page("end"));
end
client:close();
collectgarbage();
end)
end)
You can test with a normal web browser or on the command line. For instace with wget or curl.
It will look like this:
curl "http://espmodul.home/?pin=1234&switch=ON"
the response will be
ESP8266 module kitchen
Turning switch ON