#include #include #include #include #include "time.h" // Provide the token generation process info. #include "addons/TokenHelper.h" // Provide the RTDB payload printing info and other helper functions. #include "addons/RTDBHelper.h" // Insert your network credentials #define WIFI_SSID "MAKERSPACE" #define WIFI_PASSWORD "12345678" // Insert Firebase project API Key #define API_KEY "AIzaSyBGuoAUVZFy2OD0HiAvGkKj6ORqWfMpqZM" // Insert Authorized Email and Corresponding Password #define USER_EMAIL "Dsolomon@college.harvard.edu" #define USER_PASSWORD "Sololebow1" // Insert RTDB URLefine the RTDB URL #define DATABASE_URL "https://esp32-senordisplay-default-rtdb.firebaseio.com/" // Settings #define SAMPLING_FREQ_HZ 4 // Sampling frequency (Hz) #define SAMPLING_PERIOD_MS 1000 / SAMPLING_FREQ_HZ // Sampling period (ms) #define NUM_SAMPLES 8 // 8 samples at 4 Hz is 2 seconds // Define Firebase objects FirebaseData fbdo; FirebaseAuth auth; FirebaseConfig config; // Variable to save USER UID String uid; // Database main path (to be updated in setup with the user UID) String databasePath; // Database child nodes String sen0Path = "/Sensor0"; String sen1Path = "/Sensor1"; String sen2Path = "/Sensor2"; String sen3Path = "/Sensor3"; String sen4Path = "/Sensor4"; String sen5Path = "/Sensor5"; String timePath = "/timestamp"; // Parent Node (to be updated in every loop) String parentPath; String chartrangePath; int timestamp; FirebaseJson json; const char* ntpServer = "pool.ntp.org"; int sensor0 = 25; int sensor1 = 26; int sensor2 = 13; int sensor3 = 15; int sensor4 = 18; int sensor5 = 19; unsigned int sensorValue0; unsigned int sensorValue1; unsigned int sensorValue2; unsigned int sensorValue3; unsigned int sensorValue4; unsigned int sensorValue5; // Timer variables (send new readings every three minutes) unsigned long sendDataPrevMillis = 0; unsigned long timerDelay = 180000/12; // 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(); } // Function that gets current epoch time unsigned long getTime() { time_t now; struct tm timeinfo; if (!getLocalTime(&timeinfo)) { //Serial.println("Failed to obtain time"); return(0); } time(&now); return now; } void setup(){ Serial.begin(115200); // Initialize BME280 sensor initWiFi(); configTime(0, 0, ntpServer); // 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 + "/readings"; chartrangePath = "/UsersData/" + uid + "/charts/ranges"; } void loop(){ if (Firebase.ready()){ for (int i = 0; i < NUM_SAMPLES; i++) { sendDataPrevMillis = millis(); sensorValue0 = analogRead(sensor0); sensorValue1 = analogRead(sensor1); sensorValue2 = analogRead(sensor2); sensorValue3 = analogRead(sensor3); sensorValue4 = analogRead(sensor4); sensorValue5 = analogRead(sensor5); //Get current timestamp timestamp = getTime(); // Print CSV data with timestamp Serial.print(timestamp); Serial.print(","); Serial.print(sensorValue0); Serial.print(","); Serial.print(sensorValue1); Serial.print(","); Serial.print(sensorValue2); Serial.print(","); Serial.print(sensorValue3); Serial.print(","); Serial.print(sensorValue4); Serial.print(","); Serial.print(sensorValue5); Serial.println(); parentPath= databasePath + "/" + String(timestamp); json.set(sen0Path.c_str(), String(sensorValue0)); json.set(sen1Path.c_str(), String(sensorValue1)); json.set(sen2Path.c_str(), String(sensorValue2)); json.set(sen3Path.c_str(), String(sensorValue3)); json.set(sen4Path.c_str(), String(sensorValue4)); json.set(sen5Path.c_str(), String(sensorValue5)); json.set(timePath, String(timestamp)); Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json); // Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str()); Firebase.RTDB.setInt(&fbdo, chartrangePath, 25); // Wait just long enough for our sampling period while (millis() < sendDataPrevMillis + SAMPLING_PERIOD_MS); } // Print empty line to transmit termination of recording Serial.println(); } }