Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rupakpoddar committed Nov 9, 2023
1 parent ea83325 commit a025b71
Show file tree
Hide file tree
Showing 16 changed files with 777 additions and 0 deletions.
265 changes: 265 additions & 0 deletions ESP8266Firebase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
/*
MIT License
Copyright (c) 2020 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 "ESP8266Firebase.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; esp8266 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; esp8266 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; esp8266 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; esp8266 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, PORT)) && (r < 30)) {
delay(100);
r++;
}
}
65 changes: 65 additions & 0 deletions ESP8266Firebase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
MIT License
Copyright (c) 2020 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 ESP8266Firebase_h
#define ESP8266Firebase_h
#include "Arduino.h"

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#error "Please select an ESP8266 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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 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.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ESP8266Firebase
## Arduino library to read and write data to Google Firebase.
## [UPDATE Nov 2023]: Now faster than ever before! Almost instantaneous update and response speed.

# 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/ESP8266Firebase/blob/master/documentation/tutorial_1.png)

Step 2: Select 'Rules'.

![Step2](https://github.com/Rupakpoddar/ESP8266Firebase/blob/master/documentation/tutorial_2.png)

Step 3: Change the '.read' and '.write' rules to 'true' and hit enter.

![Step3.1](https://github.com/Rupakpoddar/ESP8266Firebase/blob/master/documentation/tutorial_3.png)

![Step3.2](https://github.com/Rupakpoddar/ESP8266Firebase/blob/master/documentation/tutorial_4.png)

Step 4: Select 'Publish'.

![Step4](https://github.com/Rupakpoddar/ESP8266Firebase/blob/master/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/ESP8266Firebase/blob/master/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 added documentation/tutorial_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/tutorial_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/tutorial_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/tutorial_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/tutorial_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/tutorial_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a025b71

Please sign in to comment.