MT-07 CAN Bus Documentation 1.0
Comprehensive documentation for the MT-07 CAN Bus Interface library.
 
Loading...
Searching...
No Matches
main.ino
Go to the documentation of this file.
1/**
2 * \file main.ino
3 * \brief Main program for MT-07 CAN Bus Interface.
4 *
5 * \details This program uses the MT07_CANBus library to interact with the CAN bus of a Yamaha MT-07 motorcycle (2014 model).
6 * It initializes the CAN bus, decodes incoming messages, and displays the following data in real-time:
7 * - Gear position
8 * - Throttle position sensor (TPS)
9 * - Motor and air temperature
10 * - Speed and RPM
11 *
12 * \author Andreas Panagiotakis
13 * \date 2024-08-03
14 * \license MIT License
15 *
16 * \usage
17 * - Connect the MCP2515 module to the ESP32.
18 * - Wire the CAN bus to the motorcycle.
19 * - Compile and upload the program to the ESP32.
20 * - View the decoded data in the Serial Monitor at 115200 baud.
21 */
22
23#include "MT07_CANBus.h"
24
25/// Chip select pin for MCP2515
26const int CS_PIN = 5;
27
28/// Instance of the MT07_CANBus class
29MT07_CANBus mt07(CS_PIN);
30
31/**
32 * \brief Arduino setup function.
33 *
34 * Initializes the Serial Monitor and CAN bus. If the CAN bus fails to initialize, the program halts.
35 */
36void setup() {
37 Serial.begin(115200);
38
39 if (mt07.begin(CAN_500KBPS, MCP_8MHZ)) {
40 Serial.println("MT-07 CANBus initialized successfully.");
41 } else {
42 Serial.println("Failed to initialize MT-07 CANBus.");
43 while (1); // Halt the program if initialization fails
44 }
45}
46
47/**
48 * \brief Arduino loop function.
49 *
50 * Checks for incoming CAN messages, processes them, and displays the decoded data in the Serial Monitor.
51 */
52void loop() {
53 unsigned long rxId;
54 byte rxBuf[8];
55 byte rxDlc;
56
57 // Check for incoming CAN messages
58 if (CAN_MSGAVAIL == mt07.CAN.checkReceive()) {
59 if (mt07.CAN.readMsgBuf(&rxId, &rxDlc, rxBuf) == CAN_OK) {
60 mt07.processMessage(rxId, rxBuf); // Decode the message
61
62 // Display decoded data
63 Serial.print("Gear Position: ");
64 Serial.println(mt07.getGearPosition());
65
66 Serial.print("TPS: ");
67 Serial.print(mt07.getTPS());
68 Serial.println("%");
69
70 Serial.print("Motor Temp: ");
71 Serial.println(mt07.getMotorTemp());
72
73 Serial.print("Air Temp: ");
74 Serial.println(mt07.getAirTemp());
75
76 Serial.print("Speed: ");
77 Serial.print(mt07.getSpeed());
78 Serial.println(" km/h");
79
80 Serial.print("RPM: ");
81 Serial.println(mt07.getRPM());
82 Serial.println("---------------------------");
83 } else {
84 Serial.println("Error reading CAN message.");
85 }
86 }
87}
void setup()
Arduino setup function.
Definition main.ino:36
MT07_CANBus mt07(CS_PIN)
Instance of the MT07_CANBus class.
const int CS_PIN
Chip select pin for MCP2515.
Definition main.ino:26
void loop()
Arduino loop function.
Definition main.ino:52