#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private esp As ESP32extras
Private wifi As ESP8266WiFi
Private udp As WiFiUDP
Public bc As ByteConverter
Public ser As B4RSerializator
Private js_timer As Timer
Private btn_timer As Timer
Public cntr As Int = 0
'''''''''''''''''''''''''''''''''''''''''
Public myIP As String = "222.111.7.10"
Public myGateway As String = "222.111.7.10"
Public mySubnet As String = "255.255.255.0"
Private Switch, VRx, VRy As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
'*****************************************
esp.ConfigAP(myIP, myGateway, mySubnet)
wifi.StartAccessPoint("esp32_Base")
Log(wifi.AccessPointIp)
Log(wifi.LocalIp)
'*********
Switch.Initialize(33, Switch.MODE_INPUT_PULLUP) ' ESP32 Pin 33 (Joystick Button)
VRx.Initialize(35, VRx.MODE_INPUT) ' ESP32 Pin 35
VRy.Initialize(34, VRy.MODE_INPUT) ' ESP32 Pin 34
'
js_timer.Initialize("ReadJoystick", 500)
js_timer.Enabled = True 'don't forget to enable it
'
btn_timer.Initialize("ReadButton", 500)
btn_timer.Enabled = True 'don't forget to enable it
'*********
Log("Socket Init and Listening")
udp.Initialize(55777, "udp_PacketArrived")
End Sub
Sub ReadButton
Dim B_State As Boolean
B_State = Switch.DigitalRead
If Not(B_State) Then ' Button Pushed Down is Zero
Dim xmit_Data() As Object = Array As Object (0,0,0)
xmit_Data (0) = "ESP32"
xmit_Data(1) = "Test"
xmit_Data(2) = "Packet"
Send_Packet(xmit_Data)
End If
End Sub
Sub ReadJoystick
Dim X As Int = 0
Dim Y As Int = 0
Dim B_State As Boolean
X = VRx.AnalogRead
Y = VRy.AnalogRead
B_State = Switch.DigitalRead
' Log("X: ", X)
' Log("Y: ", Y)
' Log("B_State: ", B_State)
Dim xmit_Data() As Object = Array As Object (0,0,0)
xmit_Data(0) = B_State
xmit_Data(1) = X
xmit_Data(2) = Y
'**********************
' Check X/Y Values High or Low to make sure Joystick has been moved
If ((X < 1300) Or (X > 2400)) Then
Send_Packet(xmit_Data)
Else If ((Y < 1300) Or (Y > 2400)) Then
Send_Packet(xmit_Data)
End If
End Sub
Sub Udp_PacketArrived (Buffer() As Byte, Ip() As Byte, Port As UInt)
Log("PacketArrived: ", Buffer.Length)
Log("IP: ",Ip(0),".",Ip(1),".",Ip(2),".",Ip(3))
Log("Port: ",Port)
If Buffer.Length = 0 Then
Log("Packet Length 0 ")
Return 'invalid message
End If
If Buffer.Length > 50 Then
Log("Packet Length > 50")
Return 'invalid message
End If
'********************************************************************************
Dim be(50) As Object 'used as a storage buffer. Can be a global variable
Dim Data() As Object = ser.ConvertBytesToArray(Buffer, be)
'********************************************************************************
For Each o As Object In Data
Log("Data: ", o)
Next
cntr = cntr + 1
Log("Counter: ", cntr)
Data(0) = cntr
End Sub
Sub Send_Packet(xmit_Data()As Object)
'**********************************************************************
'''' IP/Port of Client "222.111.7.100" --- "55777"'''''''''''''''''''''
'**********************************************************************
Private ip() As Byte = Array As Byte(222,111,7,11)
Private port As UInt = 55777
udp.BeginPacket(ip, port)
udp.Write(ser.ConvertArrayToBytes(xmit_Data))
udp.SendPacket
Log("SentPckt: ",xmit_Data(0)," ",xmit_Data(1)," ",xmit_Data(2))
End Sub