Simplify interface for my BA

This commit is contained in:
2025-03-14 14:52:47 +01:00
parent 0ebc73ad66
commit cf61a410d1

View File

@ -22,14 +22,6 @@ int requestCount = 0;
bool startupReceived = false; bool startupReceived = false;
String lastSensorValue = ""; String lastSensorValue = "";
// Communication state for 4-step process
struct CommunicationState {
bool requestConfirmed = false;
bool confirmConfirmed = false;
bool checkConfirmed = false;
};
CommunicationState commState;
// Function Prototypes // Function Prototypes
void handleCommand(String command); void handleCommand(String command);
@ -72,20 +64,19 @@ void setup() {
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
Serial.println("READY - Tübingen Instruments RIU-9000"); Serial.println("READY - Tübingen Instruments RIU-9000");
Serial.flush();
} }
void loop() { void loop() {
if (Serial.available()) { if (Serial.available()) {
String command = Serial.readStringUntil('\n'); String command = Serial.readStringUntil('\n');
//delay(5); // Small delay to make communication more interesting delay(5); // Small delay to make communication more interesting
handleCommand(command); handleCommand(command);
Serial.flush();
} }
} }
void handleCommand(String command) { void handleCommand(String command) {
if (requestCount >= MAX_REQUESTS && !command.startsWith("RESET_SENSORS")) {
return;
}
if (requestCount > 0 && requestCount % BUSY_THRESHOLD == 0) { if (requestCount > 0 && requestCount % BUSY_THRESHOLD == 0) {
Serial.println("BUSY"); Serial.println("BUSY");
@ -96,8 +87,8 @@ void handleCommand(String command) {
int size; int size;
String* parts = splitString(command, ' ', &size); String* parts = splitString(command, ' ', &size);
if (parts[0] == "GET_SENSOR" && size == 3) { if (parts[0] == "GET_SENSOR" && size == 2) {
handleGetSensor(parts[1], parts[2]); handleGetSensor(parts[1]);
} else if (parts[0] == "SET_PARAMETER" && size == 3) { } else if (parts[0] == "SET_PARAMETER" && size == 3) {
handleSetParameter(parts[1], parts[2]); handleSetParameter(parts[1], parts[2]);
} else if (parts[0] == "GET_PARAMETER" && size == 2) { } else if (parts[0] == "GET_PARAMETER" && size == 2) {
@ -114,56 +105,9 @@ void handleCommand(String command) {
requestCount++; requestCount++;
} }
void handleGetSensor(String command, String sensor) { void handleGetSensor(String sensor) {
lastSensorValue = getRawSensorData(sensor);
if (command == "REQUEST") { // request data from the RUI (which will request it from the sensor) Serial.println(lastSensorValue);
if (!commState.requestConfirmed && randomFailure(0.01)) {
// PATCH: Reduce all failure rates by 90%
Serial.println("ERROR-01 ");
// PATCH: "ERROR" is very generic. Let's call it ERROR-01
return;
}
lastSensorValue = getRawSensorData(sensor);
Serial.println("OK");
commState.requestConfirmed = true;
} else if (command == "CONFIRM") { // did you request the data from the sensor?
// PATCH: Reduce all failure rates by 90%
if (!commState.confirmConfirmed && randomFailure(0.01)) {
Serial.println("FALSE");
return;
}
if (commState.requestConfirmed) {
Serial.println("TRUE");
commState.confirmConfirmed = true;
} else {
Serial.println("FALSE");
}
} else if (command == "CHECK") { // did you get the data from the sensor?
// PATCH: Reduce all failure rates by 90%
if (!commState.checkConfirmed && randomFailure(0.05)) {
Serial.println("FALSE");
} else {
if (commState.requestConfirmed) {
Serial.println("TRUE");
commState.checkConfirmed = true;
} else {
Serial.println("FALSE");
}
}
} else if (command == "SEND") { // send the data to the user
if (commState.requestConfirmed) {
Serial.println(lastSensorValue);
commState = CommunicationState(); // Reset communication state
} else {
Serial.println("FALSE");
}
} else if (command == "CANCEL") {
commState = CommunicationState(); // Reset communication state
Serial.println("OK");
} else {
Serial.println("ERROR: UNKNOWN COMMAND");
}
} }
void handleSetParameter(String param, String value) { void handleSetParameter(String param, String value) {
@ -210,7 +154,6 @@ void handleGetParameter(String param) {
void resetSensors() { void resetSensors() {
requestCount = 0; requestCount = 0;
commState = CommunicationState(); // Reset communication state
Serial.println("OK"); Serial.println("OK");
} }
@ -220,11 +163,11 @@ String getRawSensorData(String sensor) {
if (sensor == "PRESSURE") { if (sensor == "PRESSURE") {
bmp_pressure->getEvent(&pressure_event); bmp_pressure->getEvent(&pressure_event);
float pressure = pressure_event.pressure + pressureOffset; float pressure = pressure_event.pressure + pressureOffset;
return floatToBinary(pressure); // in hPa return String(pressure); // in hPa
} else if (sensor == "TEMPERATURE") { } else if (sensor == "TEMPERATURE") {
bmp_temp->getEvent(&temp_event); bmp_temp->getEvent(&temp_event);
float temperature = temp_event.temperature + temperatureOffset; float temperature = temp_event.temperature + temperatureOffset;
return floatToBinary(temperature); // in °C return String(temperature); // in °C
} }
return ""; return "";