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);
64 void setup()
override {
69 if (!wifiConfigured) {
71 WiFi.setHostname(hostname);
72 wifiConfigured =
true;
73#ifdef ARDUINO_ARCH_ESP8266
75 WiFi.onStationModeGotIP([](
const WiFiEventStationModeGotIP &event) {
77 ESPWifi::printConnectionDetails();
79 disconnectedEventHandler = WiFi.onStationModeDisconnected(
80 [](
const WiFiEventStationModeDisconnected &event) {
82 SUPLA_LOG_INFO(
"WiFi station disconnected");
85 WiFiEventId_t event_gotIP = WiFi.onEvent(
86 [](WiFiEvent_t event, WiFiEventInfo_t info) {
87 ESPWifi::printConnectionDetails();
89 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
93 WiFiEventId_t event_disconnected = WiFi.onEvent(
94 [](WiFiEvent_t event, WiFiEventInfo_t info) {
95 SUPLA_LOG_INFO(
"WiFi Station disconnected");
99 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
101 (void)(event_disconnected);
104 if (mode == Supla::DEVICE_MODE_CONFIG) {
105 WiFi.mode(WIFI_MODE_AP);
107 WiFi.mode(WIFI_MODE_STA);
109 SUPLA_LOG_INFO(
"WiFi: resetting WiFi connection");
110 DisconnectProtocols();
114 if (mode == Supla::DEVICE_MODE_CONFIG) {
115 SUPLA_LOG_INFO(
"WiFi: enter config mode with SSID: \"%s\"", hostname);
116 WiFi.mode(WIFI_MODE_AP);
117 WiFi.softAP(hostname,
nullptr, 6);
120 SUPLA_LOG_INFO(
"WiFi: establishing connection with SSID: \"%s\"", ssid);
121 WiFi.mode(WIFI_MODE_STA);
122 if (hasStaticIpConfig()) {
123 IPAddress localIp((getNetifConfig().ip >> 24) & 0xFF,
124 (getNetifConfig().ip >> 16) & 0xFF,
125 (getNetifConfig().ip >> 8) & 0xFF,
126 getNetifConfig().ip & 0xFF);
127 IPAddress gateway((getNetifConfig().gateway >> 24) & 0xFF,
128 (getNetifConfig().gateway >> 16) & 0xFF,
129 (getNetifConfig().gateway >> 8) & 0xFF,
130 getNetifConfig().gateway & 0xFF);
131 IPAddress subnet((getNetifConfig().netmask >> 24) & 0xFF,
132 (getNetifConfig().netmask >> 16) & 0xFF,
133 (getNetifConfig().netmask >> 8) & 0xFF,
134 getNetifConfig().netmask & 0xFF);
135 IPAddress dns1((getNetifConfig().dns1 >> 24) & 0xFF,
136 (getNetifConfig().dns1 >> 16) & 0xFF,
137 (getNetifConfig().dns1 >> 8) & 0xFF,
138 getNetifConfig().dns1 & 0xFF);
139 IPAddress dns2((getNetifConfig().dns2 >> 24) & 0xFF,
140 (getNetifConfig().dns2 >> 16) & 0xFF,
141 (getNetifConfig().dns2 >> 8) & 0xFF,
142 getNetifConfig().dns2 & 0xFF);
143 if (!WiFi.config(localIp, gateway, subnet, dns1, dns2)) {
144 SUPLA_LOG_WARNING(
"WiFi static IP config failed, continuing");
147 WiFi.begin(ssid, password);
149 WiFi.setHostname(hostname);
155 void disable()
override {
159 void enableSSL(
bool value) {
160 setSSLEnabled(value);
164 channelState->Fields |= SUPLA_CHANNELSTATE_FIELD_IPV4 |
165 SUPLA_CHANNELSTATE_FIELD_MAC |
166 SUPLA_CHANNELSTATE_FIELD_WIFIRSSI |
167 SUPLA_CHANNELSTATE_FIELD_WIFISIGNALSTRENGTH;
168 channelState->IPv4 = WiFi.localIP();
169 getMacAddr(channelState->MAC);
170 int rssi = WiFi.RSSI();
171 channelState->WiFiRSSI = rssi;
172 channelState->WiFiSignalStrength = Supla::rssiToSignalStrength(rssi);
175 bool getMacAddr(uint8_t *out)
override {
176#ifdef ARDUINO_ARCH_ESP8266
177 WiFi.macAddress(out);
179#ifdef ESP_ARDUINO_VERSION_MAJOR
180#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
185 WiFi.macAddress(out);
192 void uninit()
override {
193 WiFi.softAPdisconnect(
true);
194 WiFi.disconnect(
true);
197 uint32_t getIP()
override {
198 return WiFi.localIP();
201 const char *getIntfName()
const override {
206 bool wifiConfigured =
false;
208#ifdef ARDUINO_ARCH_ESP8266
209 WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;