first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,266 @@
/*
MIT License
Copyright (c) 2023 Rupak Poddar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ESP32Firebase.h"
Firebase::Firebase(String referenceURL) {
_host = referenceURL;
if (_host.startsWith("https://")) {
_host.remove(0, 8);
}
if (_host.endsWith("/")) {
_host.remove(_host.length() - 1);
}
_httpsClient.setInsecure();
}
int Firebase::setString(String path, String data) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
String msg = "\"" + data + "\"";
_httpsClient.print(String("PUT ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n" +
"Accept: */*\r\n" +
"User-Agent: Mozilla/4.0 (compatible; esp32 Lua; Windows NT 5.1)\r\n" +
"Content-Type: application/json;charset=utf-8\r\n" +
"Content-Length: " + msg.length() + "\r\n" +
"\r\n" +
msg + "\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
if (line.length() > 0)
return 200; // Success
}
return 400; // Failed
}
int Firebase::setInt(String path, int data) {
String Data = String(data);
return Firebase::setNum(path, Data);
}
int Firebase::setFloat(String path, float data) {
String Data = String(data);
return Firebase::setNum(path, Data);
}
int Firebase::setNum(String path, String msg) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
_httpsClient.print(String("PUT ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n" +
"Accept: */*\r\n" +
"User-Agent: Mozilla/4.0 (compatible; esp32 Lua; Windows NT 5.1)\r\n" +
"Content-Type: application/json;charset=utf-8\r\n" +
"Content-Length: " + msg.length() + "\r\n" +
"\r\n" +
msg + "\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
if (line.length() > 0)
return 200; // Success
}
return 400; // Failed
}
int Firebase::pushString(String path, String data) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
String msg = "\"" + data + "\"";
_httpsClient.print(String("POST ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n" +
"Accept: */*\r\n" +
"User-Agent: Mozilla/4.0 (compatible; esp32 Lua; Windows NT 5.1)\r\n" +
"Content-Type: application/json;charset=utf-8\r\n" +
"Content-Length: " + msg.length() + "\r\n" +
"\r\n" +
msg + "\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
if (line.length() > 0)
return 200; // Success
}
return 400; // Failed
}
int Firebase::pushInt(String path, int data) {
String Data = String(data);
return Firebase::pushNum(path, Data);
}
int Firebase::pushFloat(String path, float data) {
String Data = String(data);
return Firebase::pushNum(path, Data);
}
int Firebase::pushNum(String path, String msg) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
_httpsClient.print(String("POST ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n" +
"Accept: */*\r\n" +
"User-Agent: Mozilla/4.0 (compatible; esp32 Lua; Windows NT 5.1)\r\n" +
"Content-Type: application/json;charset=utf-8\r\n" +
"Content-Length: " + msg.length() + "\r\n" +
"\r\n" +
msg + "\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
if (line.length() > 0)
return 200; // Success
}
return 400; // Failed
}
String Firebase::getString(String path) {
Firebase::getData(path);
return _String;
}
int Firebase::getInt(String path) {
Firebase::getData(path);
return _int;
}
float Firebase::getFloat(String path) {
Firebase::getData(path);
return _float;
}
void Firebase::getData(String path) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
_httpsClient.print(String("GET ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
_int = line.toInt();
_float = line.toFloat();
if (_json == false)
_String = line.substring(1,line.length()-1);
else
_String = line;
}
}
int Firebase::deleteData(String path) {
Connect_to_host();
String jsonObject = String("/") + path + String(".json");
_httpsClient.print(String("DELETE ") + jsonObject + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n\r\n");
while (_httpsClient.connected()) {
String line = _httpsClient.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line;
while(_httpsClient.available()) {
line = _httpsClient.readStringUntil('\n');
if (line.length() > 0)
return 200; // Success
}
return 400; // Failed
}
void Firebase::json(bool json) {
_json = json;
}
void Firebase::Connect_to_host() {
int r=0;
while((!_httpsClient.connect(_host.c_str(), PORT)) && (r < 30)) {
delay(100);
r++;
}
}

View File

@@ -0,0 +1,65 @@
/*
MIT License
Copyright (c) 2023 Rupak Poddar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ESP32Firebase_h
#define ESP32Firebase_h
#include "Arduino.h"
#if defined(ESP32)
#include <WiFiClientSecure.h>
#else
#error "Please select an ESP32 board for this sketch."
#endif
#define PORT 443
class Firebase
{
public:
Firebase(String referenceURL);
int setString(String path, String data);
int setNum(String path, String data);
int setInt(String path, int data);
int setFloat(String path, float data);
int pushString(String path, String data);
int pushNum(String path, String data);
int pushInt(String path, int data);
int pushFloat(String path, float data);
void getData(String path);
String getString(String path);
int getInt(String path);
float getFloat(String path);
int deleteData(String path);
void json(bool json);
void Connect_to_host();
private:
String _host;
bool _json = false;
String _String;
int _int;
float _float;
WiFiClientSecure _httpsClient;
};
#endif

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Rupak Poddar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,29 @@
# ESP32Firebase
## Arduino library to read and write data to Google Firebase.
# Tutorial
### The following steps are one-time process:
Step 1: Open your firebase project, select 'Database' and click 'View' under Realtime Database.
![Step1](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_1.png)
Step 2: Select 'Rules'.
![Step2](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_2.png)
Step 3: Change the '.read' and '.write' rules to 'true' and hit enter.
![Step3.1](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_3.png)
![Step3.2](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_4.png)
Step 4: Select 'Publish'.
![Step4](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_5.png)
Step 5: Go back to the 'Data' tab. Copy the reference url, and paste it in the Arduino code.
![Step5](https://github.com/Rupakpoddar/ESP32Firebase/blob/main/documentation/tutorial_6.png)
That is it. You are all set! You can go through the example codes to get familiar with the library.

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@@ -0,0 +1,79 @@
/*
Make sure your Firebase project's '.read' and '.write' rules are set to 'true'.
Ignoring this will prevent the MCU from communicating with the database.
For more details- https://github.com/Rupakpoddar/ESP32Firebase
*/
#include <ESP32Firebase.h>
#define _SSID "ENTER HERE" // Your WiFi SSID
#define _PASSWORD "ENTER HERE" // Your WiFi Password
#define REFERENCE_URL "ENTER HERE" // Your Firebase project reference url
Firebase firebase(REFERENCE_URL);
void setup() {
Serial.begin(115200);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// digitalWrite(LED_BUILTIN, HIGH);
//================================================================//
//================================================================//
// Examples of setting String, integer and float values.
firebase.setString("Example/setString", "It's Working");
firebase.setInt("Example/setInt", 123);
firebase.setFloat("Example/setFloat", 45.32);
// Examples of pushing String, integer and float values.
firebase.pushString("push", "Hello");
firebase.pushInt("push", 789);
firebase.pushFloat("push", 89.54);
// Example of getting a String.
String data1 = firebase.getString("Example/setString");
Serial.print("Received String:\t");
Serial.println(data1);
// Example of getting an int.
int data2 = firebase.getInt("Example/setInt");
Serial.print("Received Int:\t\t");
Serial.println(data2);
// Example of getting a float.
float data3 = firebase.getFloat("Example/setFloat");
Serial.print("Received Float:\t\t");
Serial.println(data3);
// Example of data deletion.
firebase.deleteData("Example");
}
void loop() {
// Nothing
}

View File

@@ -0,0 +1,86 @@
/*
Make sure your Firebase project's '.read' and '.write' rules are set to 'true'.
Ignoring this will prevent the MCU from communicating with the database.
For more details- https://github.com/Rupakpoddar/ESP32Firebase
*/
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <ESP32Firebase.h>
#define _SSID "ENTER HERE" // Your WiFi SSID
#define _PASSWORD "ENTER HERE" // Your WiFi Password
#define REFERENCE_URL "ENTER HERE" // Your Firebase project reference url
Firebase firebase(REFERENCE_URL);
void setup() {
Serial.begin(115200);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// digitalWrite(LED_BUILTIN, HIGH);
//================================================================//
//================================================================//
// Write some data to the realtime database.
firebase.setString("Example/setString", "It's Working");
firebase.setInt("Example/setInt", 123);
firebase.setFloat("Example/setFloat", 45.32);
firebase.json(true); // Make sure to add this line.
String data = firebase.getString("Example"); // Get data from the database.
// Deserialize the data.
// Consider using Arduino Json Assistant- https://arduinojson.org/v6/assistant/
const size_t capacity = JSON_OBJECT_SIZE(3) + 50;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, data);
// Store the deserialized data.
const char* received_String = doc["setString"]; // "It's Working"
int received_int = doc["setInt"]; // 123
float received_float = doc["setFloat"]; // 45.32
// Print data
Serial.print("Received String:\t");
Serial.println(received_String);
Serial.print("Received Int:\t\t");
Serial.println(received_int);
Serial.print("Received Float:\t\t");
Serial.println(received_float);
// Delete data from the realtime database.
firebase.deleteData("Example");
}
void loop() {
// Nothing
}

View File

@@ -0,0 +1,95 @@
/*
Make sure your Firebase project's '.read' and '.write' rules are set to 'true'.
Ignoring this will prevent the MCU from communicating with the database.
For more details- https://github.com/Rupakpoddar/ESP32Firebase
Download the Android app from- https://play.google.com/store/apps/details?id=com.rupak.firebaseremote
Online remote (Works with any web browser)- https://rupakpoddar.github.io/Firebase-automation-web-interface/
Use Python to control devices- https://github.com/Rupakpoddar/Firebase-with-python
*/
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <ESP32Firebase.h>
#define _SSID "ENTER HERE" // Your WiFi SSID
#define _PASSWORD "ENTER HERE" // Your WiFi Password
#define REFERENCE_URL "ENTER HERE" // Your Firebase project reference url
#define device1 3 // D5
#define device2 4 // D6
#define device3 5 // D7
#define device4 6 // D8
int device_list[4] = {device1, device2, device3, device4};
Firebase firebase(REFERENCE_URL);
void setup() {
Serial.begin(115200);
pinMode(device1, OUTPUT);
pinMode(device2, OUTPUT);
pinMode(device3, OUTPUT);
pinMode(device4, OUTPUT);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// digitalWrite(LED_BUILTIN, HIGH);
firebase.json(true); // Make sure to add this line.
}
void loop() {
String data = firebase.getString("cmd"); // Get data from database.
// Deserialize the data.
// Consider using the Arduino Json assistant- https://arduinojson.org/v6/assistant/
const size_t capacity = JSON_OBJECT_SIZE(5) + 60;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, data);
String Device1 = doc["Device1"];
String Device2 = doc["Device2"];
String Device3 = doc["Device3"];
String Device4 = doc["Device4"];
// Print data
Serial.println("Device 1: "+Device1);
Serial.println("Device 2: "+Device2);
Serial.println("Device 3: "+Device3);
Serial.println("Device 4: "+Device4);
Serial.println("");
String status_list[4] = {Device1, Device2, Device3, Device4};
for (int i=0;i<4;i++) {
if (status_list[i] == "ON"){
digitalWrite(device_list[i], HIGH);
}
else{
digitalWrite(device_list[i], LOW);
}
}
}

