Advertisement
safwan092

Untitled

Feb 9th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. #include <FirebaseESP32.h>
  2. #include <addons/TokenHelper.h>
  3. #include <addons/RTDBHelper.h>
  4.  
  5. #define WIFI_SSID "network"
  6. #define WIFI_PASSWORD "123456789"
  7. #define API_KEY "AIzaSyBvKt7zTjZXd_bRQUcB4pqb6BNE2PPccqA"
  8. #define USER_EMAIL "WaterQuality.Project.10591@gmail.com"
  9. #define USER_PASSWORD "123456789"
  10. #define DATABASE_URL "waterproject-9abe3-default-rtdb.firebaseio.com"
  11. #define DATABASE_SECRET "0OtpId3JzdsVexOAnWChAe7DMoXWttpmhJA0sVpe"
  12.  
  13. FirebaseData fbdo;
  14. FirebaseAuth auth;
  15. FirebaseConfig config;
  16.  
  17. unsigned long dataMillis = 0;
  18.  
  19. #define WaterLeak 34 //(D23)
  20. #define TdsSensorPin 35 //(D35)
  21.  
  22. #define VREF 3.3 // analog reference voltage(Volt) of the ADC
  23. #define SCOUNT 30 // sum of sample point
  24. int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
  25. int analogBufferTemp[SCOUNT];
  26. int analogBufferIndex = 0;
  27. int copyIndex = 0;
  28. float averageVoltage = 0;
  29. float tdsValue = 0;
  30. float temperature = 25;
  31. char character;
  32. String dataPacket;
  33. int8_t indexOfA, indexOfB;
  34. String var1, var2;
  35.  
  36. int WaterLevel = 0;
  37. int WaterLeakValue = 0;
  38. float flowRate;
  39.  
  40. float temperature25 = 25;
  41.  
  42. int getMedianNum(int bArray[], int iFilterLen)
  43. {
  44. int bTab[iFilterLen];
  45. for (byte i = 0; i < iFilterLen; i++)
  46. bTab[i] = bArray[i];
  47. int i, j, bTemp;
  48. for (j = 0; j < iFilterLen - 1; j++)
  49. {
  50. for (i = 0; i < iFilterLen - j - 1; i++)
  51. {
  52. if (bTab[i] > bTab[i + 1])
  53. {
  54. bTemp = bTab[i];
  55. bTab[i] = bTab[i + 1];
  56. bTab[i + 1] = bTemp;
  57. }
  58. }
  59. }
  60. if ((iFilterLen & 1) > 0)
  61. bTemp = bTab[(iFilterLen - 1) / 2];
  62. else
  63. bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  64. return bTemp;
  65. }
  66.  
  67. void setup() {
  68. Serial.begin(115200);
  69. Serial2.begin(9600);
  70. pinMode(TdsSensorPin, INPUT);
  71. pinMode(WaterLeak, INPUT);
  72. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  73. Serial.print("Connecting to Wi-Fi");
  74. while (WiFi.status() != WL_CONNECTED)
  75. {
  76. Serial.print(".");
  77. delay(300);
  78. }
  79. Serial.println();
  80. Serial.print("Connected with IP: ");
  81. Serial.println(WiFi.localIP());
  82. Serial.println();
  83. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  84. config.api_key = API_KEY;
  85. auth.user.email = USER_EMAIL;
  86. auth.user.password = USER_PASSWORD;
  87. config.database_url = DATABASE_URL;
  88. Firebase.reconnectWiFi(true);
  89. fbdo.setResponseSize(4096);
  90. config.token_status_callback = tokenStatusCallback;
  91. config.max_token_generation_retry = 5;
  92. Firebase.begin(&config, &auth);
  93. }
  94.  
  95. void loop() {
  96. static unsigned long analogSampleTimepoint = millis();
  97. if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
  98. {
  99. analogSampleTimepoint = millis();
  100. analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
  101. analogBufferIndex++;
  102. if (analogBufferIndex == SCOUNT)
  103. analogBufferIndex = 0;
  104. }
  105. static unsigned long printTimepoint = millis();
  106. if (millis() - printTimepoint > 800U)
  107. {
  108. printTimepoint = millis();
  109. for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
  110. analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  111. averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
  112. float compensationCoefficient = 1.0 + 0.02 * (temperature25 - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
  113. float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
  114. tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
  115.  
  116. WaterLeakValue = analogRead(WaterLeak);
  117. }
  118. readSerialDataFromArduino();
  119.  
  120. if (millis() - dataMillis > 5000 && Firebase.ready())
  121. {
  122. dataMillis = millis();
  123.  
  124. Serial.printf("Set Water Level [String]... %s\n", Firebase.setString(fbdo, "/WL/", var2) ? "ok" : fbdo.errorReason().c_str());
  125. Serial.printf("Set TDS Value [float]... %s\n", Firebase.setFloat(fbdo, "/TDS/", tdsValue) ? "ok" : fbdo.errorReason().c_str());
  126. Serial.printf("Set Water Flow Value [String]... %s\n", Firebase.setString(fbdo, "/WF/", var1) ? "ok" : fbdo.errorReason().c_str());
  127. Serial.printf("Set Water Leak Value [int]... %s\n", Firebase.setInt(fbdo, "/LEAK/", WaterLeakValue) ? "ok" : fbdo.errorReason().c_str());
  128. }
  129. }//end of loop
  130.  
  131.  
  132.  
  133.  
  134.  
  135. void readSerialDataFromArduino() {
  136. while (Serial2.available() > 0) {
  137. character = Serial2.read();
  138. if (character == '\n') {
  139. break;
  140. }
  141. else {
  142. dataPacket += character;
  143. }
  144. }
  145. if (character == '\n') {
  146. acquireData();
  147. ShowDataToSerial();
  148. flushSerialBufferData();
  149. }
  150. }
  151.  
  152. void acquireData() {
  153. indexOfA = dataPacket.indexOf("A");
  154. indexOfB = dataPacket.indexOf("B");
  155. var1 = dataPacket.substring(0, indexOfA);
  156. var2 = dataPacket.substring(indexOfA + 1, indexOfB);
  157. }
  158.  
  159. void ShowDataToSerial() {
  160. Serial.print("Water Leak Value:");
  161. Serial.println(WaterLeakValue);
  162.  
  163. Serial.print("Water Level Value:");
  164. Serial.println(var2);
  165.  
  166. Serial.print("TDS Value:");
  167. Serial.print(tdsValue);
  168. Serial.println("ppm");
  169.  
  170. Serial.print("Flow rate: ");
  171. Serial.print(var1);
  172. Serial.println("L/min");
  173.  
  174. Serial.println("_______________________________");
  175. }
  176.  
  177. void flushSerialBufferData() {
  178. character = 0;
  179. dataPacket = "";
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement