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

Commit 1bc895e

Browse files
committed
Stop stream when we set value to avoid memory issues. Added example.
1 parent 61b6883 commit 1bc895e

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// FirebaseDemo_ESP8266 is a sample that binds several pins to paths in
18+
// firebase using the transcriber object.
19+
20+
#include <ESP8266WiFi.h>
21+
#include <Transcriber.h>
22+
23+
// Set these to run example.
24+
#define FIREBASE_HOST ""
25+
#define FIREBASE_PATH "/fthing"
26+
#define FIREBASE_AUTH ""
27+
#define WIFI_SSID ""
28+
#define WIFI_PASSWORD ""
29+
30+
#define DIGITAL_IN D1
31+
#define DIGITAL_OUT BUILTIN_LED
32+
#define ANALOG_IN A0
33+
#define ANALOG_OUT D8
34+
35+
thing::Config config = {
36+
FIREBASE_HOST,
37+
FIREBASE_AUTH,
38+
FIREBASE_PATH,
39+
WIFI_SSID,
40+
WIFI_PASSWORD,
41+
DIGITAL_OUT,
42+
DIGITAL_IN,
43+
ANALOG_OUT,
44+
ANALOG_IN};
45+
46+
void setup() {
47+
Serial.begin(9600);
48+
49+
// connect to wifi.
50+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
51+
Serial.print("connecting");
52+
while (WiFi.status() != WL_CONNECTED) {
53+
Serial.print(".");
54+
delay(500);
55+
}
56+
Serial.println();
57+
Serial.print("connected: ");
58+
Serial.println(WiFi.localIP());
59+
60+
pinMode(DIGITAL_IN, INPUT);
61+
pinMode(DIGITAL_OUT, OUTPUT);
62+
pinMode(ANALOG_IN, INPUT);
63+
pinMode(ANALOG_OUT, OUTPUT);
64+
}
65+
66+
void loop() {
67+
static thing::Transcriber tscribe(config);
68+
tscribe.Loop();
69+
delay(200);
70+
}

src/Firebase.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
133133
}
134134
}
135135

136+
FirebaseCall::~FirebaseCall() {
137+
if (http_) {
138+
http_->end();
139+
}
140+
}
141+
136142
const JsonObject& FirebaseCall::json() {
137143
//TODO(edcoyne): This is not efficient, we should do something smarter with
138144
//the buffers.
@@ -157,6 +163,7 @@ FirebaseSet::FirebaseSet(const std::string& host, const std::string& auth,
157163
json_ = response();
158164
}
159165
}
166+
160167
// FirebasePush
161168
FirebasePush::FirebasePush(const std::string& host, const std::string& auth,
162169
const std::string& path, const std::string& value,

src/Firebase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class FirebaseCall {
7979
const char* method, const std::string& path,
8080
const std::string& data = "",
8181
FirebaseHttpClient* http = NULL);
82-
virtual ~FirebaseCall() {}
82+
virtual ~FirebaseCall();
8383

8484
virtual const FirebaseError& error() const {
8585
return error_;

src/thing/Transcriber.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ void Transcriber::UpdateConfig(const Config& config) {
1919
}
2020

2121
void Transcriber::Init(const Config& config) {
22+
path_ = config.path;
2223
pin_digital_out_ = config.pin_digital_out;
2324
pin_digital_in_ = config.pin_digital_in;
2425
pin_analog_out_ = config.pin_analog_out;
2526
pin_analog_in_ = config.pin_analog_in;
2627

2728
fbase_.reset(new Firebase(config.host, config.auth));
28-
stream_ = fbase_->streamPtr(config.path);
29+
stream_ = fbase_->streamPtr(path_);
30+
}
31+
32+
void Transcriber::SetValue(const std::string& path, const std::string& value) {
33+
stream_.reset(nullptr);
34+
fbase_->set(path, value);
35+
stream_ = fbase_->streamPtr(path_);
2936
}
3037

3138
void Transcriber::Loop() {
@@ -47,15 +54,13 @@ void Transcriber::Loop() {
4754
// Send values to cloud
4855
int digital_in = digitalRead(pin_digital_in_);
4956
if (digital_in != digital_in_) {
50-
Serial.println(String(digital_in).c_str());
51-
FirebaseSet set = fbase_->set(kSubPathDigitalIn, String(digital_in).c_str());
57+
SetValue(kSubPathDigitalIn, String(digital_in).c_str());
5258
digital_in_ = digital_in;
5359
}
5460

5561
float analog_in = analogRead(pin_analog_in_);
5662
if (analog_in != analog_in_) {
57-
Serial.println(String(analog_in).c_str());
58-
FirebaseSet set = fbase_->set(kSubPathAnalogIn, String(analog_in).c_str());
63+
SetValue(kSubPathAnalogIn, String(analog_in).c_str());
5964
analog_in_ = analog_in;
6065
}
6166
}

src/thing/Transcriber.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Transcriber {
4040
void ProcessInitialUpdate(const FirebaseObject& update);
4141
void ProcessIncrementalUpdate(const FirebaseObject& update);
4242

43+
// Wrapper around Firebase::Set that stops the stream before setting,
44+
// Currently we have memory issues if we try to have two simultanious
45+
// SSL connections.
46+
void SetValue(const std::string& path, const std::string& value);
47+
4348
std::unique_ptr<Firebase> fbase_;
4449
std::unique_ptr<FirebaseStream> stream_;
4550

@@ -50,6 +55,8 @@ class Transcriber {
5055
int pin_digital_in_;
5156
int pin_analog_out_;
5257
int pin_analog_in_;
58+
59+
std::string path_;
5360
};
5461

5562
}; // thing

0 commit comments

Comments
 (0)