I am working on a little project where I am trying to control the built-in led of 2 esp8266 modules over the web server. There are lots of examples of controlling the led of the module via web server but they are for just 1 module. I want to add an extra client and control that as well.
Modules are Lolin Nodemcu v3.
Below is the code for server.
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("DesirdTT","ddesirdsifree") --router to connect to, needs to be changed accordingly
wifi.ap.config({ssid="test",pwd="12345678"});
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip() == nil then
print("Connecting to router...")
else
print('Connected to router :)')
print('IP: ',wifi.sta.getip())
tmr.stop(0)
end
end)
led = 4
gpio.mode(led, gpio.OUTPUT)
if srv~=nil then
srv:close()
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local client1 = "";
local _GET = {}
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
--print(method);
--print(path);
--print(vars);
--print(client);
--print(request);
if (method == "CLIENT")then
client:send("you are detected by server");
end
if(method == nil)then -- when we just enter 192.168.1.1 (normal entry)
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<h1 style='color:red'> ESP8266 Web Server</h1>";
buf = buf.."<h2><i> This page is in very early stage of the development</h2></i>";
buf = buf.."<p>1. DEVICE LED <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf = buf.."<p>2. DEVICE LED <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
buf = buf.."<p>3. DEVICE LED <a href=\"?pin=ON3\"><button>ON</button></a> <a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
if(_GET.pin == "ON1")then
gpio.write(led, gpio.LOW);
elseif(_GET.pin == "OFF1")then
gpio.write(led, gpio.HIGH);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC)
end)
And below is the code for the client.
print("ESP8266 Client")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")
led = 4
gpio.mode(led, gpio.OUTPUT)
tmr.alarm(1, 2000, tmr.ALARM_AUTO, function()
if(wifi.sta.getip()~=nil) then
tmr.stop(1)
print("Connected to test!")
print("Client IP Address:",wifi.sta.getip())
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
tmr.alarm(2, 2000, 1, function()
cl:send("CLIENT /?pin=ON2 HTTP/1.1")
end)
cl:on("receive", function(conn, receivedData)
print("Received Data from server: " .. receivedData)
end)1
else
print("Connecting...")
end
end)
I know some parts of the codes are not complete. What I didn't understand is how to control the client's led via clicking on the 'ON' and 'OFF' buttons in the web page. Web page is a client as well and I think what I want to do is to control a client via another client.
Can you guys tell me if what I am trying to do is possible or not? Or is there another method to do this like using a UDP server ? Or maybe there is a specific method to send a message to connected clients via their IP or MAC adress, that would be much easier.
Thanks in advance.