Instalación y programa.
Se activan dos salidas del módulo ESP8266 a través de una página Web, funcionando como Webserver. La conexión se realiza por conexión wifi. El módulo funciona de forma independiente, sin necesitar arduino. Previamente se ha cargado el firmware NODEMCU y posteriormente el programa que viene a continuación.
La página Web es :
El código que hay que introducir en el ESP8266 es :
wifi.setmode(wifi.STATION) wifi.sta.config("NombreRouter","clavewifi") 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><CENTER> SALIDAS ESP 8266</CENTER></h1>"; buf = buf.."<p><CENTER>Salida 0 <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></CENTER></p>"; buf = buf.."<p><CENTER>Salida 2 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></CENTER></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); end client:send(buf); client:close(); collectgarbage(); end) end)