'Jully 2016
'Original Code by EREL
'Small Optimisations by Cableguy
'Call WiFiServer.Start(False) from your code
'It will force the esp to first check the Board for saved SSID and Password
'After 5 attemps, it will revert the board to AP mode
'
'SSID Checked for empty value
Sub Process_Globals
Private bc As ByteConverter
Private Astream As AsyncStreams
Private server As WiFiServerSocket
Private eeprom As EEPROM
Private const MAGIC_EEPROM As Byte = 123
Private APSSID As String = "esp"
Private bc As ByteConverter
Public WiFi As ESP8266WiFi
Public esp As ESP8266
Private NumberOfTries As Int
Private MaxNumberOfTries As Int = 5
End Sub
Public Sub Start(AP As Boolean)
If AP = False Then
NumberOfTries = 0
ConnectToNetwork
Else
Select WiFi.StartAccessPoint(APSSID)
Case 1
Log("Access Point Initialisation Successful")
Log("SSID: ",APSSID)
Log("ip: ", WiFi.AccessPointIp)
server.Initialize(80, "server_NewConnection")
server.Listen
Case 0
Log("Something Went wrong!")
WiFi.Disconnect
End Select
End If
End Sub
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub
Private Sub Astream_NewData (Buffer() As Byte)
Log(Buffer)
If bc.IndexOf(Buffer, "GET") <> -1 Then
If bc.IndexOf(Buffer, "/set") <> -1 Then
Dim ssid = "", password = "" As String
Dim i1 As Int = 0
Dim i2 As Int = 0
For Each b1() As Byte In bc.Split(Buffer, " ")
If i1 = 1 Then
For Each b2() As Byte In bc.Split(b1, "/")
Select i2
Case 2
ssid = bc.StringFromBytes(b2)
Case 3
password = bc.StringFromBytes(b2)
End Select
i2 = i2 + 1
Next
End If
i1 = i1 + 1
Next
Log("stact buffer :",StackBufferUsage)
Astream.Write("HTTP/1.1 200").Write(CRLF)
Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
Astream.Write("<script>setTimeout(function(){location.href=""http://192.168.4.1""} , 20000);</script>")
If ssid.Length = 0 Then
Astream.Write("SSID is invalid, please try again")
Else
Astream.Write("WiFi set to: ").Write(ssid).Write(", password: ").Write(password).Write("<br/>Please wait...")
SaveNetworkDetails(ssid, password)
CallSubPlus("ConnectWifi", 500, 0)
End If
Else If bc.IndexOf(Buffer, " / ") <> -1 Then
Astream.Write("HTTP/1.1 200").Write(CRLF).Write(CRLF)
If WiFi.IsConnected Then
Astream.Write("Connected to network.").Write(CRLF)
Astream.Write("ESP8266 IP address: ").Write(WiFi.LocalIp)
Else
Astream.Write("Not connected!")
End If
Else
Astream.Write("HTTP/1.1 404").Write(CRLF)
End If
CallSubPlus("CloseConnection", 200, 0)
End If
End Sub
Private Sub ConnectWifi(u As Byte)
Start(False)
End Sub
Private Sub CloseConnection(u As Byte)
Log("close connection")
If server.Socket.Connected Then
server.Socket.Stream.Flush
server.Socket.Close
End If
End Sub
Private Sub AStream_Error
Log("Disconnected")
server.Listen
End Sub
Public Sub ReplaceString(Original() As Byte, SearchFor() As Byte, ReplaceWith() As Byte) As Byte()
'count number of occurrences
Dim bc2 As ByteConverter
Dim c As Int = 0
Dim i As Int
If SearchFor.Length <> ReplaceWith.Length Then
i = bc2.IndexOf(Original, SearchFor)
Do While i > -1
c = c + 1
i = bc2.IndexOf2(Original, SearchFor, i + SearchFor.Length)
Loop
End If
Dim result(Original.Length + c * (ReplaceWith.Length - SearchFor.Length)) As Byte
Dim prevIndex As Int = 0
Dim targetIndex As Int = 0
i = bc2.IndexOf(Original, SearchFor)
Do While i > -1
bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, i - prevIndex)
targetIndex = targetIndex + i - prevIndex
bc2.ArrayCopy2(ReplaceWith, 0, result, targetIndex, ReplaceWith.Length)
targetIndex = targetIndex + ReplaceWith.Length
prevIndex = i + SearchFor.Length
i = bc2.IndexOf2(Original, SearchFor, prevIndex)
Loop
If prevIndex < Original.Length Then
bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, Original.Length - prevIndex)
End If
Return result
End Sub
Public Sub SaveNetworkDetails(SSID As String, Password As String)
Log("Saving network data: ", SSID, ", ", Password)
SSID = bc.StringFromBytes(ReplaceString(SSID, "%20", " "))
eeprom.WriteBytes(Array As Byte(MAGIC_EEPROM, SSID.Length + 1 + Password.Length), 0)
Dim pos As Int = 2
eeprom.WriteBytes(SSID, pos)
pos = pos + SSID.Length
eeprom.WriteBytes(Array As Byte(0), pos)
pos = pos + 1
eeprom.WriteBytes(Password, pos)
End Sub
Public Sub ConnectToNetwork
Log("disconecting WiFi...")
WiFi.Disconnect
'read settings from EEPROM
Dim header() As Byte = eeprom.ReadBytes(0, 2)
Do Until NumberOfTries = MaxNumberOfTries
Log("try number : ", NumberOfTries)
If header(0) = MAGIC_EEPROM Then
Dim data() As Byte = eeprom.ReadBytes(2, header(1))
Log ("DATA LENGTH =",data.Length)
Dim i As Int = bc.IndexOf(data, Array As Byte(0))
If i = -1 Then
Log("Error parsing network data.")
Return
End If
Dim SSID As String = bc.StringFromBytes(bc.SubString2(data, 0, i))
If SSID.Length = 0 Then Start(True)
Dim Password As String = bc.StringFromBytes(bc.SubString(data, i + 1))
If Password.Length = 0 Then Password = "none"
Log("Trying to connect to: ", SSID, " password: ", Password)
If WiFi.Connect2(SSID, Password) Then
Log("Connected successfully to: ", SSID)
Log(WiFi.LocalIp)
Else
Log("Failed to connect.")
End If
Else
Log("Network data not found.")
End If
NumberOfTries = NumberOfTries + 1
Loop
If NumberOfTries = MaxNumberOfTries Then
Start(True)
End If
End Sub