That was how I got it. Looking at example code and think I found some. Will try to get it into the editor and compile it.
This code compiled:
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h> // the OBD2 library depends on the CAN library
#include <OBD2.h>
// array of PID's to print values of
const int PIDS[] = {
CALCULATED_ENGINE_LOAD,
ENGINE_COOLANT_TEMPERATURE,
ENGINE_RPM,
VEHICLE_SPEED,
AIR_INTAKE_TEMPERATURE,
MAF_AIR_FLOW_RATE,
THROTTLE_POSITION,
RUN_TIME_SINCE_ENGINE_START,
FUEL_TANK_LEVEL_INPUT,
ABSOLULTE_BAROMETRIC_PRESSURE,
ABSOLUTE_LOAD_VALUE,
RELATIVE_THROTTLE_POSITION
};
const int NUM_PIDS = sizeof(PIDS) / sizeof(PIDS[0]);
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println(F("OBD2 Key Stats"));
while (true) {
Serial.print(F("Attempting to connect to OBD2 CAN bus ... "));
if (!OBD2.begin()) {
Serial.println(F("failed!"));
delay(1000);
} else {
Serial.println(F("success"));
break;
}
}
Serial.println();
}
void loop() {
// loop through all the PID's in the array
//
for (int i = 0; i < NUM_PIDS; i++) {
int pid = PIDS[i];
printPID(pid);
}
Serial.println();
delay(1000);
}
void printPID(int pid) {
// print PID name
Serial.print(OBD2.pidName(pid));
Serial.print(F(" = "));
// read the PID value
float pidValue = OBD2.pidRead(pid);
if (isnan(pidValue)) {
Serial.print("error");
} else {
// print value with units
Serial.print(pidValue);
Serial.print(F(" "));
Serial.print(OBD2.pidUnits(pid));
}
Serial.println();
}
Thanks for the flag. How fast that CAN bus runs and how many different messages being sent are yet unknown to me.
Only a few are interesting and update time is not at all critical. I intend to produce a notification running too long on too low gear.
Bad installation of the library? I used the library manager to install the OBD-master library but got this message when compiling:
Sketch uses 16072 bytes (49%) of program storage space. Maximum is 32256 bytes.
Global variables use 774 bytes (37%) of dynamic memory, leaving 1274 bytes for local variables. Maximum is 2048 bytes.
Invalid library found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master: no headers files (.h) found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master
Invalid library found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master: no headers files (.h) found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master
Just got the hardware so it's interesting to move on.
How did You handle the Serial signal to the controller?
During debugging D0 and D1 are needed for serial monitor.
Editing the library to make it use software serial?
I did use that link and went from that. The code below the link, I don't understand it. Looks like a display handler....
I tried this test code but got a missing .h file error.
Code:
/*************************************************************************
* Testing sketch for Freematics OBD-II UART Adapter V1/V2/V2.1
* Performs AT command-set test
* Reads and prints several OBD-II PIDs value
* Reads and prints motion sensor data if available
* Distributed under BSD
* Visit https://freematics.com/products for more product information
* Written by Stanley Huang <stanley@freematics.com.au>
*************************************************************************/
#include <OBD2UART.h>
// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output as the adapter occupies Serial1
// On Arduino UNO and those have no Serial1, we use software serial for output as the adapter uses Serial
#ifdef ARDUINO_AVR_UNO
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A2, A3);
#else
#define mySerial Serial
#endif
COBD obd;
bool hasMEMS;
void testATcommands()
{
static const char cmds[][6] = {"ATZ\r", "ATI\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
char buf[128];
for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
const char *cmd = cmds[i];
mySerial.print("Sending ");
mySerial.println(cmd);
if (obd.sendCommand(cmd, buf, sizeof(buf))) {
char *p = strstr(buf, cmd);
if (p)
p += strlen(cmd);
else
p = buf;
while (*p == '\r') p++;
while (*p) {
mySerial.write(*p);
if (*p == '\r' && *(p + 1) != '\r')
mySerial.write('\n');
p++;
}
mySerial.println();
} else {
mySerial.println("Timeout");
}
delay(1000);
}
mySerial.println();
}
void readPIDSingle()
{
int value;
mySerial.print('[');
mySerial.print(millis());
mySerial.print(']');
mySerial.print("RPM=");
if (obd.readPID(PID_RPM, value)) {
mySerial.print(value);
}
mySerial.println();
}
void readPIDMultiple()
{
static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP};
int values[sizeof(pids)];
if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) {
mySerial.print('[');
mySerial.print(millis());
mySerial.print(']');
for (byte i = 0; i < sizeof(pids) ; i++) {
mySerial.print((int)pids[i] | 0x100, HEX);
mySerial.print('=');
mySerial.print(values[i]);
mySerial.print(' ');
}
mySerial.println();
}
}
void readBatteryVoltage()
{
mySerial.print('[');
mySerial.print(millis());
mySerial.print(']');
mySerial.print("Battery:");
mySerial.print(obd.getVoltage(), 1);
mySerial.println('V');
}
void readMEMS()
{
int16_t acc[3] = {0};
int16_t gyro[3] = {0};
int16_t mag[3] = {0};
int16_t temp = 0;
if (!obd.memsRead(acc, gyro, mag, &temp)) return;
mySerial.print('[');
mySerial.print(millis());
mySerial.print(']');
mySerial.print("ACC:");
mySerial.print(acc[0]);
mySerial.print('/');
mySerial.print(acc[1]);
mySerial.print('/');
mySerial.print(acc[2]);
mySerial.print(" GYRO:");
mySerial.print(gyro[0]);
mySerial.print('/');
mySerial.print(gyro[1]);
mySerial.print('/');
mySerial.print(gyro[2]);
mySerial.print(" MAG:");
mySerial.print(mag[0]);
mySerial.print('/');
mySerial.print(mag[1]);
mySerial.print('/');
mySerial.print(mag[2]);
mySerial.print(" TEMP:");
mySerial.print((float)temp / 10, 1);
mySerial.println("C");
}
void setup()
{
mySerial.begin(115200);
while (!mySerial);
for (;;) {
delay(1000);
byte version = obd.begin();
mySerial.print("Freematics OBD-II Adapter ");
if (version > 0) {
mySerial.println("detected");
mySerial.print("OBD firmware version ");
mySerial.print(version / 10);
mySerial.print('.');
mySerial.println(version % 10);
break;
} else {
mySerial.println("not detected");
}
}
// send some commands for testing and show response for debugging purpose
testATcommands();
hasMEMS = obd.memsInit();
mySerial.print("MEMS:");
mySerial.println(hasMEMS ? "Yes" : "No");
// initialize OBD-II adapter
do {
mySerial.println("Connecting...");
} while (!obd.init());
mySerial.println("OBD connected!");
char buf[64];
if (obd.getVIN(buf, sizeof(buf))) {
mySerial.print("VIN:");
mySerial.println(buf);
}
uint16_t codes[6];
byte dtcCount = obd.readDTC(codes, 6);
if (dtcCount == 0) {
mySerial.println("No DTC");
} else {
mySerial.print(dtcCount);
mySerial.print(" DTC:");
for (byte n = 0; n < dtcCount; n++) {
mySerial.print(' ');
mySerial.print(codes[n], HEX);
}
mySerial.println();
}
delay(5000);
}
void loop()
{
readPIDSingle();
readPIDMultiple();
readBatteryVoltage();
if (hasMEMS) {
readMEMS();
}
}
Error:
obd_uart_test:11:10: fatal error: OBD2UART.h: No such file or directory
#include <OBD2UART.h>
^~~~~~~~~~~~
compilation terminated.
exit status 1
OBD2UART.h: No such file or directory
Invalid library found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master: no headers files (.h) found in C:\Users\Stefan\Documents\Arduino\libraries\ArduinoOBD-master
Thanks! I found those functions "Add library" etc.
I've managed to get the OBD2 library installed in the IDE world. Somehow .h files are missing when compiling the code OBD2UART. I ought to have got the OBD2UART.h but obviously it's missing.
I don't feel comfortable when clicking "download" a .zip. To what folder is it downloaded? Likely "downloads". Unzipping those files in downloads?
I download the zip to a download folder then use the IDE to install zip files to install the zip files to the default Arduino IDE location. Really the procedure is straight forward. Download zip install zip library. I've not had any issue installing library zip files.
I get it. It just bothers me that the OBD2 library is installed but the interesting sketch OBD2UART make trouble.
Using software serial to read the Freematics device having the stream decoded by the OBD2 library would be perfect.
Supplying the Arduino/libraries content. It shows both ArduinoOBD-master and the OBD but I can't find ArduinoOBD-master from the IDE.
The goal is to have an UNO reading the Freematics OBDH unit, use some of its output to make buzzing.
Some example code comes with the library. One compiles but uses serial, D0/D1.
Another example code uses software serial but an include of obd2uart.h fails.
Trying to download the lib for this the istallation in the IDE fails, telling no valid library in the folder.
Basically, reading the OBDH bus via software serial is the goal.
Software serial is pretty much useless above 9600 Baud; not sure if you can "dumb down" OBD to a baud rate that low, so you might be out of luck. Others may know for sure.