#include <ESP8266WiFi.h>
#define WIFI_SSID "Mi MIX 2S"
#define WIFI_PASSWORD "987654321"
//#define WIFI_SSID "Gosha_SmartHome"
//#define WIFI_PASSWORD "MySmartHome"
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler, stationConnectedHandler;
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long currentMillis;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previous_wifi_check = 0;
// constants won't change:
const long interval = 400; // interval at which to blink (milliseconds)
const long wifi_check = 60000;
void WIFI_connect()
{
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.printDiag(Serial);
wifi_station_set_hostname("Atmor_kitchen"); //he hostname may be changed using the following function: WiFi.hostname(aHostname) Input parameter aHostname may be a type of char*, const char* or String. Maximum length of assigned hostname is 32 characters.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to AP");
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi sucessfully.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// timeClient.forceUpdate();
Serial.println("Server started");
Serial.println(WiFi.localIP()); // Вывод полученного IP адреса
Serial.print("RRSI: "); Serial.println(WiFi.RSSI());
Serial.print("Hostname - "); Serial.println(WiFi.hostname());
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi");
WiFi.mode(WIFI_OFF);
WiFi.printDiag(Serial);
}
void check_WiFi()
{
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previous_wifi_check >= wifi_check))
{
Serial.println("check_wifi_connection");
WiFi.printDiag(Serial);
previous_wifi_check = currentMillis;
WIFI_connect();
}
}
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(74880); //115200 Start the Serial Monitor
delay (100);
Serial.println("I`m alive");
WiFi.setAutoReconnect(false);
WiFi.setAutoConnect(false);
WIFI_connect();
gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP & event)
{
Serial.print("Station connected, IP: ");
Serial.println(WiFi.localIP());
});
disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected & event)
{
Serial.println("Station disconnected");
WiFi.mode(WIFI_OFF);
});
stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected & event)
{
Serial.println("Connected event");
});
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
check_WiFi();
}