This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 491
Add Transcriber and Portal objects. #215
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
61b6883
Initial version of transcriber, works but can't write and read :(
ed7coyne 1bc895e
Stop stream when we set value to avoid memory issues. Added example.
ed7coyne 055a72d
Merge remote-tracking branch 'upstream/master' into firethings
ed7coyne afa5ea4
Bug fixes, update ArduinoJson path, added base path for updates
ed7coyne a295c1f
Fixed begin command, should have been broken before?
ed7coyne ac95169
Make init match standard init for example
ed7coyne 7069d23
Make init match standard init for example
ed7coyne 7a01167
remove unneeded std::string instantiations
ed7coyne 460ba3d
merge Portal in from branch
ed7coyne 63551e8
merged other changes
ed7coyne 7581ba5
Added analog_activation_threshold
ed7coyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
examples/FirebaseTranscriber_ESP8266/FirebaseTranscriber_ESP8266.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// 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. | ||
// | ||
|
||
// FirebaseDemo_ESP8266 is a sample that binds several pins to paths in | ||
// firebase using the transcriber object. | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <Transcriber.h> | ||
|
||
// Set these to run example. | ||
#define FIREBASE_HOST "example.firebaseio.com" | ||
#define FIREBASE_AUTH "token_or_secret" | ||
#define WIFI_SSID "SSID" | ||
#define WIFI_PASSWORD "PASSWORD" | ||
|
||
#define FIREBASE_PATH "/fthing" | ||
#define DIGITAL_IN D1 | ||
#define DIGITAL_OUT BUILTIN_LED | ||
#define ANALOG_IN A0 | ||
#define ANALOG_OUT D8 | ||
#define ANALOG_ACTIVATION_THRESHOLD 0.1 | ||
|
||
|
||
thing::Config config = { | ||
FIREBASE_HOST, | ||
FIREBASE_AUTH, | ||
FIREBASE_PATH, | ||
WIFI_SSID, | ||
WIFI_PASSWORD, | ||
ANALOG_ACTIVATION_THRESHOLD, | ||
DIGITAL_IN, | ||
DIGITAL_OUT, | ||
ANALOG_IN, | ||
ANALOG_OUT}; | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
|
||
// connect to wifi. | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
Serial.print("connecting"); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
Serial.print("."); | ||
delay(500); | ||
} | ||
Serial.println(); | ||
Serial.print("connected: "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
pinMode(DIGITAL_IN, INPUT); | ||
pinMode(DIGITAL_OUT, OUTPUT); | ||
pinMode(ANALOG_IN, INPUT); | ||
pinMode(ANALOG_OUT, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
static thing::Transcriber tscribe(config); | ||
tscribe.Loop(); | ||
delay(200); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "thing/Portal.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "thing/Transcriber.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef THING_CONFIG_H | ||
#define THING_CONFIG_H | ||
|
||
namespace thing { | ||
|
||
struct Config { | ||
std::string host; | ||
std::string auth; | ||
std::string path; | ||
|
||
std::string wifi_ssid; | ||
std::string wifi_key; | ||
|
||
// If the change is analog value is less than this amount we don't send an | ||
// update. | ||
float analog_activation_threshold; | ||
|
||
int pin_digital_in; | ||
int pin_digital_out; | ||
int pin_analog_in; | ||
int pin_analog_out; | ||
}; | ||
|
||
} // namespace thing | ||
|
||
#endif // THING_CONFIG_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include "thing/Portal.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use https://github.com/tzapu/WiFiManager instead or rolling something on our own? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm maybe we could evaluate it at least. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless there are issues with what is presented here, let's get this checked in and I will evaluate the WiFiManager this friday and let you know what I think. |
||
#include "third-party/arduino-json-5.6.7/include/ArduinoJson.h" | ||
|
||
namespace thing { | ||
|
||
Portal::Portal(const Config& config) : config_(config), callback_([](const Config&){}) {} | ||
|
||
void Portal::Start() { | ||
server_.on("/", [&] () { | ||
static const PROGMEM char page[] = R"( | ||
<head> | ||
<script> | ||
function fetch_config() { | ||
document.getElementById('loading').style.display='inline' | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "/config", true); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState==4 && xhr.status==200) { | ||
populate_values(JSON.parse(xhr.responseText)); | ||
document.getElementById('loading').style.display='none' | ||
} | ||
} | ||
xhr.send(); | ||
} | ||
function populate_values(config) { | ||
document.getElementById('host').value = config.host; | ||
document.getElementById('auth').value = config.auth; | ||
document.getElementById('path').value = config.path; | ||
document.getElementById('ssid').value = config.wifi_ssid; | ||
document.getElementById('key').value = config.wifi_key; | ||
} | ||
function build_config() { | ||
var config = { | ||
host : document.getElementById('host').value, | ||
auth : document.getElementById('auth').value, | ||
path : document.getElementById('path').value, | ||
wifi_ssid : document.getElementById('ssid').value, | ||
wifi_key : document.getElementById('key').value | ||
}; | ||
return config; | ||
} | ||
function send_config() { | ||
document.getElementById('saving').style.display='inline' | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("POST", "/config", true); | ||
xhr.send("config=" + JSON.stringify(build_config())); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState==4 && xhr.status==200) { | ||
document.getElementById('saving').style.display='none' | ||
} | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<div>Host: <input id='host'></div> | ||
<div>Auth: <input id='auth'></div> | ||
<div>Path: <input id='path'></div> | ||
<div>Wifi SSID: <input id='ssid'></div> | ||
<div>Wifi Key: <input id='key'></div> | ||
<div><button onclick='send_config();'>Save</button></div> | ||
<div id='loading'>Loading....</div> | ||
<div id='saving' style='display:none'>Saving....</div> | ||
<script>fetch_config();</script> | ||
</body> | ||
)"; | ||
static const PROGMEM char type[] = "text/html"; | ||
|
||
server_.send_P(200, type, page); | ||
}); | ||
|
||
server_.on("/config", [&] () { | ||
DynamicJsonBuffer jsonBuffer; | ||
if (server_.method() == HTTP_GET) { | ||
JsonObject& root = jsonBuffer.createObject(); | ||
root["host"] = config_.host.c_str(); | ||
root["auth"] = config_.auth.c_str(); | ||
root["path"] = config_.path.c_str(); | ||
root["wifi_ssid"] = config_.wifi_ssid.c_str(); | ||
root["wifi_key"] = config_.wifi_key.c_str(); | ||
|
||
String buffer; | ||
root.printTo(buffer); | ||
server_.send(200, "application/json", buffer); | ||
} else if (server_.method() == HTTP_POST) { | ||
if (!server_.hasArg("config")) { | ||
server_.send(500, "text/plain", "Missing config.\r\n"); | ||
return; | ||
} | ||
String config = server_.arg("config"); | ||
JsonObject& root = jsonBuffer.parseObject(config.c_str()); | ||
config_.host = root["host"].asString(); | ||
config_.auth = root["auth"].asString(); | ||
config_.path = root["path"].asString(); | ||
config_.wifi_ssid = root["wifi_ssid"].asString(); | ||
config_.wifi_key = root["wifi_key"].asString(); | ||
callback_(config_); | ||
server_.send(200, "text/plain", ""); | ||
} | ||
}); | ||
server_.begin(); | ||
} | ||
|
||
void Portal::Loop() { | ||
server_.handleClient(); | ||
} | ||
|
||
void Portal::NotifyOnUpdate(std::function<void(const Config& config)> callback) { | ||
callback_ = callback; | ||
} | ||
|
||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef THING_PORTAL_H | ||
#define THING_PORTAL_H | ||
|
||
#include "ESP8266WebServer.h" | ||
#include "thing/Config.h" | ||
|
||
namespace thing { | ||
|
||
class Portal { | ||
public: | ||
Portal(const Config& config); | ||
|
||
void Start(); | ||
void Loop(); | ||
void NotifyOnUpdate(std::function<void(const Config& config)> cb); | ||
|
||
private: | ||
Config config_; | ||
ESP8266WebServer server_; | ||
std::function<void(const Config& config)> callback_; | ||
|
||
}; | ||
|
||
} // namespace thing | ||
|
||
#endif // THING_PORTAL_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should keep the namespace in the Arduino surface, iirc, it's not very common.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I think this has to do with splitting the library, This example isn't really meant to be arduino developer friendly (like the config struct isn't too friendly), the example is just to give people a chance to play with the Transcriber outside of the rest of the system.