I would like to present simple circuit which allow to control device via relay with usage of web server running on ESP8266 board. The main component is ESP8266 board which allow to run script in LUA language. This ESP8266 board is connected with relay module via pin headers. Here is relay module:
Coil of relay is driven by NPN transistor from GPIO16 pin (PIN0) of ESP8266 board. Here is schematic of relay module:
To ESP8266 board was uploaded code (init.lua) which do following things:
- creates WiFi software access point with static IP address,
- sets PIN D0 as output and proper state,
- creates web server with simple HTML page which allow to control relay remotely.
Here code of LUA script:
cfg={}
cfg.ssid="WiFiSwitch"
cfg.pwd="Password"
wifi.ap.config(cfg)
cfg={}
cfg.ip="192.168.1.1";
cfg.netmask="255.255.255.0";
cfg.gateway="192.168.1.1";
wifi.ap.setip(cfg);
wifi.setmode(wifi.SOFTAP)
gpio.mode(0, gpio.OUTPUT)
gpio.write(0, gpio.LOW)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,request)
print(request)
_, j = string.find(request, 'switchState=')
if j ~= nil then
command = string.sub(request, j + 1)
if command == 'on' then
gpio.write(0, gpio.HIGH)
else
gpio.write(0, gpio.LOW)
end
end
conn:send('HTTP/1.1 200 OK\n\n')
conn:send('<!DOCTYPE html>')
conn:send('<html lang="en">')
conn:send('<head><meta charset="utf-8" />')
conn:send('<title>Remote switch</title></head>')
conn:send('<body><h1 align="center">Remote switch</h1>')
if gpio.read(0) == gpio.HIGH then
switchValue = "ON"
else
switchValue = "OFF"
end
conn:send('<p>The switch is ' .. switchValue .. '</p>')
conn:send('<form method="post">')
conn:send('<input type="radio" name="switchState" value="on">ON</input><br />')
conn:send('<input type="radio" name="switchState" value="off">OFF</input><br />')
conn:send('<input type="submit" value="Switch" />')
conn:send('</form></body></html>')
conn:on("sent",function(conn) conn:close() end)
end)
end)



Top Comments