Sub Process_Globals
Public Serial1 As Serial
Private eth As Ethernet
Private udp As EthernetUDP
Private serverIp1() As Byte = Array As Byte(192, 168, 0, 104) 'IP Cell Phone 1
Private serverIp2() As Byte = Array As Byte(192, 168, 0, 106) 'IP Cell Phone 2
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private const serverPort As UInt = 56646'51042
Private leds(2) As Pin
Private btn As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
leds(0).Initialize(13, leds(0).MODE_OUTPUT)
btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
btn.AddListener("btn_StateChanged")
eth.Initialize(MacAddress, Array As Byte(192, 168, 0, 200))
Log("Connected to network. My Arduino ip address: ", eth.LocalIp)
'End If
udp.Initialize(5, "udp_PacketArrived")
End Sub
Sub udp_PacketArrived (Data() As Byte, Ip() As Byte, Port As UInt)
Log("PacketArrived from:")
PrintIp(Ip)
Log("Porta:",Port)
Log( "Packet:",Data(0),Data(1))
Log(" ")
If Data.Length < 2 Then Return
'first byte is the led index
'second byte is the state
leds(Data(0)).DigitalWrite(Data(1) = 1)
End Sub
Sub PrintIp(ip() As Byte)
Log("IP: ",ip(0),".", ip(1),".", ip(2),".", ip(3))
End Sub
Sub Btn_StateChanged (State As Boolean)
For i=1 To 2
If i=1 Then
udp.BeginPacket(serverIp1, serverPort)
Else If i=2 Then
udp.BeginPacket(serverIp2, serverPort)
End If
'(It makes more sense to send a single byte instead of a string.)
udp.Write("Button is ".GetBytes)
If State = False Then
udp.Write("down".GetBytes)
Log("down")
Else
udp.Write("up".GetBytes)
Log("up")
End If
udp.SendPacket
Next
End Sub