B4R Tutorial ESP8266 - WiFi Remote Configuration

Edit: A simpler and more powerful tool is available: https://www.b4x.com/android/forum/threads/esp-configurator-based-on-b4rserializator.81452/

The attached project allows the user to configure the ESP8266 WiFi settings (SSID and password).

When the program starts it does several things:
1. Creates an access point named 'esp'.
2. Creates a WiFiServerSocket that listens on port 80.
3. Tries to connect to a network based on the last settings stored in the EEPROM (if such are available).

The ip address of the device in the esp network is known. It is 192.168.4.1.
This means that we can connect to the esp network and then access the board with this ip address from any browser.

The server accepts two requests:
http://192.168.4.1 - Shows the current status
http://192.168.4.1/set/<ssid>/<password> - Sets the new ssid and password. Note that the password is optional.

Examples:

http://192.168.4.1/set/dlink
http://192.168.4.1/set/iPhone (2)/12345678

The SSID and password are stored in the EEPROM so the values are kept after a restart.

Note that the #StackBufferSize attribute is set to 600 as the string related methods require a larger stack buffer.
 

Attachments

  • ESP8266WiFiRemote.zip
    2.6 KB · Views: 5,075
Last edited:

positrom2

Active Member
Licensed User
Longtime User
The PC not (it is on LAN), but therefore I tried with the android smartphone, connected to the esp network and calling that address.
 

positrom2

Active Member
Licensed User
Longtime User
After connecting with the Android to the esp and calling that address I get this B4R log, the Android saying "not connected" despite being connected to esp Wlan.
(Can't copy the text because of the junk characters.)
upload_2016-11-27_10-35-32.png
 

positrom2

Active Member
Licensed User
Longtime User
Yes, that works. On Android: Password etc had been set and then new page: "connected to network", "ESP8255 IP address: 192....
Thank you very much.
 

Bladimir Carrillo

Member
Licensed User
Longtime User
Hi all,
I am testing the provided code but have a problem when parse a large parameter on browser, the buffer received is not complete on Astream_NewData sub.
This is the url containing parsed paramter ....

http://192.168.4.1/set/Claro_NETWOR...1042/0/0/0/0/1/0/0/0/0/0/1/2/3/4/5/6/7/8/9/10

The uncomplete text parsed is ...
Claro_NETWORKSSID000012345/3915852319442/192/168/0/3/51042/0/0/0/0/1/0/0/0/0/0/1/2/3/4/5/6/7

Any idea ?
 

Peter Simpson

Expert
Licensed User
Longtime User
This is absolutely brilliant @Erel, I've been trying to create my own version of this all day on my ESP8266 devices, then I came across this post. Your solution worked 'First Time of asking' with ease. I tested it on all my mobile devices and laptops, it just works seamlessly, and setting up the network is a breeze through the browsers.

Cheers, you have once again saved us all plenty of time with an absolutely great solution.

I do have one question though. Ever since writing to the EEPROM on 2 of my ESP8266 devices, I now have to add a delay of around 2 seconds just under 'Serial1.Initialize(115200)'. I take it that writing to the EEPROM somehow causes something to happen speed wise for a second or two at first boot. I mean nothing serious, maybe because it reading the EEPROM. But without the delay it will miss the start of the program.

It really does not matter though, as adding the 2 second delay remedies the situation, I was just wondering why it happens though?

Thank you again :D
 

pappicio

Active Member
Licensed User
Longtime User
Hi all,with example on page 1, I've coded a home automation relays with a wemos mini, and configured also remote access by username and password. the problem now is:
by using GET, all works well, but username and password are visible in web page title, while, using POST, the http buffer is truncated and cannot see in header username and password to process them, but, if I configure also:
Astream.WaitForMoreDataDelay = 500
Astream.MaxBufferSize = 512

the I can see complete http POST header

B4X:
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
   Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
   'configure them only with POST Connections
   Astream.WaitForMoreDataDelay = 500
   Astream.MaxBufferSize = 512
End Sub

but in this way, when call by webpage the POST, the esp8266 restart, with error (3), and sometime with error (9) or erro (2), there is other way to obtain all header chars, without use:
Astream.WaitForMoreDataDelay = 500
Astream.MaxBufferSize = 512

becouse all seems to work well, but only with GET, cos HTTP buffer header is shorter.
thanks a lot.
 

pappicio

Active Member
Licensed User
Longtime User
Hi all,with example on page 1, I've coded a home automation relays with a wemos mini, and configured also remote access by username and password. the problem now is:
by using GET, all works well, but username and password are visible in web page title, while, using POST, the http buffer is truncated and cannot see in header username and password to process them, but, if I configure also:
Astream.WaitForMoreDataDelay = 500
Astream.MaxBufferSize = 512

the I can see complete http POST header

B4X:
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
   Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
   'configure them only with POST Connections
   Astream.WaitForMoreDataDelay = 500
   Astream.MaxBufferSize = 512
End Sub

but in this way, when call by webpage the POST, the esp8266 restart, with error (3), and sometime with error (9) or erro (2), there is other way to obtain all header chars, without use:
Astream.WaitForMoreDataDelay = 500
Astream.MaxBufferSize = 512

becouse all seems to work well, but only with GET, cos HTTP buffer header is shorter.
thanks a lot.

okok, solved with POST in http header, so now no more visible userame and password, thanks a lot!
 

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
Is it possible to store the IP address and port number of a server running mqtt broker service in EEPROM via the same method? i.e; accessing the esp through browser.
regards,
 

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
Tried but not able to succeed. Can anyone please post a code snippet for configuring mqtt server ip and port number via browser?
 
Top