#Region Project Attributes
#AutoFlushLogs: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public TimerHB As Timer
Public TimerRFID As Timer
Private astream As AsyncStreams
Private eth As Ethernet
Private ethClient As EthernetSocket
Private btn As Pin
Private serverIp() As Byte = Array As Byte(10, 0, 0, 140)
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private const serverPort As UInt = 11000
Private RFID As MFRC522
Private bc As ByteConverter
Private pin9, pin10, pin11, pin12, pin13 As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
pin9.Initialize(9, pin9.MODE_OUTPUT)
pin10.Initialize(10, pin10.MODE_OUTPUT)
pin11.Initialize(11, pin11.MODE_OUTPUT)
pin12.Initialize(12, pin12.MODE_OUTPUT)
pin13.Initialize(13, pin13.MODE_OUTPUT)
SetSPI(False)
If eth.InitializeDHCP(MacAddress) = False Then
Log("Error connecting to network.")
Return
Else
Log("Connected to network. My ip address: ", eth.LocalIp)
End If
btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
btn.AddListener("Btn_StateChanged")
TimerHB.Initialize("TimerHB_Tick", 60000)
SetSPI(True)
RFID.Initialize(10, 9, "rfid_CardPresent")
SetSPI(False)
' TimerRFID.Initialize("TimerRFID_Tick", 1000)
' TimerRFID.Enabled = True
Connect(0)
End Sub
Sub SetSPI(state As Boolean)
pin9.DigitalWrite(state)
pin10.DigitalWrite(state)
pin11.DigitalWrite(state)
pin12.DigitalWrite(state)
pin13.DigitalWrite(state)
End Sub
Sub TimerHB_Tick
Dim message() As Byte
message = "|HB00101??@"
message(0) = 02
message(10) = 04
Log(message)
astream.Write(message)
End Sub
Sub TimerRFID_Tick
'SetSPI(True)
End Sub
Sub Btn_StateChanged (State As Boolean)
If ethClient.Connected Then
Dim s() As Byte
TimerHB.Enabled = False
'If State Then s = 1 Else s = 0
s = "|GT00101@@"
astream.Write(s)
'astream.Write(Array As Byte(s))
TimerHB.Enabled = True
End If
End Sub
Sub Connect(unused As Byte)
Dim message() As Byte
If ethClient.ConnectIP(serverIp, serverPort) = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to server")
astream.Initialize(ethClient.Stream, "Astream_NewData", "Astream_Error")
message = "|GT00101@@"
'message(0) = 02
astream.Write(message)
TimerHB.Enabled = True
End Sub
Sub Astream_NewData (Buffer() As Byte)
Log("Received from server: ", Buffer) 'pass the array of bytes. Don't convert to string.
End Sub
Sub Astream_Error
Log("error")
ethClient.Close
CallSubPlus("Connect", 1000, 0)
End Sub
Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
Log("UID: ", bc.HexFromBytes(UID))
End Sub