im connecting to the battery via BMS, BMS is based on the query type where im trying to send the requestID, i couldn't connect it, its not working, kindly please update me, where i did the mistake and explain how should i have to handle it.
#include <mcp_can.h>
#include <SPI.h>
void voltage(void);
void current(void);
// CAN TX Variables
//unsigned long prevTX = 0;
//const unsigned int invlTX = 1000;
byte data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// CAN RX Variables
long unsigned int rxId ;
unsigned char len;
unsigned char rxBuf[8];
// CAN0 INT and CS
#define CAN0_INT 4 // #define MCP2515_CS_PIN 5 // Chip select pin
MCP_CAN CAN0(5);//#define MCP2515_INT_PIN 4
void setup() {
Serial.begin(115200);
if (CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
pinMode(CAN0_INT, INPUT);
}
void loop() {
byte sendV = CAN0.sendMsgBuf(0x18910180, 0, 0, data);
delay(100);
voltage();
byte sendC = CAN0.sendMsgBuf(0x18900180, 0, 0, data);
delay(1000);
current();
}
void voltage() {
if (CAN0.readMsgBuf(&rxId, &len, rxBuf)) {
byte voltage[8];
for (byte i = 0; i < len; i++) {
voltage[i] = rxBuf[i];
}
int voltageMax = ((voltage[0] << 8) | voltage[1]) * 0.001; // milli v to v
Serial.print("Voltagemax: ");
Serial.println(voltageMax);
int voltageMin = ((voltage[3] << 8) | voltage[4] ) * 0.001; // milli v to volt
Serial.print("Voltagemin: ");
Serial.println(voltageMin);
}
}
void current() {
if (CAN0.readMsgBuf(&rxId, &len, rxBuf)) {
byte current[8];
for (int i = 0; i < len; i++) {
current[i] = rxBuf[i];
}
int currentval = ((current[4] << 8 | current[5]) - 30000 ) * 0.01; // getting amps 0.1 converting from centi
Serial.print("current: ");
Serial.println(currentval);
}
}