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

Commit 2aacf15

Browse files
committed
add FirebaseSerial w/ streaming
1 parent 96bf030 commit 2aacf15

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed

Firebase.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,24 @@ String makeFirebaseURL(const String& path, const String& auth) {
4040

4141
} // namespace
4242

43-
Firebase::Firebase(const String& host) : host_(host) {
43+
Firebase::Firebase() {
44+
}
45+
46+
Firebase::Firebase(const String& host, const String& auth) {
47+
begin(host, auth);
48+
}
49+
50+
void Firebase::begin(const String& host, const String& auth) {
51+
host_ = host;
52+
auth_ = auth;
4453
http_.setReuse(true);
4554
}
4655

56+
Firebase& Firebase::host(const String& host) {
57+
host_ = host;
58+
return *this;
59+
}
60+
4761
Firebase& Firebase::auth(const String& auth) {
4862
auth_ = auth;
4963
return *this;
@@ -73,7 +87,14 @@ FirebaseStream Firebase::stream(const String& path) {
7387
// FirebaseCall
7488
FirebaseCall::FirebaseCall(const String& host, const String& auth,
7589
const char* method, const String& path,
76-
const String& data, HTTPClient* http) : http_(http) {
90+
const String& data, HTTPClient* http) {
91+
begin(host, auth, method, path, data, http);
92+
}
93+
94+
void FirebaseCall::begin(const String& host, const String& auth,
95+
const char* method, const String& path,
96+
const String& data, HTTPClient* http) {
97+
http_ = http;
7798
String url = makeFirebaseURL(path, auth);
7899
http_->setReuse(true);
79100
http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint);
@@ -164,6 +185,12 @@ FirebaseStream::FirebaseStream(const String& host, const String& auth,
164185
: FirebaseCall(host, auth, "STREAM", path, "", http) {
165186
}
166187

188+
void FirebaseStream::begin(const String& host, const String& auth,
189+
const String& path,
190+
HTTPClient* http) {
191+
FirebaseCall::begin(host, auth, "STREAM", path, "", http);
192+
}
193+
167194
bool FirebaseStream::available() {
168195
return http_->getStreamPtr()->available();
169196
}
@@ -183,3 +210,17 @@ FirebaseStream::Event FirebaseStream::read(String& event) {
183210
client->readStringUntil('\n'); // consume separator
184211
return type;
185212
}
213+
214+
void FirebaseSerial::begin(const String& host, const String& auth, const String& path) {
215+
stream_.begin(host, auth, path, &httpStream_);
216+
}
217+
218+
bool FirebaseSerial::available() {
219+
return httpStream_.getStreamPtr()->available();
220+
}
221+
222+
String FirebaseSerial::read() {
223+
String event;
224+
auto type = stream_.read(event);
225+
return event;
226+
}

Firebase.h

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ class FirebaseStream;
3434
// Firebase REST API client.
3535
class Firebase {
3636
public:
37-
Firebase(const String& host);
37+
Firebase();
38+
Firebase(const String& host, const String& auth = "");
39+
void begin(const String& host, const String& auth = "");
40+
41+
Firebase& host(const String& host);
3842
Firebase& auth(const String& auth);
3943

4044
// Fetch json encoded `value` at `path`.
@@ -62,11 +66,11 @@ class FirebaseError {
6266
public:
6367
FirebaseError() {}
6468
FirebaseError(int code, const String& message) : code_(code), message_(message) {
65-
}
69+
}
6670
operator bool() const { return code_ != 0; }
6771
int code() const { return code_; }
6872
const String& message() const { return message_; }
69-
private:
73+
private:
7074
int code_ = 0;
7175
String message_ = "";
7276
};
@@ -76,8 +80,12 @@ class FirebaseCall {
7680
FirebaseCall() {}
7781
FirebaseCall(const String& host, const String& auth,
7882
const char* method, const String& path,
79-
const String& data = "",
83+
const String& data = "",
8084
HTTPClient* http = NULL);
85+
void begin(const String& host, const String& auth,
86+
const char* method, const String& path,
87+
const String& data = "",
88+
HTTPClient* http = NULL);
8189
const FirebaseError& error() const {
8290
return error_;
8391
}
@@ -95,7 +103,7 @@ class FirebaseGet : public FirebaseCall {
95103
FirebaseGet() {}
96104
FirebaseGet(const String& host, const String& auth,
97105
const String& path, HTTPClient* http = NULL);
98-
106+
99107
const String& json() const {
100108
return json_;
101109
}
@@ -145,7 +153,9 @@ class FirebaseStream : public FirebaseCall {
145153
FirebaseStream() {}
146154
FirebaseStream(const String& host, const String& auth,
147155
const String& path, HTTPClient* http = NULL);
148-
156+
void begin(const String& host, const String& auth,
157+
const String& path, HTTPClient* http = NULL);
158+
149159
// Return if there is any event available to read.
150160
bool available();
151161

@@ -157,14 +167,27 @@ class FirebaseStream : public FirebaseCall {
157167
};
158168

159169
// Read next json encoded `event` from stream.
160-
Event read(String& event);
170+
Event read(String& event);
161171

162172
const FirebaseError& error() const {
163173
return _error;
164174
}
165-
175+
166176
private:
167177
FirebaseError _error;
168178
};
169179

180+
class FirebaseSerial {
181+
public:
182+
FirebaseSerial() {}
183+
void begin(const String& url, const String& auth = "", const String& path = "/");
184+
bool available();
185+
String read();
186+
187+
private:
188+
HTTPClient http_;
189+
HTTPClient httpStream_;
190+
FirebaseStream stream_;
191+
};
192+
170193
#endif // firebase_h
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright 2016 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+
// FirebaseStream_ESP8266 is a sample that stream bitcoin price from a
18+
// public Firebase and optionally display them on a OLED i2c screen.
19+
20+
#include <Firebase.h>
21+
22+
FirebaseSerial firebaseSerial;
23+
24+
void setup() {
25+
Serial.begin(9600);
26+
firebaseSerial.begin("example.firebaseio.com", "secret");
27+
28+
// connect to wifi.
29+
WiFi.begin("GoogleGuest", "PASSWORD");
30+
Serial.print("connecting");
31+
while (WiFi.status() != WL_CONNECTED) {
32+
Serial.print(".");
33+
delay(500);
34+
}
35+
Serial.println();
36+
Serial.print("connected: ");
37+
Serial.println(WiFi.localIP());
38+
}
39+
40+
41+
void loop() {
42+
if (firebaseSerial.available()) {
43+
Serial.print(firebaseSerial.read());
44+
}
45+
}

0 commit comments

Comments
 (0)