Skip to content

Commit 9029683

Browse files
committed
mdns now working correctly also
1 parent 5d8f64f commit 9029683

6 files changed

+51
-42
lines changed

HeishaMon/HeishaMon.ino

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool mqttcallbackinprogress = false; // mutex for processing mqtt callback
4646
#define MQTTRECONNECTTIMER 30000 //it takes 30 secs for each mqtt server reconnect attempt
4747
unsigned long nextMqttReconnectAttempt = 0;
4848

49-
#define WIFIRETRYTIMER 30000 // switch between hotspot and configured SSID each 30 secs if SSID is lost
49+
#define WIFIRETRYTIMER 10000 // switch between hotspot and configured SSID each 10 secs if SSID is lost
5050
unsigned long nextWifiRetryTimer = WIFIRETRYTIMER;
5151

5252
unsigned long nexttime = 0;
@@ -98,66 +98,70 @@ DoubleResetDetect drd(DRD_TIMEOUT, DRD_ADDRESS);
9898
WiFiClient mqtt_wifi_client;
9999
PubSubClient mqtt_client(mqtt_wifi_client);
100100

101-
//check wifi bools
102-
bool softAPenabled = false;
103-
bool reconnectingWiFi = false;
101+
bool firstConnectSinceBoot = true; //if this is true there is no first connection made yet
104102

