Home › Forums › Other Data Loggers › SDI-12 Library › Reply To: SDI-12 Library
2020-03-20 at 10:33 AM
#13967
My intent was to help you by giving you an example of something you could modify to your needs, not to give you a final working program.
This version will compile. I don’t know if it will actually work for your sensors or log data as expected. You will need to test that.
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <SDI12.h> #include <SD.h> #include <Wire.h> #include <DS3232RTC.h> #include <Adafruit_MLX90614.h> #define DATA_PIN 3 SDI12 mySDI12(DATA_PIN); const int chipSelect = 10; unsigned long time; File logFile; Adafruit_MLX90614 sensor_52 = Adafruit_MLX90614(0x52); Adafruit_MLX90614 sensor_53 = Adafruit_MLX90614(0x53); Adafruit_MLX90614 sensor_03 = Adafruit_MLX90614(0x03); Adafruit_MLX90614 sensor_07 = Adafruit_MLX90614(0x07); // keeps track of the wait time for each active addresses uint8_t waitTime[8] = { 0, }; // keeps track of the time each sensor was started uint32_t millisStarted[8] = { 0, }; // keeps track of the time each sensor will be ready uint32_t millisReady[8] = { 0, }; // How many sensors to read uint8_t numSensors = 8; // converts allowable address characters '0'-'9', 'a'-'z', 'A'-'Z', // to a decimal number between 0 and 61 (inclusive) to cover the 62 possible addresses byte charToDec(char i){ if((i >= '0') && (i <= '9')) return i - '0'; if((i >= 'a') && (i <= 'z')) return i - 'a' + 10; if((i >= 'A') && (i <= 'Z')) return i - 'A' + 37; else return i; } void startConcurrentMeasurement(char i) { String command = ""; command += i; command += "C!"; // SDI-12 concurrent measurement command format [address]['C'][!] mySDI12.sendCommand(command); delay(30); // wait for acknowlegement with format [address][ttt (3 char, seconds)][number of measurments available, 0-9] String sdiResponse = ""; delay(30); while (mySDI12.available()) // build response string { char c = mySDI12.read(); if ((c != '\n') && (c != '\r')) { sdiResponse += c; delay(5); } } mySDI12.clearBuffer(); // find out how long we have to wait (in seconds). uint8_t wait = 0; wait = sdiResponse.substring(1, 4).toInt(); uint8_t sensorNum = charToDec(i); // e.g. convert '0' to 0, 'a' to 10, 'Z' to 61. waitTime[sensorNum] = wait; millisStarted[sensorNum] = millis(); millisReady[sensorNum] = millis() + wait * 1000; } void logSDI12Results(char i) { String command = ""; // in this example we will only take the 'DO' measurement command = ""; command += i; command += "D0!"; // SDI-12 command to get data [address][D][dataOption][!] mySDI12.sendCommand(command); // wait for acknowlegement uint32_t startMillis = millis(); while (!(mySDI12.available() > 7) && (millis() - startMillis < 5000L)) { } mySDI12.read(); // throw away the repeated address logFile.print(mySDI12.readStringUntil('+')); // read and log red band logFile.print(";"); logFile.print(mySDI12.readStringUntil('+')); // read and log nir band logFile.print(";"); logFile.print(mySDI12.readStringUntil('\n')); // read and log orientation logFile.print(";"); mySDI12.clearBuffer(); } void setup() { Serial.begin(115200); sensor_52.begin(); sensor_53.begin(); sensor_03.begin(); sensor_07.begin(); Wire.begin(); if (!SD.begin(chipSelect)) { return; } while (!Serial) ; mySDI12.begin(); delay(500); logFile = SD.open("hwheat.txt", FILE_WRITE); if (logFile) { logFile.println("fecha;hora;r_red;r_nir;r_orientation;r_red;r_nir;r_orientation;r_red;r_nir;r_orientation;r_532;r_570;orientation;r_532;r_570;orientation;r_532;r_570;orientation;i_532;i_570;orientation;r_red;r_nir;r_orientation;Temp1;Temp2;Temp3;Temp4"); logFile.close(); } } void lectura_sensores() { logFile.print(sensor_52.readObjectTempC()); logFile.print(";"); logFile.print(sensor_53.readObjectTempC()); logFile.print(";"); logFile.print(sensor_03.readObjectTempC()); logFile.print(";"); logFile.println(sensor_07.readObjectTempC()); } void loop() { time_t p; p = RTC.get(); logFile = SD.open("hwheat.txt", FILE_WRITE); if (logFile) { logFile.print(String(day(p)) + "/" + String(month(p)) + "/" + String(year(p)) + ";" + String(hour(p)) + ":" + String(minute(p)) + ":" + String(second(p))); // Start a concurrent measurement on all of the SDI-12 sensors for (char i = '0'; i < '8'; i++) { startConcurrentMeasurement(i); } // get all readings uint8_t numReadingsRecorded = 0; while (numReadingsRecorded < numSensors) { for (char i = '0'; i < '8'; i++) { if (millis() > millisReady[charToDec(i)]) { logSDI12Results(i); numReadingsRecorded++; } } } logFile.print(";"); lectura_sensores(); logFile.close(); } } |