Arduino+mcp2515 sketch

Hello everyone. Please share a sketch for arduino mcp2515 to work with realdash

I don’t have a sketch for you, but I will say that since I don’t know how to code, almost at all, I’ve been very successful using AI, in my case I used Google Bard to help me write, and understand the code for an arduino sketch. I find bits of code and have Bard explain what each line does, or help me simplify some code that I come up with, so that it’s more elegant. I would start with the sketch that RealDash provides, and see if you can patch in what you need to make your CAN chip do what you want it to.

2 Likes

Here you have this is for r4 uno but should work on other aswell just change wifi library.

Make sure to setup mcp2515 library correct with chrystal etc.

#include <WiFiS3.h>
#include <CAN.h>

WiFiClient client;
char ssid[] = “HarleyDavidson”; // your network SSID (name)
char pass[] = “123456789”; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(35000);

const byte RD_FRAME_HEADER[] = {0x44, 0x33, 0x22, 0x11};
byte canFrameBuffer[16];

void setup() {
WiFi.config(IPAddress(192, 48, 56, 2));
status = WiFi.beginAP(ssid, pass);
delay(3000);

// Initialize CAN bus
if (!CAN.begin(500E3)) {
Serial.println(“Starting CAN failed!”);
while (1);
}
server.begin();
}

void loop() {
client = server.available();
if (client) {
while (client.connected()) {
int packetSize = CAN.parsePacket();
if (packetSize) {
unsigned long PacketId = CAN.packetId();
byte buf3[8];
int i = 0;
while (i < 8 && CAN.available()) {
buf3[i] = CAN.read();
i++;
}

    // Combine header, ID, and data into canFrameBuffer 
    memcpy(canFrameBuffer, RD_FRAME_HEADER, 4); 
    memcpy(canFrameBuffer + 4, (const byte*)&PacketId, 4); 
    memcpy(canFrameBuffer + 8, (const byte*)buf3, 8); 

    // Send the entire CAN frame to client 
    client.write(canFrameBuffer, 16); 
  } 
} 
client.stop(); 
Serial.println("client disconnected"); 

}
}