- Tue Dec 02, 2014 1:50 am
#3780
I've written code to do the following:
- When you power-cycle the module, it checks to see if has an AP IP address. If it doesn't, then it configures itself as an AP with a known SSID and password. This currently causes the module to reboot, but the module gets configured as an AP and assigns itself an IP address.
- After the automatic reboot, we call another script: "onboardServer.lua". In this script, the module listen on port 80 and allows a browser (PC, iOS/Android device) to configure it by issuing the following http command (the & at the end is needed. 192.168.4.1 is the IP address that my ESP module assigns to itself. Your device that issues the http command needs to be on the wifi network that the ESP module creates with SSID="esp8266" and password="12345678" (or whatever you set these values to be in the init.lua).
Code: Select allhttp://192.168.4.1/SSID=ssid_of_your_wifi_network&PASSWORD=password_of_your_wifi_network&
This allows you to use your phone to configure your ESP module with your home WIFI credentials by just using a browser. This is typical "onboarding" process and is used in most devices without a monitor and keyboard.
I understand that you want the browser to configure you AP credentials. You should be able to modify this easily to do that.
init.lua
Code: Select allif (wifi.ap.getip() == "0.0.0.0") then
print("Configuring as AP...")
wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ssid="esp8266",pwd="12345678"})
else
print("Waiting to be configured with the wifi credentials...")
dofile("onboardServer.lua")
end
print("AP address: " .. wifi.ap.getip())
onboardServer.lua
Code: Select allss=net.createServer(net.TCP)
ss:listen(80,function(c)
c:on("receive",function(c,pl)
print(pl)
ssidBegin = string.find(pl, "=", 0)+1
ssidEnd = string.find(pl, "&", 0)-1
passBegin = string.find(pl, "=", ssidEnd+2)+1
passEnd = string.find(pl, "&", ssidEnd+2)-1
ssidName = string.sub(pl, ssidBegin, ssidEnd)
pass = string.sub(pl, passBegin, passEnd)
print ("Got SSID: " .. ssidName)
print ("key: " .. pass)
c:send("HTTP/1.1 200 OK\n\n")
c:send("<html><body>")
c:send("<h1>Your ESP device is now connected to the following SSID.</h1><BR>")
c:send("SSID : " .. ssidName .. "<BR>")
c:send("key : " .. pass .. "<BR>")
c:send("</html></body>")
c:send("\nTMR:"..tmr.now().." MEM:"..node.heap())
c:on("sent",function(c) c:close() ss:close() end)
wifi.sta.config(ssidName,pass)
print("Connected to your wifi network!")
end)
end)