safwan092

Untitled

May 11th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <FirebaseESP8266.h>
  3. #include <addons/TokenHelper.h>
  4. #include <addons/RTDBHelper.h>
  5.  
  6. #define WIFI_SSID "HUAWEI-930D"
  7. #define WIFI_PASSWORD "49418874"
  8. #define API_KEY "AIzaSyBApzVK8nVRR3G-T0Nn4Bk-BDCpFCn9ujk"
  9. #define DATABASE_URL "mw-ultra-water-level-indicator-default-rtdb.firebaseio.com"
  10. #define USER_EMAIL "[email protected]"
  11. #define USER_PASSWORD "123456789"
  12.  
  13.  
  14. ////////////////////////////////////////////////////
  15. int maxdistance = 23;
  16. ////////////////////////////////////////////////////
  17.  
  18.  
  19. long duration;
  20. float distance;
  21. const int TRIGGER = 12; //D6 = GPIO12
  22. const int ECHO = 14; //D5 = GPIO14
  23.  
  24. FirebaseData fbdo;
  25. FirebaseAuth auth;
  26. FirebaseConfig config;
  27.  
  28. unsigned long sendDataPrevMillis = 0;
  29.  
  30. int count = 0;
  31.  
  32. void setup()
  33. {
  34. pinMode(TRIGGER, OUTPUT);
  35. pinMode(ECHO, INPUT);
  36. Serial.begin(115200);
  37. Serial.println();
  38. Serial.println();
  39.  
  40. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  41. Serial.print("Connecting to Wi-Fi");
  42. while (WiFi.status() != WL_CONNECTED)
  43. {
  44. Serial.print(".");
  45. delay(300);
  46. }
  47. Serial.println();
  48. Serial.print("Connected with IP: ");
  49. Serial.println(WiFi.localIP());
  50. Serial.println();
  51.  
  52. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  53.  
  54. /* Assign the api key (required) */
  55. config.api_key = API_KEY;
  56.  
  57. /* Assign the user sign in credentials */
  58. auth.user.email = USER_EMAIL;
  59. auth.user.password = USER_PASSWORD;
  60.  
  61. /* Assign the RTDB URL (required) */
  62. config.database_url = DATABASE_URL;
  63.  
  64. /* Assign the callback function for the long running token generation task */
  65. config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  66.  
  67. //Or use legacy authenticate method
  68. //config.database_url = DATABASE_URL;
  69. //config.signer.tokens.legacy_token = "<database secret>";
  70.  
  71. Firebase.begin(&config, &auth);
  72.  
  73. Firebase.reconnectWiFi(true);
  74. #if defined(ESP8266)
  75. fbdo.setBSSLBufferSize(512, 2048);
  76. #endif
  77. }
  78.  
  79. void loop()
  80. {
  81. digitalWrite(TRIGGER, LOW);
  82. delayMicroseconds(2);
  83. digitalWrite(TRIGGER, HIGH);
  84. delayMicroseconds(10);
  85. digitalWrite(TRIGGER, LOW);
  86. duration = pulseIn(ECHO, HIGH);
  87. distance = (duration / 2) / 29.1;
  88. Serial.print(distance);
  89. Serial.println("Centimeter:");
  90. if (distance > maxdistance) {
  91. distance = maxdistance;
  92. }
  93. count = map(distance, maxdistance , 4, 0, 100);
  94.  
  95. if (count > 100) {
  96. count = 100;
  97. }
  98. Serial.print(count);
  99. Serial.println("%");
  100.  
  101. if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0))
  102. {
  103. sendDataPrevMillis = millis();
  104.  
  105. Serial.print("Set int async... ");
  106.  
  107. for (size_t i = 0; i < 10; i++)
  108. {
  109. Firebase.setIntAsync(fbdo, "/WL", count);
  110. }
  111. Serial.println("ok");
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment