Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <Firebase_ESP_Client.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_MLX90614.h>
- #include <Wire.h>
- #include <SPI.h>
- ...
- // Define Firebase objects
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- // Variable to save USER UID
- String uid;
- // Variables to save database paths
- String databasePath;
- String ambientTempPath;
- String objectTempPath;
- //String presPath;
- // MLX90614 sensor
- Adafruit_MLX90614 mlx = Adafruit_MLX90614();
- float ambientTemp;
- float objectTemp;
- // Timer variables (send new readings every three minutes)
- unsigned long sendDataPrevMillis = 0;
- unsigned long timerDelay = 10000; //180000
- // Initialize WiFi
- void initWiFi()
- {
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- Serial.println();
- }
- // Write float values to the database
- void sendFloat(String path, float value)
- {
- if (Firebase.RTDB.setFloat(&fbdo, path.c_str(), value))
- {
- Serial.print("Writing value: ");
- Serial.print(value);
- Serial.print(" on the following path: ");
- Serial.println(path);
- Serial.println("PASSED");
- Serial.println("PATH: " + fbdo.dataPath());
- Serial.println("TYPE: " + fbdo.dataType());
- }
- else
- {
- Serial.println("FAILED");
- Serial.println("REASON: " + fbdo.errorReason());
- }
- }
- void setup()
- {
- Serial.begin(115200);
- // Initialize sensor
- mlx.begin();
- initWiFi();
- // Assign the api key (required)
- config.api_key = API_KEY;
- // Assign the user sign in credentials
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- // Assign the RTDB URL (required)
- config.database_url = DATABASE_URL;
- Firebase.reconnectWiFi(true);
- fbdo.setResponseSize(4096);
- // Assign the callback function for the long running token generation task */
- config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
- // Assign the maximum retry of token generation
- config.max_token_generation_retry = 5;
- // Initialize the library with the Firebase authen and config
- Firebase.begin(&config, &auth);
- // Getting the user UID might take a few seconds
- Serial.println("Getting User UID");
- while ((auth.token.uid) == "")
- {
- Serial.print('.');
- delay(1000);
- }
- // Print user UID
- uid = auth.token.uid.c_str();
- Serial.print("User UID: ");
- Serial.println(uid);
- // Update database path
- databasePath = "/UsersData/" + uid;
- // Define database path for sensor readings
- ambientTempPath = databasePath + "/sensor/ambientTemp"; // --> UsersData/<user_uid>/sensor/ambientTemp
- objectTempPath = databasePath + "/sensor/objectTemp"; // --> UsersData/<user_uid>/sensor/objectTemp
- }
- void loop()
- {
- // Send new readings to database
- if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0))
- {
- sendDataPrevMillis = millis();
- // Get latest sensor readings
- objectTemp = mlx.readObjectTempC();
- ambientTemp = mlx.readAmbientTempC();
- // Send readings to database:
- sendFloat(objectTempPath, objectTemp);
- sendFloat(ambientTempPath, ambientTemp);
- }
- }
Advertisement
Advertisement