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

Commit 3d03b52

Browse files
committed
Add base for SerialTransciever.
Getting serial example together Cleaning up Changes to make both arduino and tests compile All existing examples work now. changes to get SerialHost building Fixed up Stream example Fixed up Stream example Fixes to get FirebaseSerialHost.ino working! Switch to huzzah module. All examples are working Add instructions to example and test files. unit tests are all working with new changes Cleanup comments remove arduino-mock Revert "remove arduino-mock" This reverts commit e7aa75f. remove old arduino-mock Add modified submodule Added stream command using simple syntax. Fixes from testing with serial modem
1 parent 7a5c827 commit 3d03b52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+513
-106
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
url = https://github.com/google/googletest.git
44
[submodule "test/arduino-mock"]
55
path = test/arduino-mock
6-
url = https://github.com/ikeyasu/arduino-mock.git
6+
url = https://github.com/ed7coyne/arduino-mock.git

FirebaseHttpClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "Stream.h"
66

77
struct HttpStatus {
8-
static const int TEMPORARY_REDIRECT = 302;
8+
static const int TEMPORARY_REDIRECT = 307;
99
};
1010

1111
class FirebaseHttpClient {
@@ -19,7 +19,7 @@ class FirebaseHttpClient {
1919
virtual void end() = 0;
2020

2121
virtual void addHeader(const String& name, const String& value) = 0;
22-
virtual void collectHeaders(const String header_keys[],
22+
virtual void collectHeaders(const char* header_keys[],
2323
const int header_key_count) = 0;
2424
virtual String header(const String& name) = 0;
2525

FirebaseHttpClient_Esp8266.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

22
#include "FirebaseHttpClient.h"
3-
#include <ESP8266HTTPClient.h>
4-
#include <ESP8266WiFi.h>
3+
4+
// The ordering of these includes matters greatly.
55
#include <WiFiClientSecure.h>
6+
#include <ESP8266WiFi.h>
7+
#include <ESP8266HTTPClient.h>
68

79
// Detect whether stable version of HTTP library is installed instead of
810
// master branch and patch in missing status and methods.
@@ -11,13 +13,12 @@
1113
#define USE_ESP_ARDUINO_CORE_2_0_0
1214
#endif
1315

14-
1516
class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
1617
public:
1718
FirebaseHttpClientEsp8266() {}
1819

19-
void SetReuseConnection(bool reuse) override {
20-
http_.reuse(reuse);
20+
void setReuseConnection(bool reuse) override {
21+
http_.setReuse(reuse);
2122
}
2223

2324
void begin(const String& url) override {
@@ -36,16 +37,16 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
3637
http_.addHeader(name, value);
3738
}
3839

39-
void collectHeaders(const String[]& header_keys, const int count) override {
40+
void collectHeaders(const char* header_keys[], const int count) override {
4041
http_.collectHeaders(header_keys, count);
4142
}
4243

4344
String header(const String& name) override {
44-
return http_.header(name);
45+
return http_.header(name.c_str());
4546
}
4647

4748
int sendRequest(const String& method, const String& data) override {
48-
return http_.sendRequest(method, data);
49+
return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length());
4950
}
5051

5152
String getString() override {
@@ -56,19 +57,19 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
5657
return http_.getStreamPtr();
5758
}
5859

59-
String errorToStream(int error_code) override {
60+
String errorToString(int error_code) override {
6061
#ifdef USE_ESP_ARDUINO_CORE_2_0_0
61-
return String(status);
62+
return String(error_code);
6263
#else
63-
return HTTPClient::errorToString(status);
64+
return HTTPClient::errorToString(error_code);
6465
#endif
6566
}
6667

6768
private:
6869
HTTPClient http_;
6970
};
7071

71-
Firebase* FirebaseHttpClient::create() {
72+
FirebaseHttpClient* FirebaseHttpClient::create() {
7273
return new FirebaseHttpClientEsp8266();
7374
}
7475

SerialTransceiver.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "SerialTransceiver.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
void SerialTransceiver::begin(Stream* serial) {
7+
std::unique_ptr<Firebase> fbase;
8+
9+
in_.reset(new ArduinoInputStream(serial));
10+
out_.reset(new ArduinoOutputStream(serial));
11+
}
12+
13+
void SerialTransceiver::loop() {
14+
String command_name = in_->readStringUntil(' ');
15+
16+
if (command_name.length() == 0 // Generally means a timeout has occured.
17+
|| command_name == "\n") {
18+
return;
19+
}
20+
21+
if (command_name == "BEGIN") {
22+
BeginCommand command;
23+
if (command.execute(command_name, in_.get(), out_.get())) {
24+
fbase_ = std::move(command.firebase());
25+
}
26+
return;
27+
} else if (!fbase_) {
28+
in_->drain();
29+
out_->println("-FAIL Must call BEGIN before anything else.");
30+
return;
31+
}
32+
33+
std::unique_ptr<Command> command = CreateCommand(command_name, fbase_.get());
34+
if (!command) {
35+
in_->drain();
36+
out_->println(String("-FAIL Invalid command '") + command_name + "'." );
37+
return;
38+
}
39+
40+
command->execute(command_name, in_.get(), out_.get());
41+
}
42+
43+
std::unique_ptr<Command> SerialTransceiver::CreateCommand(const String& text,
44+
Firebase* fbase) {
45+
std::unique_ptr<Command> command;
46+
if (text == "GET") {
47+
command.reset(new GetCommand(fbase));
48+
} else if (text == "SET") {
49+
command.reset(new SetCommand(fbase));
50+
} else if (text == "PUSH") {
51+
command.reset(new PushCommand(fbase));
52+
} else if (text == "REMOVE") {
53+
command.reset(new RemoveCommand(fbase));
54+
} else if (text == "BEGIN_STREAM") {
55+
command.reset(new StreamCommand(fbase));
56+
}
57+
return command;
58+
}
59+
60+
} // modem
61+
} // firebase

SerialTransceiver.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef MODEM_SERIAL_TRANSCIEVER_H
2+
#define MODEM_SERIAL_TRANSCIEVER_H
3+
4+
#include <memory>
5+
6+
#include "Firebase.h"
7+
#include "modem/commands.h"
8+
9+
namespace firebase {
10+
namespace modem {
11+
12+
class SerialTransceiver {
13+
public:
14+
void begin(Stream* serial);
15+
void loop();
16+
17+
private:
18+
std::unique_ptr<Command> CreateCommand(const String& name, Firebase* fbase);
19+
20+
std::unique_ptr<Firebase> fbase_;
21+
std::unique_ptr<ArduinoInputStream> in_;
22+
std::unique_ptr<ArduinoOutputStream> out_;
23+
};
24+
25+
} // modem
26+
} // firebase
27+
28+
#endif // MODEM_SERIAL_TRANSCIEVER_H

examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
// FirebasePush_ESP8266 is a sample that push a new timestamp to firebase
1818
// on each reset.
1919

20+
#include <ESP8266WiFi.h>
21+
2022
#include <Firebase.h>
2123

2224
// create firebase client.
23-
Firebase fbase = Firebase("example.firebaseio.com")
24-
.auth("secret_or_token");
25+
Firebase fbase("example.firebaseio.com")
26+
.auth("secret_or_token");
27+
2528
void setup() {
2629
Serial.begin(9600);
2730

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
18+
// FirebasePush_ESP8266 is a sample that will start our serial transciever
19+
// listening on a software port and allow debug over the main serial port.
20+
//
21+
// A suggested setup for testing this example would be a USB to TTL cable
22+
// with the green wire connected to pin 5 and the white wire connected to
23+
// pin 4 (on the huzzah feather esp8266).
24+
// First edit begin.txt and put in your host and secret.
25+
// Then run the following commands to setup the serial port in linux:
26+
// stty -F /dev/ttyUSB0 9600 raw -echo -echoe -echok
27+
// Then on one console do:
28+
// cat /dev/ttyUSB0 &
29+
// This console will now read all responses from the modem. Then do:
30+
// cat begin.txt > /dev/ttyUSB0
31+
// You should see +OK and you can now feed in the other example commmands.
32+
33+
#include <SoftwareSerial.h>
34+
#include <ESP8266WiFi.h>
35+
36+
#include <Firebase.h>
37+
#include <SerialTransceiver.h>
38+
39+
SoftwareSerial data_serial(5 /*RX*/, 4/*TX*/);
40+
firebase::modem::SerialTransceiver transceiver;
41+
42+
void setup() {
43+
Serial.begin(9600);
44+
45+
// connect to wifi.
46+
WiFi.begin("SSID", "PASSWORD");
47+
Serial.print("connecting");
48+
while (WiFi.status() != WL_CONNECTED) {
49+
Serial.print(".");
50+
delay(500);
51+
}
52+
Serial.println();
53+
Serial.print("connected: ");
54+
Serial.println(WiFi.localIP());
55+
56+
data_serial.begin(9600);
57+
while (!data_serial) {
58+
Serial.println("Error initilizing serial.");
59+
delay(5000);
60+
}
61+
62+
transceiver.begin(&data_serial);
63+
}
64+
65+
void loop() {
66+
transceiver.loop();
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BEGIN $YOUR_HOST $YOUR_SECRET
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BEGIN_STREAM /serial/test
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
END_STREAM
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GET /serial/test
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET /serial/push_test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PUSH /seria/push_test "this is a test string \ "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REMOVE /serial/test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SET /serial/test this is a test string \ "
2+

examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// public Firebase and optionally display them on a OLED i2c screen.
1919

2020
#include <Firebase.h>
21+
#include <ESP8266WiFi.h>
2122
#include <Adafruit_GFX.h>
2223
#include <Adafruit_SSD1306.h>
2324
#include <ArduinoJson.h>
@@ -65,7 +66,7 @@ void loop() {
6566
Serial.println(event);
6667
JsonObject& json = buf.parseObject((char*)event.c_str());
6768
String path = json["path"];
68-
float data = json["data"];
69+
float data = json["data"]["last"];
6970

7071
// TODO(proppy): parse JSON object.
7172
display.clearDisplay();

modem/begin-command.cc renamed to modem/begin-command.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,24 @@ bool BeginCommand::execute(const String& command,
1717
String auth;
1818

1919
String data(in->readLine());
20-
// Remove leading ' '.
21-
data = data.substring(1);
2220

2321
int space_index = data.indexOf(' ');
2422
if (space_index == -1) {
2523
// host only.
2624
host = data;
2725
} else {
2826
// host and auth.
29-
host = data.substr(0, space_index);
30-
auth = data.substr(space_index + 1);
27+
host = data.substring(0, space_index);
28+
auth = data.substring(space_index + 1);
3129
}
3230

33-
if (host.empty()) {
31+
if (host.length() == 0) {
3432
out->println("-FAIL Missing host");
3533
return false;
3634
}
3735

3836
new_firebase_.reset(new Firebase(host));
39-
if (!auth.empty()) {
37+
if (auth.length() != 0) {
4038
new_firebase_->auth(auth);
4139
}
4240

modem/commands.h

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class BeginCommand : public Command {
6666
std::unique_ptr<Firebase> new_firebase_;
6767
};
6868

69+
class StreamCommand : public Command {
70+
public:
71+
StreamCommand(Firebase* fbase) : Command(fbase) {}
72+
73+
bool execute(const String& command, InputStream* in, OutputStream* out);
74+
};
75+
6976
} // modem
7077
} // firebase
7178

File renamed without changes.

0 commit comments

Comments
 (0)