forked from Brunas/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ino
72 lines (64 loc) · 1.67 KB
/
utils.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
void blinkLed(int numberOfBlinks, int msBetweenBlinks) {
int msDurationForLow=200;
for (int i = 0; i < numberOfBlinks; i++) {
if (i > 0) {
delay(msDurationForLow);
}
digitalWrite(LED_PIN, HIGH);
delay(msBetweenBlinks);
digitalWrite(LED_PIN, LOW);
}
}
void makeSureWiFiConnected(bool setupMode) {
if (setupMode) {
#ifdef TURN_OFF_WIFI_PS
//turn WiFi power saving mode off
esp_wifi_set_ps(WIFI_PS_NONE);
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
delay(3000);
}
if (WiFi.status() != WL_CONNECTED) {
blinkLed(20, 50); // Blink fast to indicate failed WiFi connection
WiFi.begin(WIFI_SSID, WIFI_PASS);
int _retries = WIFI_MAX_RECONNECT_TRIES;
while (WiFi.waitForConnectResult() != WL_CONNECTED && _retries > 0) {
#ifdef DEBUG
Serial.print("WiFi Connection Failed! Retries left: ");
Serial.println(_retries);
#endif
_retries--;
delay(3000);
}
if (WiFi.status() != WL_CONNECTED) {
blinkLed(10, 50); // Blink fast to indicate failed WiFi connection
#ifdef DEBUG
Serial.println("Connection Failed! Rebooting...");
#endif
ESP.restart();
}
}
if (setupMode) {
debug ("WiFi Ready. IP: " + WiFi.localIP().toString() + ", RSSI: " + String(WiFi.RSSI()));
}
}
void debug(String msg) {
#ifdef DEBUG
unsigned int _temp = (temprature_sens_read() - 32) / 1.8;
msg += " (T: " + String(_temp) + "C)";
Serial.println(msg);
#ifdef MQTT_DEBUGGING;
sendMQTTMessage(MQTT_DEBUG_TOPIC, msg.c_str());
#endif
#endif
;
}