aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2022-04-02 12:46:36 +0100
committerdakkar <dakkar@thenautilus.net>2022-04-02 12:46:36 +0100
commit7ece72b39a003c9793a77628e93235964c8cfd57 (patch)
tree5c4442b7f4b23ff1287e8c53443a33610ee84046
parentprefix for Serial prints (diff)
downloadenv-sensor-7ece72b39a003c9793a77628e93235964c8cfd57.tar.gz
env-sensor-7ece72b39a003c9793a77628e93235964c8cfd57.tar.bz2
env-sensor-7ece72b39a003c9793a77628e93235964c8cfd57.zip
simple serial command protocol
tagged responses, just because
-rw-r--r--battery.h4
-rw-r--r--co2.h38
-rw-r--r--datalog.h22
-rw-r--r--display.h4
-rw-r--r--main.ino26
-rw-r--r--pm.h4
6 files changed, 98 insertions, 0 deletions
diff --git a/battery.h b/battery.h
index 9843241..d86fbec 100644
--- a/battery.h
+++ b/battery.h
@@ -46,4 +46,8 @@ public:
void read(SensorData *data) {
data->batteryVoltage = voltage();
}
+
+ bool serialCommand(const String &tag, const String &command) {
+ return false;
+ }
};
diff --git a/co2.h b/co2.h
index f75c94c..a6a1480 100644
--- a/co2.h
+++ b/co2.h
@@ -72,4 +72,42 @@ public:
Serial.println(errorMessage);
}
}
+
+ bool serialCommand(const String &tag, const String &command) {
+ if (command.startsWith("setco ")) {
+ uint16_t error;
+ char errorMessage[256];
+
+ uint16_t target = command.substring(6).toInt();
+ uint16_t correction;
+
+ scd4x.stopPeriodicMeasurement();
+ delay(500);
+ error = scd4x.performForcedRecalibration(target, correction);
+ if (error) {
+ Serial.print("! CO2 performForcedRecalibration() error: ");
+ errorToString(error, errorMessage, 256);
+ Serial.println(errorMessage);
+ }
+
+ Serial.print(tag);Serial.print(" setco ");
+ if (correction == 0xFFFF) {
+ Serial.println("failed");
+ }
+ else {
+ Serial.println(correction - 0x8000);
+ }
+
+ error = scd4x.startLowPowerPeriodicMeasurement();
+ if (error) {
+ Serial.print("! CO2 startLowPowerPeriodicMeasurement() error: ");
+ errorToString(error, errorMessage, 256);
+ Serial.println(errorMessage);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
};
diff --git a/datalog.h b/datalog.h
index 96a3dfd..8f7bd81 100644
--- a/datalog.h
+++ b/datalog.h
@@ -68,4 +68,26 @@ public:
Serial.println("# flushed");
}
}
+
+ bool serialCommand(const String &tag, const String &command) {
+ if (command == "log") {
+ Serial.print(tag);Serial.println(" begin");
+
+ uint8_t buffer[1024];
+ logfile.flush();
+ int remaining = logfile.position();
+ logfile.seek(0);
+ while (remaining > 0) {
+ int bytesRead = logfile.read(buffer, 1024);
+ Serial.write(buffer, bytesRead);
+ remaining -= bytesRead;
+ }
+
+ Serial.print(tag);Serial.println(" end");
+
+ return true;
+ }
+
+ return false;
+ }
};
diff --git a/display.h b/display.h
index b6dd433..7a60317 100644
--- a/display.h
+++ b/display.h
@@ -107,4 +107,8 @@ public:
display.powerDown();
}
+
+ bool serialCommand(const String &tag, const String &command) {
+ return false;
+ }
};
diff --git a/main.ino b/main.ino
index 1963870..4eb35cc 100644
--- a/main.ino
+++ b/main.ino
@@ -17,6 +17,25 @@ SensorData data;
Display display;
DataLog datalog;
+void handleCommand(const String &line) {
+ int firstSpace = line.indexOf(' ');
+ if (firstSpace < 0) return;
+
+ String tag = line.substring(0,firstSpace);
+ String command = line.substring(firstSpace+1);
+ tag.trim();command.trim();
+
+ Serial.print("# <");Serial.print(tag);
+ Serial.print("|");Serial.print(command);
+ Serial.println(">");
+
+ if (battery.serialCommand(tag,command)) return;
+ if (pm.serialCommand(tag,command)) return;
+ if (co2.serialCommand(tag,command)) return;
+ if (display.serialCommand(tag,command)) return;
+ if (datalog.serialCommand(tag,command)) return;
+}
+
void setup() {
Serial.begin(115200);
while (!Serial) {
@@ -51,5 +70,12 @@ void loop() {
datalog.show(&data);
}
+ if (Serial.available()) {
+ Serial.println("# reading");
+ String line = Serial.readStringUntil('\n');
+ Serial.print("# ");Serial.println(line);
+ handleCommand(line);
+ }
+
delay(5000);
}
diff --git a/pm.h b/pm.h
index 7ac2050..116c74b 100644
--- a/pm.h
+++ b/pm.h
@@ -102,4 +102,8 @@ public:
sps30_sleep();
awake=false;
}
+
+ bool serialCommand(const String &tag, const String &command) {
+ return false;
+ }
};