diff --git a/Part2_Central_board/Firmware/CentralUnit/CentralUnit.ino b/Part2_Central_board/Firmware/CentralUnit/CentralUnit.ino index c22eebc..1e51b29 100644 --- a/Part2_Central_board/Firmware/CentralUnit/CentralUnit.ino +++ b/Part2_Central_board/Firmware/CentralUnit/CentralUnit.ino @@ -1,3 +1,4 @@ +#include #include "WiFi.h" #include "ESPAsyncWebServer.h" #include @@ -7,6 +8,7 @@ #include "Wire.h" #define WDT_TIMEOUT 20000 +#define EEPROM_SIZE 4 AsyncWebServer server(80); int WiFi_status = WL_IDLE_STATUS; @@ -15,6 +17,7 @@ volatile uint32_t nFlowSensorCount_last = 0; char dbgBuff[4024] = {0}; uint32_t dbgBuffPos = 0; +float fImpulsePerML = 0; void IRAM_ATTR ISR_flowSensor() { nFlowSensorCount++; @@ -40,6 +43,21 @@ void setup() Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); + // EEPROM begin + EEPROM.begin(EEPROM_SIZE); + + // Debug EEPROM before read + Serial.println("Before read: fImpulsePerML"); + Serial.println((float)(fImpulsePerML),4); + + // Read flow value from EEPROM + fImpulsePerML = EEPROM.readFloat(0); + + // Debug EEPROM after read + Serial.println("After read: fImpulsePerML"); + Serial.println((float)(fImpulsePerML),4); + + // Configure WDT Serial.println("Configuring WDT..."); esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts @@ -273,6 +291,36 @@ void setupWebServer(void) request->send(200, "text/plain", "OK"); return; }); + + // Calibrate flow sensor + server.on("/calibrateFlowSensor", HTTP_GET, [] (AsyncWebServerRequest *request) { + Serial.println("Calibrate edges per mL"); + + if ( request->hasParam("edges") ) + { + String xEdges; + float fEdges = 0; + + xEdges = request->getParam("edges")->value(); + fEdges = xEdges.toFloat(); + + // Update value + fImpulsePerML = fEdges; + EEPROM.writeFloat(0, fEdges);//EEPROM.put(address, param); + EEPROM.commit(); + + Serial.print("Changing fImpulsePerML to "); + Serial.println((float)(fImpulsePerML),4); + + request->send(200, "text/plain", "OK"); + return; + } + else + { + request->send(404, "text/plain", "MISSING_ARGUMENT"); + return; + } + }); } diff --git a/Part2_Central_board/Firmware/CentralUnit/PlantSystem_MainLogic.ino b/Part2_Central_board/Firmware/CentralUnit/PlantSystem_MainLogic.ino index 312140b..7c9f2a7 100644 --- a/Part2_Central_board/Firmware/CentralUnit/PlantSystem_MainLogic.ino +++ b/Part2_Central_board/Firmware/CentralUnit/PlantSystem_MainLogic.ino @@ -161,8 +161,8 @@ void PlantSystem_tick(void) flowInterruptEnabled(true); // - Turn ON pump + bWateringError = true; Serial.println("Turning pump ON"); - delay(200); digitalWrite(WATER_PUMP_EN_PIN, HIGH); esp_task_wdt_reset(); @@ -170,31 +170,32 @@ void PlantSystem_tick(void) // Calculate water flow uint32_t nWaterFlow_millis_timestamp = millis(); - float flowRate = 0.0; - unsigned int flowMilliLitres =0; + float flowMilliLitres = 0.0; unsigned long totalMilliLitres = 0; + // Wait until target mL or timeout is reached while(millis() < nTimeout) { - if((millis() - nWaterFlow_millis_timestamp) > 1000) // Only process counters once per second + // Calculate mL every second + if( millis() >= nWaterFlow_millis_timestamp) { + // Disable interrupts flowInterruptEnabled(false); - nFlowSensorCount_last = nFlowSensorCount; - nFlowSensorCount = 0; - bWateringError = true; - - - flowRate = ((1000.0 / (millis() - nWaterFlow_millis_timestamp)) * nFlowSensorCount_last) / WATERING_FLOW_EDGES_PER_L; - flowMilliLitres = (flowRate / 60) * 1000; + // Update mL + flowMilliLitres = nFlowSensorCount/fImpulsePerML; totalMilliLitres += flowMilliLitres; + // Check if target has been reached if(totalMilliLitres >= nWatering_volume) { + bWateringError = false; break; } - - nWaterFlow_millis_timestamp = millis(); + + // Update for next iteration + nWaterFlow_millis_timestamp = millis() + 1000; + nFlowSensorCount = 0; esp_task_wdt_reset(); flowInterruptEnabled(true); } @@ -221,7 +222,7 @@ void PlantSystem_tick(void) bWatering_requestPending = false; if(bWateringError) { - LED_Blink(3, 250); + LED_Blink(4, 250); } else {