View File

@@ -0,0 +1,107 @@
/*
Make sure your Firebase project's '.read' and '.write' rules are set to 'true'.
Ignoring this will prevent the MCU from communicating with the database.
For more details- https://github.com/Rupakpoddar/ESP32Firebase
Download the Android app from- https://play.google.com/store/apps/details?id=com.rupak.firebaseremote
*/
#include <ESP32Firebase.h>
#define _SSID "ENTER HERE" // Your WiFi SSID
#define _PASSWORD "ENTER HERE" // Your WiFi Password
#define REFERENCE_URL "ENTER HERE" // Your Firebase project reference url
#define M1A 3 // D5: Output 1 for motor driver
#define M1B 4 // D6: Output 2 for motor driver
#define M2A 5 // D7: Output 3 for motor driver
#define M2B 6 // D8: Output 4 for motor driver
#define TURN_DELAY 100
#define FORWARD_BACKWARD_DELAY 500
Firebase firebase(REFERENCE_URL);
void setup() {
Serial.begin(115200);
pinMode(M1A, OUTPUT);
pinMode(M1B, OUTPUT);
pinMode(M2A, OUTPUT);
pinMode(M2B, OUTPUT);
digitalWrite(M1A, LOW);
digitalWrite(M1B, LOW);
digitalWrite(M2A, LOW);
digitalWrite(M2B, LOW);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
int command = firebase.getInt("cmd/Robot"); // Get data from database.
if(command == 0){ // STOP
digitalWrite(M1A, LOW);
digitalWrite(M1B, LOW);
digitalWrite(M2A, LOW);
digitalWrite(M2B, LOW);
}
if(command == 1){ // FORWARD
digitalWrite(M1A, HIGH);
digitalWrite(M2A, HIGH);
delay(FORWARD_BACKWARD_DELAY);
digitalWrite(M1A, LOW);
digitalWrite(M2A, LOW);
}
if(command == 2){ // BACKWARD
digitalWrite(M1B, HIGH);
digitalWrite(M2B, HIGH);
delay(FORWARD_BACKWARD_DELAY);
digitalWrite(M1B, LOW);
digitalWrite(M2B, LOW);
}
if(command == 3){ // LEFT
digitalWrite(M1B, HIGH);
digitalWrite(M2A, HIGH);
delay(TURN_DELAY);
digitalWrite(M1B, LOW);
digitalWrite(M2A, LOW);
}
if(command == 4){ // RIGHT
digitalWrite(M1A, HIGH);
digitalWrite(M2B, HIGH);
delay(TURN_DELAY);
digitalWrite(M1A, LOW);
digitalWrite(M2B, LOW);
}
}

View File

@@ -0,0 +1,16 @@
Firebase KEYWORD1
setString KEYWORD2
setInt KEYWORD2
setFloat KEYWORD2
pushString KEYWORD2
pushInt KEYWORD2
pushFloat KEYWORD2
getString KEYWORD2
getInt KEYWORD2
getFloat KEYWORD2
deleteData KEYWORD2
json KEYWORD2

View File

@@ -0,0 +1,9 @@
name=ESP32 Firebase
version=1.0.0
author=Rupak Poddar
maintainer=Rupak Poddar <poddarrupak2808@gmail.com>
sentence=Library for ESP32 to read and write data to Firebase Realtime Database.
paragraph=A reliable low latency library to read, write, update and delete data from Firebase Realtime Database.
category=Communication
url=https://github.com/Rupakpoddar/ESP32Firebase
architectures=esp32