Left here for archival purposes.

User avatar
By rrezaii
#3575 Rebooting after configuring the AP is problematic and unnecessary.
For example, I'm trying to configure the wifi credentials using an http command so, initially I want the module to be configured as an AP. If the module reboots right after its is configured as an AP, then the rest of the script that configures the server cannot be executed.

Can you please make it such that the module does not reboot after configuring it with ssid and key?
User avatar
By gerardwr
#3596
rrezaii wrote:For example, I'm trying to configure the wifi credentials using an http command so, initially I want the module to be configured as an AP.


Would like to do a similar thing in my webserver code (published in the examples section).

My ideal would be to have a ESP that:
- webserver to work in AP + STATION mode
- configure the AP credentials for AP mode via STATION access

If you have any code to share that might help me do that, please do.

Currently I use a simple function:
Code: Select all-- connect as station to a Wifi Access Point
-- Use as : connecttoap("yourSSID","yourpassword")
connecttoap = function (ssid,pw)
    if wifi.sta.getip() == "0.0.0.0" then
      wifi.setmode(wifi.STATION)
      wifi.sta.config(ssid,pw)
    end
    print("Connected to " .. ssid .. " as " .. wifi.sta.getip())
end
User avatar
By rrezaii
#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)