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

Commit d72b3ff

Browse files
committed
libraries/firebase: simpler api
- method return raw http requests instead of sending them - removed value() method: need helper for reading response.
1 parent 41dab5c commit d72b3ff

File tree

3 files changed

+79
-74
lines changed

3 files changed

+79
-74
lines changed

libraries/Firebase/Firebase.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515
//
16-
1716
#include "Firebase.h"
18-
#include "Client.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;
1922

2023
FirebaseRef::FirebaseRef(FirebaseRoot& root, const String& path) : _root(root), _path(path) {
2124
}
@@ -24,16 +27,12 @@ FirebaseRef& FirebaseRef::root() {
2427
return _root;
2528
}
2629

27-
String FirebaseRef::value() {
28-
return _root.makeRequest("GET", _path, "");
29-
}
30-
31-
String FirebaseRef::set(const String& key, const String& value) {
32-
return _root.makeRequest("PUSH", _path + "/" + key, value);
30+
String FirebaseRef::set(const String& value) {
31+
return _root.buildRequest("PUT", _path, value);
3332
}
3433

3534
String FirebaseRef::push(const String& value) {
36-
return _root.makeRequest("POST", _path, value);
35+
return _root.buildRequest("POST", _path, value);
3736
}
3837

3938
FirebaseRef FirebaseRef::child(const String& key) {
@@ -43,32 +42,21 @@ FirebaseRef FirebaseRef::child(const String& key) {
4342
FirebaseRoot::FirebaseRoot() : FirebaseRef(*this, "") {
4443
}
4544

46-
void FirebaseRoot::begin(Client& client, const String& host) {
47-
_client = &client;
45+
void FirebaseRoot::begin(const String& host) {
4846
_host = host;
4947
}
5048

5149
void FirebaseRoot::auth(const String& token) {
5250
_token = token;
5351
}
5452

55-
void FirebaseRoot::setError(const String& err) {
56-
_err = err;
57-
}
58-
59-
const String& FirebaseRoot::error() {
60-
return _err;
53+
FirebaseRef FirebaseRoot::child(const String& key) {
54+
return FirebaseRef(*this, key);
6155
}
6256

63-
String FirebaseRoot::makeRequest(const String& method, const String& path, const String& data) {
64-
_err = "";
65-
66-
if (!_client->connect(_host.c_str(), 443)) {
67-
setError("failed to connect");
68-
return "";
69-
}
57+
String FirebaseRoot::buildRequest(const String& method, const String& path, const String& data) {
7058
String req;
71-
req += method + " " + path + ".json";
59+
req += method + " /" + path + ".json";
7260
if (_token.length() > 0) {
7361
req += "?auth=" + _token;
7462
}
@@ -82,12 +70,17 @@ String FirebaseRoot::makeRequest(const String& method, const String& path, const
8270
req += "\r\n\r\n";
8371
req += data;
8472
}
85-
_client->print(req);
86-
String resp;
87-
while(_client->available() > 0) {
88-
resp += _client->readStringUntil('\n');
89-
}
90-
return resp;
73+
return req;
9174
}
9275

93-
FirebaseRoot Firebase;
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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,50 @@
1414
// limitations under the License.
1515
//
1616

17+
// Firebase is an helper library for build Firebase request.
1718
#ifndef firebase_h
1819
#define firebase_h
1920

20-
#include "WString.h"
21+
#include "Arduino.h"
2122

2223
class Client;
2324
class FirebaseRoot;
2425

26+
// FirebaseRef is a reference in the firebase hierarchy.
27+
//
28+
// Methods `set()` and `push()` returns the HTTP request as a raw
29+
// `String`, the requests then need be sent using an Arduino Wifi,
30+
// Ethernet or GSM `Client`.
2531
class FirebaseRef {
2632
public:
2733
FirebaseRef(FirebaseRoot& root, const String& path);
2834
FirebaseRef& root();
29-
String value();
30-
String set(const String& key, const String& value);
35+
String set(const String& value);
3136
String push(const String& value);
3237
FirebaseRef child(const String& key);
3338
private:
3439
FirebaseRoot& _root;
3540
String _path;
3641
};
3742

43+
// FirebaseRoot is a root of firebase hierarchy.
44+
//
45+
// A global `Firebase` instance is available for convenience, and need
46+
// to be initialized with the `begin()` and `auth()` methods.
3847
class FirebaseRoot : public FirebaseRef {
3948
friend FirebaseRef;
4049
public:
4150
FirebaseRoot();
42-
void begin(Client& client, const String& host);
51+
void begin(const String& host);
4352
void auth(const String& token);
44-
void setError(const String& err);
45-
const String& error();
53+
const char* host() const;
54+
uint16_t port() const;
55+
const char* fingerprint() const;
56+
FirebaseRef child(const String& key);
4657
private:
47-
String makeRequest(const String& method, const String& path, const String& data);
48-
Client* _client;
58+
String buildRequest(const String& method, const String& path, const String& data);
4959
String _host;
5060
String _token;
51-
String _err;
5261
};
5362

5463
extern FirebaseRoot Firebase;
Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
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-
//
1+
// FirebasePush_ESP8266 is a sample that write a timestamp to a firebase
2+
// everytime it is powered on.
163

174
#include <ESP8266WiFi.h>
185
#include <WiFiClientSecure.h>
196

207
#include <Firebase.h>
218

22-
WiFiClientSecure wifiClient;
23-
24-
int pushButtonPin = 1;
25-
int previousState = 1;
9+
#define WIFI_SSID "..."
10+
#define WIFI_PASSWORD "..."
11+
#define FIREBASE_HOST "..."
12+
#define FIREBASE_TOKEN "..."
2613

2714
void setup() {
28-
pinMode(pushButtonPin, INPUT);
29-
digitalWrite(pushButtonPin, HIGH);
30-
31-
Serial.begin(9600);
32-
33-
WiFi.begin("my-ssid", "my-password");
34-
Firebase.begin(wifiClient, "my-firebase.firebaseio.com");
35-
Firebase.auth("my-firebase-token-or-secrent");
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+
}
3645
}
3746

3847
void loop() {
39-
int buttonState = digitalRead(pushButtonPin);
40-
if (buttonState == 0 && buttonState != previousState) {
41-
String record = Firebase.child("hits").push("{\".sv\": \"timestamp\"}");
42-
Serial.print(record);
43-
}
44-
previousState = buttonState;
4548
}

0 commit comments

Comments
 (0)