Can bus network and MQTT

Hi all,
I have an arduino mega with ENC28J60 board, I use this configuration with MQTT to talk with home assistant.
Now I need to connect this arduino mega to a can bus network. So I need to add to the mega the mcp2515 board.

But the ethernet board and the can bus board use the SPI... So, can I cannect two SPI device to the mega?

Thanks

SPI is a bus protocol which means that you can add several devices to the bus data and clock pins and use a separate select pin for each to control which device you are communicating with

CS pin must be different is it right? So I need to change the pin number from the library?

Which library ?

It is not normal for a library to have a hard coded CS pin without the option to change the default without editing the library, if that is what you meant

Yes, I mean that....

For example with the library "EthernetENC.h" I don't define any pins in my sketch....
And this is the wiring:

This is the wiring of the can bus board:

two boards (ethernet and canbus) will share MISO/MOSI/SCK pins, is it right?

In this example I see that I can modify CS pin with the command:
MCP2515 mcp2515(10);


#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#include <DHT.h> //Library for using DHT sensor
#define DHTPIN A0
#define DHTTYPE DHT11

struct can_frame canMsg;
MCP2515 mcp2515(10);
DHT dht(DHTPIN, DHTTYPE); //initialize object dht for class DHT with DHT pin with STM32 and DHT type as DHT11

void setup(){
  while (!Serial);
  Serial.begin(9600);
  SPI.begin(); //Begins SPI communication
  dht.begin(); //Begins to read temperature & humidity sensor value
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){
  int h = dht.readHumidity(); //Gets Humidity value
  int t = dht.readTemperature(); //Gets Temperature value
  canMsg.can_id = 0x036; //CAN id as 0x036
  canMsg.can_dlc = 8; //CAN data length as 8
  canMsg.data[0] = h; //Update humidity value in [0]
  canMsg.data[1] = t; //Update temperature value in [1]
  canMsg.data[2] = 0x00; //Rest all with 0
  canMsg.data[3] = 0x00;
  canMsg.data[4] = 0x00;
  canMsg.data[5] = 0x00;
  canMsg.data[6] = 0x00;
  canMsg.data[7] = 0x00;
  mcp2515.sendMessage(&canMsg); //Sends the CAN message
  delay(1000);
}

So, after changed CS pin of canbus board, the wiring will be ok?

Yes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.