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

Commit 442baf8

Browse files
committed
Merge pull request #98 from proppy/arduino
FirebaseArduino: simpler Arduino wrapper
2 parents c4608b0 + 13ce951 commit 442baf8

File tree

8 files changed

+371
-31
lines changed

8 files changed

+371
-31
lines changed

.travis.yml

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
language: c
22
sudo: false
3+
addons:
4+
apt:
5+
sources:
6+
- ubuntu-toolchain-r-test
7+
packages:
8+
- g++-4.8
39
env:
4-
- 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
5-
- 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
6-
- 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
10+
- 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}
11+
- 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}
12+
- 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}
713
install:
814
- ( cd ${HOME} && curl -O https://downloads.arduino.cc/arduino-${ARDUINO_VERSION}-linux64.tar.xz && tar xvf arduino-${ARDUINO_VERSION}-linux64.tar.xz )
915
- git clone --branch ${ARDUINO_ESP8266_VERSION} https://github.com/esp8266/Arduino.git ${ARDUINO_ESP8266_ROOT}
@@ -13,3 +19,4 @@ before_script:
1319
- ( cd ${ARDUINO_HOME}/libraries && ln -s ${TRAVIS_BUILD_DIR} firebase-arduino && ln -s ${TRAVIS_BUILD_DIR}/src/third-party/arduino-json-5.2 ArduinoJson )
1420
script:
1521
- ${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
22+
- cd test && make check

examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

+16-28
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
// limitations under the License.
1515
//
1616

17-
// FirebasePush_ESP8266 is a sample that push a new timestamp to firebase
18-
// on each reset.
17+
// FirebasePush_ESP8266 is a sample that push a new value to Firebase
18+
// every seconds.
1919

2020
#include <ESP8266WiFi.h>
21-
22-
#include <Firebase.h>
23-
24-
// create firebase client.
25-
Firebase fbase("example.firebaseio.com", "secret_or_token");
21+
#include <FirebaseArduino.h>
2622

2723
void setup() {
2824
Serial.begin(9600);
@@ -37,29 +33,21 @@ void setup() {
3733
Serial.println();
3834
Serial.print("connected: ");
3935
Serial.println(WiFi.localIP());
36+
37+
Firebase.begin("example.firebaseio.com", "auth_or_token");
38+
}
4039

41-
// add a new entry.
42-
FirebasePush push = fbase.push("/logs", "{\".sv\": \"timestamp\"}");
43-
if (push.error()) {
44-
Serial.println("Firebase push failed");
45-
Serial.println(push.error().message());
46-
return;
47-
}
48-
49-
// print key.
50-
Serial.println("Name: " + push.name());
40+
int n = 0;
5141

52-
// get all entries.
53-
FirebaseGet get = fbase.get("/logs");
54-
if (get.error()) {
55-
Serial.println("Firebase get failed");
56-
Serial.println(push.error().message());
42+
void loop() {
43+
// push a new value.
44+
String name = Firebase.push("/logs", n++);
45+
if (Firebase.failed()) {
46+
Serial.print("push failed: ");
47+
Serial.println(Firebase.error());
5748
return;
5849
}
59-
// Print written timestamp.
60-
String data = get.json()[push.name()];
61-
Serial.println("Timestamp:" + data);
62-
}
63-
64-
void loop() {
50+
Serial.print("pushed: ");
51+
Serial.println(name);
52+
delay(1000);
6553
}

src/FirebaseArduino.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#include "FirebaseArduino.h"
18+
19+
void FirebaseArduino::begin(const char* host, const char* auth) {
20+
http_.reset(FirebaseHttpClient::create());
21+
http_->setReuseConnection(true);
22+
host_ = host;
23+
auth_ = auth;
24+
}
25+
26+
String FirebaseArduino::FirebaseArduino::push(const String& path, const JsonVariant& value) {
27+
String buf;
28+
value.printTo(buf);
29+
auto push = FirebasePush(host_, auth_, path, buf, http_.get());
30+
error_ = push.error();
31+
return push.name();
32+
}
33+
34+
void FirebaseArduino::set(const String& path, const JsonVariant& value) {
35+
String buf;
36+
value.printTo(buf);
37+
auto set = FirebaseSet(host_, auth_, path, buf, http_.get());
38+
error_ = set.error();
39+
}
40+
41+
FirebaseObject FirebaseArduino::get(const char* path) {
42+
auto get = FirebaseGet(host_, auth_, path, http_.get());
43+
error_ = get.error();
44+
if (!error_) {
45+
return FirebaseObject{""};
46+
}
47+
return FirebaseObject(get.response());
48+
}
49+
50+
void FirebaseArduino::remove(const char* path) {
51+
auto remove = FirebaseRemove(host_, auth_, path, http_.get());
52+
error_ = remove.error();
53+
}
54+
55+
void FirebaseArduino::stream(const char* path) {
56+
auto stream = FirebaseStream(host_, auth_, path, http_.get());
57+
error_ = stream.error();
58+
}
59+
60+
bool FirebaseArduino::available() {
61+
return http_->getStreamPtr()->available();
62+
}
63+
64+
FirebaseObject FirebaseArduino::readEvent() {
65+
auto client = http_->getStreamPtr();
66+
String type = client->readStringUntil('\n').substring(7);;
67+
String event = client->readStringUntil('\n').substring(6);
68+
client->readStringUntil('\n'); // consume separator
69+
FirebaseObject obj = FirebaseObject(event);
70+
obj["type"] = type;
71+
return obj;
72+
}
73+
74+
bool FirebaseArduino::success() {
75+
return error_.code() == 0;
76+
}
77+
78+
bool FirebaseArduino::failed() {
79+
return error_.code() != 0;
80+
}
81+
82+
const String& FirebaseArduino::error() {
83+
return error_.message();
84+
}
85+
86+
FirebaseArduino Firebase;

src/FirebaseArduino.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#ifndef FIREBASE_ARDUINO_H
18+
#define FIREBASE_ARDUINO_H
19+
20+
#include "Firebase.h"
21+
#include "FirebaseObject.h"
22+
23+
#ifndef FIREBASE_JSONBUFFER_SIZE
24+
#define FIREBASE_JSONBUFFER_SIZE 200
25+
#endif // FIREBASE_JSONBUFFER_SIZE
26+
27+
class FirebaseArduino {
28+
public:
29+
void begin(const char* host, const char* auth = "");
30+
String push(const String& path, const JsonVariant& value);
31+
void set(const String& path, const JsonVariant& value);
32+
FirebaseObject get(const char* path);
33+
void remove(const char* path);
34+
void stream(const char* path);
35+
bool available();
36+
FirebaseObject readEvent();
37+
bool success();
38+
bool failed();
39+
const String& error();
40+
private:
41+
String host_;
42+
String auth_;
43+
FirebaseError error_;
44+
std::unique_ptr<FirebaseHttpClient> http_;
45+
};
46+
47+
extern FirebaseArduino Firebase;
48+
49+
#endif // FIREBASE_ARDUINO_H

src/FirebaseObject.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#include "FirebaseObject.h"
18+
19+
namespace {
20+
template<typename T>
21+
T decodeJsonLiteral(const String& json) {
22+
return JsonVariant{ArduinoJson::RawJson{json.c_str()}};
23+
}
24+
25+
// ugly workaround to https://github.com/bblanchon/ArduinoJson/issues/265
26+
template<>
27+
String decodeJsonLiteral<String>(const String& json) {
28+
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> buf;
29+
String array = "[" + json + "]";
30+
return buf.parseArray(&array[0])[0];
31+
}
32+
} // namespace
33+
34+
FirebaseObject::FirebaseObject(const String& data) : data_{data} {
35+
if (data_[0] == '{') {
36+
json_ = &buffer_.parseObject(&data_[0]);
37+
} else if (data_[0] == '"') {
38+
data_ = decodeJsonLiteral<String>(data_);
39+
}
40+
}
41+
42+
FirebaseObject::operator bool() {
43+
return decodeJsonLiteral<bool>(data_);
44+
}
45+
46+
FirebaseObject::operator int() {
47+
return decodeJsonLiteral<int>(data_);
48+
}
49+
50+
FirebaseObject::operator float() {
51+
return decodeJsonLiteral<float>(data_);
52+
}
53+
54+
FirebaseObject::operator const String&() {
55+
return data_;
56+
}
57+
58+
FirebaseObject::operator const JsonObject&() {
59+
return *json_;
60+
}
61+
62+
JsonObjectSubscript<const char*> FirebaseObject::operator[](const char* key) {
63+
return json_->operator[](key);
64+
}
65+
66+
JsonObjectSubscript<const String&> FirebaseObject::operator[](const String& key) {
67+
return json_->operator[](key);
68+
}
69+
70+
JsonVariant FirebaseObject::operator[](JsonObjectKey key) const {
71+
return json_->operator[](key);
72+
}

src/FirebaseObject.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Copyright 2015 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#ifndef FIREBASE_OBJECT_H
18+
#define FIREBASE_OBJECT_H
19+
20+
#include "third-party/arduino-json-5.2/include/ArduinoJson.h"
21+
22+
#define FIREBASE_JSONBUFFER_SIZE 200
23+
24+
class FirebaseObject {
25+
public:
26+
FirebaseObject(const String& data);
27+
operator bool();
28+
operator int();
29+
operator float();
30+
operator const String&();
31+
operator const JsonObject&();
32+
JsonObjectSubscript<const char*> operator[](const char* key);
33+
JsonObjectSubscript<const String&> operator[](const String& key);
34+
JsonVariant operator[](JsonObjectKey key) const;
35+
private:
36+
String data_;
37+
StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE> buffer_;
38+
JsonObject* json_;
39+
};
40+
41+
#endif // FIREBASE_OBJECT_H

test/FirebaseArduino_test.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#include "FirebaseObject.h"
18+
#include "gtest/gtest.h"
19+
20+
TEST(FirebaseObjectTest, JsonLiteral) {
21+
EXPECT_EQ(bool(FirebaseObject("true")), true);
22+
EXPECT_EQ(bool(FirebaseObject("false")), false);
23+
EXPECT_EQ(int(FirebaseObject("42")), 42);
24+
EXPECT_EQ(float(FirebaseObject("43.0")), 43.0);
25+
EXPECT_EQ(String(FirebaseObject("\"foo\"")), "foo");
26+
}
27+
28+
TEST(FirebaseObjectTest, JsonObject) {
29+
{
30+
const JsonObject& obj = FirebaseObject("{\"foo\":\"bar\"}");
31+
String foo = obj["foo"];
32+
EXPECT_EQ(foo, "bar");
33+
}
34+
{
35+
String foo = FirebaseObject("{\"foo\":\"bar\"}")["foo"];
36+
EXPECT_EQ(foo, "bar");
37+
}
38+
}
39+
40+
int main(int argc, char **argv) {
41+
::testing::InitGoogleTest(&argc, argv);
42+
return RUN_ALL_TESTS();
43+
}

0 commit comments

Comments
 (0)