44 ESPWifi(
const char *wifiSsid =
nullptr,
45 const char *wifiPassword =
nullptr,
46 unsigned char *ip =
nullptr)
47 : Wifi(wifiSsid, wifiPassword, ip) {
51 bool isReady()
override {
52 return WiFi.status() == WL_CONNECTED;
55 static void printConnectionDetails() {
56 SUPLA_LOG_INFO(
"Connected BSSID: %s", WiFi.BSSIDstr().c_str());
57 SUPLA_LOG_INFO(
"local IP: %s", WiFi.localIP().toString().c_str());
58 SUPLA_LOG_INFO(
"subnetMask: %s", WiFi.subnetMask().toString().c_str());
59 SUPLA_LOG_INFO(
"gatewayIP: %s", WiFi.gatewayIP().toString().c_str());
60 int rssi = WiFi.RSSI();
61 SUPLA_LOG_INFO(
"Signal strength (RSSI): %d dBm", rssi);
65 void setup()
override {
70 if (!wifiConfigured) {
72 WiFi.setHostname(hostname);
73 wifiConfigured =
true;
74#ifdef ARDUINO_ARCH_ESP8266
76 WiFi.onStationModeGotIP([](
const WiFiEventStationModeGotIP &event) {
78 ESPWifi::printConnectionDetails();
80 disconnectedEventHandler = WiFi.onStationModeDisconnected(
81 [](
const WiFiEventStationModeDisconnected &event) {
83 SUPLA_LOG_INFO(
"WiFi station disconnected");
86 WiFiEventId_t event_gotIP = WiFi.onEvent(
87 [](WiFiEvent_t event, WiFiEventInfo_t info) {
88 ESPWifi::printConnectionDetails();
90 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
94 WiFiEventId_t event_disconnected = WiFi.onEvent(
95 [](WiFiEvent_t event, WiFiEventInfo_t info) {
96 SUPLA_LOG_INFO(
"WiFi Station disconnected");
100 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
102 (void)(event_disconnected);
105 if (mode == Supla::DEVICE_MODE_CONFIG) {
106 WiFi.mode(WIFI_MODE_AP);
108 WiFi.mode(WIFI_MODE_STA);
110 SUPLA_LOG_INFO(
"WiFi: resetting WiFi connection");
111 DisconnectProtocols();
115 if (mode == Supla::DEVICE_MODE_CONFIG) {
116 SUPLA_LOG_INFO(
"WiFi: enter config mode with SSID: \"%s\"", hostname);
117 WiFi.mode(WIFI_MODE_AP);
118 WiFi.softAP(hostname,
nullptr, 6);
121 SUPLA_LOG_INFO(
"WiFi: establishing connection with SSID: \"%s\"", ssid);
122 WiFi.mode(WIFI_MODE_STA);
123 WiFi.begin(ssid, password);
125 WiFi.setHostname(hostname);
131 void disable()
override {
135 void enableSSL(
bool value) {
136 setSSLEnabled(value);
140 channelState->Fields |= SUPLA_CHANNELSTATE_FIELD_IPV4 |
141 SUPLA_CHANNELSTATE_FIELD_MAC |
142 SUPLA_CHANNELSTATE_FIELD_WIFIRSSI |
143 SUPLA_CHANNELSTATE_FIELD_WIFISIGNALSTRENGTH;
144 channelState->IPv4 = WiFi.localIP();
145 getMacAddr(channelState->MAC);
146 int rssi = WiFi.RSSI();
147 channelState->WiFiRSSI = rssi;
148 channelState->WiFiSignalStrength = Supla::rssiToSignalStrength(rssi);
151 bool getMacAddr(uint8_t *out)
override {
152#ifdef ARDUINO_ARCH_ESP8266
153 WiFi.macAddress(out);
155#ifdef ESP_ARDUINO_VERSION_MAJOR
156#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
161 WiFi.macAddress(out);
168 void uninit()
override {
169 WiFi.softAPdisconnect(
true);
170 WiFi.disconnect(
true);
174 bool wifiConfigured =
false;
176#ifdef ARDUINO_ARCH_ESP8266
177 WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;