Sunday, April 13, 2014

Listen for a spesiffic Message on the CAN-BUS

Listening for a specific Message on the CAN-BUS.

When turning on the High Beams of the Jeep (JK 2010 2dr) it send a can message
with ID 680, 32,0,0,0,0,0 when its on and 0,0,0,0,0,0 when its off.



#include <SPI.h>
#include "mcp_can.h"
INT32U canId = 0x000;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
String CanMessage="";
int HeadLightsDetected=0;
void setup()
{
    Serial.begin(115200);
START_INIT:

    if(CAN_OK == CAN.begin(CAN_125KBPS))
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}
void loop()
{
    if(CAN_MSGAVAIL == CAN.checkReceive()) 
    {
        CAN.readMsgBuf(&len, buf);
        CanMessage="";
        canId = CAN.getCanId();
       
        //Detect Main Beam HeadLisghts
        if (canId==680)
        {
          //Build Complete Message Without CAN ID From BUS
          for(int i = 0; i<len; i++)    // print the data
          {
            CanMessage = CanMessage + buf[i];
          }
          //Check Main Beams / Brights
          if (CanMessage=="3200000"){HeadLightsDetected=1;}else{HeadLightsDetected=0;}
        
          if (HeadLightsDetected==1)
          {
            //PUT CODE HERE To TURN ON Transistor/Relays
            Serial.println("Brights turned on!!!!!");
          }
        }
       

       
       

       
    }
}

No comments:

Post a Comment