Thank you so much for your replies. I tried the code suggested and i can control the LED using Bluetooth terminal from the phone, so the connection it's ok.
I have read the attached files but i still couldn't make it work.
Here's my wiring:
Bluetooth logic level converter 5V -> 3.3 V
MCP2515 Wiring
HC-05
- TX Bluetooth -> RX Arduino (Digital 0)
- RX Bluetooth -> TX Arduino (Digital 1)
Also this is the code :
#include <SoftwareSerial.h>
#include <String.h>
#include <mcp_can.h>
#include <SPI.h>
const String ATE = "ATE"; // Echo off/on
const String ATI = "ATI"; // Version id
const String ATZ = "ATZ"; // Reset
const String ATS = "ATS"; // Set protocol X
const String ATH = "ATH"; // Headers off / on
const String ATL = "ATL"; // Linefeeds off/on
const String ATM = "ATM"; // Memory off/on
const String GETDEFINITIONS = "GETDEFINITIONS"; // Get sensor definitions
const String GETCONFIGURATION = "GETCONFIGURATION"; // Get config of app (hide car sensors, devices sensors, etc)
const String GETSENSORS = "G"; // Get sensor values, one shot.
const String SETSENSOR = "S"; // Set a sensor value
const String PROMPT = ">";
const String CANBUS = "6"; // canbus 500k 11 bit protocol id for elm.
const String ATDPN = "ATDPN";
const String ATDESC = "AT@1";
const String ATAT = "ATAT";
const String LF = "\n";
const String VERSION = "Torque Protocol Interface v0.0.1"; // Don't change this - it's used by Torque so it knows what interface it is connected to
const String VERSION_DESC = "Torque For Android Protocol Interface";
const String OK = "OK";
const String ANALOG = "a";
const String DIGITAL = "d";
const String IS_INPUT = "i";
const String IS_OUTPUT = "o";
String fromTorque = "";
const String CONFIGURATION = "";
SoftwareSerial mySerial(0,1);
unsigned long prevTX = 0; // Variable to store last execution time
const unsigned int invlTX = 1000; // One second interval constant
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56}; // Generic CAN data to send
// Serial Output String Buffer
char msgString[128];
// CAN0 INT and CS
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup() {
mySerial.begin(9600);
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
mySerial.println("MCP2515 Initialized Successfully!");
else
mySerial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT);
mySerial.println("MCP2515 Library Loopback Example...");
}
void loop() {
if (mySerial.available()) {
char c = mySerial.read();
if ((c == '\n' || c == '\r') && fromTorque.length() > 0) {
fromTorque.toUpperCase();
processCommand(fromTorque);
fromTorque = "";
} else if (c != ' ' && c != '\n' && c !='\r') {
// Ignore spaces.
fromTorque += c;
}
}
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
mySerial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
mySerial.print(msgString);
}
}
mySerial.println();
}
if(millis() - prevTX >= invlTX){ // Send this at a one second interval.
prevTX = millis();
byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);
}
}
void processCommand(String command) {
Serial.println(command);
if (command.equals(ATZ)) {
mySerial.print(VERSION);
mySerial.print(LF);
mySerial.print(OK);
} else if (command.startsWith(ATE)) {
mySerial.print(OK);
} else if(command.startsWith(ATI)) {
mySerial.print(VERSION);
mySerial.print(LF);
mySerial.print(OK);
} else if (command.startsWith(ATDESC)) {
mySerial.print(VERSION_DESC);
mySerial.print(LF);
mySerial.print(OK);
} else if (command.startsWith(ATL)) {
mySerial.print(OK);
} else if (command.startsWith(ATAT)) {
mySerial.print(OK);
} else if (command.startsWith(ATH)) {
mySerial.print(OK);
} else if (command.startsWith(ATM)) {
mySerial.print(OK);
} else if (command.startsWith(ATS)) {
// Set protocol
mySerial.print(OK);
} else if (command.startsWith(ATDPN)) {
mySerial.print(CANBUS);
} else if (command.startsWith(GETCONFIGURATION)) {
getConfiguration();
}
mySerial.print(LF);
mySerial.print(PROMPT);
}
void getConfiguration() {
mySerial.print(CONFIGURATION);
}
There are some AT cmds for the Torque app in order to negociate with the arduino (took part of the code from here Arduino code - Torque Wiki)
At this moment, I have the Torque app connected to arduino via bluetooth but not getting at data. No connection to the car, i tried to use a test send code to simulate a package but haven't received anything on the bluetooth terminal.
To summarize all, i need the can messages to be broadcasted via bluetooth and torque app to recognize them. As far as i know, torque app sends a "request" like 01 05 (mode and pid) and expects to receive something like this (41 05 45), i can also be wrong.
Sorry but i truly don't know how to better explain it.
