diff options
author | dakkar <dakkar@thenautilus.net> | 2022-04-02 12:46:36 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2022-04-02 12:46:36 +0100 |
commit | 7ece72b39a003c9793a77628e93235964c8cfd57 (patch) | |
tree | 5c4442b7f4b23ff1287e8c53443a33610ee84046 | |
parent | prefix for Serial prints (diff) | |
download | env-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.h | 4 | ||||
-rw-r--r-- | co2.h | 38 | ||||
-rw-r--r-- | datalog.h | 22 | ||||
-rw-r--r-- | display.h | 4 | ||||
-rw-r--r-- | main.ino | 26 | ||||
-rw-r--r-- | pm.h | 4 |
6 files changed, 98 insertions, 0 deletions
@@ -46,4 +46,8 @@ public: void read(SensorData *data) { data->batteryVoltage = voltage(); } + + bool serialCommand(const String &tag, const String &command) { + return false; + } }; @@ -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; + } }; @@ -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; + } }; @@ -107,4 +107,8 @@ public: display.powerDown(); } + + bool serialCommand(const String &tag, const String &command) { + return false; + } }; @@ -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); } @@ -102,4 +102,8 @@ public: sps30_sleep(); awake=false; } + + bool serialCommand(const String &tag, const String &command) { + return false; + } }; |