This script connects an ESP32 to Wi-Fi and interfaces with a Firebase Realtime Database. It demonstrates two main functionalities: pushing data to Firebase and retrieving data from Firebase.
Function Explanations: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. connect_to_wifi()
Purpose: Establishes a Wi-Fi connection using provided SSID and password.
How it works: 1.Activates the ESP32's network interface. 2.Attempts to connect to the specified Wi-Fi network. 3.Prints the IP configuration upon successful connection. Key Use: Prepares the ESP32 to communicate over the internet.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. push_data(path, data)
Purpose: Sends data to Firebase at a specified path.
How it works: 1.Constructs the Firebase URL using the database URL, path, and authentication key. 2.Sends a POST request with JSON-formatted data and the required headers. 3.Prints the server response or an error if the operation fails. Key Use: Logs sensor readings or any data into Firebase.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. get_data(path)
Purpose: Fetches data from a specified path in the Firebase database.
How it works: 1.Constructs the Firebase URL with the given path and API key. 2.Sends a GET request to retrieve the data. 3.Parses the response JSON and returns the data. 4.Handles HTTP errors or network exceptions. Key Use: Reads stored data, such as configuration values or logs, from Firebase.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Program Execution, Main Program Workflow:
1.Wi-Fi Connection: Calls connect_to_wifi() to ensure the ESP32 is online.
2.Push Data Example: Pushes a dictionary ({"temperature": 25, "humidity": 60}) to the Firebase path "test_path".
3.Retrieve Data Example: Fetches data from the same path ("test_path") to verify the push operation. Prints the retrieved data or error messages if something goes wrong.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Resources and websites: