Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SDI12_device.h@19:a29b98ae0005, 2018-08-08 (annotated)
- Committer:
- amateusz
- Date:
- Wed Aug 08 16:47:55 2018 +0000
- Revision:
- 19:a29b98ae0005
- Parent:
- 18:365e8aa4002c
- Child:
- 20:abccaaa16955
added present function to check if device is still connected
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amateusz | 0:d58ebb2bed9a | 1 | #include "SDI12.h" |
amateusz | 0:d58ebb2bed9a | 2 | #include <string> |
amateusz | 2:e2db05bc4708 | 3 | #include "mbed.h" |
amateusz | 0:d58ebb2bed9a | 4 | |
amateusz | 0:d58ebb2bed9a | 5 | class SDI12_device |
amateusz | 0:d58ebb2bed9a | 6 | { |
amateusz | 0:d58ebb2bed9a | 7 | public: |
amateusz | 0:d58ebb2bed9a | 8 | struct Identification_struct { |
amateusz | 0:d58ebb2bed9a | 9 | unsigned char sdi_version; |
amateusz | 0:d58ebb2bed9a | 10 | char company[8+1]; |
amateusz | 0:d58ebb2bed9a | 11 | char model[6+1]; |
amateusz | 0:d58ebb2bed9a | 12 | char version[3+1]; |
amateusz | 0:d58ebb2bed9a | 13 | }; |
amateusz | 0:d58ebb2bed9a | 14 | |
amateusz | 0:d58ebb2bed9a | 15 | struct Measurement_struct { |
amateusz | 0:d58ebb2bed9a | 16 | unsigned long timestampMeasurementReadyAt; |
amateusz | 4:11438803516c | 17 | char readyIn; |
amateusz | 0:d58ebb2bed9a | 18 | bool _measurementAlreadyRead; |
amateusz | 0:d58ebb2bed9a | 19 | char count; |
amateusz | 15:530bf2e09814 | 20 | float * values; // not float values [] !! :O |
amateusz | 0:d58ebb2bed9a | 21 | }; |
amateusz | 0:d58ebb2bed9a | 22 | |
amateusz | 0:d58ebb2bed9a | 23 | Identification_struct _ident_struct; |
amateusz | 0:d58ebb2bed9a | 24 | |
amateusz | 19:a29b98ae0005 | 25 | const static int detect(SDI12 &sdi12, char indices[], int stop = -1, int start = -1) { |
amateusz | 0:d58ebb2bed9a | 26 | char found = 0; |
amateusz | 10:efd1e313fe8c | 27 | for (size_t i = (start==-1?0:start); i < (stop==-1?ADDRRICES.size():stop+1); ++i) { |
amateusz | 0:d58ebb2bed9a | 28 | string question = "?!"; |
amateusz | 5:e53104570fde | 29 | question[0] = ADDRRICES[i]; |
amateusz | 0:d58ebb2bed9a | 30 | sdi12.sendCommand(question); |
amateusz | 0:d58ebb2bed9a | 31 | Timer detectTimer; |
amateusz | 0:d58ebb2bed9a | 32 | detectTimer.start(); |
amateusz | 15:530bf2e09814 | 33 | while (detectTimer.read_ms() < RESPONSE_TIMEOUT * 4) { |
amateusz | 5:e53104570fde | 34 | if (sdi12.RxInProgress()) detectTimer.reset(); |
amateusz | 7:1f506c65c4e8 | 35 | if (sdi12.RxBufferAvailable()) { |
amateusz | 7:1f506c65c4e8 | 36 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 7:1f506c65c4e8 | 37 | char buffer[bufferSize]; |
amateusz | 7:1f506c65c4e8 | 38 | sdi12.getRxBuffer(buffer); |
amateusz | 7:1f506c65c4e8 | 39 | // if first char is valid address char |
amateusz | 7:1f506c65c4e8 | 40 | if (ADDRRICES.find(buffer[0])) { |
amateusz | 7:1f506c65c4e8 | 41 | indices[found++] = buffer[0]; |
amateusz | 19:a29b98ae0005 | 42 | debug("FOUND (%d): %d -> %c\r\n", found, i, indices[found-1]); |
amateusz | 7:1f506c65c4e8 | 43 | } |
amateusz | 7:1f506c65c4e8 | 44 | } |
amateusz | 0:d58ebb2bed9a | 45 | } |
amateusz | 13:0093792f2325 | 46 | osDelay(100); |
amateusz | 0:d58ebb2bed9a | 47 | } |
amateusz | 0:d58ebb2bed9a | 48 | return found; |
amateusz | 0:d58ebb2bed9a | 49 | }; |
amateusz | 17:5cabfc047b40 | 50 | |
amateusz | 17:5cabfc047b40 | 51 | SDI12_device() { |
amateusz | 15:530bf2e09814 | 52 | debug("DEFAULT\r\n"); |
amateusz | 17:5cabfc047b40 | 53 | } |
amateusz | 17:5cabfc047b40 | 54 | |
amateusz | 18:365e8aa4002c | 55 | SDI12_device (SDI12 *inst, char address) : _sdi12(inst) { |
amateusz | 15:530bf2e09814 | 56 | setAddress(address); |
amateusz | 1:6b1a21925a81 | 57 | if(getIdentification(_ident_struct)); |
amateusz | 0:d58ebb2bed9a | 58 | }; |
amateusz | 15:530bf2e09814 | 59 | |
amateusz | 15:530bf2e09814 | 60 | |
amateusz | 15:530bf2e09814 | 61 | SDI12_device & operator= (const SDI12_device & old) { |
amateusz | 15:530bf2e09814 | 62 | _address = old._address; |
amateusz | 15:530bf2e09814 | 63 | _ident_struct = old._ident_struct; |
amateusz | 15:530bf2e09814 | 64 | // _sdi12 = old._sdi12; // non-copyable -.- |
amateusz | 15:530bf2e09814 | 65 | |
amateusz | 15:530bf2e09814 | 66 | _measurement = old._measurement; |
amateusz | 15:530bf2e09814 | 67 | _valuesReadyCount = old._valuesReadyCount; |
amateusz | 15:530bf2e09814 | 68 | _measurementReady = old._measurementReady; |
amateusz | 19:a29b98ae0005 | 69 | return *this; |
amateusz | 15:530bf2e09814 | 70 | } |
amateusz | 15:530bf2e09814 | 71 | |
amateusz | 10:efd1e313fe8c | 72 | ~SDI12_device () { |
amateusz | 19:a29b98ae0005 | 73 | // delete [] _measurement.values; |
amateusz | 10:efd1e313fe8c | 74 | } |
amateusz | 19:a29b98ae0005 | 75 | bool present() { |
amateusz | 19:a29b98ae0005 | 76 | std::string question = string("?") + string("!"); |
amateusz | 19:a29b98ae0005 | 77 | question[0] = _address; |
amateusz | 0:d58ebb2bed9a | 78 | |
amateusz | 19:a29b98ae0005 | 79 | _sdi12->sendCommand(question); |
amateusz | 19:a29b98ae0005 | 80 | |
amateusz | 19:a29b98ae0005 | 81 | Timer timeout; |
amateusz | 19:a29b98ae0005 | 82 | timeout.start(); |
amateusz | 19:a29b98ae0005 | 83 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 19:a29b98ae0005 | 84 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 19:a29b98ae0005 | 85 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 19:a29b98ae0005 | 86 | return false; |
amateusz | 19:a29b98ae0005 | 87 | } |
amateusz | 19:a29b98ae0005 | 88 | } |
amateusz | 19:a29b98ae0005 | 89 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 19:a29b98ae0005 | 90 | char buffer[bufferSize]; |
amateusz | 19:a29b98ae0005 | 91 | sdi12.getRxBuffer(buffer); // only to flush the buffer |
amateusz | 19:a29b98ae0005 | 92 | return true; |
amateusz | 19:a29b98ae0005 | 93 | } |
amateusz | 15:530bf2e09814 | 94 | // concurrent means that device under measurement won't signal its prempt readiness to the bus |
amateusz | 13:0093792f2325 | 95 | int measure(bool concurrent = true, char group = '0') { |
amateusz | 4:11438803516c | 96 | |
amateusz | 2:e2db05bc4708 | 97 | std::string question = string("?") + string(concurrent?"C":"M") + string("!"); |
amateusz | 2:e2db05bc4708 | 98 | question[0] = _address; |
amateusz | 13:0093792f2325 | 99 | if (group != '0') |
amateusz | 13:0093792f2325 | 100 | question.insert(2, 1, group); // e.g. 2M! -> 2M1! |
amateusz | 15:530bf2e09814 | 101 | _sdi12->sendCommand(question); |
amateusz | 2:e2db05bc4708 | 102 | |
amateusz | 2:e2db05bc4708 | 103 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 104 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 105 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 106 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 107 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 19:a29b98ae0005 | 108 | return -2; |
amateusz | 2:e2db05bc4708 | 109 | } |
amateusz | 2:e2db05bc4708 | 110 | } |
amateusz | 10:efd1e313fe8c | 111 | |
amateusz | 2:e2db05bc4708 | 112 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 113 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 114 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 115 | buffer[bufferSize] = '\0'; |
amateusz | 2:e2db05bc4708 | 116 | std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 117 | |
amateusz | 2:e2db05bc4708 | 118 | if (response[0] == _address) { |
amateusz | 2:e2db05bc4708 | 119 | char measurementsCount = std::atoi(response.substr(1+3,(concurrent?2:1)).c_str()); |
amateusz | 2:e2db05bc4708 | 120 | char waitFor = std::atoi(response.substr(1,3).c_str()); |
amateusz | 2:e2db05bc4708 | 121 | _measurement.count = measurementsCount; |
amateusz | 15:530bf2e09814 | 122 | _measurement.values = new float[measurementsCount]; |
amateusz | 4:11438803516c | 123 | _measurement.readyIn = waitFor; |
amateusz | 2:e2db05bc4708 | 124 | // _measurement.timestampMeasurementReadyAt |
amateusz | 2:e2db05bc4708 | 125 | debug("wait for %d measurement for %d seconds...", _measurement.count, waitFor); |
amateusz | 11:e0ccc1072c42 | 126 | _valuesReadyCount = 0; |
amateusz | 13:0093792f2325 | 127 | _measurementReady = false; |
amateusz | 9:a3062b9e4324 | 128 | // debug("measurement scheduled"); |
amateusz | 4:11438803516c | 129 | // if (_measurement.values != NULL) delete[] _measurement.values; |
amateusz | 4:11438803516c | 130 | // _measurement.values = new float[measurementsCount]; |
amateusz | 2:e2db05bc4708 | 131 | return 0; |
amateusz | 2:e2db05bc4708 | 132 | } |
amateusz | 2:e2db05bc4708 | 133 | return -1; |
amateusz | 2:e2db05bc4708 | 134 | }; |
amateusz | 13:0093792f2325 | 135 | |
amateusz | 4:11438803516c | 136 | char readyIn() { |
amateusz | 4:11438803516c | 137 | return _measurement.readyIn; |
amateusz | 4:11438803516c | 138 | } |
amateusz | 4:11438803516c | 139 | |
amateusz | 13:0093792f2325 | 140 | bool isReady() { |
amateusz | 13:0093792f2325 | 141 | return _measurementReady; |
amateusz | 13:0093792f2325 | 142 | } |
amateusz | 13:0093792f2325 | 143 | |
amateusz | 11:e0ccc1072c42 | 144 | bool read(char group = '0') { |
amateusz | 11:e0ccc1072c42 | 145 | // Measurement (M), Continuous (R), and Concurrent (C) commands and subsequent Data (D) |
amateusz | 19:a29b98ae0005 | 146 | std::string question = std::string(_address, 1) + std::string("D") + std::string(1,group)+ std::string("!"); |
amateusz | 2:e2db05bc4708 | 147 | question[0] = _address; |
amateusz | 15:530bf2e09814 | 148 | _sdi12->sendCommand(question); |
amateusz | 2:e2db05bc4708 | 149 | |
amateusz | 2:e2db05bc4708 | 150 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 151 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 152 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 153 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 154 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 2:e2db05bc4708 | 155 | return false; |
amateusz | 2:e2db05bc4708 | 156 | } |
amateusz | 2:e2db05bc4708 | 157 | } |
amateusz | 2:e2db05bc4708 | 158 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 159 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 160 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 161 | buffer[bufferSize] = '\0'; |
amateusz | 17:5cabfc047b40 | 162 | const std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 163 | |
amateusz | 4:11438803516c | 164 | // debug("parser. recv: %s\r\n", response); |
amateusz | 3:2847f7c543d3 | 165 | if (response[0] == _address) { |
amateusz | 3:2847f7c543d3 | 166 | // parser here |
amateusz | 17:5cabfc047b40 | 167 | // const!!! response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123 |
amateusz | 3:2847f7c543d3 | 168 | // the only two possible delimeters of a value |
amateusz | 17:5cabfc047b40 | 169 | size_t next_start_index = response.find_first_of("-+", 0 + 1); // address offset! |
amateusz | 3:2847f7c543d3 | 170 | size_t next_end_index; |
amateusz | 3:2847f7c543d3 | 171 | // ready, steady, parse!! |
amateusz | 3:2847f7c543d3 | 172 | while(next_start_index != std::string::npos) { |
amateusz | 3:2847f7c543d3 | 173 | // determine start index: |
amateusz | 3:2847f7c543d3 | 174 | next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos |
amateusz | 17:5cabfc047b40 | 175 | |
amateusz | 3:2847f7c543d3 | 176 | float value = std::atof(response.substr(next_start_index, next_end_index).c_str()); |
amateusz | 4:11438803516c | 177 | // debug("parsed: %f\r\n", value); |
amateusz | 11:e0ccc1072c42 | 178 | _measurement.values[_valuesReadyCount++] = value; |
amateusz | 3:2847f7c543d3 | 179 | next_start_index = response.find_first_of("-+", next_end_index); |
amateusz | 3:2847f7c543d3 | 180 | } |
amateusz | 11:e0ccc1072c42 | 181 | // if current parsing doesn't return all the expexted measurements, then press harder and poll further Dx commands. RECURSION HERE *.* |
amateusz | 11:e0ccc1072c42 | 182 | if (_valuesReadyCount < _measurement.count) |
amateusz | 11:e0ccc1072c42 | 183 | read(group + 1); |
amateusz | 13:0093792f2325 | 184 | else { |
amateusz | 13:0093792f2325 | 185 | _measurementReady = true; |
amateusz | 13:0093792f2325 | 186 | } |
amateusz | 3:2847f7c543d3 | 187 | } |
amateusz | 4:11438803516c | 188 | return true; |
amateusz | 2:e2db05bc4708 | 189 | } |
amateusz | 4:11438803516c | 190 | |
amateusz | 4:11438803516c | 191 | |
amateusz | 0:d58ebb2bed9a | 192 | // |
amateusz | 4:11438803516c | 193 | const unsigned char getMeasurementCount() { |
amateusz | 2:e2db05bc4708 | 194 | return _measurement.count; |
amateusz | 2:e2db05bc4708 | 195 | } |
amateusz | 2:e2db05bc4708 | 196 | |
amateusz | 4:11438803516c | 197 | const float getMeasurementValue(char which) { |
amateusz | 13:0093792f2325 | 198 | _measurementReady = false; // reading /any/ of the values resets this |
amateusz | 2:e2db05bc4708 | 199 | return _measurement.values[which]; |
amateusz | 2:e2db05bc4708 | 200 | } |
amateusz | 2:e2db05bc4708 | 201 | |
amateusz | 0:d58ebb2bed9a | 202 | void setAddress(char address) { |
amateusz | 0:d58ebb2bed9a | 203 | _address = address; |
amateusz | 0:d58ebb2bed9a | 204 | }; |
amateusz | 0:d58ebb2bed9a | 205 | // |
amateusz | 0:d58ebb2bed9a | 206 | // |
amateusz | 0:d58ebb2bed9a | 207 | private: |
amateusz | 13:0093792f2325 | 208 | static const int RESPONSE_TIMEOUT = 15+1; // device should start to respond within [ms] |
amateusz | 5:e53104570fde | 209 | static const std::string ADDRRICES; // as in index -> indices, |
amateusz | 5:e53104570fde | 210 | |
amateusz | 15:530bf2e09814 | 211 | SDI12 *_sdi12; |
amateusz | 0:d58ebb2bed9a | 212 | char _address; |
amateusz | 17:5cabfc047b40 | 213 | |
amateusz | 0:d58ebb2bed9a | 214 | Measurement_struct _measurement; |
amateusz | 11:e0ccc1072c42 | 215 | size_t _valuesReadyCount; |
amateusz | 13:0093792f2325 | 216 | bool _measurementReady; |
amateusz | 0:d58ebb2bed9a | 217 | |
amateusz | 1:6b1a21925a81 | 218 | bool getIdentification(Identification_struct &ident) { |
amateusz | 15:530bf2e09814 | 219 | // _sdi12->sendCommand(std::string(_address) + "I!"); |
amateusz | 1:6b1a21925a81 | 220 | std::string question = "?I!"; |
amateusz | 1:6b1a21925a81 | 221 | question[0] = _address; |
amateusz | 15:530bf2e09814 | 222 | _sdi12->sendCommand(question); |
amateusz | 0:d58ebb2bed9a | 223 | |
amateusz | 1:6b1a21925a81 | 224 | Timer timeout; |
amateusz | 1:6b1a21925a81 | 225 | timeout.start(); |
amateusz | 1:6b1a21925a81 | 226 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 1:6b1a21925a81 | 227 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 228 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 1:6b1a21925a81 | 229 | return false; |
amateusz | 1:6b1a21925a81 | 230 | } |
amateusz | 1:6b1a21925a81 | 231 | } |
amateusz | 1:6b1a21925a81 | 232 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 1:6b1a21925a81 | 233 | char buffer[bufferSize+1]; |
amateusz | 1:6b1a21925a81 | 234 | sdi12.getRxBuffer(buffer); |
amateusz | 1:6b1a21925a81 | 235 | buffer[bufferSize] = '\0'; |
amateusz | 17:5cabfc047b40 | 236 | const std::string response(buffer); |
amateusz | 0:d58ebb2bed9a | 237 | |
amateusz | 1:6b1a21925a81 | 238 | if (response[0] == _address) { |
amateusz | 1:6b1a21925a81 | 239 | // e.g.: 113DECAGON GS3 402 |
amateusz | 2:e2db05bc4708 | 240 | |
amateusz | 1:6b1a21925a81 | 241 | ident.sdi_version = std::atoi(response.substr(1, 2).c_str()); |
amateusz | 17:5cabfc047b40 | 242 | const std::string tempStr = response.substr(3, 8); |
amateusz | 1:6b1a21925a81 | 243 | strcpy(ident.company, tempStr.c_str()); |
amateusz | 1:6b1a21925a81 | 244 | |
amateusz | 17:5cabfc047b40 | 245 | strcpy(ident.model, response.substr(3+8, 6).c_str()); |
amateusz | 1:6b1a21925a81 | 246 | |
amateusz | 17:5cabfc047b40 | 247 | strcpy(ident.version, response.substr(3+8+6, 3).c_str()); |
amateusz | 1:6b1a21925a81 | 248 | return true; |
amateusz | 0:d58ebb2bed9a | 249 | } |
amateusz | 1:6b1a21925a81 | 250 | return false; |
amateusz | 1:6b1a21925a81 | 251 | } |
amateusz | 5:e53104570fde | 252 | }; |
amateusz | 5:e53104570fde | 253 | |
amateusz | 5:e53104570fde | 254 | const std::string SDI12_device::ADDRRICES = "0123456789abcdefgijklmnoprstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ"; |