2023-03-30 01:28:31 +02:00
|
|
|
#include "wlan.h"
|
|
|
|
#include "credentials.h"
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
|
2023-03-30 02:10:49 +02:00
|
|
|
void initial_connection() {
|
|
|
|
WiFi.begin(SSID, PSK);
|
|
|
|
WiFi.persistent(true);
|
|
|
|
WiFi.setAutoConnect(true);
|
|
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlan_connection(int max_retries) {
|
|
|
|
if (WiFi.SSID() != SSID)
|
|
|
|
initial_connection();
|
|
|
|
int retries = 0;
|
2023-03-30 01:28:31 +02:00
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
2023-03-30 02:10:49 +02:00
|
|
|
retries++;
|
|
|
|
if (retries == max_retries)
|
|
|
|
enter_deep_sleep(true);
|
2023-03-30 01:28:31 +02:00
|
|
|
delay(1000);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("WiFi connected");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_connection(char *server, int port, char *fingerprint) {
|
|
|
|
WiFiClientSecure client;
|
|
|
|
client.connect(server, port);
|
|
|
|
}
|
|
|
|
|
2023-03-30 02:10:49 +02:00
|
|
|
void enter_deep_sleep(bool wifi_timeout) {
|
|
|
|
if (wifi_timeout)
|
2023-03-30 01:28:31 +02:00
|
|
|
WiFi.disconnect();
|
|
|
|
ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
|
|
|
|
}
|