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

example/stream: use ArduinoJson #51

Merged
merged 1 commit into from
Feb 2, 2016
Merged
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
20 changes: 10 additions & 10 deletions Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ 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 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 @@ -105,8 +105,8 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,

// FirebaseGet
FirebaseGet::FirebaseGet(const String& host, const String& auth,
const String& path,
HTTPClient* http)
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "GET", path, "", http) {

if (!error()) {
Expand All @@ -117,8 +117,8 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth,

// FirebasePush
FirebasePush::FirebasePush(const String& host, const String& auth,
const String& path, const String& value,
HTTPClient* http)
const String& path, const String& value,
HTTPClient* http)
: FirebaseCall(host, auth, "POST", path, value, http) {
if (!error()) {
// TODO: parse name
Expand All @@ -128,15 +128,15 @@ FirebasePush::FirebasePush(const String& host, const String& auth,

// FirebasePush
FirebaseRemove::FirebaseRemove(const String& host, const String& auth,
const String& path,
HTTPClient* http)
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "DELETE", path, "", http) {
}

// FirebaseStream
FirebaseStream::FirebaseStream(const String& host, const String& auth,
const String& path,
HTTPClient* http)
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "STREAM", path, "", http) {
}

Expand Down
14 changes: 7 additions & 7 deletions Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class FirebaseCall {
public:
FirebaseCall() {}
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
const String& data = "",
HTTPClient* http = NULL);
const char* method, const String& path,
const String& data = "",
HTTPClient* http = NULL);
const FirebaseError& error() const {
return error_;
}
Expand All @@ -92,7 +92,7 @@ class FirebaseGet : public FirebaseCall {
public:
FirebaseGet() {}
FirebaseGet(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);
const String& path, HTTPClient* http = NULL);

const String& json() const {
return json_;
Expand All @@ -106,7 +106,7 @@ class FirebasePush : public FirebaseCall {
public:
FirebasePush() {}
FirebasePush(const String& host, const String& auth,
const String& path, const String& value, HTTPClient* http = NULL);
const String& path, const String& value, HTTPClient* http = NULL);

const String& name() const {
return name_;
Expand All @@ -120,15 +120,15 @@ class FirebaseRemove : public FirebaseCall {
public:
FirebaseRemove() {}
FirebaseRemove(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);
const String& path, HTTPClient* http = NULL);
};


class FirebaseStream : public FirebaseCall {
public:
FirebaseStream() {}
FirebaseStream(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);
const String& path, HTTPClient* http = NULL);

// True if there is an event available.
bool available();
Expand Down
14 changes: 10 additions & 4 deletions examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Firebase.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>

#define OLED_RESET 10
Adafruit_SSD1306 display(OLED_RESET);
Expand Down Expand Up @@ -47,7 +48,7 @@ void setup() {
}


void loop() {
void loop() {
if (stream.error()) {
Serial.println("streaming error");
Serial.println(stream.error().message());
Expand All @@ -58,16 +59,21 @@ void loop() {
auto type = stream.read(event);
Serial.print("event: ");
Serial.println(type);
if (type != FirebaseStream::Event::UNKNOWN) {
if (type == FirebaseStream::Event::PUT) {
StaticJsonBuffer<200> buf;
Serial.print("data: ");
Serial.println(event);
JsonObject& json = buf.parseObject((char*)event.c_str());
String path = json["path"];
float data = json["data"];

// TODO(proppy): parse JSON object.
display.clearDisplay();
display.setTextSize(1);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(event);
display.println(path.c_str()+1);
display.println(data);
display.display();
}
}
Expand Down