Skip to content

Commit a96f212

Browse files
committed
Added automatic reconnect if WiFi-Connection is lost. #1
Added configurable Signal-Bar-Display for WiFi. #2
1 parent 64cc3da commit a96f212

File tree

3 files changed

+299
-16
lines changed

3 files changed

+299
-16
lines changed

RoomGuard.ino

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
#include <DHT_U.h>
77
#include "secrets.h"
88
#include "config.h"
9+
#include "icons.h"
910

1011
#ifdef SD_ENABLED
1112
#include <SPI.h>
1213
#include <SD.h>
1314
File oSDFile;
14-
String sSSID = "";
15-
String sPSK = "";
1615
#endif
1716

1817
// Variables for WiFi Connection.
1918
int iWiFiStatus = WL_IDLE_STATUS;
2019
WiFiServer oServer(80);
2120
IPAddress WIFI_IP;
2221
char cClientData;
22+
int iSSIDLen = 0;
23+
int iPSKLen = 0;
24+
int iWiFiSignal = -100;
25+
String sSSID = "";
26+
String sPSK = "";
2327

2428
// OLED Display.
2529
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
@@ -38,6 +42,8 @@ long lUpdateInterval = 2000;
3842
long lNow = 0;
3943
long lLastSensorUpdate = 0;
4044
long lSensorInterval = 5000;
45+
long lLastWiFiCheck = 0;
46+
long lWiFiInterval = 5000;
4147

4248
// Setup all components and connect to WiFi.
4349
void setup() {
@@ -87,10 +93,10 @@ void setup() {
8793
oSDFile.close();
8894

8995
// Process/Convert strings from config-file.
90-
int iSSIDLen = sSSID.length();
96+
iSSIDLen = sSSID.length();
9197
char aSSID[iSSIDLen];
9298
sSSID.toCharArray(aSSID, iSSIDLen);
93-
int iPSKLen = sPSK.length();
99+
iPSKLen = sPSK.length();
94100
char aPSK[iPSKLen];
95101
sPSK.toCharArray(aPSK, iPSKLen);
96102

@@ -119,6 +125,10 @@ void setup() {
119125
// If SD-Card is not enabled, use settings from config.h and secrets.h
120126
char aSSID[] = SECRET_SSID;
121127
char aPSK[] = SECRET_PASS;
128+
sSSID = SECRET_SSID;
129+
sPSK = SECRET_PASS;
130+
iSSIDLen = sSSID.length();
131+
iPSKLen = sPSK.length();
122132
display.print(F("SSID: "));
123133
display.println(SECRET_SSID);
124134
#endif
@@ -166,6 +176,12 @@ void setup() {
166176
void loop() {
167177
lNow = millis();
168178

179+
// Check WiFi status after timeout.
180+
if (lNow >= lLastWiFiCheck + lWiFiInterval) {
181+
checkWiFi();
182+
lLastWiFiCheck = lNow;
183+
}
184+
169185
// Update sensor data after refresh-timeout.
170186
if (lNow >= lLastSensorUpdate + lSensorInterval || lLastSensorUpdate == 0) {
171187
refreshSensorValues();
@@ -234,6 +250,26 @@ void loop() {
234250
}
235251
}
236252

253+
// Check WiFi Status and Reconnect if necessary.
254+
void checkWiFi() {
255+
iWiFiStatus = WiFi.status();
256+
if (iWiFiStatus != WL_CONNECTED) {
257+
display.fillRect(0,0,128,7,SSD1306_BLACK);
258+
display.setCursor(0, 0);
259+
display.setTextSize(1);
260+
display.print(F("Reconnecting..."));
261+
display.display();
262+
263+
char aSSID[iSSIDLen];
264+
sSSID.toCharArray(aSSID, iSSIDLen);
265+
266+
char aPSK[iPSKLen];
267+
sPSK.toCharArray(aPSK, iPSKLen);
268+
269+
iWiFiStatus = WiFi.begin(aSSID, aPSK);
270+
}
271+
}
272+
237273
// Wipe Screen
238274
void blackoutScreen() {
239275
display.fillRect(0,0,128,64,SSD1306_BLACK);
@@ -243,13 +279,46 @@ void blackoutScreen() {
243279
// Print WiFi Status on Display.
244280
void printWiFiStatus() {
245281
display.setTextSize(1);
246-
display.setCursor(0,0); // Start at top-left corner
247-
display.println(SECRET_SSID);
248-
display.print("IP: ");
249-
display.println(WIFI_IP);
250-
display.print("Signal: ");
251-
display.print(WiFi.RSSI());
252-
display.println(" dBm");
282+
display.setCursor(0,0);
283+
284+
if(iWiFiStatus == WL_CONNECTED) {
285+
display.print(F("IP: "));
286+
display.println(WIFI_IP);
287+
}
288+
else
289+
{
290+
display.print(F("DISCONNECTED ("));
291+
display.print(iWiFiStatus);
292+
display.print(F(")"));
293+
}
294+
295+
display.setCursor(0,8);
296+
iWiFiSignal = WiFi.RSSI();
297+
298+
// Print Signal-Status as Signal-Bar if WIFI_SIGNAL_BAR is defined.
299+
#ifdef WIFI_SIGNAL_BAR
300+
if (iWiFiSignal <= -80 ||iWiFiSignal == 0) {
301+
// No signal, so nothing to show except an error...
302+
display.drawBitmap(111, 29, icWarning, 16, 16, 1);
303+
}
304+
else if(iWiFiSignal <= -70) {
305+
display.drawBitmap(111, 27, icWIFI1, 16, 16, 1);
306+
}
307+
else if(iWiFiSignal <= -60) {
308+
display.drawBitmap(111, 27, icWIFI2, 16, 16, 1);
309+
}
310+
else if(iWiFiSignal <= -50) {
311+
display.drawBitmap(111, 27, icWIFI3, 16, 16, 1);
312+
}
313+
else {
314+
display.drawBitmap(111, 27, icWIFI4, 16, 16, 1);
315+
}
316+
#else
317+
// Print Signal-Status as Text if WIFI_SIGNAL_BAR is commented out.
318+
display.print(F("Signal: "));
319+
display.print(iWiFiSignal);
320+
display.println(F(" dBm"));
321+
#endif
253322
}
254323

255324
// Refresh Sensor-Values from DHT sensor.
@@ -279,23 +348,26 @@ void refreshSensorValues() {
279348
}
280349

281350
// Print Sensor Values on Display.
282-
void printSensorValues() {
351+
void printSensorValues() {
352+
display.drawBitmap(0, 30, icTemperature, 16, 16, 1);
353+
display.drawBitmap(0, 46, icHumidity, 16, 16, 1);
354+
283355
display.setTextSize(2);
284-
display.setCursor(0,34);
356+
display.setCursor(18, 30);
285357

286358
if(bHasTemp) {
287359
display.print(fRealTemp);
288360
}
289361
else {
290362
display.print(F("EE"));
291363
}
292-
display.println(" *C");
293-
364+
365+
display.setCursor(18, 46);
366+
294367
if(bHasHumid) {
295368
display.print(fRealHumid);
296369
}
297370
else {
298371
display.print(F("EE"));
299372
}
300-
display.println(" %");
301373
}

config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ float ALERT_HUMID = 40.0;
1717

1818
#define SD_ENABLED true
1919
#define SD_CS 10
20+
21+
// Display-Options
22+
#define WIFI_SIGNAL_BAR true

0 commit comments

Comments
 (0)