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

FirebaseArduino: simpler Arduino wrapper #98

Merged
merged 5 commits into from
Apr 27, 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
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
language: c
sudo: false
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
env:
- ARDUINO_VERSION=1.6.8 ARDUINO_ESP8266_VERSION=2.1.0 ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino
- ARDUINO_VERSION=1.6.8 ARDUINO_ESP8266_VERSION=2.2.0-rc1 ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino
- ARDUINO_VERSION=nightly ARDUINO_ESP8266_VERSION=master ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino
- ARDUINO_VERSION=1.6.8 ARDUINO_ESP8266_VERSION=2.1.0 GCC_VERSION=4.8 ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino CXX=g++-${GCC_VERSION}
- ARDUINO_VERSION=1.6.8 ARDUINO_ESP8266_VERSION=2.2.0-rc1 GCC_VERSION=4.8 ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino CXX=g++-${GCC_VERSION}
- ARDUINO_VERSION=nightly ARDUINO_ESP8266_VERSION=master GCC_VERSION=4.8 ARDUINO_ROOT=${HOME}/arduino-${ARDUINO_VERSION} ARDUINO_ESP8266_ROOT=${ARDUINO_ROOT}/hardware/esp8266com/esp8266 ARDUINO_HOME=${HOME}/Arduino CXX=g++-${GCC_VERSION}
install:
- ( cd ${HOME} && curl -O https://downloads.arduino.cc/arduino-${ARDUINO_VERSION}-linux64.tar.xz && tar xvf arduino-${ARDUINO_VERSION}-linux64.tar.xz )
- git clone --branch ${ARDUINO_ESP8266_VERSION} https://github.com/esp8266/Arduino.git ${ARDUINO_ESP8266_ROOT}
Expand All @@ -13,3 +19,4 @@ before_script:
- ( cd ${ARDUINO_HOME}/libraries && ln -s ${TRAVIS_BUILD_DIR} firebase-arduino && ln -s ${TRAVIS_BUILD_DIR}/src/third-party/arduino-json-5.2 ArduinoJson )
script:
- ${ARDUINO_ROOT}/arduino-builder -verbose -hardware ${ARDUINO_ROOT}/hardware/ -tools ${ARDUINO_ESP8266_ROOT}/tools/ -tools ${ARDUINO_ROOT}/tools-builder/ -fqbn esp8266com:esp8266:nodemcuv2 -libraries ${ARDUINO_HOME}/libraries/ -prefs build.flash_ld=${ARDUINO_ESP8266_ROOT}/tools/sdk/ld/eagle.flash.4m.ld -prefs build.flash_freq=40 -prefs build.flash_size=4M examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino
- cd test && make check
44 changes: 16 additions & 28 deletions examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
// limitations under the License.
//

// FirebasePush_ESP8266 is a sample that push a new timestamp to firebase
// on each reset.
// FirebasePush_ESP8266 is a sample that push a new value to Firebase
// every seconds.

#include <ESP8266WiFi.h>

#include <Firebase.h>

// create firebase client.
Firebase fbase("example.firebaseio.com", "secret_or_token");
#include <FirebaseArduino.h>

void setup() {
Serial.begin(9600);
Expand All @@ -37,29 +33,21 @@ void setup() {
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());

Firebase.begin("example.firebaseio.com", "auth_or_token");
}

// add a new entry.
FirebasePush push = fbase.push("/logs", "{\".sv\": \"timestamp\"}");
if (push.error()) {
Serial.println("Firebase push failed");
Serial.println(push.error().message());
return;
}

// print key.
Serial.println("Name: " + push.name());
int n = 0;

// get all entries.
FirebaseGet get = fbase.get("/logs");
if (get.error()) {
Serial.println("Firebase get failed");
Serial.println(push.error().message());
void loop() {
// push a new value.
String name = Firebase.push("/logs", n++);
if (Firebase.failed()) {
Serial.print("push failed: ");
Serial.println(Firebase.error());
return;
}
// Print written timestamp.
String data = get.json()[push.name()];
Serial.println("Timestamp:" + data);
}

void loop() {
Serial.print("pushed: ");
Copy link
Collaborator

@ed7coyne ed7coyne Apr 27, 2016

Choose a reason for hiding this comment

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

While it is kind of pointless I still like having a Get command in here too to show you the timestamp that was actually written and to demonstrate using get and jsonobject since we don't have a get example.

Copy link
Contributor Author

@proppy proppy Apr 27, 2016

Choose a reason for hiding this comment

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

Yes, I think I will adapt the same to show all the api (but stream) #99. Maybe in a different PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good.

Serial.println(name);
delay(1000);
}
86 changes: 86 additions & 0 deletions src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// 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.
//

#include "FirebaseArduino.h"

void FirebaseArduino::begin(const char* host, const char* auth) {
http_.reset(FirebaseHttpClient::create());
http_->setReuseConnection(true);
host_ = host;
auth_ = auth;
}

String FirebaseArduino::FirebaseArduino::push(const String& path, const JsonVariant& value) {
String buf;
value.printTo(buf);
auto push = FirebasePush(host_, auth_, path, buf, http_.get());
error_ = push.error();
return push.name();
}

void FirebaseArduino::set(const String& path, const JsonVariant& value) {
String buf;
value.printTo(buf);
auto set = FirebaseSet(host_, auth_, path, buf, http_.get());
error_ = set.error();
}

FirebaseObject FirebaseArduino::get(const char* path) {
auto get = FirebaseGet(host_, auth_, path, http_.get());
error_ = get.error();
if (!error_) {
return FirebaseObject{""};
}
return FirebaseObject(get.response());
}

void FirebaseArduino::remove(const char* path) {
auto remove = FirebaseRemove(host_, auth_, path, http_.get());
error_ = remove.error();
}

void FirebaseArduino::stream(const char* path) {
auto stream = FirebaseStream(host_, auth_, path, http_.get());
error_ = stream.error();
}

bool FirebaseArduino::available() {
return http_->getStreamPtr()->available();
}

FirebaseObject FirebaseArduino::readEvent() {
auto client = http_->getStreamPtr();
String type = client->readStringUntil('\n').substring(7);;
String event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
FirebaseObject obj = FirebaseObject(event);
obj["type"] = type;
return obj;
}

bool FirebaseArduino::success() {
return error_.code() == 0;
}

bool FirebaseArduino::failed() {
return error_.code() != 0;
}

const String& FirebaseArduino::error() {
return error_.message();
}

FirebaseArduino Firebase;
49 changes: 49 additions & 0 deletions src/FirebaseArduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// 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.
//

#ifndef FIREBASE_ARDUINO_H
#define FIREBASE_ARDUINO_H

#include "Firebase.h"
#include "FirebaseObject.h"

#ifndef FIREBASE_JSONBUFFER_SIZE
#define FIREBASE_JSONBUFFER_SIZE 200
#endif // FIREBASE_JSONBUFFER_SIZE

class FirebaseArduino {
public:
void begin(const char* host, const char* auth = "");
Copy link
Collaborator

@ed7coyne ed7coyne Apr 27, 2016

Choose a reason for hiding this comment

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

Since we are presenting this as the super easy way to get into firebase I think we should have a lot of comments on all of the methods here.

Also maybe some simple usage examples in the header comment at the top of the file.

Copy link
Contributor Author

@proppy proppy Apr 27, 2016

Choose a reason for hiding this comment

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

Yep, I'd like to do #30. maybe in a different PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good.

String push(const String& path, const JsonVariant& value);
void set(const String& path, const JsonVariant& value);
FirebaseObject get(const char* path);
void remove(const char* path);
void stream(const char* path);
bool available();
FirebaseObject readEvent();
bool success();
bool failed();
const String& error();
private:
String host_;
String auth_;
FirebaseError error_;
std::unique_ptr<FirebaseHttpClient> http_;
};

extern FirebaseArduino Firebase;

#endif // FIREBASE_ARDUINO_H
72 changes: 72 additions & 0 deletions src/FirebaseObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// 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.
//

#include "FirebaseObject.h"

namespace {
template<typename T>
T decodeJsonLiteral(const String& json) {
return JsonVariant{ArduinoJson::RawJson{json.c_str()}};
}

// ugly workaround to https://github.com/bblanchon/ArduinoJson/issues/265
template<>
String decodeJsonLiteral<String>(const String& json) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> buf;
String array = "[" + json + "]";
return buf.parseArray(&array[0])[0];
}
} // namespace

FirebaseObject::FirebaseObject(const String& data) : data_{data} {
if (data_[0] == '{') {
json_ = &buffer_.parseObject(&data_[0]);
} else if (data_[0] == '"') {
data_ = decodeJsonLiteral<String>(data_);
}
}

FirebaseObject::operator bool() {
return decodeJsonLiteral<bool>(data_);
}

FirebaseObject::operator int() {
return decodeJsonLiteral<int>(data_);
}

FirebaseObject::operator float() {
return decodeJsonLiteral<float>(data_);
}

FirebaseObject::operator const String&() {
return data_;
}

FirebaseObject::operator const JsonObject&() {
return *json_;
}

JsonObjectSubscript<const char*> FirebaseObject::operator[](const char* key) {
return json_->operator[](key);
}

JsonObjectSubscript<const String&> FirebaseObject::operator[](const String& key) {
return json_->operator[](key);
}

JsonVariant FirebaseObject::operator[](JsonObjectKey key) const {
return json_->operator[](key);
}
41 changes: 41 additions & 0 deletions src/FirebaseObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// 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_OBJECT_H
#define FIREBASE_OBJECT_H

#include "third-party/arduino-json-5.2/include/ArduinoJson.h"

#define FIREBASE_JSONBUFFER_SIZE 200

class FirebaseObject {
public:
FirebaseObject(const String& data);
operator bool();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar point here about comments on everything.

operator int();
operator float();
operator const String&();
operator const JsonObject&();
JsonObjectSubscript<const char*> operator[](const char* key);
JsonObjectSubscript<const String&> operator[](const String& key);
JsonVariant operator[](JsonObjectKey key) const;
private:
String data_;
StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE> buffer_;
JsonObject* json_;
};

#endif // FIREBASE_OBJECT_H
43 changes: 43 additions & 0 deletions test/FirebaseArduino_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// 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.
//

#include "FirebaseObject.h"
#include "gtest/gtest.h"

TEST(FirebaseObjectTest, JsonLiteral) {
EXPECT_EQ(bool(FirebaseObject("true")), true);
EXPECT_EQ(bool(FirebaseObject("false")), false);
EXPECT_EQ(int(FirebaseObject("42")), 42);
EXPECT_EQ(float(FirebaseObject("43.0")), 43.0);
EXPECT_EQ(String(FirebaseObject("\"foo\"")), "foo");
}

TEST(FirebaseObjectTest, JsonObject) {
{
const JsonObject& obj = FirebaseObject("{\"foo\":\"bar\"}");
String foo = obj["foo"];
EXPECT_EQ(foo, "bar");
}
{
String foo = FirebaseObject("{\"foo\":\"bar\"}")["foo"];
EXPECT_EQ(foo, "bar");
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading