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

Commit 2e2a87d

Browse files
authored
Merge pull request #234 from ed7coyne/firethings
Add main class for Firething and example.
2 parents baf8f89 + 67c880d commit 2e2a87d

13 files changed

+330
-107
lines changed

examples/FirebaseTranscriber_ESP8266/FirebaseTranscriber_ESP8266.ino

Lines changed: 0 additions & 73 deletions
This file was deleted.
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+
// FirethingDemo_ESP8266 is a sample that demos operation of the firething
18+
// portion of this library. This is a firmware for the esp that acts as
19+
// a bridge between pins on the esp and a firebase database. This includes
20+
// a captive configuration portal.
21+
22+
23+
#include <ESP8266WiFi.h>
24+
#include <Thing.h>
25+
26+
// No config variables.
27+
// Everything is handled through portal.
28+
29+
thing::FireThing fire_thing;
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
Serial.println("Firething starting up...");
34+
fire_thing.SetDebugHandler([](const char* message) { Serial.println(message); });
35+
fire_thing.Setup();
36+
}
37+
38+
void loop() {
39+
fire_thing.Loop();
40+
41+
Serial.println(".");
42+
delay(250);
43+
}

src/Thing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "thing/FireThing.h"
12
#include "thing/Transcriber.h"
23
#include "thing/Portal.h"
34
#include "thing/WiFiManager.h"

src/thing/Config.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "Arduino.h"
2+
#include "thing/Config.h"
3+
#include "third-party/arduino-json-5.6.7/include/ArduinoJson.h"
4+
5+
namespace thing {
6+
7+
void Config::SerializeToJson(Stream* output, std::function<void(int size)> handle_size) const {
8+
DynamicJsonBuffer jsonBuffer;
9+
JsonObject& root = jsonBuffer.createObject();
10+
root["host"] = host.c_str();
11+
root["auth"] = auth.c_str();
12+
root["path"] = path.c_str();
13+
root["wifi_ssid"] = wifi_ssid.c_str();
14+
root["wifi_key"] = wifi_key.c_str();
15+
root["analog_activation"] = analog_activation_threshold;
16+
root["wifi_connect_attempts"] = wifi_connect_attempts;
17+
18+
JsonObject& pins_root = root.createNestedObject("pins");
19+
pins_root["digital_in"] = pins.digital_in;
20+
pins_root["digital_out"] = pins.digital_out;
21+
pins_root["analog_in"] = pins.analog_in;
22+
pins_root["analog_out"] = pins.analog_out;
23+
pins_root["config_mode_button"] = pins.config_mode_button;
24+
25+
handle_size(root.measureLength());
26+
root.printTo(*output);
27+
}
28+
29+
void Config::ReadFromJson(char* string) {
30+
DynamicJsonBuffer jsonBuffer;
31+
JsonObject& root = jsonBuffer.parseObject(string);
32+
host = root["host"].asString();
33+
auth = root["auth"].asString();
34+
path = root["path"].asString();
35+
wifi_ssid = root["wifi_ssid"].asString();
36+
wifi_key = root["wifi_key"].asString();
37+
analog_activation_threshold = root["activation_threshold"];
38+
wifi_connect_attempts = root["wifi_connect_attempts"];
39+
40+
pins.digital_in = root["pins"]["digital_in"];
41+
pins.digital_out = root["pins"]["digital_out"];
42+
pins.analog_in = root["pins"]["analog_in"];
43+
pins.analog_out = root["pins"]["analog_out"];
44+
pins.config_mode_button = root["pins"]["config_mode_button"];
45+
}
46+
47+
};
48+

src/thing/Config.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#ifndef THING_CONFIG_H
22
#define THING_CONFIG_H
33

