#include #include #include #include int flowRatePin = 34; int phPin = 14; int tdsPin = 35; int temperaturePin = 26; OneWire oneWire(temperaturePin); DallasTemperature sensors(&oneWire); volatile int flowRatePulses; int timeBetweenSamples = 15; //s const char* ssid = ""; const char* password = ""; int wiFiConnectTimeout = 10; //seconds IPAddress metricsServerIp = IPAddress(192,168,1,200); void setup() { sensors.begin(); pinMode(flowRatePin, INPUT_PULLUP); pinMode(phPin, INPUT); pinMode(tdsPin, INPUT); Serial.begin(115200); } bool sendApiRequest(float flowRate, float temperature, int phRaw, int tdsRaw) { Serial.print("Sending API request..."); const String metricsServerUrl = "http://" + metricsServerIp.toString() + ":3445/"; HTTPClient http; http.setTimeout(3000); http.setConnectTimeout(3000); http.begin(metricsServerUrl); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String httpRequestData = "instance=a&flowRate=" + String(flowRate) + "&temp[0]=" + String(temperature) + "&phRaw[0]=" + String(phRaw) + "&tdsRaw[0]=" + String(tdsRaw); int httpResponseCode = http.POST(httpRequestData); http.end(); if (httpResponseCode >= 200) { Serial.println(" OK!"); return true; } else { Serial.println(" FAIL! (" + String(httpResponseCode) + ")"); return false; } } bool connectToWifi() { Serial.print("Connecting to WiFi"); WiFi.mode(WIFI_STA); /* Configure ESP32 in STA Mode */ WiFi.begin(ssid, password); /* Connect to Wi-Fi based on the above SSID and Password */ int wiFiConnectAttempts = 0; while(WiFi.status() != WL_CONNECTED && wiFiConnectAttempts++ < wiFiConnectTimeout) { Serial.print("."); delay(1000); } if (WiFi.status() != WL_CONNECTED) { Serial.println(" FAILED!"); return false; } else { Serial.println(" OK!"); return true; } } void loop() { // Flow rate attachInterrupt(digitalPinToInterrupt(flowRatePin), FlowRatePulseIncrement, FALLING); flowRatePulses = 0; delay(timeBetweenSamples * 1000); detachInterrupt(digitalPinToInterrupt(flowRatePin)); float flowRate = (flowRatePulses * 2.22 * (60/timeBetweenSamples)); // Serial.println("Flow rate: " + String(flowRate) + "mL/minute"); // Temperature sensors.requestTemperatures(); float temperature = sensors.getTempCByIndex(0); // PH int phRaw = analogRead(phPin); // TDS float tdsRaw = 0; for (int i=0; i<20; i++) { tdsRaw += analogRead(tdsPin); delay(100); } tdsRaw /= 20; Serial.print(flowRate); Serial.print(","); Serial.print(temperature); Serial.print(","); Serial.print(phRaw); Serial.print(","); Serial.print(tdsRaw); Serial.println(); if (connectToWifi() == true) { sendApiRequest(flowRate, temperature, phRaw, tdsRaw); } WiFi.disconnect(true, true); } void FlowRatePulseIncrement() { flowRatePulses++; }