Skip to content

Instantly share code, notes, and snippets.

@yildak
Last active October 17, 2015 20:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yildak/66b210174517564c7651 to your computer and use it in GitHub Desktop.
Save yildak/66b210174517564c7651 to your computer and use it in GitHub Desktop.
esp-12 standalone mode
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("SSID","PASS")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
tmr.delay(3000)
dofile("webapp.lua")
end
end)
id = 0
pin = 7
gpio.mode(7, gpio.OUTPUT)
print("webapp running @ ip")
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> hello akin</h1><form src=\"/\">control outlet 1 <select name=\"pin\" onchange=\"form.submit()\">";
local _on,_off = "",""
if(_GET.pin == "ON")then
_on = " selected=true";
gpio.write(7, gpio.LOW);
elseif(_GET.pin == "OFF")then
_off = " selected=\"true\"";
gpio.write(7, gpio.HIGH);
end
buf = buf.."<option".._on..">ON</option><option".._off..">OFF</option></select></form>";
lightVal = adc.read(0)
buf = buf.."sensor value:"..lightVal;
client:send(buf);
client:close();
collectgarbage();
end)
end)
@yildak
Copy link
Author

yildak commented Mar 27, 2015

wiring; http://www.instructables.com/image/FHBYOBHI7RG9PVB
webapp screenshot; http://www.instructables.com/image/FPKIUL3I7RG9PV9
.
make sure that your esp8266 chip has NodeMCU running; http://www.instructables.com/id/Low-cost-WIFI-temperature-data-logger-based-on-ESP/step2
.
and also make sure that you have ESPlorer downloaded to use in programming the esp8266 chip; http://esp8266.ru/esplorer/#download
.
first "save to esp" the webapp.lua and then "save to esp" init.lua using the ESPlorer
remember to change your SSID and PASS inside init.lua
.
you can reach me at; http://www.instructables.com/member/Akin+Yildiz/ for any questions
thank you ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment