Skip to content

Commit ea5614c

Browse files
author
Robert Strouse
committed
Add MQTT settings to backup/restore #284
1 parent 632dd39 commit ea5614c

12 files changed

+89
-47
lines changed

ConfigFile.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern Preferences pref;
99

10-
#define SHADE_HDR_VER 21
10+
#define SHADE_HDR_VER 22
1111
#define SHADE_HDR_SIZE 76
1212
#define SHADE_REC_SIZE 276
1313
#define GROUP_REC_SIZE 194
@@ -554,6 +554,9 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename,
554554
}
555555
}
556556
}
557+
else {
558+
this->file.seek(this->file.position() + this->header.repeaterRecordSize, SeekSet);
559+
}
557560
if(opts.settings) {
558561
// First read out the data.
559562
this->readSettingsRecord();
@@ -584,6 +587,7 @@ bool ShadeConfigFile::restoreFile(SomfyShadeController *s, const char *filename,
584587
}
585588
bool ShadeConfigFile::readNetRecord() {
586589
if(this->header.netRecordSize > 0) {
590+
uint32_t startPos = this->file.position();
587591
Serial.println("Reading network settings from file...");
588592
settings.connType = static_cast<conn_types>(this->readUInt8(static_cast<uint8_t>(conn_types::unset)));
589593
settings.IP.dhcp = this->readBool(true);
@@ -598,9 +602,18 @@ bool ShadeConfigFile::readNetRecord() {
598602
settings.IP.dns1.fromString(ip);
599603
this->readVarString(ip, sizeof(ip));
600604
settings.IP.dns2.fromString(ip);
605+
if(this->header.version >= 22) {
606+
this->readVarString(settings.MQTT.protocol, sizeof(settings.MQTT.protocol));
607+
this->readVarString(settings.MQTT.hostname, sizeof(settings.MQTT.hostname));
608+
settings.MQTT.port = this->readUInt16(1883);
609+
settings.MQTT.pubDisco = this->readBool(false);
610+
this->readVarString(settings.MQTT.rootTopic, sizeof(settings.MQTT.rootTopic));
611+
this->readVarString(settings.MQTT.discoTopic, sizeof(settings.MQTT.discoTopic));
612+
}
601613
// Now lets check to see if we are the same board. If we are then we will restore
602614
// the ethernet phy settings.
603615
if(strncmp(settings.serverId, this->header.serverId, sizeof(settings.serverId)) == 0) {
616+
Serial.println("Restoring Ethernet adapter settings");
604617
settings.Ethernet.boardType = this->readUInt8(1);
605618
settings.Ethernet.phyType = static_cast<eth_phy_type_t>(this->readUInt8(0));
606619
settings.Ethernet.CLKMode = static_cast<eth_clock_mode_t>(this->readUInt8(0));
@@ -609,16 +622,16 @@ bool ShadeConfigFile::readNetRecord() {
609622
settings.Ethernet.MDCPin = this->readInt8(16);
610623
settings.Ethernet.MDIOPin = this->readInt8(23);
611624
}
612-
else {
613-
// We are not going to get the network adapter settings.
614-
Serial.println("Skipping Ethernet adapter settings (Chip ids do not match)...");
625+
if(this->file.position() != startPos + this->header.netRecordSize) {
626+
Serial.println("Reading to end of network record");
615627
this->seekChar(CFG_REC_END);
616628
}
617629
}
618630
return true;
619631
}
620632
bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) {
621633
if(this->header.transRecordSize > 0) {
634+
uint32_t startPos = this->file.position();
622635
Serial.println("Reading Transceiver settings from file...");
623636
cfg.enabled = this->readBool(false);
624637
cfg.proto = static_cast<radio_proto>(this->readUInt8(0));
@@ -633,6 +646,11 @@ bool ShadeConfigFile::readTransRecord(transceiver_config_t &cfg) {
633646
cfg.rxBandwidth = this->readFloat(cfg.rxBandwidth);
634647
cfg.deviation = this->readFloat(cfg.deviation);
635648
cfg.txPower = this->readInt8(cfg.txPower);
649+
if(this->file.position() != startPos + this->header.transRecordSize) {
650+
Serial.println("Reading to end of transceiver record");
651+
this->seekChar(CFG_REC_END);
652+
}
653+
636654
}
637655
return true;
638656
}
@@ -947,6 +965,12 @@ bool ShadeConfigFile::writeNetRecord() {
947965
this->writeVarString(settings.IP.subnet.toString().c_str());
948966
this->writeVarString(settings.IP.dns1.toString().c_str());
949967
this->writeVarString(settings.IP.dns2.toString().c_str());
968+
this->writeVarString(settings.MQTT.protocol);
969+
this->writeVarString(settings.MQTT.hostname);
970+
this->writeUInt16(settings.MQTT.port);
971+
this->writeBool(settings.MQTT.pubDisco);
972+
this->writeVarString(settings.MQTT.rootTopic);
973+
this->writeVarString(settings.MQTT.discoTopic);
950974
this->writeUInt8(settings.Ethernet.boardType);
951975
this->writeUInt8(static_cast<uint8_t>(settings.Ethernet.phyType));
952976
this->writeUInt8(static_cast<uint8_t>(settings.Ethernet.CLKMode));

ConfigSettings.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ uint16_t ConfigSettings::calcNetRecSize() {
267267
+ this->IP.subnet.toString().length() + 3
268268
+ this->IP.dns1.toString().length() + 3
269269
+ this->IP.dns2.toString().length() + 3
270+
+ strlen(this->MQTT.protocol) + 3
271+
+ strlen(this->MQTT.hostname) + 3
272+
+ 6 // MQTT Port
273+
+ 6 // PubDisco
274+
+ strlen(this->MQTT.rootTopic) + 3
275+
+ strlen(this->MQTT.discoTopic) + 3
270276
+ 4 // ETH.boardType
271277
+ 4 // ETH.phyType
272278
+ 4 // ETH.clkMode

Network.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,46 +71,44 @@ void Network::loop() {
7171
mqtt.loop();
7272
}
7373
void Network::emitSockets() {
74-
if(WiFi.status() == WL_CONNECTED) {
75-
if(abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel) {
76-
char buf[128];
77-
snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel());
78-
sockEmit.sendToClients("wifiStrength", buf);
79-
this->lastRSSI = WiFi.RSSI();
80-
this->lastChannel = WiFi.channel();
81-
sockEmit.loop();
82-
}
83-
}
84-
else {
85-
if(this->connType == conn_types::ethernet && this->lastRSSI != -100 && this->lastChannel != -1) {
86-
sockEmit.sendToClients("wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}");
87-
this->lastRSSI = -100;
88-
this->lastChannel = -1;
89-
sockEmit.loop();
90-
}
74+
if(this->needsBroadcast || abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel) {
75+
this->emitSockets(255);
76+
sockEmit.loop();
77+
this->needsBroadcast = false;
9178
}
9279
}
9380
void Network::emitSockets(uint8_t num) {
9481
char buf[128];
95-
if(WiFi.status() == WL_CONNECTED) {
96-
snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel());
97-
sockEmit.sendToClient(num, "wifiStrength", buf);
98-
this->lastRSSI = WiFi.RSSI();
99-
this->lastChannel = WiFi.channel();
82+
if(this->connType == conn_types::ethernet) {
83+
snprintf(buf, sizeof(buf), "{\"connected\":%s,\"speed\":%d,\"fullduplex\":%s}", this->connected() ? "true" : "false", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false");
84+
if(num == 255)
85+
sockEmit.sendToClients("ethernet", buf);
86+
else
87+
sockEmit.sendToClient(num, "ethernet", buf);
10088
}
10189
else {
102-
if(this->connType == conn_types::ethernet && this->lastRSSI != -100 && this->lastChannel != -1)
103-
sockEmit.sendToClient(num, "wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}");
104-
this->lastRSSI = -100;
105-
this->lastChannel = -1;
106-
}
107-
if(this->connType == conn_types::ethernet) {
108-
snprintf(buf, sizeof(buf), "{\"connected\":true,\"speed\":%d,\"fullduplex\":%s}", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false");
109-
sockEmit.sendToClient(num, "ethernet", buf);
90+
if(WiFi.status() == WL_CONNECTED) {
91+
snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel());
92+
if(num == 255)
93+
sockEmit.sendToClients("wifiStrength", buf);
94+
else
95+
sockEmit.sendToClient(num, "wifiStrength", buf);
96+
this->lastRSSI = WiFi.RSSI();
97+
this->lastChannel = WiFi.channel();
98+
}
99+
else {
100+
if(num == 255) {
101+
sockEmit.sendToClients("wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}");
102+
sockEmit.sendToClients("ethernet", "{\"connected\":false,\"speed\":0,\"fullduplex\":false}");
103+
}
104+
else {
105+
sockEmit.sendToClient(num, "wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}");
106+
sockEmit.sendToClient(num, "ethernet", "{\"connected\":false,\"speed\":0,\"fullduplex\":false}");
107+
}
108+
this->lastRSSI = -100;
109+
this->lastChannel = -1;
110+
}
110111
}
111-
else
112-
sockEmit.sendToClient(num, "ethernet", "{\"connected\":false, \"speed\":0,\"fullduplex\":false}");
113-
114112
}
115113
void Network::setConnected(conn_types connType) {
116114
this->connType = connType;

Network.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Network {
1313
public:
1414
bool wifiFallback = false;
1515
bool softAPOpened = false;
16+
bool needsBroadcast = true;
1617
conn_types connType = conn_types::unset;
1718
bool connected();
1819
String ssid;

Sockets.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ void SocketEmitter::wsEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t
190190
Serial.printf("Socket [%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
191191
// Send all the current shade settings to the client.
192192
sockServer.sendTXT(num, "Connected");
193+
sockServer.loop();
193194
settings.emitSockets(num);
194195
somfy.emitState(num);
195-
net.emitSockets(num);
196196
git.emitUpdateCheck(num);
197+
net.emitSockets(num);
198+
sockServer.loop();
197199
}
198200
break;
199201
case WStype_TEXT:

SomfyController.ino.esp32.bin

336 Bytes
Binary file not shown.

SomfyController.ino.esp32s3.bin

336 Bytes
Binary file not shown.

SomfyController.littlefs.bin

0 Bytes
Binary file not shown.

Web.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ void Web::handleDiscovery(WebServer &server) {
818818
snprintf(g_content, sizeof(g_content), "{\"serverId\":\"%s\",\"version\":\"%s\",\"latest\":\"%s\",\"model\":\"%s\",\"hostname\":\"%s\",\"authType\":%d,\"permissions\":%d,\"chipModel\":\"%s\",\"connType\":\"%s\",\"checkForUpdate\":%s",
819819
settings.serverId, settings.fwVersion.name, git.latest.name, "ESPSomfyRTS", settings.hostname, static_cast<uint8_t>(settings.Security.type), settings.Security.permissions, settings.chipModel, connType, settings.checkForUpdate ? "true" : "false");
820820
server.send_P(200, _encoding_json, g_content);
821+
/*
822+
if(net.connType == conn_types::ethernet) {
823+
snprintf(g_content, sizeof(g_content), ",\"ethernet\":{\"connected\":true,\"speed\":%d,\"fullduplex\":%s}", ETH.linkSpeed(), ETH.fullDuplex() ? "true" : "false");
824+
server.sendContent(g_content);
825+
}
826+
else {
827+
snprintf(g_content, sizeof(g_content), ",\"wifi\":{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel());
828+
server.sendContent(g_content);
829+
}
830+
*/
821831
server.sendContent(",\"rooms\":");
822832
this->chunkRoomsResponse(server);
823833
server.sendContent(",\"shades\":");
@@ -826,6 +836,7 @@ void Web::handleDiscovery(WebServer &server) {
826836
this->chunkGroupsResponse(server);
827837
server.sendContent("}");
828838
server.sendContent("", 0);
839+
net.needsBroadcast = true;
829840
}
830841
else
831842
server.send(500, _encoding_text, "Invalid http method");

data/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1">
55
<meta charset="UTF-8">
6-
<link rel="stylesheet" href="main.css?v=2.4.0a" type="text/css" />
7-
<link rel="stylesheet" href="widgets.css?v=2.4.0a" type="text/css" />
8-
<link rel="stylesheet" href="icons.css?v=2.4.0a" type="text/css" />
6+
<link rel="stylesheet" href="main.css?v=2.4.0c" type="text/css" />
7+
<link rel="stylesheet" href="widgets.css?v=2.4.0c" type="text/css" />
8+
<link rel="stylesheet" href="icons.css?v=2.4.0c" type="text/css" />
99
<link rel="icon" type="image/png" href="favicon.png" />
10-
<script type="text/javascript" src="index.js?v=2.4.0a"></script>
10+
<script type="text/javascript" src="index.js?v=2.4.0c"></script>
1111
</head>
1212
<body>
1313
<div id="divContainer" class="container main" data-auth="false">

0 commit comments

Comments
 (0)