Current Lua downloadable firmware will be posted here

User avatar
By Kosi_101
#22657 I am currently working on a wifi garage door opener that has a ESP-01 board connected to a relay on GPIO2. I successfully managed to flash the lua interpreter on to the chip. After doing so I experimented with various examples on the net. Although the example were helpful in understanding the code, I couldn't find that provided what I was looking for. It would be appreciated if you guys could help me on this one. I need lua code for a webserver that has:
-Basic HTTP Authentication
-A button to switch GPIO2
-SOFTAP network mode
Thanks In advance.
User avatar
By mtlevine0
#22868 The Wikipedia article for basic HTTP auth is a good starting point. The username and password are stored in a request header like so: "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" where the string proceeding after basic is a base64 encoded string containing the credentials. Decoded, the credentials look like this "username:password". You would ideally be doing this over HTTPS to prevent someone from stealing the plain-text credentials. I don't think there has been successful HTTPS implementation on the ESP yet though.

https://en.wikipedia.org/wiki/Basic_acc ... entication
User avatar
By Kosi_101
#22889 Thanks for posting. I just searched for examples using http basic authentication and I found this
Code: Select allfunction setState(state)
     if(state == "ON")then
          gpio.write(4, gpio.HIGH);
     elseif(state == "OFF")then
          gpio.write(4, gpio.LOW);
     end
end
pin = 4;
gpio.mode(pin, gpio.OUTPUT);
 
file.open("pin", "r");
setState(file.readline());
file.close();
 
wifi.setmode(wifi.SOFTAP);
wifi.ap.config({ssid="test",pwd="12345678"});
 
srv=net.createServer(net.TCP);
srv:listen(80,function(conn)
     conn:on("receive", function(client,request)
          local buf = "HTTP/1.0 200 OK\r\n\r\n";
          local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)%?(.-) HTTP");
          if(method == nil)then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP"); end
          local _, _, auth = string.find(request, "%cAuthorization: Basic ([%w=\+\/]+)");--Authorization:
          if (auth == nil or auth ~= "dXNlcjoxMjM0")then --user:1234
               client:send("HTTP/1.0 401 Authorization Required\r\nWWW-Authenticate: Basic realm=\"NodeMCU\"\r\n\r\n<h1>Access DENIDED</h1>");
               client:close();
               return;
          end
          local _GET = {};
          if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end
          buf = buf.."<meta name=\"viewport\" content=\"initial-scale=1.5\"/><h3>NodeMcu Тест.</h3><form src=/>Состояние GPIO2 <select name=pin onchange=\"form.submit()\">";
          local _on,_off = "","";
          if(_GET.pin == nil)then
               if(gpio.read(pin) == gpio.HIGH)then
                    _GET.pin = "ON";
               else
                    _GET.pin = "OFF";
               end
          end
          local tr = " selected=true";
          if(_GET.pin == "ON")then
               _on = tr;
          elseif(_GET.pin == "OFF")then
               _off = tr;
          end
          setState(_GET.pin);
          file.open("pin", "w+");
          file.write(_GET.pin);
          file.close();
          buf = buf.."<option value=\"ON\"".._on..">ВКЛ</opton><option value=\"OFF\"".._off..">ОТКЛ</option></select></form>";
          client:send(buf);
          client:close();
          collectgarbage();
     end)
     
end)


I tried to compile it and ended up with no success. I am using ESPloere v0.2.0-rc2 and my board firmware version is "nodemcu_integer_0.9.6-dev_20150704". It would be appreciated if you could please look at this code and fix it so that it can compile successfully. I am very new to the whole lua environment. I have had some programming experience however that was quite a while ago.
User avatar
By mtlevine0
#22892 I haven't worked much with NodeMCU. I have been messing around with the Arduino IDE and prefer programming in C. You should give Arduino a look here: https://github.com/esp8266/Arduino

I'll look into working on implementing HTTP Auth into the existing basic webserver example that comes with the Arduino IDE over the weekend. I'll let you know my results in this thread.

Edit: Just realized this is the Lua subform, my bad I just found this thread from the bottom suggested thread bar. I didn't realize you were specifically looking for lua help.