Advertisement
NittyGritty

mycode.lua

May 10th, 2016
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. print('\nSolar PV Controller - ESP8266 Server v1b\n')
  2. tmr.alarm(0, 1000, 1, function()
  3.    if wifi.sta.getip() == nil then
  4.       print("Connecting to AP...\n")
  5.    else
  6.       ip, nm, gw=wifi.sta.getip()
  7.       macAdd = wifi.sta.getmac();
  8.       print("IP Info: \nIP Address: ",ip)
  9.       print("Netmask: ",nm)
  10.       print("Gateway Addr: ",gw,'\n')
  11.       print("Mac Addr: ",macAdd,'\n')
  12.       tmr.stop(0)
  13.    end
  14. end)
  15. Relay1 = 0
  16. Relay2 = 2
  17. gpio.mode(Relay1, gpio.OUTPUT)
  18. gpio.mode(Relay2, gpio.OUTPUT)
  19. srv=net.createServer(net.TCP)
  20. srv:listen(80,function(conn)
  21.     conn:on("receive", function(client,request)
  22.         local buf = "";
  23.         local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
  24.         if(method == nil)then
  25.             _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
  26.         end
  27.         local _GET = {}
  28.         if (vars ~= nil)then
  29.             for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
  30.                 _GET[k] = v
  31.             end
  32.         end
  33.         buf = buf.."<h1> Solar Relay ESP8266 Web Server</h1>";
  34.         buf = buf.."<h2> IOT Project irun4fun 2016</h2>";
  35.         buf = buf.."<p>Battery <a href=\"?pin=ON1\"><button>Power ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>Power OFF</button></a></p>";
  36.         buf = buf.."<p>PV Panel <a href=\"?pin=ON2\"><button>Power ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>Power OFF</button></a></p>";
  37.         local _on,_off = "",""
  38.         if(_GET.pin == "ON1")then
  39.               gpio.write(Relay1, gpio.HIGH);
  40.         elseif(_GET.pin == "OFF1")then
  41.               gpio.write(Relay1, gpio.LOW);
  42.         elseif(_GET.pin == "ON2")then
  43.               gpio.write(Relay2, gpio.HIGH);
  44.         elseif(_GET.pin == "OFF2")then
  45.               gpio.write(Relay2, gpio.LOW);
  46.         end
  47.         client:send(buf);
  48.         client:close();
  49.         collectgarbage();
  50.     end)
  51. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement