├── README.md └── ESP32-Firebase-Demo.ino /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-Firebase-Demo 2 | In this repository, you'll find the codebase that accompanies our YouTube tutorial, "Empower Your IoT: Control Devices Anywhere with Google Firebase & ESP32!" The code is organized into clear and well-documented sections, making it easy for developers of all levels to understand and implement. 3 | -------------------------------------------------------------------------------- /ESP32-Firebase-Demo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 1. Define the WiFi credentials */ 5 | #define WIFI_SSID "Your_SSID" 6 | #define WIFI_PASSWORD "YOUR_PASSWORD" 7 | 8 | /* 2. Define the API Key */ 9 | #define API_KEY "YOUR_API_KEY" 10 | 11 | /* 3. Define the RTDB URL */ 12 | #define DATABASE_URL "YOUR_RTDB_URL" 13 | 14 | /* 4. Define the user Email and password that alreadey registerd or added in your project */ 15 | #define USER_EMAIL "user_email" 16 | #define USER_PASSWORD "user_password" 17 | 18 | // Define Firebase Data object 19 | FirebaseData fbdo; 20 | 21 | FirebaseAuth auth; 22 | FirebaseConfig config; 23 | 24 | unsigned long sendDataPrevMillis = 0; 25 | 26 | const int ledPin = 4; 27 | 28 | void setup() 29 | { 30 | pinMode(ledPin, OUTPUT); 31 | digitalWrite(ledPin, LOW); 32 | 33 | Serial.begin(115200); 34 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 35 | 36 | Serial.print("Connecting to Wi-Fi"); 37 | while (WiFi.status() != WL_CONNECTED) 38 | { 39 | Serial.print("."); 40 | delay(300); 41 | } 42 | Serial.println(); 43 | Serial.print("Connected with IP: "); 44 | Serial.println(WiFi.localIP()); 45 | Serial.println(); 46 | 47 | /* Assign the api key (required) */ 48 | config.api_key = API_KEY; 49 | 50 | /* Assign the user sign in credentials */ 51 | auth.user.email = USER_EMAIL; 52 | auth.user.password = USER_PASSWORD; 53 | 54 | /* Assign the RTDB URL (required) */ 55 | config.database_url = DATABASE_URL; 56 | 57 | // Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager 58 | Firebase.reconnectNetwork(true); 59 | 60 | // Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set. 61 | // Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred. 62 | fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */); 63 | 64 | // Limit the size of response payload to be collected in FirebaseData 65 | fbdo.setResponseSize(2048); 66 | 67 | Firebase.begin(&config, &auth); 68 | 69 | Firebase.setDoubleDigits(5); 70 | 71 | config.timeout.serverResponse = 10 * 1000; 72 | } 73 | 74 | void loop() 75 | { 76 | // Firebase.ready() should be called repeatedly to handle authentication tasks. 77 | if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0)) 78 | { 79 | sendDataPrevMillis = millis(); 80 | 81 | int ledState; 82 | if(Firebase.RTDB.getInt(&fbdo, "/led/state", &ledState)){ 83 | digitalWrite(ledPin, ledState); 84 | }else{ 85 | Serial.println(fbdo.errorReason().c_str()); 86 | } 87 | } 88 | } --------------------------------------------------------------------------------