Guest User

question RNT

a guest
Nov 22nd, 2021
26
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Firebase_ESP_Client.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_MLX90614.h>
  6. #include <Wire.h>
  7. #include <SPI.h>
  8.  
  9. ...
  10.  
  11. // Define Firebase objects
  12. FirebaseData fbdo;
  13. FirebaseAuth auth;
  14. FirebaseConfig config;
  15.  
  16. // Variable to save USER UID
  17. String uid;
  18.  
  19. // Variables to save database paths
  20. String databasePath;
  21. String ambientTempPath;
  22. String objectTempPath;
  23. //String presPath;
  24.  
  25. // MLX90614 sensor
  26. Adafruit_MLX90614 mlx = Adafruit_MLX90614();
  27. float ambientTemp;
  28. float objectTemp;
  29.  
  30. // Timer variables (send new readings every three minutes)
  31. unsigned long sendDataPrevMillis = 0;
  32. unsigned long timerDelay = 10000; //180000
  33.  
  34. // Initialize WiFi
  35. void initWiFi()
  36. {
  37. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  38. Serial.print("Connecting to WiFi ..");
  39. while (WiFi.status() != WL_CONNECTED)
  40. {
  41. Serial.print('.');
  42. delay(1000);
  43. }
  44. Serial.println(WiFi.localIP());
  45. Serial.println();
  46. }
  47.  
  48. // Write float values to the database
  49. void sendFloat(String path, float value)
  50. {
  51. if (Firebase.RTDB.setFloat(&fbdo, path.c_str(), value))
  52. {
  53. Serial.print("Writing value: ");
  54. Serial.print(value);
  55. Serial.print(" on the following path: ");
  56. Serial.println(path);
  57. Serial.println("PASSED");
  58. Serial.println("PATH: " + fbdo.dataPath());
  59. Serial.println("TYPE: " + fbdo.dataType());
  60. }
  61. else
  62. {
  63. Serial.println("FAILED");
  64. Serial.println("REASON: " + fbdo.errorReason());
  65. }
  66. }
  67.  
  68. void setup()
  69. {
  70. Serial.begin(115200);
  71.  
  72. // Initialize sensor
  73. mlx.begin();
  74. initWiFi();
  75.  
  76. // Assign the api key (required)
  77. config.api_key = API_KEY;
  78.  
  79. // Assign the user sign in credentials
  80. auth.user.email = USER_EMAIL;
  81. auth.user.password = USER_PASSWORD;
  82.  
  83. // Assign the RTDB URL (required)
  84. config.database_url = DATABASE_URL;
  85.  
  86. Firebase.reconnectWiFi(true);
  87. fbdo.setResponseSize(4096);
  88.  
  89. // Assign the callback function for the long running token generation task */
  90. config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  91.  
  92. // Assign the maximum retry of token generation
  93. config.max_token_generation_retry = 5;
  94.  
  95. // Initialize the library with the Firebase authen and config
  96. Firebase.begin(&config, &auth);
  97.  
  98. // Getting the user UID might take a few seconds
  99. Serial.println("Getting User UID");
  100. while ((auth.token.uid) == "")
  101. {
  102. Serial.print('.');
  103. delay(1000);
  104. }
  105. // Print user UID
  106. uid = auth.token.uid.c_str();
  107. Serial.print("User UID: ");
  108. Serial.println(uid);
  109.  
  110. // Update database path
  111. databasePath = "/UsersData/" + uid;
  112.  
  113. // Define database path for sensor readings
  114. ambientTempPath = databasePath + "/sensor/ambientTemp"; // --> UsersData/<user_uid>/sensor/ambientTemp
  115. objectTempPath = databasePath + "/sensor/objectTemp"; // --> UsersData/<user_uid>/sensor/objectTemp
  116. }
  117.  
  118. void loop()
  119. {
  120. // Send new readings to database
  121. if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0))
  122. {
  123. sendDataPrevMillis = millis();
  124.  
  125. // Get latest sensor readings
  126. objectTemp = mlx.readObjectTempC();
  127. ambientTemp = mlx.readAmbientTempC();
  128.  
  129. // Send readings to database:
  130. sendFloat(objectTempPath, objectTemp);
  131. sendFloat(ambientTempPath, ambientTemp);
  132.  
  133. }
  134. }
  135.  
RAW Paste Data