4+
#include "Arduino.h"
5+
#include <string>
6+
#include <functional>
7+
48
namespace thing {
59

10+
struct Pins {
11+
int digital_in;
12+
int digital_out;
13+
int analog_in;
14+
int analog_out;
15+
int config_mode_button;
16+
};
17+
618
struct Config {
719
std::string host;
820
std::string auth;
@@ -14,11 +26,14 @@ struct Config {
1426
// If the change is analog value is less than this amount we don't send an
1527
// update.
1628
float analog_activation_threshold;
29+
int wifi_connect_attempts;
30+
31+
Pins pins;
32+
33+
void SerializeToJson(Stream* output, std::function<void(int size)> handle_size) const;
1734

18-
int pin_digital_in;
19-
int pin_digital_out;
20-
int pin_analog_in;
21-
int pin_analog_out;
35+
// We need a mutable char array here, otherwise a copy will be made.
36+
void ReadFromJson(char* string);
2237
};
2338

2439
} // namespace thing

src/thing/FireThing.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "thing/FireThing.h"
2+
#include "Arduino.h"
3+
#include "FS.h"
4+
5+
namespace thing {
6+
namespace {
7+
8+
Config kDefaultConfig = {
9+
"", // firebase host
10+
"", // firebase auth
11+
"/fthing", // path in firebase
12+
"", // wifi ssid
13+
"", // wifi key
14+
0.1, // analog activation threshold
15+
D1, // digital in
16+
BUILTIN_LED, // digital out
17+
A0, // analog in
18+
D1, // analog out
19+
D0, // config mode button
20+
};
21+
22+
const char kStorageFilename[] = "fthing.cfg";
23+
24+
} // namespace
25+
26+
FireThing::FireThing() : debug_([](const char*) {}) {}
27+
28+
bool FireThing::Setup() {
29+
Config config;
30+
if (!ReadConfigFromStorage(&config)) {
31+
debug_("Failed to read config from storage.");
32+
return false;
33+
}
34+
SetPinModes(config);
35+
36+
if (digitalRead(config.pins.config_mode_button) || !ConnectToWiFi(config)) {
37+
wifi_.StartAP();
38+
}
39+
40+
portal_.NotifyOnUpdate([this](const Config& config) {
41+
if (!WriteConfigToStorage(config)) {
42+
debug_("Failed to write config to storage.");
43+
}
44+
SetPinModes(config);
45+
transcriber_.UpdateConfig(config);
46+
ConnectToWiFi(config);
47+
});
48+
portal_.Start(config);
49+
}
50+
51+
void FireThing::Loop() {
52+
wifi_.Loop();
53+
portal_.Loop();
54+
transcriber_.Loop();
55+
}
56+
57+
bool FireThing::ConnectToWiFi(const Config& config) {
58+
debug_("Connecting to wifi:");
59+
debug_(config.wifi_ssid.c_str());
60+
debug_(config.wifi_key.c_str());
61+
if (wifi_.Connect(config.wifi_ssid, config.wifi_key)) {
62+
debug_("Connected");
63+
return true;
64+
}
65+
debug_("Failed to Connect.");
66+
return false;
67+
}
68+
69+
void FireThing::SetPinModes(const Config& config) {
70+
pinMode(config.pins.digital_in, INPUT);
71+
pinMode(config.pins.digital_out, OUTPUT);
72+
pinMode(config.pins.analog_in, INPUT);
73+
pinMode(config.pins.analog_out, OUTPUT);
74+
75+
pinMode(config.pins.config_mode_button, INPUT);
76+
}
77+
78+
bool FireThing::ReadConfigFromStorage(Config* config) {
79+
if (!SPIFFS.begin()) {
80+
debug_("Failed to mount FS.");
81+
return false;
82+
}
83+
84+
if (!SPIFFS.exists(kStorageFilename)) {
85+
debug_("Config not found, using default.");
86+
*config = kDefaultConfig;
87+
} else {
88+
File cfg = SPIFFS.open(kStorageFilename, "r");
89+
if (!cfg) {
90+
debug_("Failed to open config for read");
91+
SPIFFS.end();
92+
return false;
93+
}
94+
char buffer[cfg.size()];
95+
cfg.readBytes(buffer, cfg.size());
96+
config->ReadFromJson(buffer);
97+
debug_("Config read from disk.");
98+
}
99+
100+
SPIFFS.end();
101+
return true;
102+
}
103+
104+
bool FireThing::WriteConfigToStorage(const Config& config) {
105+
if (!SPIFFS.begin()) {
106+
debug_("Failed to mount FS.");
107+
return false;
108+
}
109+
110+
File cfg = SPIFFS.open(kStorageFilename, "w");
111+
if (!cfg) {
112+
debug_("Failed to open config for write");
113+
SPIFFS.end();
114+
return false;
115+
}
116+
config.SerializeToJson(&cfg, [](int){});
117+
118+
SPIFFS.end();
119+
return true;
120+
}
121+
122+
void FireThing::SetDebugHandler(std::function<void(const char* message)> debug) {
123+
debug_ = debug;
124+
wifi_.SetDebugHandler(debug);
125+
portal_.SetDebugHandler(debug);
126+
transcriber_.SetDebugHandler(debug);
127+
}
128+
129+
} // namespace thing

src/thing/FireThing.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef THING_FIRETHING_H
2+
#define THING_FIRETHING_H
3+
4+
#include "thing/Config.h"
5+
#include "thing/WiFiManager.h"
6+
#include "thing/Transcriber.h"
7+
#include "thing/Portal.h"
8+
9+
namespace thing {
10+
11+
// Operates the device as a FireThing which manages a connection
12+
// between several pins and a firebase database. The configuration
13+
// will be read from and persisted too flash.
14+
class FireThing {
15+
public:
16+
FireThing();
17+
bool Setup();
18+
void Loop();
19+
20+
void SetDebugHandler(std::function<void(const char* message)> debug);
21+
22+
private:
23+
bool ReadConfigFromStorage(Config* config);
24+
bool WriteConfigToStorage(const Config& config);
25+
26+
bool ConnectToWiFi(const Config& config);
27+
void SetPinModes(const Config& config);
28+
29+
WiFiManager wifi_;
30+
Portal portal_;
31+
Transcriber transcriber_;
32+
std::function<void(const char* message)> debug_;
33+
};
34+
35+
} // namespace thing
36+
37+
38+
#endif // THING_FIRETHING_H

0 commit comments

Comments
 (0)