Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 2da1c49

Browse files
committed
libraries: add wip Firebase client library
1 parent efb24d7 commit 2da1c49

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

libraries/Firebase/Firebase.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "Firebase.h"
2+
#include "Client.h"
3+
4+
FirebaseRef::FirebaseRef(FirebaseRoot& root, const String& path) : _root(root), _path(path) {
5+
}
6+
7+
FirebaseRef& FirebaseRef::root() {
8+
return _root;
9+
}
10+
11+
String FirebaseRef::value() {
12+
return _root.makeRequest("GET", _path, "");
13+
}
14+
15+
String FirebaseRef::set(const String& key, const String& value) {
16+
return _root.makeRequest("PUSH", _path + "/" + key, value);
17+
}
18+
19+
String FirebaseRef::push(const String& value) {
20+
return _root.makeRequest("POST", _path, value);
21+
}
22+
23+
FirebaseRef FirebaseRef::child(const String& key) {
24+
return FirebaseRef(_root, _path + "/" + key);
25+
}
26+
27+
FirebaseRoot::FirebaseRoot() : FirebaseRef(*this, "") {
28+
}
29+
30+
void FirebaseRoot::begin(Client& client, const String& host) {
31+
_client = &client;
32+
_host = host;
33+
}
34+
35+
void FirebaseRoot::auth(const String& token) {
36+
_token = token;
37+
}
38+
39+
void FirebaseRoot::setError(const String& err) {
40+
_err = err;
41+
}
42+
43+
const String& FirebaseRoot::error() {
44+
return _err;
45+
}
46+
47+
String FirebaseRoot::makeRequest(const String& method, const String& path, const String& data) {
48+
_err = "";
49+
50+
if (!_client->connect(_host.c_str(), 443)) {
51+
setError("failed to connect");
52+
return "";
53+
}
54+
String req;
55+
req += method + " " + path + ".json";
56+
if (_token.length() > 0) {
57+
req += "?auth=" + _token;
58+
}
59+
req += " HTTP/1.1\r\n";
60+
req += "Host: " + _host + "\r\n";
61+
req += "User-Agent: Arduino\r\n";
62+
req += "Connection: close\r\n";
63+
if (data.length()) {
64+
req += "Content-Length: ";
65+
req += data.length();
66+
req += "\r\n\r\n";
67+
req += data;
68+
}
69+
_client->print(req);
70+
String resp;
71+
while(_client->available() > 0) {
72+
resp += _client->readStringUntil('\n');
73+
}
74+
return resp;
75+
}
76+
77+
FirebaseRoot Firebase;

libraries/Firebase/Firebase.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef firebase_h
2+
#define firebase_h
3+
4+
#include "WString.h"
5+
6+
class Client;
7+
class FirebaseRoot;
8+
9+
class FirebaseRef {
10+
public:
11+
FirebaseRef(FirebaseRoot& root, const String& path);
12+
FirebaseRef& root();
13+
String value();
14+
String set(const String& key, const String& value);
15+
String push(const String& value);
16+
FirebaseRef child(const String& key);
17+
private:
18+
FirebaseRoot& _root;
19+
String _path;
20+
};
21+
22+
class FirebaseRoot : public FirebaseRef {
23+
friend FirebaseRef;
24+
public:
25+
FirebaseRoot();
26+
void begin(Client& client, const String& host);
27+
void auth(const String& token);
28+
void setError(const String& err);
29+
const String& error();
30+
private:
31+
String makeRequest(const String& method, const String& path, const String& data);
32+
Client* _client;
33+
String _host;
34+
String _token;
35+
String _err;
36+
};
37+
38+
extern FirebaseRoot Firebase;
39+
40+
#endif // firebase_h
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <ESP8266WiFi.h>
2+
#include <WiFiClientSecure.h>
3+
4+
#include <Firebase.h>
5+
6+
WiFiClientSecure wifiClient;
7+
8+
int pushButtonPin = 1;
9+
int previousState = 1;
10+
11+
void setup() {
12+
pinMode(pushButtonPin, INPUT);
13+
digitalWrite(pushButtonPin, HIGH);
14+
15+
Serial.begin(9600);
16+
17+
WiFi.begin("my-ssid", "my-password");
18+
Firebase.begin(wifiClient, "my-firebase.firebaseio.com");
19+
Firebase.auth("my-firebase-token-or-secrent");
20+
}
21+
22+
void loop() {
23+
int buttonState = digitalRead(pushButtonPin);
24+
if (buttonState == 0 && buttonState != previousState) {
25+
String record = Firebase.child("hits").push("{\".sv\": \"timestamp\"}");
26+
Serial.print(record);
27+
}
28+
previousState = buttonState;
29+
}

0 commit comments

Comments
 (0)