Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit442baf8

Browse files
committed
Merge pull requestFirebaseExtended#98 from proppy/arduino
FirebaseArduino: simpler Arduino wrapper
2 parentsc4608b0 +13ce951 commit442baf8

File tree

8 files changed

+371
-31
lines changed

8 files changed

+371
-31
lines changed

‎.travis.yml

Lines changed: 10 additions & 3 deletions
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.0GCC_VERSION=4.8ARDUINO_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-rc1GCC_VERSION=4.8ARDUINO_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=masterGCC_VERSION=4.8ARDUINO_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

Lines changed: 16 additions & 28 deletions
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 newtimestamp tofirebase
18-
//on each reset.
17+
// FirebasePush_ESP8266 is a sample that push a newvalue toFirebase
18+
//every seconds.
1919

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

2723
voidsetup() {
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+
voidloop() {
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-
voidloop() {
50+
Serial.print("pushed:");
51+
Serial.println(name);
52+
delay(1000);
6553
}

‎src/FirebaseArduino.cpp

Lines changed: 86 additions & 0 deletions
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+
voidFirebaseArduino::begin(constchar* host,constchar* auth) {
20+
http_.reset(FirebaseHttpClient::create());
21+
http_->setReuseConnection(true);
22+
host_ = host;
23+
auth_ = auth;
24+
}
25+
26+
StringFirebaseArduino::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+
voidFirebaseArduino::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+
FirebaseObjectFirebaseArduino::get(constchar* path) {
42+
auto get =FirebaseGet(host_, auth_, path, http_.get());
43+
error_ = get.error();
44+
if (!error_) {
45+
return FirebaseObject{""};
46+
}
47+
returnFirebaseObject(get.response());
48+
}
49+
50+
voidFirebaseArduino::remove(constchar* path) {
51+
auto remove =FirebaseRemove(host_, auth_, path, http_.get());
52+
error_ = remove.error();
53+
}
54+
55+
voidFirebaseArduino::stream(constchar* path) {
56+
auto stream =FirebaseStream(host_, auth_, path, http_.get());
57+
error_ = stream.error();
58+
}
59+
60+
boolFirebaseArduino::available() {
61+
return http_->getStreamPtr()->available();
62+
}
63+
64+
FirebaseObjectFirebaseArduino::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+
boolFirebaseArduino::success() {
75+
return error_.code() ==0;
76+
}
77+
78+
boolFirebaseArduino::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

Lines changed: 49 additions & 0 deletions
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+
#defineFIREBASE_ARDUINO_H
19+
20+
#include"Firebase.h"
21+
#include"FirebaseObject.h"
22+
23+
#ifndef FIREBASE_JSONBUFFER_SIZE
24+
#defineFIREBASE_JSONBUFFER_SIZE200
25+
#endif// FIREBASE_JSONBUFFER_SIZE
26+
27+
classFirebaseArduino {
28+
public:
29+
voidbegin(constchar* host,constchar* auth ="");
30+
Stringpush(const String& path,const JsonVariant& value);
31+
voidset(const String& path,const JsonVariant& value);
32+
FirebaseObjectget(constchar* path);
33+
voidremove(constchar* path);
34+
voidstream(constchar* path);
35+
boolavailable();
36+
FirebaseObjectreadEvent();
37+
boolsuccess();
38+
boolfailed();
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

Lines changed: 72 additions & 0 deletions
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+
TdecodeJsonLiteral(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+
}elseif (data_[0] =='"') {
38+
data_ = decodeJsonLiteral<String>(data_);
39+
}
40+
}
41+
42+
FirebaseObject::operatorbool() {
43+
return decodeJsonLiteral<bool>(data_);
44+
}
45+
46+
FirebaseObject::operatorint() {
47+
return decodeJsonLiteral<int>(data_);
48+
}
49+
50+
FirebaseObject::operatorfloat() {
51+
return decodeJsonLiteral<float>(data_);
52+
}
53+
54+
FirebaseObject::operatorconst String&() {
55+
return data_;
56+
}
57+
58+
FirebaseObject::operatorconst JsonObject&() {
59+
return *json_;
60+
}
61+
62+
JsonObjectSubscript<constchar*> FirebaseObject::operator[](constchar* 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

Lines changed: 41 additions & 0 deletions
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+
#defineFIREBASE_OBJECT_H
19+
20+
#include"third-party/arduino-json-5.2/include/ArduinoJson.h"
21+
22+
#defineFIREBASE_JSONBUFFER_SIZE200
23+
24+
classFirebaseObject {
25+
public:
26+
FirebaseObject(const String& data);
27+
operatorbool();
28+
operatorint();
29+
operatorfloat();
30+
operatorconst String&();
31+
operatorconst JsonObject&();
32+
JsonObjectSubscript<constchar*>operator[](constchar* key);
33+
JsonObjectSubscript<const String&>operator[](const String& key);
34+
JsonVariantoperator[](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

Lines changed: 43 additions & 0 deletions
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+
intmain(int argc,char **argv) {
41+
::testing::InitGoogleTest(&argc, argv);
42+
returnRUN_ALL_TESTS();
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp