safwan092

ESP32-C3 XIAO 1.3inch OLED Display TDS Sensor Firebase WiFi Setup

Jan 14th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <Wire.h> //default library
  2. #include <U8g2lib.h> //https://github.com/olikraus/U8g2_Arduino
  3. #include <WiFi.h>
  4. #include <FirebaseESP32.h> //https://github.com/mobizt/Firebase-ESP8266
  5. #include <addons/TokenHelper.h>
  6. #include <addons/RTDBHelper.h>
  7. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  8.  
  9.  
  10. U8G2_SSD1306_128X64_NONAME_F_HW_I2C DISPB(U8G2_R0, U8X8_PIN_NONE);
  11.  
  12. #define SDA 4
  13. #define SCL 5
  14. #define TDS_SENSOR_PIN A0
  15.  
  16. String WIFI_SSID = "Device [Setup WiFi]";
  17. #define API_KEY "AIzaSyCzMchetkDlrD-9YdZ4KuTiigAfjzPUl14"
  18. #define USER_EMAIL "[email protected]"
  19. #define USER_PASSWORD "Password123456789"
  20. #define DATABASE_URL "ai-glucose-tool-default-rtdb.firebaseio.com"
  21. #define DATABASE_SECRET "rLsxPgTD8YY8NaiOkWVjwjZzhi3kS8F3fDCpbHZX"
  22.  
  23. FirebaseData fbdo;
  24. FirebaseAuth auth;
  25. FirebaseConfig config;
  26. unsigned long dataMillis = 0;
  27. int count = 0;
  28.  
  29. int tdsValue = 0;
  30. float tds = 0;
  31.  
  32.  
  33.  
  34.  
  35.  
  36. void setup() {
  37. Serial.begin(115200);
  38. setupDisplay();
  39. connect_To_WiFi();
  40. connectingToFirebaseDatabase();
  41. }
  42.  
  43. void loop() {
  44. reConnect_To_WiFi();
  45. readSensor();
  46. showDataOnSerialMonitor();
  47. showDataOnDisplay();
  48. sendingDataToFirebase(5);
  49. delay(1000);
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. void setupDisplay() {
  60. pinMode(SDA, OUTPUT);
  61. pinMode(SCL, OUTPUT);
  62. Wire.setClock(100000);
  63. DISPB.setBusClock(100000);
  64. DISPB.setI2CAddress(0x78);
  65. DISPB.begin();
  66. DISPB.setFont(u8g2_font_logisoso16_tf ); //smaller font to show difference
  67. DISPB.clearBuffer();
  68. }
  69.  
  70. void readSensor() {
  71. tdsValue = analogRead(TDS_SENSOR_PIN);
  72. tds = tdsValue * 0.5;
  73. }
  74.  
  75. void showDataOnSerialMonitor() {
  76. Serial.print("TDS Value: ");
  77. Serial.print(tds);
  78. Serial.println(" ppm");
  79. }
  80.  
  81. void showDataOnDisplay() {
  82. DISPB.clearBuffer();
  83. DISPB.setCursor(0, 31);
  84. DISPB.print("TDS Value:");
  85. DISPB.setCursor(10, 63);
  86. DISPB.print(tds);
  87. DISPB.print(" ml");
  88. DISPB.sendBuffer();
  89. }
  90.  
  91. void connectingToFirebaseDatabase() {
  92. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  93. config.api_key = API_KEY;
  94. auth.user.email = USER_EMAIL;
  95. auth.user.password = USER_PASSWORD;
  96. config.database_url = DATABASE_URL;
  97. Firebase.reconnectWiFi(true);
  98. fbdo.setResponseSize(4096);
  99. String base_path = "/UsersData/";
  100. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  101. config.max_token_generation_retry = 5;
  102. Firebase.begin(&config, &auth);
  103. String var = "$userId";
  104. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  105. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  106. }
  107.  
  108.  
  109. void sendingDataToFirebase(unsigned long timeDelay) {
  110. if (millis() - dataMillis > (timeDelay * 1000) && Firebase.ready())
  111. {
  112. dataMillis = millis();
  113. String path = "/UsersData/";
  114. path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
  115. path += "/test/TDS";
  116. Serial.printf("Set TDS... %s\n", Firebase.setFloat(fbdo, path, tds) ? "ok" : fbdo.errorReason().c_str());
  117. }
  118. }
  119.  
  120.  
  121. void reConnect_To_WiFi() {
  122. while (WiFi.status() != WL_CONNECTED) {
  123. //delay(500);
  124. Serial.print(".");
  125. connect_To_WiFi();
  126. }
  127. }
  128.  
  129. void connect_To_WiFi() {
  130. WiFiManager wm;
  131. bool res;
  132. res = wm.autoConnect(WIFI_SSID.c_str()); // password protected ap
  133.  
  134. if (!res) {
  135. Serial.println("Failed to Connect!!");
  136. // ESP.restart();
  137. }
  138. else {
  139. //if you get here you have connected to the WiFi
  140. Serial.println("Connected...");
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment