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!!!!!");
          }
        }
       

       
       

       
    }
}

Friday, April 4, 2014

Martin's CAN-BUS Sniffer Tool V1.0 Utillity


This Utility has been abandoned! And completely re-developed to connect directly to the Arduino Instead of analyzing saved data

Please see 

http://jeepjkcanbus.blogspot.com/p/can-bus-supersniffer.html


Purpose of the "Martin's CAN-BUS Sniffer Tool"
Developed Using Visual Basic 6
Downloadable from git-hub
Compares incoming CAN-BUS data with common Vehicle Idle Messages.
When the vehicle's Key is in the on position "Engine Not Started" The can-bus sends regular messages on a very frequent basis.
I.E when monitoring the can bus and saving the data to text file for approximately 5 Minutes will generate about 1Mb of data
1 Mb of text data = 1,048,576 characters or 873 pages worth of data. Of which 1 can bus message could contain 1 message (27)
Bytes which means that in between a million keyboard characters will be the message you are looking for.

How it works.
You need to generate a database before you start using Martin's CAN-BUS Sniffer, by hooking up the can-bus device to the vehicles CAN-BUS.
Use a serial monitoring tool such as putty and log the data to file, The longer you run it the better.
If you let it run for about 5 Minutes it will generate enough data.

Martin's CAN-BUS Sniffer Tool, will by default load "\ignoredatabase.can" in the application directory.
So you can just copy and rename the log file you generated into the same path as the "Martin's CAN-BUS Sniffer Tool" exe file.

Comparing data.
By default the "Martin's CAN-BUS Sniffer Tool" loads the "\incomming.can" log file,
You need to generate this file the same way you generated the initial ingnoredatabase.can file.
I.E connect the arduino CAN-BUS device to your laptop, open putty specify a new log file called I.E "LeftBlinker.can"
Conect with putty then when ready connect the Arduino CAN-BUS interface to the vehicle and turn on the left blinker and as quickly as quickly as
possible disconnect the Arduino CAN-BUS interface or disconnect the putty session, so that minimal data can be generated.
Then rename and copy "LeftBlinker.can" to "\incomming.can" also in the same path as "Martin's CAN-BUS Sniffer Tool" exe file.

Start "Martin's CAN-BUS Sniffer Tool" then load both files by clicking the load button next to each file's path and then finally click compare.
It will then dump all unuiqe messages into the log box below ,the messages displayed will be related to the buttons you pressed in the vehicle in this case the left blinker.

You can also use the serial connection to do this on a live basis.
Simply select the COM port to which the Arduino CAN-BUS Shield is connected to and set the baud rate. (Default baud rate = 115200)

Extra Information:
All tests were performed on a Jeep Wrangler (AKA Jeep JK) Rubicon 2010 2dr Manual.
The Interior can-bus can be accessed by connecting to the White/Orange CAN-L  & White/Grey CAN-H
The bus speed should be set to 125Kb/s in the Arduino Shield's Bus Init section. I.E if(CAN_OK == CAN.begin(CAN_125KBPS)).........
If the BUS speed was not set correctly the Cluster will bing a couple of times and multiple dash lights will appear as well as the wipers will move.
This will not harm it , if you managed to swop the CAN-H & CAN-L pos/neg wires, it shouldn't blow, whilst testing i managed to do this and it didnt damage
Although its not recommended.

WHAT EVER YOU DO! DO NOT ACCIDENTALLY CONNECT 12V Vehicle Power or power from the LAPTOP into THE CAN BUS!
The results are unknown but it cant be good.

Tuesday, April 1, 2014

Listen to CAN-BUS Messages using the Arduino & Seeed CAN-BUS Shield

In the Setting up a CAN-BUS Interface for a Jeep JK, with pin out information. blog post i've shown you the basic way of connecting to the can-bus in your vehicle.

The next step will be to upload a sketch to the Arduino that will listen to all messages on the CAN-BUS and print them, to the serial terminal as it comes in.

