Skip to content

Commit

Permalink
Fix concurrent example
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Damiano <[email protected]>
  • Loading branch information
SRGDamia1 committed Aug 12, 2024
1 parent a2eaa8b commit 52b185b
Showing 1 changed file with 59 additions and 68 deletions.
127 changes: 59 additions & 68 deletions examples/k_concurrent_logger/k_concurrent_logger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@ int8_t lastAddress = 62; /* The last address in the address space to check (62
SDI12 mySDI12(dataPin);

// keeps track of active addresses
bool isActive[64] = {
0,
};
bool isActive[64];

// keeps track of the wait time for each active addresses
uint8_t meas_time_ms[64] = {
0,
};
uint8_t meas_time_ms[64];

// keeps track of the time each sensor was started
uint32_t millisStarted[64] = {
0,
};
uint32_t millisStarted[64];

// keeps track of the time each sensor will be ready
uint32_t millisReady[64] = {
0,
};
uint32_t millisReady[64];

// keeps track of the number of results expected
uint8_t returnedResults[64] = {
0,
};
uint8_t expectedResults[64];

// keeps track of the number of results returned
uint8_t returnedResults[64];

uint8_t numSensors = 0;

Expand Down Expand Up @@ -81,44 +74,13 @@ char decToChar(byte i) {
return i;
}

/**
* @brief gets identification information from a sensor, and prints it to the serial
* port
*
* @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
*/
void printInfo(char i) {
String command = "";
command += (char)i;
command += "I!";
mySDI12.sendCommand(command);
delay(100);

String sdiResponse = mySDI12.readStringUntil('\n');
sdiResponse.trim();
// allccccccccmmmmmmvvvxxx...xx<CR><LF>
Serial.print(sdiResponse.substring(0, 1)); // address
Serial.print(", ");
Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
Serial.print(", ");
Serial.print(sdiResponse.substring(3, 11)); // vendor id
Serial.print(", ");
Serial.print(sdiResponse.substring(11, 17)); // sensor model
Serial.print(", ");
Serial.print(sdiResponse.substring(17, 20)); // sensor version
Serial.print(", ");
Serial.print(sdiResponse.substring(20)); // sensor id
Serial.print(", ");
}

bool getResults(char i, int resultsExpected) {
bool getResults(char address, int resultsExpected) {
uint8_t resultsReceived = 0;
uint8_t cmd_number = 0;
while (resultsReceived < resultsExpected && cmd_number <= 9) {
String command = "";
// in this example we will only take the 'DO' measurement
command = "";
command += i;
command = "";
command += address;
command += "D";
command += cmd_number;
command += "!"; // SDI-12 command to get data [address][D][dataOption][!]
Expand Down Expand Up @@ -148,7 +110,7 @@ bool getResults(char i, int resultsExpected) {
cmd_number++;
}
mySDI12.clearBuffer();

returnedResults[charToDec(address)] = resultsReceived;
return resultsReceived == resultsExpected;
}

Expand Down Expand Up @@ -181,7 +143,7 @@ int startConcurrentMeasurement(char i, String meas_type = "") {
} else {
millisReady[sensorNum] = millis() + wait * 1000;
}
returnedResults[sensorNum] = numResults;
expectedResults[sensorNum] = numResults;

return numResults;
}
Expand All @@ -206,6 +168,38 @@ boolean checkActive(char i) {
return false;
}

/**
* @brief gets identification information from a sensor, and prints it to the serial
* port
*
* @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
* @param printCommands true to print the raw output and input from the command
*/
void printInfo(char i) {
String command = "";
command += (char)i;
command += "I!";
mySDI12.sendCommand(command);
delay(100);

String sdiResponse = mySDI12.readStringUntil('\n');
sdiResponse.trim();
// allccccccccmmmmmmvvvxxx...xx<CR><LF>
Serial.print("Address: ");
Serial.print(sdiResponse.substring(0, 1)); // address
Serial.print(", SDI-12 Version: ");
Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
Serial.print(", Vendor ID: ");
Serial.print(sdiResponse.substring(3, 11)); // vendor id
Serial.print(", Sensor Model: ");
Serial.print(sdiResponse.substring(11, 17)); // sensor model
Serial.print(", Sensor Version: ");
Serial.print(sdiResponse.substring(17, 20)); // sensor version
Serial.print(", Sensor ID: ");
Serial.print(sdiResponse.substring(20)); // sensor id
Serial.println();
}

void setup() {
Serial.begin(serialBaud);
while (!Serial)
Expand All @@ -231,7 +225,7 @@ void setup() {
Serial.println("Protocol Version, Sensor Address, Sensor Vendor, Sensor Model, "
"Sensor Version, Sensor ID");

for (byte i = firstAddress; i < lastAddress; i++) {
for (int8_t i = firstAddress; i <= lastAddress; i++) {
char addr = decToChar(i);
if (checkActive(addr)) {
numSensors++;
Expand All @@ -257,31 +251,28 @@ void setup() {

void loop() {
// start all sensors measuring concurrently
for (byte i = firstAddress; i < lastAddress; i++) {
for (int8_t i = firstAddress; i <= lastAddress; i++) {
char addr = decToChar(i);
if (isActive[i]) { startConcurrentMeasurement(addr); }
}

// get all readings
uint8_t numReadingsRecorded = 0;
while (numReadingsRecorded < numSensors) {
for (byte i = firstAddress; i < lastAddress; i++) {
do {
for (int8_t i = firstAddress; i <= lastAddress; i++) {
char addr = decToChar(i);
if (isActive[i]) {
if (millis() > millisReady[i]) {
if (returnedResults[i] > 0) {
Serial.print(millis() / 1000);
Serial.print(", ");
Serial.print(addr);
Serial.print(", ");
getResults(addr, returnedResults[i]);
Serial.println();
}
numReadingsRecorded++;
}
if (isActive[i] && millis() > millisReady[i] && expectedResults[i] > 0 &&
(returnedResults[i] < expectedResults[i])) {
Serial.print(millis() / 1000);
Serial.print(", ");
Serial.print(addr);
Serial.print(", ");
getResults(addr, expectedResults[i]);
numReadingsRecorded++;
Serial.println();
}
}
}
} while (numReadingsRecorded < numSensors);

delay(10000); // wait ten seconds between measurement attempts.
}

0 comments on commit 52b185b

Please sign in to comment.