B4R Question ESP8266 and CAN bus

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Any suggestion how to try ESP8266 with MCP2515 adapter ?
1620895007890.png
 

peacemaker

Expert
Licensed User
Longtime User
Wiring:
MCP2515 SCLK -> ESP8266 D5 (GPIO14)
MCP2515 MISO-> ESP8266 D6 (GPIO12)
MCP2515 MOSI -> ESP8266 D7 (GPIO13)
MCP2515 CS -> ESP8266 D8 (GPIO15)

Testing code:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public can As MCP2515
    Private timReceive As Timer
    Private timSend As Timer
    Public bc As ByteConverter
    #if Sender
        Public MyCAN As ULong = 1
    #else if Receiver
        Public MyCAN As ULong = 2
    #end if
    Public StartData As ULong = 0
End Sub

Private Sub AppStart
    Serial1.Initialize(256000)
    Log("AppStart")
    others.Get_MacAddress
    Log("Device_ID = ", bc.HexFromBytes(others.MacArray))
  
    'The constants For the baud settings are:
    '1 = CAN_BAUD_10K
    '2 = CAN_BAUD_50K
    '3 = CAN_BAUD_100K
    '4 = CAN_BAUD_125K
    '5 = CAN_BAUD_250K
    '6 = CAN_BAUD_500K
    Dim inited As Boolean = can.InitCAN(3)
    Log("CAN inited = ", inited)
    If inited Then
        can.SetCANNormalMode(False)
        timReceive.Initialize("timReceive_Tick", 200)
        timSend.Initialize("timSend_Tick", 1000)
        timReceive.Enabled = True
        timSend.Enabled = True
    End If
End Sub

Sub timSend_Tick
    #if Sender
    StartData = StartData + 1
    Dim data(4) As Byte
    data(0) = StartData
    Dim result As Boolean = can.TransmitCANMessage(data, 2, 10)
    If result Then
        Log("Sent OK: ", StartData)
    End If
    #end if
End Sub


Sub timReceive_Tick
    #if Receiver
    Dim result As Boolean = can.ReceiveMessage(10)
    'Log("Receiving result=", result)
    If result Then
        Dim ReceivedAddress As ULong = can.ReceivedAddress
        Log("ReceivedAddress=", ReceivedAddress)
        If ReceivedAddress = MyCAN Then    'for me
            Dim ReceivedData() As Byte = can.ReceivedData
            Log("Received from ", ReceivedAddress, ": ", bc.StringFromBytes(ReceivedData))
        End If
    End If
    #end if
End Sub

Using Erel's lib v.1.1 - the init = 1, seems inited OK.
The sender board sending log:

B4X:
********************* PROGRAM STARTING ****************
<�<�<�<�<�<��<<<�<��������<����<��<�<�<��<<<<�<<<�����<��<�<�<�<�<�<�<�<<<��<���<���<<<�<<�<�<�<<�<��<���<����<���<��<<<�<����<�����<���<�<�<��<�<�����<�<�<<��<�<��<�<�<�<<<�<<<��<��<<�<�<�<�<<<��<��<<�<�<��<��������<<<�<<<��<�<�<�<<AppStart
Device_ID = 40F5203F52B0
CAN inited = 1
Sent OK: 1
Sent OK: 2
Sent OK: 3
Sent OK: 4
Sent OK: 5
Sent OK: 6
Sent OK: 7

But the receiver board shows some random:
B4X:
CAN inited = 1
ReceivedAddress=570
ReceivedAddress=1027
ReceivedAddress=432506841
ReceivedAddress=1084
ReceivedAddress=2044
ReceivedAddress=536840071
ReceivedAddress=2044
ReceivedAddress=325
ReceivedAddress=435259179
ReceivedAddress=1569
ReceivedAddress=455696319
ReceivedAddress=2044
ReceivedAddress=536840071
ReceivedAddress=2044
ReceivedAddress=1084
ReceivedAddress=0
ReceivedAddress=167641306
ReceivedAddress=505323008
ReceivedAddress=1084
ReceivedAddress=2044
ReceivedAddress=1084
ReceivedAddress=1084
ReceivedAddress=285148087
ReceivedAddress=0
ReceivedAddress=167641306
ReceivedAddress=505323008
ReceivedAddress=1084
ReceivedAddress=2044

Any suggestion ?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Now i doubt already about the wiring between adapters. Maybe something wrong just to pass signals between boards: i use just a 30 cm of LAN twisted pair.
And no ESP32 boards to test with them, as mostly detailed\proved solution, instead of ESP8266.
But in my tasks ESP32 is overkill actually, so, i'm suffering with ESP8266...
 
Upvote 0
Top