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

Commit 1f72ca0

Browse files
committed
Merge pull request #22 from proppy/httpclient
switch to ESP8266HTTPClient.
2 parents 1a66ef1 + 27e9746 commit 1f72ca0

File tree

4 files changed

+115
-94
lines changed

4 files changed

+115
-94
lines changed

Firebase.cpp

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//
1616
#include "Firebase.h"
1717

18-
const char* firebaseFingerprint = "C1 56 CD D8 49 A3 7D D2 1D 49 60 7E 0D 59 A7 7C C1 0E 58 D2";
18+
const char* firebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
1919
const uint16_t firebasePort = 443;
2020

2121
FirebaseRoot Firebase;
@@ -27,60 +27,50 @@ FirebaseRef& FirebaseRef::root() {
2727
return _root;
2828
}
2929

30-
String FirebaseRef::set(const String& value) {
31-
return _root.buildRequest("PUT", _path, value);
30+
String FirebaseRef::val() {
31+
return _root.sendRequest("GET", _path);
3232
}
3333

3434
String FirebaseRef::push(const String& value) {
35-
return _root.buildRequest("POST", _path, value);
35+
return _root.sendRequest("POST", _path, (uint8_t*)value.c_str(), value.length());
3636
}
3737

3838
FirebaseRef FirebaseRef::child(const String& key) {
3939
return FirebaseRef(_root, _path + "/" + key);
4040
}
4141

4242
FirebaseRoot::FirebaseRoot() : FirebaseRef(*this, "") {
43+
_http.setReuse(true);
4344
}
4445

45-
void FirebaseRoot::begin(const String& host) {
46+
FirebaseRoot& FirebaseRoot::begin(const String& host) {
4647
_host = host;
48+
return *this;
4749
}
4850

49-
void FirebaseRoot::auth(const String& token) {
50-
_token = token;
51+
FirebaseRoot& FirebaseRoot::auth(const String& auth) {
52+
_auth = auth;
53+
return *this;
5154
}
5255

5356
FirebaseRef FirebaseRoot::child(const String& key) {
5457
return FirebaseRef(*this, key);
5558
}
5659

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;
60+
String FirebaseRoot::sendRequest(const char* method, const String& path, uint8_t* value, size_t size) {
61+
_error.reset();
62+
String url = "/" + path + ".json";
63+
if (_auth.length() > 0) {
64+
url += "?auth=" + _auth;
6265
}
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;
66+
_http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
67+
int statusCode = _http.sendRequest(method, value, size);
68+
if (statusCode < 0) {
69+
_error.set(statusCode,
70+
String(method) + " " + url + ": "
71+
+ HTTPClient::errorToString(statusCode));
72+
return "";
7273
}
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;
74+
return _http.getString();
75+
// NOTE: no end() because reuse.
8676
}

Firebase.h

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,76 @@
1414
// limitations under the License.
1515
//
1616

17-
// Firebase is an helper library for building Firebase request.
18-
// TODO(proppy): add value() method for GET.
17+
// firebase-arduino is an Arduino client for Firebase.
18+
// It is currently limited to the ESP8266 board family.
19+
20+
// TODO(proppy): add set() method for PUT.
1921
// TODO(proppy): add update() method for PATCH.
2022
// TODO(proppy): add remove() method for DELETE.
21-
// TODO(proppy): add helper for decoding response.
23+
// TODO(proppy): add connect() + non blocking
2224
#ifndef firebase_h
2325
#define firebase_h
2426

25-
#include "Arduino.h"
27+
#include <Arduino.h>
28+
#include <ESP8266WiFi.h>
29+
#include <WiFiClientSecure.h>
30+
#include <ESP8266HTTPClient.h>
2631

27-
class Client;
2832
class FirebaseRoot;
2933

30-
// FirebaseRef is a reference in the firebase hierarchy.
34+
// FirebaseRef is a node in the firebase hierarchy.
3135
//
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`.
36+
// Methods val() and push() performs respectivly HTTP GET and POST
37+
// requests on the corresponding child reference and return the
38+
// response body.
3539
class FirebaseRef {
3640
public:
3741
FirebaseRef(FirebaseRoot& root, const String& path);
3842
FirebaseRef& root();
39-
String set(const String& value);
43+
String val();
4044
String push(const String& value);
4145
FirebaseRef child(const String& key);
4246
private:
4347
FirebaseRoot& _root;
4448
String _path;
4549
};
4650

47-
// FirebaseRoot is a root of firebase hierarchy.
51+
52+
// FirebaseError is a string representation of a firebase HTTP error.
53+
class FirebaseError {
54+
public:
55+
operator bool() const { return _code < 0; }
56+
int code() const { return _code; }
57+
const String& message() const { return _message; }
58+
void reset() { set(0, ""); }
59+
void set(int code, const String& message) {
60+
_code = code;
61+
_message = message;
62+
}
63+
private:
64+
int _code = 0;
65+
String _message = "";
66+
};
67+
68+
69+
// FirebaseRoot is the root node of firebase hierarchy.
4870
//
4971
// A global `Firebase` instance is available for convenience, and need
5072
// to be initialized with the `begin()` and `auth()` methods.
5173
class FirebaseRoot : public FirebaseRef {
5274
friend FirebaseRef;
5375
public:
5476
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);
77+
FirebaseRoot& begin(const String& host);
78+
FirebaseRoot& auth(const String& auth);
79+
FirebaseRef child(const String& key);
80+
const FirebaseError& error() { return _error; }
6181
private:
62-
String buildRequest(const String& method, const String& path, const String& data);
82+
String sendRequest(const char* method, const String& path, uint8_t* value = NULL, size_t size = 0);
83+
HTTPClient _http;
6384
String _host;
64-
String _token;
85+
String _auth;
86+
FirebaseError _error;
6587
};
6688

6789
extern FirebaseRoot Firebase;

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# firebase-arduino
22

3-
This sample show how to call firebase from an arduino sketch.
4-
5-
*This is not an official Google product*.
3+
This sample shows how to call [Firebase](https://www.firebase.com/) from the [ESP8266 Arduino core](https://github.com/esp8266/Arduino).
64

75
## Requirements
86

9-
- 1 Arduino compatible esp8266 board.
10-
- Arduino 1.6.x
7+
- 1 [ESP8266 Arduino board](https://www.adafruit.com/products/2821).
8+
- [Arduino 1.6.x](https://www.arduino.cc/en/Main/Software)
9+
- ESP8266 Arduino board definition [(master branch)](https://github.com/esp8266/Arduino#using-git-version-)
1110

1211
## Setup
1312

1413
- Clone the repo in your Arduino libraries directory.
1514
- open the `FirebasePush_ESP8266` example.
16-
- Update `WIFI_SSID` `WIFI_PASSWORD` `FIREBASE_HOST` `FIREBASE_TOKEN` constant.
15+
- Replace `SSID` `PASSWORD` `example.firebaseio.com` `secret_or_token` `node` placeholders.
1716

1817
## Run
19-
- Open firebase dashboard on `/logs.json`.
18+
19+
- Open firebase dashboard.
2020
- Power up the arduino board.
2121
- Notice the new entry.
22+
23+
*This is not an official Google product*.
Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
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>
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+
// FirebasePush_ESP8266 is a sample that push a new timestamp to firebase
18+
// on each reset.
619

720
#include <Firebase.h>
821

9-
#define WIFI_SSID "..."
10-
#define WIFI_PASSWORD "..."
11-
#define FIREBASE_HOST "..."
12-
#define FIREBASE_TOKEN "..."
13-
1422
void setup() {
1523
Serial.begin(9600);
16-
17-
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
24+
25+
// connect to wifi.
26+
WiFi.begin("SSID", "PASSWORD");
27+
Serial.print("connecting");
1828
while (WiFi.status() != WL_CONNECTED) {
29+
Serial.print(".");
1930
delay(500);
2031
}
32+
Serial.println();
33+
Serial.print("connected: ");
2134
Serial.println(WiFi.localIP());
2235

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");
36+
// get firebase child reference.
37+
FirebaseRef logsRef = Firebase.begin("example.firebaseio.com")
38+
.auth("secret_or_token")
39+
.child("node");
40+
41+
// add a new entry.
42+
String logEntry = logsRef.push("{\".sv\": \"timestamp\"}");
43+
// handle error.
44+
if (Firebase.error()) {
45+
Serial.println("Firebase request failed");
46+
Serial.println(Firebase.error().message());
47+
return;
2948
}
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-
}
49+
Serial.println(logEntry);
50+
// print all entries.
51+
Serial.println(logsRef.val());
4552
}
4653

4754
void loop() {
48-
}
55+
}

0 commit comments

Comments
 (0)