diff --git a/Firebase.cpp b/Firebase.cpp index 154a72f3..53b3682c 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); @@ -139,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(); @@ -164,6 +192,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(); } diff --git a/Firebase.h b/Firebase.h index 5691038d..bde45bec 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_; } @@ -123,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_; @@ -145,7 +155,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,12 +169,12 @@ 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; }; 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 new file mode 100644 index 00000000..29cc5457 --- /dev/null +++ b/examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino @@ -0,0 +1,53 @@ +// +// 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 + +void setup() { + Serial.begin(9600); + + // connect to wifi. + WiFi.begin("SSID", "PASSWORD"); + Serial.print("connecting to wifi"); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(500); + } + 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.println(FirebaseSerial.read()); + } + delay(1000); + FirebaseSerial.println("{\".sv\": \"timestamp\"}"); +}