LUA questions

irun4fundotca
Posts: 2
Joined: Tue Apr 12, 2016 1:46 pm

LUA questions

Postby irun4fundotca » Tue Apr 12, 2016 1:53 pm

I created a shield for a esp8266-01 to run two relays but i'm not sure the commands from the webpage are programmed right as any button shows no change in the gpio pins, can someone verify the code is right?
init.lua as follows:

Code: Select all

wifi.setmode(wifi.STATION)

wifi.sta.config("ssid","weppassword")

print('\nSolar PV Controller - ESP8266 Server v1b\n')

tmr.alarm(0, 1000, 1, function()

if wifi.sta.getip() == nil then

print("Connecting to AP...\n")

else

ip, nm, gw=wifi.sta.getip()

macAdd = wifi.sta.getmac();

print("IP Info: \nIP Address: ",ip)

print("Netmask: ",nm)

print("Gateway Addr: ",gw,'\n')

print("Mac Addr: ",macAdd,'\n')

tmr.stop(0)

end

end)

Relay1 = 0

Relay2 = 2

gpio.mode(Relay1, gpio.OUTPUT)

gpio.mode(Relay2, 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> Solar Relay ESP8266 Web Server</h1>";

buf = buf.."<h2> IOT Project irun4fun 2016</h2>";

buf = buf.."<p>Battery Bank <a href=\"?pin=ON1\"><button>Power ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>Power OFF</button></a></p>";

buf = buf.."<p>Solar Array <a href=\"?pin=ON2\"><button>Power ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>Power OFF</button></a></p>";

local _on,_off = "",""

if(_GET.pin == "ON1")then

gpio.write(Relay1, gpio.HIGH);

elseif(_GET.pin == "OFF1")then

gpio.write(Relay1, gpio.LOW);

elseif(_GET.pin == "ON2")then

gpio.write(Relay2, gpio.HIGH);

elseif(_GET.pin == "OFF2")then

gpio.write(Relay2, gpio.LOW);

end

client:send(buf);

client:close();

collectgarbage();

end)

end)


Running nodemcu_integer_0.9.5_20150318.bin

irun4fundotca
Posts: 2
Joined: Tue Apr 12, 2016 1:46 pm

Re: LUA questions

Postby irun4fundotca » Wed Apr 13, 2016 8:51 am

Is there anyone here that understands the code well?

Who is online

Users browsing this forum: No registered users and 3 guests