105103
/*
106-
* check_wifi will process wifi reconnecting managing
107-
*/
104+
check_wifi will process wifi reconnecting managing
105+
*/
108106
void check_wifi()
109107
{
110-
if ((WiFi.status() != WL_CONNECTED) || (!WiFi.localIP())) {
108+
if ((WiFi.status() != WL_CONNECTED) || (!WiFi.localIP())) {
111109
/*
112-
* if we are not connected to an AP
113-
* we must be in softAP so respond to DNS
114-
*/
110+
if we are not connected to an AP
111+
we must be in softAP so respond to DNS
112+
*/
115113
dnsServer.processNextRequest();
116114

117-
if ((reconnectingWiFi) && (WiFi.softAPgetStationNum() > 0)) {
115+
if (((WiFi.status() != WL_DISCONNECTED)) && (WiFi.softAPgetStationNum() > 0)) {
118116
log_message((char *)"WiFi lost, but softAP station connecting, so stop scanning...");
119-
reconnectingWiFi = false;
120-
WiFi.disconnect();
117+
WiFi.disconnect(true);
121118
}
122119

123120
/*
124-
* only start this routine if timeout on
125-
* reconnecting to AP and SSID is set
126-
*/
121+
only start this routine if timeout on
122+
reconnecting to AP and SSID is set
123+
*/
127124
if ((strlen(heishamonSettings.wifi_ssid) > 0) && (nextWifiRetryTimer < millis())) {
128125
nextWifiRetryTimer = millis() + WIFIRETRYTIMER;
129-
if (!softAPenabled) {
126+
if (WiFi.softAPSSID() == "") {
130127
log_message((char *)"WiFi lost, starting setup hotspot...");
131-
softAPenabled = true;
132128
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
133129
WiFi.softAP("HeishaMon-Setup");
134130
}
135-
if ((!reconnectingWiFi) && (WiFi.softAPgetStationNum() == 0 )) {
136-
reconnectingWiFi = true;
131+
if ((WiFi.status() == WL_DISCONNECTED) && (WiFi.softAPgetStationNum() == 0 )) {
137132
log_message((char *)"Retrying configured WiFi, ...");
138133
if (strlen(heishamonSettings.wifi_password) == 0) {
139134
WiFi.begin(heishamonSettings.wifi_ssid);
140135
} else {
141136
WiFi.begin(heishamonSettings.wifi_ssid, heishamonSettings.wifi_password);
142137
}
143138
} else {
144-
reconnectingWiFi = false;
145139
log_message((char *)"Reconnecting to WiFi failed. Waiting a few seconds before trying again.");
146-
WiFi.disconnect();
140+
WiFi.disconnect(true);
147141
}
148142
}
149-
} else {
150-
if (softAPenabled) {
143+
} else { //WiFi connected
144+
if (WiFi.softAPSSID() != "") {
151145
log_message((char *)"WiFi (re)connected, shutting down hotspot...");
152-
softAPenabled = false;
153-
reconnectingWiFi = false;
154146
WiFi.softAPdisconnect(true);
147+
MDNS.notifyAPChange();
155148
}
149+
150+
if (firstConnectSinceBoot) { // this should start only when softap is down or else it will not work properly so run after the routine to disable softap
151+
firstConnectSinceBoot = false;
152+
setupOTA();
153+
MDNS.begin(heishamonSettings.wifi_hostname);
154+
MDNS.addService("http", "tcp", 80);
155+
}
156+
156157
/*
157-
* always update if wifi is working so next time on ssid failure
158-
* it only starts the routine above after this timeout
159-
*/
158+
always update if wifi is working so next time on ssid failure
159+
it only starts the routine above after this timeout
160+
*/
160161
nextWifiRetryTimer = millis() + WIFIRETRYTIMER;
162+
163+
// Allow MDNS processing
164+
MDNS.update();
161165
}
162166
}
163167

@@ -172,7 +176,7 @@ void mqtt_reconnect()
172176
if (mqtt_client.connect(heishamonSettings.wifi_hostname, heishamonSettings.mqtt_username, heishamonSettings.mqtt_password, topic, 1, true, "Offline"))
173177
{
174178
mqttReconnects++;
175-
MDNS.begin(heishamonSettings.wifi_hostname); //assume reconnect on wifi so maybe need mdns restart
179+
176180
sprintf(topic, "%s/%s/#", heishamonSettings.mqtt_topic_base, mqtt_topic_commands);
177181
mqtt_client.subscribe(topic);
178182
sprintf(topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_send_raw_value_topic);
@@ -532,9 +536,7 @@ void setup() {
532536
Serial.println(F("starting..."));
533537
WiFi.printDiag(Serial);
534538
setupWifi(drd, &heishamonSettings);
535-
MDNS.begin(heishamonSettings.wifi_hostname);
536-
MDNS.addService("http", "tcp", 80);
537-
setupOTA();
539+
538540
setupMqtt();
539541
setupHttp();
540542

@@ -604,8 +606,6 @@ void loop() {
604606
httpServer.handleClient();
605607
// handle Websockets
606608
webSocket.loop();
607-
// Allow MDNS processing
608-
MDNS.update();
609609

610610
mqtt_client.loop();
611611

@@ -682,9 +682,12 @@ void loop() {
682682
//get new data
683683
if (!heishamonSettings.listenonly) send_panasonic_query();
684684

685-
MDNS.announce();
686685
//Make sure the LWT is set to Online, even if the broker have marked it dead.
687686
sprintf(mqtt_topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_willtopic);
688687
mqtt_client.publish(mqtt_topic, "Online");
688+
689+
if (WiFi.isConnected()) {
690+
MDNS.announce();
691+
}
689692
}
690693
}

HeishaMon/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
static const char* heishamon_version = "1.0-iy-cm-PR70";
1+
static const char* heishamon_version = "1.0-iy-cm-05";

HeishaMon/webfunctions.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
void log_message(char* string);
1818

19+
1920
int getWifiQuality() {
2021
if (WiFi.status() != WL_CONNECTED)
2122
return -1;
@@ -196,19 +197,21 @@ void setupWifi(DoubleResetDetect &drd, settingsStruct *heishamonSettings) {
196197
//no sleep wifi
197198
WiFi.setSleepMode(WIFI_NONE_SLEEP);
198199
WiFi.mode(WIFI_AP_STA);
199-
WiFi.persistent(true);
200+
WiFi.softAPdisconnect(true);
200201
if (strlen(heishamonSettings->wifi_ssid) > 0) {
201202
log_message((char *)"Wifi client mode...");
203+
WiFi.persistent(true);
202204
if (strlen(heishamonSettings->wifi_password) == 0) {
203205
WiFi.begin(heishamonSettings->wifi_ssid);
204206
} else {
205207
WiFi.begin(heishamonSettings->wifi_ssid, heishamonSettings->wifi_password);
206208
}
207209
}
208-
209-
log_message((char *)"Wifi hotspot mode...");
210-
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
211-
WiFi.softAP("HeishaMon-Setup");
210+
else {
211+
log_message((char *)"Wifi hotspot mode...");
212+
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
213+
WiFi.softAP("HeishaMon-Setup");
214+
}
212215

213216
if (strlen(heishamonSettings->wifi_hostname) == 0) {
214217
//Set hostname on wifi rather than ESP_xxxxx

HeishaMon/webfunctions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
static IPAddress apIP(192, 168, 4, 1);
1313

14+
15+
1416
struct settingsStruct {
1517
unsigned int waitTime = 5; // how often data is read from heatpump
1618
unsigned int waitDallasTime = 5; // how often temps are read from 1wire
@@ -42,6 +44,7 @@ struct settingsStruct {
4244
SmartControlSettingsStruct SmartControlSettings;
4345
};
4446

47+
4548
String getUptime(void);
4649
void setupWifi(DoubleResetDetect &drd, settingsStruct *heishamonSettings);
4750
int getWifiQuality(void);

0 commit comments

Comments
 (0)