Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FirebaseESP32.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- #define API_KEY "AIzaSyBvKt7zTjZXd_bRQUcB4pqb6BNE2PPccqA"
- #define USER_EMAIL "WaterQuality.Project.10591@gmail.com"
- #define USER_PASSWORD "123456789"
- #define DATABASE_URL "waterproject-9abe3-default-rtdb.firebaseio.com"
- #define DATABASE_SECRET "0OtpId3JzdsVexOAnWChAe7DMoXWttpmhJA0sVpe"
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- unsigned long dataMillis = 0;
- #define WaterLeak 34 //(D23)
- #define TdsSensorPin 35 //(D35)
- #define VREF 3.3 // analog reference voltage(Volt) of the ADC
- #define SCOUNT 30 // sum of sample point
- int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
- int analogBufferTemp[SCOUNT];
- int analogBufferIndex = 0;
- int copyIndex = 0;
- float averageVoltage = 0;
- float tdsValue = 0;
- float temperature = 25;
- char character;
- String dataPacket;
- int8_t indexOfA, indexOfB;
- String var1, var2;
- int WaterLevel = 0;
- int WaterLeakValue = 0;
- float flowRate;
- float temperature25 = 25;
- int getMedianNum(int bArray[], int iFilterLen)
- {
- int bTab[iFilterLen];
- for (byte i = 0; i < iFilterLen; i++)
- bTab[i] = bArray[i];
- int i, j, bTemp;
- for (j = 0; j < iFilterLen - 1; j++)
- {
- for (i = 0; i < iFilterLen - j - 1; i++)
- {
- if (bTab[i] > bTab[i + 1])
- {
- bTemp = bTab[i];
- bTab[i] = bTab[i + 1];
- bTab[i + 1] = bTemp;
- }
- }
- }
- if ((iFilterLen & 1) > 0)
- bTemp = bTab[(iFilterLen - 1) / 2];
- else
- bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
- return bTemp;
- }
- void setup() {
- Serial.begin(115200);
- Serial2.begin(9600);
- pinMode(TdsSensorPin, INPUT);
- pinMode(WaterLeak, INPUT);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(300);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- config.api_key = API_KEY;
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- config.database_url = DATABASE_URL;
- Firebase.reconnectWiFi(true);
- fbdo.setResponseSize(4096);
- config.token_status_callback = tokenStatusCallback;
- config.max_token_generation_retry = 5;
- Firebase.begin(&config, &auth);
- }
- void loop() {
- static unsigned long analogSampleTimepoint = millis();
- if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
- {
- analogSampleTimepoint = millis();
- analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
- analogBufferIndex++;
- if (analogBufferIndex == SCOUNT)
- analogBufferIndex = 0;
- }
- static unsigned long printTimepoint = millis();
- if (millis() - printTimepoint > 800U)
- {
- printTimepoint = millis();
- for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
- analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
- averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
- float compensationCoefficient = 1.0 + 0.02 * (temperature25 - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
- float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
- tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
- WaterLeakValue = analogRead(WaterLeak);
- }
- readSerialDataFromArduino();
- if (millis() - dataMillis > 5000 && Firebase.ready())
- {
- dataMillis = millis();
- Serial.printf("Set Water Level [String]... %s\n", Firebase.setString(fbdo, "/WL/", var2) ? "ok" : fbdo.errorReason().c_str());
- Serial.printf("Set TDS Value [float]... %s\n", Firebase.setFloat(fbdo, "/TDS/", tdsValue) ? "ok" : fbdo.errorReason().c_str());
- Serial.printf("Set Water Flow Value [String]... %s\n", Firebase.setString(fbdo, "/WF/", var1) ? "ok" : fbdo.errorReason().c_str());
- Serial.printf("Set Water Leak Value [int]... %s\n", Firebase.setInt(fbdo, "/LEAK/", WaterLeakValue) ? "ok" : fbdo.errorReason().c_str());
- }
- }//end of loop
- void readSerialDataFromArduino() {
- while (Serial2.available() > 0) {
- character = Serial2.read();
- if (character == '\n') {
- break;
- }
- else {
- dataPacket += character;
- }
- }
- if (character == '\n') {
- acquireData();
- ShowDataToSerial();
- flushSerialBufferData();
- }
- }
- void acquireData() {
- indexOfA = dataPacket.indexOf("A");
- indexOfB = dataPacket.indexOf("B");
- var1 = dataPacket.substring(0, indexOfA);
- var2 = dataPacket.substring(indexOfA + 1, indexOfB);
- }
- void ShowDataToSerial() {
- Serial.print("Water Leak Value:");
- Serial.println(WaterLeakValue);
- Serial.print("Water Level Value:");
- Serial.println(var2);
- Serial.print("TDS Value:");
- Serial.print(tdsValue);
- Serial.println("ppm");
- Serial.print("Flow rate: ");
- Serial.print(var1);
- Serial.println("L/min");
- Serial.println("_______________________________");
- }
- void flushSerialBufferData() {
- character = 0;
- dataPacket = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement