Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase serial #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
28 changes: 20 additions & 8 deletions Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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_ = "";
};
Expand All @@ -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_;
}
Expand All @@ -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_;
}
Expand Down Expand Up @@ -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_;
Expand All @@ -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();

Expand All @@ -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;
};
Expand Down
43 changes: 43 additions & 0 deletions FirebaseSerial.cpp
Original file line number Diff line number Diff line change
@@ -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_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the push a member and not just on the stack? We don't need it around from what I see.

Also how do we report if there is an error sending? We could return bytes written and -1 for error maybe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it could be on the stack, and I wouldn't have to add the weird-looking .begin interface.

}
42 changes: 42 additions & 0 deletions FirebaseSerial.h
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions examples/FirebaseSerial_ESP8266/FirebaseSerial_ESP8266.ino
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment needs updating.

// public Firebase and optionally display them on a OLED i2c screen.

#include <FirebaseSerial.h>

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\"}");
}