Sub Process_Globals
Public Serial1 As Serial
Private usocket As WiFiUDP
Private wifi As ESP8266WiFi
Private fs As ESP8266FileSystem
Private ip(4) As Byte
Private port As UInt = 3661
Private astream As AsyncStreams
Private bc As ByteConverter
Private lcd As LiquidCrystal
Private TextLine(5) As String = Array As String (" ABCDEFGHIJKLMNO", "PQRSTUVWXYZ01234", "56789abcdefghijk", "lmnopqrstuvwxyz?", "!@#$%^&*()+-=_/ ")
Private PIN_A0 As Pin
Private btnValues() As UInt = Array As UInt(50, 250, 450, 650, 850, 1025) ' RIGHT, UP, DOWN, LEFT, SELECT, NO BUTTON
Private btn As UInt
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
lcd.Initialize(16, 255, 5, Array As Byte (12, 13, 14, 15))
lcd.Begin(16, 2)
PIN_A0.Initialize(PIN_A0.A0, PIN_A0.MODE_INPUT)
If(fs.initialize) Then
Log("File system successfully initialized")
Else
Log("Could not initialize filesystem")
End If
SelectNetwork
CheckForPassword
ConnectToNetwork
ListFiles
End Sub
Sub astream_NewData (Buffer() As Byte)
usocket.BeginPacket(ip, port)
For Each s() As Byte In bc.Split(Buffer, CRLF)
If s.Length <> 0 Then
usocket.Write(s)
usocket.SendPacket
If bc.StringFromBytes(bc.SubString2((s),0, 1)) = "T" Then
lcd.Clear
lcd.SetCursor(0,0)
lcd.Write(bc.StringFromBytes(bc.SubString2((s),1,6)))
End If
If bc.StringFromBytes(bc.SubString2((s),0, 1)) = "S" Then
lcd.SetCursor(6,0)
lcd.Write(bc.StringFromBytes(bc.SubString2((s),1,11)))
End If
If bc.StringFromBytes(bc.SubString2((s),0, 1)) = "H" Then
lcd.setcursor(0, 1)
lcd.Write(bc.StringFromBytes(bc.SubString2((s),1,3)))
End If
If bc.StringFromBytes(bc.SubString2((s),0, 1)) = "F" Then
lcd.setcursor(5,1)
lcd.Write(bc.StringFromBytes(bc.SubString2((s),1,10)))
End If
End If
Next
End Sub
Sub usocket_PacketArrived (Data() As Byte, ip1() As Byte, port1 As UInt)
'not used
Log("Packet arrived")
End Sub
Sub astream_Error
Log("Error")
End Sub
Sub SelectNetwork 'List all SSIDs & select desired one via LCD UP/DOWN/SELECT buttons
Dim i As UInt = 0
btn = 5 'Reset button value
lcd.Clear
lcd.Write("Scan for network")
Dim numberOfNetworks As Byte = wifi.Scan
Log("Found ", numberOfNetworks, " networks")
For c = 0 To numberOfNetworks - 1
Log("SSID: ", wifi.ScannedSSID(c), "|RSSI: ", wifi.ScannedRSSI(c))
Next
lcd.Clear
lcd.Write("Select SSID ")
lcd.Write("1/")
lcd.Write(numberOfNetworks)
lcd.SetCursor(0, 1)
lcd.Write(wifi.ScannedSSID(i))
Do Until btn = 4 'SELECT
Dim reading As UInt = PIN_A0.AnalogRead
For j = 0 To btnValues.Length - 1
If reading < btnValues(j) Then
btn = j
Exit
End If
Next
If btn = 1 And i > 0 Then 'UP
i = i - 1
lcd.Clear
lcd.Write("Select SSID ")
lcd.Write(i + 1)
lcd.Write("/")
lcd.Write(numberOfNetworks)
lcd.SetCursor(0, 1)
lcd.Write(wifi.ScannedSSID(i))
End If
If btn = 2 And i < numberOfNetworks - 1 Then 'DOWN
i = i + 1
lcd.Clear
lcd.Write("Select SSID ")
lcd.Write(i + 1)
lcd.Write("/")
lcd.Write(numberOfNetworks)
lcd.SetCursor(0, 1)
lcd.Write(wifi.ScannedSSID(i))
End If
Delay(100)
Loop
GlobalStore.Put(0, wifi.ScannedSSID(i)) 'SSID
End Sub
Sub EnterPassword 'Enter password for selected SSID via LCD UP/DOWN/LEFT/RIGHT/SELECT buttons
Dim i As UInt = 0 'TextLine() line
Dim j As UInt = 0 'Column within TextLine
Dim char As String = ""
Dim pwd As String = ""
btn = 5 'Reset button value
lcd.Clear
lcd.Write("Enter password")
lcd.SetCursor(0,1)
lcd.Write(TextLine(i))
lcd.SetCursor(0, 1)
lcd.Blink = True
Do Until char = " "
Dim reading As UInt = PIN_A0.AnalogRead
For k = 0 To btnValues.Length - 1
If reading < btnValues(k) Then
btn = k
Exit
End If
Next
If btn = 1 And i > 0 Then 'UP
i = i - 1
lcd.Clear
lcd.Write(pwd)
lcd.SetCursor(0, 1)
lcd.Write(TextLine(i))
lcd.SetCursor(0, 1)
lcd.Blink = True
j = 0
End If
If btn = 2 And i < 4 Then 'DOWN
i = i + 1
lcd.Clear
lcd.Write(pwd)
lcd.SetCursor(0, 1)
lcd.Write(TextLine(i))
lcd.SetCursor(0, 1)
lcd.Blink = True
j = 0
End If
If btn = 3 And j > 0 Then 'LEFT
j = j - 1
lcd.SetCursor(j, 1)
End If
If btn = 0 And j < 15 Then 'RIGHT
j = j + 1
lcd.SetCursor(j, 1)
End If
If btn = 4 Then 'SELECT
char = bc.StringFromBytes(bc.SubString2(TextLine(i), j, j+1))
If char <> " " Then
pwd = JoinStrings (Array As String(pwd, char))
lcd.SetCursor(0, 0)
lcd.Write(pwd)
lcd.SetCursor(j, 1)
Log(pwd)
End If
End If
If btn = 3 And j = 0 And pwd <> "" Then 'BACKSPACE using LEFT button when cursor is at start of row
pwd = bc.StringFromBytes(bc.SubString2(bc.StringToBytes(pwd),0, pwd.Length - 1))
lcd.SetCursor(0, 0)
For c = 0 To pwd.Length + 1
lcd.write(" ")
Next
lcd.SetCursor(0, 0)
lcd.Write(pwd)
lcd.SetCursor(j, 1)
Log(pwd)
End If
Delay(100)
Loop
GlobalStore.Put(1, pwd) 'Password
lcd.Clear
lcd.Write("Connecting to:")
lcd.SetCursor(0,1)
lcd.Write(GlobalStore.Slot0)
WriteFile
End Sub
Sub WriteFile 'Writes password in file named with the SSID
If fs.Exists(bc.StringFromBytes(GlobalStore.Slot0)) Then
fs.Remove(bc.StringFromBytes(GlobalStore.Slot0))
End If
If fs.OpenReadWrite(bc.StringFromBytes(GlobalStore.Slot0)) Then
Log("File successfully opened for write processing")
Dim buffer(bc.StringFromBytes(GlobalStore.Slot1).length) As Byte = bc.StringFromBytes(GlobalStore.Slot1).GetBytes
Log("Bytes written: ", fs.Stream.WriteBytes(buffer,0,buffer.Length))
Log("File system total size ", fs.TotalSize)
Log("File system used size ", fs.UsedSize)
Log("File size ", fs.CurrentFile.size)
fs.Close
End If
End Sub
Sub ReadSSIDFile 'Reads password from file named with the SSID
If fs.OpenRead(bc.StringFromBytes(GlobalStore.Slot0)) Then
Log("File successfully opened for read processing")
Log("File name ", fs.CurrentFile.Name)
Log("File size ", fs.CurrentFile.size)
fs.Position=0
Dim buffer(fs.CurrentFile.size) As Byte
Log("Number of bytes read: ", fs.Stream.ReadBytes(buffer,0,buffer.Length))
Log("Position after reading file: ", fs.Position)
Log("Data read: ", bc.StringFromBytes(buffer))
GlobalStore.Put(1, bc.StringFromBytes(buffer))
Log(GlobalStore.Slot0)
Log(GlobalStore.Slot1)
fs.Close
Else
Log("Cannot open file for read: ", bc.StringFromBytes(GlobalStore.Slot0))
End If
End Sub
Sub CheckForPassword 'Checks to see if there is an existing password file for the selected SSID
If fs.Exists(bc.StringFromBytes(GlobalStore.Slot0)) Then
lcd.Clear
lcd.Write("Use existing?")
lcd.SetCursor(0, 1)
lcd.Write("Yes / No")
btn = 5 'Reset button value
Do While True 'Until LEFT or RIGHT pushed
Dim reading As UInt = PIN_A0.AnalogRead
For j = 0 To btnValues.Length - 1
If reading < btnValues(j) Then
btn = j
Exit
End If
Next
If btn = 3 Then 'Yes using LEFT button
ReadSSIDFile
lcd.Clear
lcd.Write("Connecting to:")
lcd.SetCursor(0,1)
lcd.Write(GlobalStore.Slot0)
Return
End If
If btn = 0 Then 'No using RIGHT button
EnterPassword
Return
End If
Delay(100)
Loop
Else
EnterPassword
End If
End Sub
Sub ListFiles
Log("Listing files")
For Each f As File In fs.ListFiles("")
Log("Name: ", f.Name, ", Size: ", f.Size)
Next
End Sub
Sub ConnectToNetwork
If wifi.Connect2(bc.StringFromBytes(GlobalStore.slot0), bc.StringFromBytes(GlobalStore.Slot1)) = False Then
Log("Error connecting to network")
lcd.Clear
lcd.write("Error connecting")
Return
Else
Log("Connected to network: ", GlobalStore.Slot0)
lcd.Clear
lcd.write("Connected to:")
lcd.SetCursor(0,1)
lcd.Write(GlobalStore.Slot0)
lcd.Blink = False
Log("My IP address: ", wifi.LocalIp)
Dim index As UInt = 0
For Each s() As Byte In bc.Split(wifi.localIP, ".")
Dim b As Byte = bc.StringFromBytes(s)
ip(index) = b
index = index + 1
Next
ip(3) = 255
Log("Broadcast IP address: ", ip(0), ".", ip(1), ".", ip(2), ".", ip(3))
astream.Initialize(Serial1.Stream, "astream_NewData", "astream_Error")
usocket.Initialize(51042, "usocket_PacketArrived")
End If
End Sub