From 7ece72b39a003c9793a77628e93235964c8cfd57 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 2 Apr 2022 12:46:36 +0100 Subject: simple serial command protocol tagged responses, just because --- battery.h | 4 ++++ co2.h | 38 ++++++++++++++++++++++++++++++++++++++ datalog.h | 22 ++++++++++++++++++++++ display.h | 4 ++++ main.ino | 26 ++++++++++++++++++++++++++ pm.h | 4 ++++ 6 files changed, 98 insertions(+) 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; + } }; -- cgit v1.2.3