Tuesday, August 23, 2016

ESP8266 lua code to store and change SSID and Password

I've been messing with these ESP-12's with the NodeMCU firmware.  I thought a good way to start a project would be to check if the ESP has an IP address, and if not, go into Access Point mode to get a new SSID/Password from the user.  I searched around the internet for a good and easy way to set up a web server in AP mode to get a new SSID/Password in the case of moving a project or changing your home WiFi's password.  I didn't really find anything that I was happy with using, so I decided to write something.  I feel like the code I wrote could probably be cleaned up a bit, use text boxes to get the input from the user, etc., but I'm happy with how this works so far.  For the AP, I set the SSID to something like "try000000" and set the password to (you guessed it!) "000000".
Change the IP in client mode based on the ip of the device you want to connect.
The password change is stored in a txt file and can be read in the start.
GPIO is used to switch to AP mode.

wifi.setmode(wifi.STATION)
gpio.mode(4, gpio.OUTPUT)
file.open("protect.txt", "r")
a=file.readline()
print(a)
if string.find(a,"ssid=") then
  local startssid
  startssid = string.find(a, "ssid=") + 5 
  local endssid  = string.find(a, "&pass=") - 1
  newssid = string.sub(a, startssid, endssid)  --look for ssid in http call
  print("newssid "..newssid)  --prints to serial, for testing mostly
end
 if string.find(a,"pass=") then
       local startpass
  startpass = string.find(a, "pass=") + 5
       local endpass  = string.find(a, "!") - 1 
       newpass = string.sub(a, startpass, endpass)  --look for password in http call
       print("newpass "..newpass)  --prints to serial, for testing
end
file.close()
--wifi.sta.setip({ip="192.168.1.10",netmask="255.255.255.0",gateway="192.168.1.1"})
wifi.sta.config(newssid,newpass)
gpio.mode(3, gpio.INPUT)
while(1)
do
 if wifi.sta.getip() == nil then
     wifi.sta.config(newssid, newpass)   --set new ssid and pass
print("connecting")
 elseif gpio.read(3) == gpio.LOW then
wifi.setmode(wifi.SOFTAP)
cfg={}
cfg.ssid="thepasswordis12345678"
cfg.pwd="12345678"
wifi.ap.setip({ip="192.168.1.11",netmask="255.255.255.0",gateway="192.168.1.11"})
wifi.ap.config(cfg)  --setup ap
srv=net.createServer(net.TCP)   --make http server
     srv:listen(80,function(conn) 
     conn:on("receive",function(conn,payload) 
     print("payload "..payload) 
     conn:send("<h1> format is 192.168.1.11/ssid=(yourssid)&pass=(yourpass)!<br><br>Don't forget the exclamation!!!!</h1>")
       if string.find(payload,"ssid=") then
     local startssid
     startssid = string.find(payload, "ssid=") + 5 
     local endssid  = string.find(payload, "&pass=") - 1
     newssid = string.sub(payload, startssid, endssid)  --look for ssid in http call
     print("newssid "..newssid)  --prints to serial, for testing mostly
       end
       if string.find(payload,"pass=") then
         local startpass
    startpass = string.find(payload, "pass=") + 5
         local endpass  = string.find(payload, "!") - 1 
         newpass = string.sub(payload, startpass, endpass)  --look for password in http call
         print("newpass "..newpass)  --prints to serial, for testing
    srv:close()
        end
  file.open("protect.txt","w+")
       b="ssid="..newssid.."&pass="..newpass.."!"
       print(b)
       file.writeline(b)
       file.close()
    end )
end)
file.open("protect.txt","w+")
    b="ssid="..newssid.."&pass="..newpass.."!"
    print(b)
    file.writeline(b)
    file.close()
else if wifi.sta.getip()
print(wifi.sta.getip())
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>HOTME</h1>";
        buf = buf.."<p>LIGHT 1       >> <a href=\"?pin=ON1\"><button>UP</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>DOWN</button></a></p>";
        buf = buf.."<p>LIGHT 1>> <a href=\"?pin=ON2\"><button>OFF</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>ALL</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              
 gpio.write(4, gpio.HIGH);
 print("j0*")
 
        elseif(_GET.pin == "OFF1")then
              
 gpio.write(4, gpio.LOW);
 print("j1*")
 
        elseif(_GET.pin == "ON2")then
              
 gpio.write(4, gpio.HIGH);
 print("j2*")
 
        elseif(_GET.pin == "OFF2")then
              
 gpio.write(4, gpio.LOW);
 print("j3*")
 
        end
   client:send(buf);
   client:close();
   collectgarbage();
   end)
   end)
   end 
   end

The instability in the code can be eliminated by fragmenting the code into different files .
The content of text file to store SSID and Password is 

ssid=ConnectFree&pass=123456! 

The text file can be uploaded into hardware normally in lua loader. For testing after changing SSID and Password txt file can be read to see the changes.

No comments:

Post a Comment

What is the difference between Socket and Port?

Socket Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to ...