Getting right down to business.
In order for the Arduino UNO to interface with the Seeed CAN-BUS Shield you need to download the libraries from here --> GitHub Its also a good idea to get to know how the device works. Head over here and read more. After downloading the libraries you need to import the Zip file into your local library db by clicking.
Sketch --> Import Library --> Add Library  , you don't have to unzip the contents the zip file can be imported as is.

After importing the library you will see the CAN-BUS example sketches under the example sketch menu.


To save you allot of trouble the following is very important.
The example sketch for receiving works perfectly fine when connecting to the CAN-BUS, but it does not include the CAN ID where the messages comes from which will be useless for you.

IE Without the ID you will get. 8 Bytes
235 05 200 123 9 173 000 123 which could be from pressing the Sway Bar Disconnect.
235 05 200 123 9 173 000 123 which could be some general can bus data.


Looking at the above you will never find what you are looking for, because it seems to be the same message, but its not.

With the CAN ID it will look like this.


ID234- 235 05 200 123 9 173 000 123 which could be from pressing the Sway Bar Disconnect.
ID135- 05 200 123 9 173 000 123 which could be some general can bus data.


Hope you catch the drift.

I modified the sketch to include the CAN ID when it prints the message to the terminal. you can download it here
 Working Example CAN-BUS message receiver

Once you have uploaded the above sketch and you have connected to the CAN-BUS in your vehicle, using the Arduino Editor's Serial monitor, you will see the CAN-BUS messages bombarding the terminal, it will be virtually impossible for you to catch messages, such as pressing the Central Locking or switching on lights ect ect.

Instead of using the Arduino serial monitor i'm using putty.exe (Google it you will find it).
putty is a ssh client but it can also be used as a serial hyper-terminal.

Use the following settings.



Once you connect it will save all in-coming data to the file you have you have specified in text format.

From here you are on your own.
But to give you a tip.

You will want to connect the everything up, and let this system run for a few minutes so that you can build a decent size database, I let it run for about 5Mins which generated about 30 000 lines.

I then disconnect everything (Remember the Arduino Can-bus shield remembers some of the traffic and does not flush it when you disconnect putty, so rather stop everything)

Then start over by connecting using putty, then quickly press a button  a few times (It helps to count how many times you pressed it and use the counts as the file name of the log file) in your vehicle for the can bus shield to capture, then as quickly disconnect.
Another tip, repeat the "Button Capture" process twice.

Once you have the 3 files you can then start to Analise it, I removed all messages found in the "Button Press" log files found in the first run, that way you can be sure they are not to be confused with the button presses, Once you have filtered everything out you will be left with not too many different messages.

My way of removing "Idle" messages
I simply  opened each file then Find & Replace all the "Tabs" with nothing and also Find & Replace all spaces with nothing using Notepad++.

I then create a new sheet copy and paste directly from notepad++ into excel.
Putting the idle messages (The long list into column 1) and the Button press messages into column 2
using the below VBA code i find all the messages from col2 in col1 then flag them so i can remove them afterwards.

Dim LineCount1 As Double
Dim LineCount2 As Double

Dim Col1 As String
Dim Col2 As String


Private Sub CommandButton1_Click()
For LineCount1 = 1 To 65000
DoEvents
    If Sheet1.Cells(LineCount1, 2) = "" Then Exit For
    Label1 = "Busy with Line:" & LineCount1
    Col2 = Sheet1.Cells(LineCount1, 2)
   
    For LineCount2 = 1 To 65000
        Col1 = Sheet1.Cells(LineCount2, 1)
        If Sheet1.Cells(LineCount2, 1) = "" Then Exit For
          
        If Col1 = Col2 Then
        Sheet1.Cells(LineCount1, 3) = "Match Found"
        Exit For
        End If
           
    Next LineCount2

Next LineCount1

MsgBox "Done"


End Sub




In my next guide i will show you how to send those messages back into the can but and have your vehicle react to it.