Spaces:
Build error
Build error
File size: 5,429 Bytes
6308dc1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
/**
* ESP32 Firebase Sensor Monitoring System
*
* This script collects data from DHT sensor (temperature and humidity)
* and a photoresistor (light intensity), then uploads it to Firebase
* Realtime Database.
*
* Libraries required:
* - WiFiManager by tzapu (https://github.com/tzapu/WiFiManager)
* - Firebase-ESP32 by mobizt (https://github.com/mobizt/Firebase-ESP32)
* - DHT sensor library by Adafruit
*/
#include <WiFiManager.h> // WiFiManager library for easy network configuration
#include <FirebaseESP32.h> // Firebase ESP32 client library
#include <DHT.h> // DHT sensor library
#include <addons/TokenHelper.h> // Firebase token generation helper
#include <addons/RTDBHelper.h> // Firebase RTDB helper
// Firebase credentials - Get these from Firebase Console
#define API_KEY "AIzaSyAJpYEXlwTjMOIAueqqDiti49PKRnsXAns" // Project settings -> General -> Your apps -> Web API Key
#define DATABASE_URL "https://projectstrata-57a90-default-rtdb.asia-southeast1.firebasedatabase.app" // e.g. "https://your-project-id-default-rtdb.firebasedatabase.app/"
// User Email and Password for Firebase Authentication
// Create this user in Firebase Console -> Authentication -> Users
#define USER_EMAIL "device@projectstrata.com"
#define USER_PASSWORD "projectstrata"
// Sensor and button pin definitions
#define DHT_PIN 15 // Digital pin connected to the DHT sensor
#define LDR_PIN 34 // Analog pin connected to the LDR (photoresistor)
#define RESET_BTN_PIN 23 // Digital pin for WiFi reset button
#define DHT_TYPE DHT11 // DHT sensor type (DHT11, DHT22, etc.)
// Data upload interval (milliseconds)
#define UPLOAD_INTERVAL 5000
#define BUTTON_HOLD_TIME 5000
// Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
// Global variables
unsigned long lastUploadTime = 0;
unsigned long buttonPressTime = 0;
String deviceId;
bool signupOK = false;
bool resetButtonPressed = false;
bool checkResetButton() {
if (digitalRead(RESET_BTN_PIN) == LOW) {
if (!resetButtonPressed) {
resetButtonPressed = true;
buttonPressTime = millis();
// Serial.println("Hold to reset WiFi");
}
if (resetButtonPressed && (millis() - buttonPressTime >= BUTTON_HOLD_TIME)) {
return true;
}
} else {
if (resetButtonPressed) {
resetButtonPressed = false;
}
}
return false;
}
void setupWiFi() {
WiFiManager wifiManager;
if (checkResetButton()) {
// Serial.println("Resetting WiFi settings");
wifiManager.resetSettings();
}
String apName = "ProjectSTRATA - WiFiSetter";
wifiManager.setConfigPortalTimeout(180);
if (!wifiManager.autoConnect(apName.c_str())) {
Serial.println("Failed to connect");
ESP.restart();
}
// Serial.print("Connected: ");
Serial.println(WiFi.localIP());
deviceId = String((uint32_t)ESP.getEfuseMac(), HEX);
}
void setupFirebase() {
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
if (Firebase.signUp(&config, &auth, USER_EMAIL, USER_PASSWORD)) {
// Serial.println("Firebase sign up OK");
signupOK = true;
} else {
Serial.printf("Sign up failed: %s\n", config.signer.signupError.message.c_str());
}
Firebase.reconnectWiFi(true);
}
bool readSensorData(float &temperature, float &humidity, int &light) {
// Read temperature
temperature = dht.readTemperature();
if (isnan(temperature)) {
return false;
}
// Read humidity
humidity = dht.readHumidity();
if (isnan(humidity)) {
return false;
}
// Read light
light = analogRead(LDR_PIN);
Serial.printf("Temp: %.1f°C, Humidity: %.1f%%, Light: %d\n",
temperature, humidity, light);
return true;
}
void uploadToFirebase() {
if (!signupOK || !Firebase.ready()) {
// Serial.println("Firebase not ready");
return;
}
float temperature, humidity;
int lightIntensity;
if (!readSensorData(temperature, humidity, lightIntensity)) {
return;
}
unsigned long timestamp = millis();
// Create JSON with minimal memory footprint
FirebaseJson json;
json.add("id", deviceId);
json.add("ts", timestamp);
json.add("temp", temperature);
json.add("hum", humidity);
json.add("light", lightIntensity);
// Path to save data
String path = "/sensor_data/" + deviceId + "/" + String(timestamp);
if (Firebase.setJSON(fbdo, path, json)) {
Serial.println("Data uploaded OK");
} else {
Serial.println("Upload failed: " + fbdo.errorReason());
}
// Update latest data with minimal path length
if (Firebase.setJSON(fbdo, "/latest/" + deviceId, json)) {
Serial.println("Latest data updated");
}
}
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LDR_PIN, INPUT);
pinMode(RESET_BTN_PIN, INPUT_PULLUP);
setupWiFi();
setupFirebase();
}
void loop() {
// Check for WiFi reset
if (checkResetButton()) {
// Serial.println("WiFi reset requested");
WiFiManager wifiManager;
wifiManager.resetSettings();
ESP.restart();
}
// Upload data at interval
if (millis() - lastUploadTime >= UPLOAD_INTERVAL) {
uploadToFirebase();
lastUploadTime = millis();
}
delay(100);
} |