Skip to content

Commit be94002

Browse files
committed
Use config
1 parent 63efab0 commit be94002

File tree

2 files changed

+78
-48
lines changed

2 files changed

+78
-48
lines changed

src/ConfigEntry.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ class ConfigEntry : public MqttEntry {
1515
key.replace(".", "/");
1616
return get(key).getValue();
1717
}
18+
19+
const char* getCString(String key, String defaultValue) {
20+
return getString(key, defaultValue).c_str();
21+
}
22+
23+
int getInt(String key, int defaultValue) {
24+
return getString(key, String(defaultValue)).toInt();
25+
}
26+
27+
void set(String key, String value) {
28+
key.replace(".", "/");
29+
get(key).setValue(value);
30+
}
1831

1932
void reset() {
2033
SPIFFS.remove("/config.cfg");

src/EasyMqtt.h

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,55 @@ class EasyMqtt : public MqttEntry {
1313
PubSubClient mqttClient;
1414
WebPortal webPortal;
1515

16-
ConfigEntry* config;
16+
ConfigEntry* configEntry;
1717

1818
String deviceId = "deviceId";
19-
const char* mqtt_username = "N/A";
20-
const char* mqtt_password = "N/A";
2119

2220
protected:
2321
/**
2422
Handle connections to mqtt
2523
*/
26-
void mqttReconnect() {
27-
// if(WiFi.status() != WL_CONNECTED) {
28-
while (!mqttClient.connected()) {
29-
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
24+
void connect() {
25+
if(WiFi.status() != WL_CONNECTED) {
26+
const char* ssid = config().getCString("wifi.ssid", "");
27+
const char* password = config().getCString("wifi.password", "");
28+
WiFi.mode(WIFI_STA);
29+
WiFi.begin(ssid, password);
30+
31+
int timer = 0;
32+
while (WiFi.status() != WL_CONNECTED && timer < 10) {
33+
delay(500);
34+
timer++;
35+
// ToDo: handle timeout, and create AP
36+
}
37+
if(timer < 10) {
38+
debug("WiFi connected");
39+
} else {
40+
debug("WiFi connection timeout");
41+
// ToDo: create AP
42+
}
43+
debug("IP address", WiFi.localIP().toString());
44+
debug("devideId", deviceId);
45+
webPortal.setup(*this);
46+
}
47+
if(mqttClient.state() == MQTT_DISCONNECTED) {
48+
// Setup MQTT
49+
const char* host = config().getCString("mqtt.host", "");
50+
int port = config().getInt("mqtt.port", 1883);
51+
mqttClient.setClient(wifiClient);
52+
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
53+
each([=](MqttEntry* entry){
54+
entry->callback(topic, payload, length);
55+
});
56+
});
57+
mqttClient.setServer(host, port);
58+
}
59+
if (!mqttClient.connected()) {
60+
const char* username = config().getCString("mqtt.username", "");
61+
const char* password = config().getCString("mqtt.password", "");
62+
if (mqttClient.connect(deviceId.c_str(), username, password)) {
3063
debug("Connected to MQTT");
64+
debug("Topic", getTopic());
3165
each([&](MqttEntry* entry){
3266
if (entry->isOut()) {
3367
mqttClient.subscribe(entry->getTopic().c_str());
@@ -42,12 +76,11 @@ class EasyMqtt : public MqttEntry {
4276

4377
public:
4478
EasyMqtt() : MqttEntry("easyMqtt", mqttClient) {
45-
Serial.begin(115200);
4679
deviceId = String(ESP.getChipId());
4780

4881
debug("test");
49-
config = new ConfigEntry(mqttClient, *this);
50-
addChild(config);
82+
configEntry = new ConfigEntry(mqttClient, *this);
83+
addChild(configEntry);
5184

5285
get("system").setInterval(30);
5386
get("system")["deviceId"] << [this]() {
@@ -59,6 +92,18 @@ class EasyMqtt : public MqttEntry {
5992
get("system")["uptime"] << []() {
6093
return String(millis() / 1000);
6194
};
95+
96+
// Setup wifi diag
97+
get("system")["wifi"]["rssi"] << []() {
98+
return String(WiFi.RSSI());
99+
};
100+
get("system")["wifi"]["ssid"] << []() {
101+
return WiFi.SSID();
102+
};
103+
get("system")["wifi"]["ip"] << []() {
104+
return WiFi.localIP().toString();
105+
};
106+
62107
get("system")["restart"] >> [this](String value) {
63108
if(value == "restart") {
64109
debug("Restart");
@@ -67,13 +112,13 @@ class EasyMqtt : public MqttEntry {
67112
};
68113
get("system")["config"]["reset"] >> [this](String value) {
69114
if(value == "reset") {
70-
getConfig()->reset();
115+
config().reset();
71116
}
72117
};
73118
}
74119

75-
ConfigEntry* getConfig() {
76-
return config;
120+
ConfigEntry & config() {
121+
return *configEntry;
77122
}
78123

79124
void debug(String msg) {
@@ -101,53 +146,25 @@ class EasyMqtt : public MqttEntry {
101146
Setup connection to wifi
102147
*/
103148
void wifi(const char* ssid, const char* password) {
104-
WiFi.mode(WIFI_STA);
105-
WiFi.begin(ssid, password);
106-
107-
while (WiFi.status() != WL_CONNECTED) {
108-
delay(500);
109-
// ToDo: handle timeout, and create AP
110-
}
111-
debug("WiFi connected");
112-
debug("IP address", WiFi.localIP().toString());
113-
debug("devideId", deviceId);
114-
115-
// Setup wifi diag
116-
get("system")["wifi"]["rssi"] << []() {
117-
return String(WiFi.RSSI());
118-
};
119-
get("system")["wifi"]["ssid"] << []() {
120-
return WiFi.SSID();
121-
};
122-
get("system")["wifi"]["ip"] << []() {
123-
return WiFi.localIP().toString();
124-
};
125-
126-
webPortal.setup(*this);
149+
config().set("wifi.ssid", ssid);
150+
config().set("wifi.password", password);
127151
}
128152

129153
/**
130154
Setup connection to mqtt
131155
*/
132156
void mqtt(const char* host, int port, const char* username, const char* password) {
133-
mqttClient.setClient(wifiClient);
134-
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
135-
each([=](MqttEntry* entry){
136-
entry->callback(topic, payload, length);
137-
});
138-
});
139-
mqttClient.setServer(host, port);
140-
mqtt_username = username;
141-
mqtt_password = password;
142-
143-
debug("Topic", getTopic());
157+
config().set("mqtt.host", host);
158+
config().set("mqtt.port", String(port));
159+
config().set("mqtt.username", username);
160+
config().set("mqtt.password", password);
144161
}
145162

146163
/**
147164
Handle the normal loop
148165
*/
149166
void loop() {
150-
mqttReconnect();
167+
connect();
151168
mqttClient.loop();
152169
webPortal.loop();
153170
each([](MqttEntry* entry){

0 commit comments

Comments
 (0)