B4R Question Ethernet shield and RC522

Helmut S

Member
Licensed User
Longtime User
Hello Community,

I would like to use the the ethernet shield together with the RFID reader RC522. Is that possible with the Uno?

IMG_20170805_084857.jpg


Separately used they work fine, but when I use the ethernet shield the reader is not recognized. (the reader connections were plugged in when testing, of course ;-))

Thanks for your responses in advance.

H.
 

Helmut S

Member
Licensed User
Longtime User
here are Code and wiring

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public TimerHB 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
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    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)
    RFID.Initialize(10, 9, "rfid_CardPresent")
    Connect(0)
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 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

RFID_ETH_1.jpg
 
Upvote 0

Helmut S

Member
Licensed User
Longtime User
Hi Erel, I guess you are right, but at the moment I only have the Uno R3. Here ist the code, could you have a quick look at it?

B4X:
#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

The RC522 doesn't work at all, the device connects to the network but immediately reports an error, gets disconnected and connects again, and so on...

Log:

Connected to network. My ip address: 10.0.0.39
Connected to server
error
Connected to server
error
Connected to server
error
Connected to server
error
Connected to server
error
Connected to server
error
...
 
Upvote 0

Helmut S

Member
Licensed User
Longtime User
Hello everyone,

just to give you a short update on the current status of the project. The entire problem was solved by using a MEGA2569 board. Now even the network shield, the card reader, an LCD display and an RTC work perfectly together.

Thanks to all for your valuable help.

H.
 
Upvote 0
Top