From 2aacf1598ca977fcf24a288648209eebca41a9d7 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 1 Apr 2016 17:03:41 -0700 Subject: [PATCH 1/3] add FirebaseSerial w/ streaming --- Firebase.cpp | 45 ++++++++++++++++++- Firebase.h | 39 ++++++++++++---- .../FirebaseSerial_ESP8266.ino | 45 +++++++++++++++++++ 3 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino diff --git a/Firebase.cpp b/Firebase.cpp index 154a72f3..33f4ce8b 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -40,10 +40,24 @@ String makeFirebaseURL(const String& path, const String& auth) { } // namespace -Firebase::Firebase(const String& host) : host_(host) { +Firebase::Firebase() { +} + +Firebase::Firebase(const String& host, const String& auth) { + begin(host, auth); +} + +void Firebase::begin(const String& host, const String& auth) { + host_ = host; + auth_ = auth; http_.setReuse(true); } +Firebase& Firebase::host(const String& host) { + host_ = host; + return *this; +} + Firebase& Firebase::auth(const String& auth) { auth_ = auth; return *this; @@ -73,7 +87,14 @@ FirebaseStream Firebase::stream(const String& path) { // FirebaseCall FirebaseCall::FirebaseCall(const String& host, const String& auth, const char* method, const String& path, - const String& data, HTTPClient* http) : http_(http) { + const String& data, HTTPClient* http) { + begin(host, auth, method, path, data, http); +} + +void FirebaseCall::begin(const String& host, const String& auth, + const char* method, const String& path, + const String& data, HTTPClient* http) { + http_ = http; String url = makeFirebaseURL(path, auth); http_->setReuse(true); http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint); @@ -164,6 +185,12 @@ FirebaseStream::FirebaseStream(const String& host, const String& auth, : FirebaseCall(host, auth, "STREAM", path, "", http) { } +void FirebaseStream::begin(const String& host, const String& auth, + const String& path, + HTTPClient* http) { + FirebaseCall::begin(host, auth, "STREAM", path, "", http); +} + bool FirebaseStream::available() { return http_->getStreamPtr()->available(); } @@ -183,3 +210,17 @@ FirebaseStream::Event FirebaseStream::read(String& event) { client->readStringUntil('\n'); // consume separator return type; } + +void FirebaseSerial::begin(const String& host, const String& auth, const String& path) { + stream_.begin(host, auth, path, &httpStream_); +} + +bool FirebaseSerial::available() { + return httpStream_.getStreamPtr()->available(); +} + +String FirebaseSerial::read() { + String event; + auto type = stream_.read(event); + return event; +} diff --git a/Firebase.h b/Firebase.h index 5691038d..ca130718 100644 --- a/Firebase.h +++ b/Firebase.h @@ -34,7 +34,11 @@ class FirebaseStream; // Firebase REST API client. class Firebase { public: - Firebase(const String& host); + Firebase(); + Firebase(const String& host, const String& auth = ""); + void begin(const String& host, const String& auth = ""); + + Firebase& host(const String& host); Firebase& auth(const String& auth); // Fetch json encoded `value` at `path`. @@ -62,11 +66,11 @@ class FirebaseError { public: FirebaseError() {} FirebaseError(int code, const String& message) : code_(code), message_(message) { - } + } operator bool() const { return code_ != 0; } int code() const { return code_; } const String& message() const { return message_; } - private: + private: int code_ = 0; String message_ = ""; }; @@ -76,8 +80,12 @@ class FirebaseCall { FirebaseCall() {} FirebaseCall(const String& host, const String& auth, const char* method, const String& path, - const String& data = "", + const String& data = "", HTTPClient* http = NULL); + void begin(const String& host, const String& auth, + const char* method, const String& path, + const String& data = "", + HTTPClient* http = NULL); const FirebaseError& error() const { return error_; } @@ -95,7 +103,7 @@ class FirebaseGet : public FirebaseCall { FirebaseGet() {} FirebaseGet(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); - + const String& json() const { return json_; } @@ -145,7 +153,9 @@ class FirebaseStream : public FirebaseCall { FirebaseStream() {} FirebaseStream(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); - + void begin(const String& host, const String& auth, + const String& path, HTTPClient* http = NULL); + // Return if there is any event available to read. bool available(); @@ -157,14 +167,27 @@ class FirebaseStream : public FirebaseCall { }; // Read next json encoded `event` from stream. - Event read(String& event); + Event read(String& event); const FirebaseError& error() const { return _error; } - + private: FirebaseError _error; }; +class FirebaseSerial { + public: + FirebaseSerial() {} + void begin(const String& url, const String& auth = "", const String& path = "/"); + bool available(); + String read(); + + private: + HTTPClient http_; + HTTPClient httpStream_; + FirebaseStream stream_; +}; + #endif // firebase_h diff --git a/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino b/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino new file mode 100644 index 00000000..475f0dc4 --- /dev/null +++ b/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino @@ -0,0 +1,45 @@ +// +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +// FirebaseStream_ESP8266 is a sample that stream bitcoin price from a +// public Firebase and optionally display them on a OLED i2c screen. + +#include + +FirebaseSerial firebaseSerial; + +void setup() { + Serial.begin(9600); + firebaseSerial.begin("example.firebaseio.com", "secret"); + + // connect to wifi. + WiFi.begin("GoogleGuest", "PASSWORD"); + Serial.print("connecting"); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(500); + } + Serial.println(); + Serial.print("connected: "); + Serial.println(WiFi.localIP()); +} + + +void loop() { + if (firebaseSerial.available()) { + Serial.print(firebaseSerial.read()); + } +} From c960edf07edb847ca9543313c92fa05c4732ac3f Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 1 Apr 2016 17:32:42 -0700 Subject: [PATCH 2/3] FirebaseSerial: add print --- Firebase.cpp | 18 ++++++++++++++++-- Firebase.h | 11 +++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Firebase.cpp b/Firebase.cpp index 33f4ce8b..7bd2d20d 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -160,11 +160,18 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth, json_ = response(); } } + // FirebasePush FirebasePush::FirebasePush(const String& host, const String& auth, const String& path, const String& value, - HTTPClient* http) - : FirebaseCall(host, auth, "POST", path, value, http) { + HTTPClient* http) { + begin(host, auth, path, value, http); +} + +void FirebasePush::begin(const String& host, const String& auth, + const String& path, const String& value, + HTTPClient* http) { + FirebaseCall::begin(host, auth, "POST", path, value, http); if (!error()) { // TODO: parse name name_ = response(); @@ -212,6 +219,9 @@ FirebaseStream::Event FirebaseStream::read(String& event) { } void FirebaseSerial::begin(const String& host, const String& auth, const String& path) { + host_ = host; + auth_ = auth; + path_ = path; stream_.begin(host, auth, path, &httpStream_); } @@ -224,3 +234,7 @@ String FirebaseSerial::read() { auto type = stream_.read(event); return event; } + +void FirebaseSerial::print(String data) { + push_.begin(host_, auth_, path_, data, &httpPush_); +} diff --git a/Firebase.h b/Firebase.h index ca130718..e2cc3cfc 100644 --- a/Firebase.h +++ b/Firebase.h @@ -131,6 +131,8 @@ class FirebasePush : public FirebaseCall { FirebasePush() {} FirebasePush(const String& host, const String& auth, const String& path, const String& value, HTTPClient* http = NULL); + void begin(const String& host, const String& auth, + const String& path, const String& value, HTTPClient* http = NULL); const String& name() const { return name_; @@ -180,14 +182,19 @@ class FirebaseStream : public FirebaseCall { class FirebaseSerial { public: FirebaseSerial() {} - void begin(const String& url, const String& auth = "", const String& path = "/"); + void begin(const String& host, const String& auth = "", const String& path = "/"); bool available(); String read(); + void print(String data); private: - HTTPClient http_; + String host_; + String auth_; + String path_; HTTPClient httpStream_; FirebaseStream stream_; + HTTPClient httpPush_; + FirebasePush push_; }; #endif // firebase_h From 13d533b6034c915a51d87453f2e2220a51168976 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Sat, 2 Apr 2016 23:52:54 -0700 Subject: [PATCH 3/3] FirebaseSerial: split and add global --- Firebase.cpp | 21 --------- Firebase.h | 18 -------- FirebaseSerial.cpp | 43 +++++++++++++++++++ FirebaseSerial.h | 42 ++++++++++++++++++ .../FirebaseSerial_ESP8266.ino | 26 +++++++---- 5 files changed, 102 insertions(+), 48 deletions(-) create mode 100644 FirebaseSerial.cpp create mode 100644 FirebaseSerial.h diff --git a/Firebase.cpp b/Firebase.cpp index 7bd2d20d..53b3682c 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -217,24 +217,3 @@ FirebaseStream::Event FirebaseStream::read(String& event) { client->readStringUntil('\n'); // consume separator return type; } - -void FirebaseSerial::begin(const String& host, const String& auth, const String& path) { - host_ = host; - auth_ = auth; - path_ = path; - stream_.begin(host, auth, path, &httpStream_); -} - -bool FirebaseSerial::available() { - return httpStream_.getStreamPtr()->available(); -} - -String FirebaseSerial::read() { - String event; - auto type = stream_.read(event); - return event; -} - -void FirebaseSerial::print(String data) { - push_.begin(host_, auth_, path_, data, &httpPush_); -} diff --git a/Firebase.h b/Firebase.h index e2cc3cfc..bde45bec 100644 --- a/Firebase.h +++ b/Firebase.h @@ -179,22 +179,4 @@ class FirebaseStream : public FirebaseCall { FirebaseError _error; }; -class FirebaseSerial { - public: - FirebaseSerial() {} - void begin(const String& host, const String& auth = "", const String& path = "/"); - bool available(); - String read(); - void print(String data); - - private: - String host_; - String auth_; - String path_; - HTTPClient httpStream_; - FirebaseStream stream_; - HTTPClient httpPush_; - FirebasePush push_; -}; - #endif // firebase_h diff --git a/FirebaseSerial.cpp b/FirebaseSerial.cpp new file mode 100644 index 00000000..3bb848f6 --- /dev/null +++ b/FirebaseSerial.cpp @@ -0,0 +1,43 @@ +// +// Copyright 2015 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#include "FirebaseSerial.h" + +FirebaseSerialESP8266 FirebaseSerial; + +void FirebaseSerialESP8266::begin(const String& host, const String& auth, const String& path) { + host_ = host; + auth_ = auth; + path_ = path; + stream_.begin(host, auth, path, &httpStream_); +} + +bool FirebaseSerialESP8266::connected() { + return httpStream_.connected(); +} + +bool FirebaseSerialESP8266::available() { + return httpStream_.getStreamPtr()->available(); +} + +String FirebaseSerialESP8266::read() { + String event; + auto type = stream_.read(event); + return event; +} + +void FirebaseSerialESP8266::println(String data) { + push_.begin(host_, auth_, path_, data, &httpPush_); +} diff --git a/FirebaseSerial.h b/FirebaseSerial.h new file mode 100644 index 00000000..53367d38 --- /dev/null +++ b/FirebaseSerial.h @@ -0,0 +1,42 @@ +// +// Copyright 2015 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#ifndef firebase_serial_h +#define firebase_serial_h + +#include "Firebase.h" + +class FirebaseSerialESP8266 { + public: + FirebaseSerialESP8266() {} + void begin(const String& host, const String& auth = "", const String& path = "/"); + bool connected(); + bool available(); + String read(); + void println(String data); + + private: + String host_; + String auth_; + String path_; + HTTPClient httpStream_; + FirebaseStream stream_; + HTTPClient httpPush_; + FirebasePush push_; +}; + +extern FirebaseSerialESP8266 FirebaseSerial; + +#endif // firebase_serial_h diff --git a/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino b/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino index 475f0dc4..29cc5457 100644 --- a/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino +++ b/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino @@ -17,17 +17,14 @@ // FirebaseStream_ESP8266 is a sample that stream bitcoin price from a // public Firebase and optionally display them on a OLED i2c screen. -#include - -FirebaseSerial firebaseSerial; +#include void setup() { Serial.begin(9600); - firebaseSerial.begin("example.firebaseio.com", "secret"); // connect to wifi. - WiFi.begin("GoogleGuest", "PASSWORD"); - Serial.print("connecting"); + WiFi.begin("SSID", "PASSWORD"); + Serial.print("connecting to wifi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); @@ -35,11 +32,22 @@ void setup() { Serial.println(); Serial.print("connected: "); Serial.println(WiFi.localIP()); + + FirebaseSerial.begin("example.firebaseio.com", "secret"); + Serial.print("connecting to firebase"); + while (!FirebaseSerial.connected()) { + Serial.print("."); + delay(500); + } + Serial.println(); + Serial.println("connected"); } void loop() { - if (firebaseSerial.available()) { - Serial.print(firebaseSerial.read()); - } + if (FirebaseSerial.available()) { + Serial.println(FirebaseSerial.read()); + } + delay(1000); + FirebaseSerial.println("{\".sv\": \"timestamp\"}"); }