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

Commit 0453832

Browse files
committed
Merge pull request #14 from proppy/firebase-lib
libraries: add wip Firebase client library
2 parents efb24d7 + 3614a84 commit 0453832

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

libraries/Firebase/Firebase.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Copyright 2015 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
#include "Firebase.h"
17+
18+
const char* firebaseFingerprint = "C1 56 CD D8 49 A3 7D D2 1D 49 60 7E 0D 59 A7 7C C1 0E 58 D2";
19+
const uint16_t firebasePort = 443;
20+
21+
FirebaseRoot Firebase;
22+
23+
FirebaseRef::FirebaseRef(FirebaseRoot& root, const String& path) : _root(root), _path(path) {
24+
}
25+
26+
FirebaseRef& FirebaseRef::root() {
27+
return _root;
28+
}
29+
30+
String FirebaseRef::set(const String& value) {
31+
return _root.buildRequest("PUT", _path, value);
32+
}
33+
34+
String FirebaseRef::push(const String& value) {
35+
return _root.buildRequest("POST", _path, value);
36+
}
37+
38+
FirebaseRef FirebaseRef::child(const String& key) {
39+
return FirebaseRef(_root, _path + "/" + key);
40+
}
41+
42+
FirebaseRoot::FirebaseRoot() : FirebaseRef(*this, "") {
43+
}
44+
45+
void FirebaseRoot::begin(const String& host) {
46+
_host = host;
47+
}
48+
49+
void FirebaseRoot::auth(const String& token) {
50+
_token = token;
51+
}
52+
53+
FirebaseRef FirebaseRoot::child(const String& key) {
54+
return FirebaseRef(*this, key);
55+
}
56+
57+
String FirebaseRoot::buildRequest(const String& method, const String& path, const String& data) {
58+
String req;
59+
req += method + " /" + path + ".json";
60+
if (_token.length() > 0) {
61+
req += "?auth=" + _token;
62+
}
63+
req += " HTTP/1.1\r\n";
64+
req += "Host: " + _host + "\r\n";
65+
req += "User-Agent: Arduino\r\n";
66+
req += "Connection: close\r\n";
67+
if (data.length()) {
68+
req += "Content-Length: ";
69+
req += data.length();
70+
req += "\r\n\r\n";
71+
req += data;
72+
}
73+
return req;
74+
}
75+
76+
const char* FirebaseRoot::host() const {
77+
return _host.c_str();
78+
}
79+
80+
uint16_t FirebaseRoot::port() const {
81+
return firebasePort;
82+
}
83+
84+
const char* FirebaseRoot::fingerprint() const {
85+
return firebaseFingerprint;
86+
}

libraries/Firebase/Firebase.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Copyright 2015 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// Firebase is an helper library for building Firebase request.
18+
// TODO(proppy): add value() method for GET.
19+
// TODO(proppy): add update() method for PATCH.
20+
// TODO(proppy): add remove() method for DELETE.
21+
// TODO(proppy): add helper for decoding response.
22+
#ifndef firebase_h
23+
#define firebase_h
24+
25+
#include "Arduino.h"
26+
27+
class Client;
28+
class FirebaseRoot;
29+
30+
// FirebaseRef is a reference in the firebase hierarchy.
31+
//
32+
// Methods `set()` and `push()` returns the HTTP request as a raw
33+
// `String`, the requests then need be sent using an Arduino Wifi,
34+
// Ethernet or GSM `Client`.
35+
class FirebaseRef {
36+
public:
37+
FirebaseRef(FirebaseRoot& root, const String& path);
38+
FirebaseRef& root();
39+
String set(const String& value);
40+
String push(const String& value);
41+
FirebaseRef child(const String& key);
42+
private:
43+
FirebaseRoot& _root;
44+
String _path;
45+
};
46+
47+
// FirebaseRoot is a root of firebase hierarchy.
48+
//
49+
// A global `Firebase` instance is available for convenience, and need
50+
// to be initialized with the `begin()` and `auth()` methods.
51+
class FirebaseRoot : public FirebaseRef {
52+
friend FirebaseRef;
53+
public:
54+
FirebaseRoot();
55+
void begin(const String& host);
56+
void auth(const String& token);
57+
const char* host() const;
58+
uint16_t port() const;
59+
const char* fingerprint() const;
60+
FirebaseRef child(const String& key);
61+
private:
62+
String buildRequest(const String& method, const String& path, const String& data);
63+
String _host;
64+
String _token;
65+
};
66+
67+
extern FirebaseRoot Firebase;
68+
69+
#endif // firebase_h
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// FirebasePush_ESP8266 is a sample that write a timestamp to a firebase
2+
// everytime it is powered on.
3+
4+
#include <ESP8266WiFi.h>
5+
#include <WiFiClientSecure.h>
6+
7+
#include <Firebase.h>
8+
9+
#define WIFI_SSID "..."
10+
#define WIFI_PASSWORD "..."
11+
#define FIREBASE_HOST "..."
12+
#define FIREBASE_TOKEN "..."
13+
14+
void setup() {
15+
Serial.begin(9600);
16+
17+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
18+
while (WiFi.status() != WL_CONNECTED) {
19+
delay(500);
20+
}
21+
Serial.println(WiFi.localIP());
22+
23+
Firebase.begin(FIREBASE_HOST);
24+
Firebase.auth(FIREBASE_TOKEN);
25+
26+
WiFiClientSecure wifiClient;
27+
if (!wifiClient.connect(Firebase.host(), Firebase.port())) {
28+
Serial.print("connected failed");
29+
}
30+
if (!wifiClient.verify(Firebase.fingerprint(), Firebase.host())) {
31+
Serial.println("certificate verification failed");
32+
return;
33+
}
34+
35+
String req = Firebase.child("logs").push("{\".sv\": \"timestamp\"}");
36+
Serial.println("request:");
37+
Serial.println(req);
38+
wifiClient.print(req);
39+
40+
Serial.println("response:");
41+
while(wifiClient.connected() || wifiClient.available() > 0) {
42+
String data = wifiClient.readStringUntil('\n');
43+
Serial.println(data);
44+
}
45+
}
46+
47+
void loop() {
48+
}

0 commit comments

Comments
 (0)