|
| 1 | +/* |
| 2 | + Title: |
| 3 | + 05.Single_measurement_test.ino |
| 4 | +
|
| 5 | + Description: |
| 6 | + Demonstrates how to read a single measurement from a module's measuring blocks continuously. |
| 7 | +
|
| 8 | + Notes: |
| 9 | + *You can change which block and which measurement to read by modifying the #defines below. |
| 10 | + *This measurement will be read continuously while the sketch is running. |
| 11 | + *It is not necessary to maintain the connection with "diag.update();" in the loop, because any request will have the same effect (update() must be used in |
| 12 | + periods of inactivity). |
| 13 | +*/ |
| 14 | + |
| 15 | +//Change which measurement to read, from which block. |
| 16 | +#define BLOCK_TO_READ 1 |
| 17 | +#define MEASUREMENT_TO_READ 0 //valid range: 0-3 |
| 18 | + |
| 19 | +/* |
| 20 | + *This sketch displays information in the Serial Monitor, so another serial port is required for the K-line. |
| 21 | + *If another port is not available on the selected board, the regular port "Serial" can be used, although no information can be printed to it (good for |
| 22 | + projects with external displays or for data logging). |
| 23 | +
|
| 24 | + ***Uncomment the appropriate options in the configuration.h file! |
| 25 | +
|
| 26 | + Arduino UNO |
| 27 | + *no additional serial port, software serial is used |
| 28 | + *library of choice: AltSoftSerial (better than SoftwareSerial because it can also receive while sending) |
| 29 | + *pins: |
| 30 | + *K-line TX -> RX pin 8 |
| 31 | + *K-line RX -> TX pin 9 |
| 32 | +
|
| 33 | + Arduino MEGA |
| 34 | + *has three additional serial ports |
| 35 | + *pins: |
| 36 | + *K-line TX -> Serial1 - RX pin 19 / Serial2 - RX pin 17 / Serial3 - RX pin 15 |
| 37 | + *K-line RX -> Serial1 - TX pin 18 / Serial2 - TX pin 16 / Serial3 - TX pin 14 |
| 38 | +
|
| 39 | + ESP32 |
| 40 | + *has one additional serial port |
| 41 | + *pins: |
| 42 | + *K-line TX -> RX pin 16 |
| 43 | + *K-line RX -> TX pin 17 |
| 44 | +
|
| 45 | + ESP8266 |
| 46 | + *has no additional serial ports |
| 47 | + *library of choice: SoftwareSerial (the ESP version of SoftwareSerial can also receive while sending) |
| 48 | + *pins: any unused |
| 49 | + *chosen for this demo: |
| 50 | + *K-line TX -> RX pin 4 (D2) |
| 51 | + *K-line RX -> TX pin 5 (D1) |
| 52 | +
|
| 53 | + ***If using the first hardware serial port (Serial) (with other sketches), the interface must be disconnected during code upload, and no "Serial.print"s |
| 54 | + should be used. |
| 55 | + |
| 56 | + *This sketch will not fit on an Arduino UNO by default! To fix this, open the library's /src/KLineKWP1281Lib.h file in a text editor and comment out the line |
| 57 | + "#define KWP1281_TEXT_TABLE_SUPPORTED" at the top, as explained in the comments. |
| 58 | +*/ |
| 59 | + |
| 60 | +//Include the library. |
| 61 | +#include <KLineKWP1281Lib.h> |
| 62 | + |
| 63 | +//Include the two files containing configuration options and the functions used for communication. |
| 64 | +#include "configuration.h" |
| 65 | +#include "communication.h" |
| 66 | + |
| 67 | +//Debugging can be enabled in configuration.h in order to print connection-related info on the Serial Monitor. |
| 68 | +#if debug_info |
| 69 | + KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin, is_full_duplex, &Serial); |
| 70 | +#else |
| 71 | + KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin, is_full_duplex); |
| 72 | +#endif |
| 73 | + |
| 74 | +//Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. |
| 75 | +#if debug_traffic |
| 76 | +void KWP1281debugFunction(bool type, uint8_t sequence, uint8_t command, uint8_t* data, uint8_t length) { |
| 77 | + Serial.println(); |
| 78 | + |
| 79 | + Serial.println(type ? "RECEIVE:" : "SEND:"); |
| 80 | + |
| 81 | + Serial.print("*command: "); |
| 82 | + if (command < 0x10) Serial.print(0); |
| 83 | + Serial.println(command, HEX); |
| 84 | + |
| 85 | + Serial.print("*sequence: "); |
| 86 | + if (sequence < 0x10) Serial.print(0); |
| 87 | + Serial.println(sequence, HEX); |
| 88 | + |
| 89 | + if (length) { |
| 90 | + Serial.print("*data bytes: "); |
| 91 | + Serial.println(length); |
| 92 | + |
| 93 | + Serial.print("*data: "); |
| 94 | + for (uint16_t i = 0; i < length; i++) { //iterate through the message's contents |
| 95 | + if (data[i] < 0x10) Serial.print(0); //print a leading 0 where necessary to display 2-digit HEX |
| 96 | + Serial.print(data[i], HEX); //print the byte in HEX |
| 97 | + Serial.print(' '); |
| 98 | + } |
| 99 | + Serial.println(); |
| 100 | + } |
| 101 | +} |
| 102 | +#endif |
| 103 | + |
| 104 | +uint8_t measurements[3 * 4]; //buffer to store the measurements; each measurement takes 3 bytes; one block contains 4 measurements |
| 105 | + |
| 106 | +void setup() { |
| 107 | + //Initialize the Serial Monitor. |
| 108 | + Serial.begin(115200); |
| 109 | + delay(500); |
| 110 | + Serial.println("Sketch started."); |
| 111 | + |
| 112 | + //If debugging bus traffic was enabled, attach the debugging function. |
| 113 | +#if debug_traffic |
| 114 | + diag.KWP1281debugFunction(KWP1281debugFunction); |
| 115 | +#endif |
| 116 | + |
| 117 | + //Change these according to your module, in configuration.h. |
| 118 | + diag.connect(connect_to_module, module_baud_rate); |
| 119 | + |
| 120 | + Serial.print("Requesting block "); |
| 121 | + Serial.print(BLOCK_TO_READ); |
| 122 | + Serial.print(", measurement index "); |
| 123 | + Serial.print(MEASUREMENT_TO_READ); |
| 124 | + Serial.println(" continuously."); |
| 125 | +} |
| 126 | + |
| 127 | +void loop() { |
| 128 | + showSingleMeasurement(BLOCK_TO_READ, MEASUREMENT_TO_READ); |
| 129 | +} |
| 130 | + |
| 131 | +void showSingleMeasurement(uint8_t block, uint8_t measurement_index) { |
| 132 | + /* |
| 133 | + The readGroup() function can return: |
| 134 | + *KLineKWP1281Lib::SUCCESS - received measurements |
| 135 | + *KLineKWP1281Lib::FAIL - the requested block does not exist |
| 136 | + *KLineKWP1281Lib::ERROR - communication error |
| 137 | + */ |
| 138 | + uint8_t amount_of_measurements = 0; |
| 139 | + switch (diag.readGroup(amount_of_measurements, block, measurements, sizeof(measurements))) { |
| 140 | + case KLineKWP1281Lib::ERROR: |
| 141 | + Serial.println("Error reading measurements!"); |
| 142 | + break; |
| 143 | + |
| 144 | + case KLineKWP1281Lib::FAIL: |
| 145 | + Serial.print("Block "); |
| 146 | + Serial.print(block); |
| 147 | + Serial.println(" does not exist!"); |
| 148 | + break; |
| 149 | + |
| 150 | + case KLineKWP1281Lib::SUCCESS: |
| 151 | + //Will hold the measurement's units |
| 152 | + char units_string[16]; |
| 153 | + |
| 154 | + //Display the selected measurement. |
| 155 | + |
| 156 | + /* |
| 157 | + The getMeasurementType() function can return: |
| 158 | + *KLineKWP1281Lib::UNKNOWN - index out of range (measurement doesn't exist in block) |
| 159 | + *KLineKWP1281Lib::UNITS - the measurement contains human-readable text in the units string |
| 160 | + *KLineKWP1281Lib::VALUE - "regular" measurement, with a value and units |
| 161 | + */ |
| 162 | + switch (KLineKWP1281Lib::getMeasurementType(measurement_index, amount_of_measurements, measurements, sizeof(measurements))) { |
| 163 | + //Value and units |
| 164 | + case KLineKWP1281Lib::VALUE: |
| 165 | + Serial.print(KLineKWP1281Lib::getMeasurementValue(measurement_index, amount_of_measurements, measurements, sizeof(measurements))); |
| 166 | + Serial.print(' '); |
| 167 | + Serial.println(KLineKWP1281Lib::getMeasurementUnits(measurement_index, amount_of_measurements, measurements, sizeof(measurements), units_string, sizeof(units_string))); |
| 168 | + break; |
| 169 | + |
| 170 | + //Units string containing text |
| 171 | + case KLineKWP1281Lib::UNITS: |
| 172 | + Serial.println(KLineKWP1281Lib::getMeasurementUnits(measurement_index, amount_of_measurements, measurements, sizeof(measurements), units_string, sizeof(units_string))); |
| 173 | + break; |
| 174 | + |
| 175 | + //Invalid measurement index |
| 176 | + case KLineKWP1281Lib::UNKNOWN: |
| 177 | + Serial.println("N/A"); |
| 178 | + break; |
| 179 | + } |
| 180 | + break; |
| 181 | + } |
| 182 | +} |
0 commit comments