supla-device
Loading...
Searching...
No Matches
esp_wifi.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_NETWORK_ESP_WIFI_H_
20#define SRC_SUPLA_NETWORK_ESP_WIFI_H_
21
22#include <Arduino.h>
23
24#include <supla/log_wrapper.h>
25#include <supla/tools.h>
26
27#ifdef ARDUINO_ARCH_ESP8266
28#include <ESP8266WiFi.h>
29
30// workaround for incompatible names in ESP8266 and ESP32 boards
31#define WIFI_MODE_AP WIFI_AP
32#define WIFI_MODE_STA WIFI_STA
33
34#else
35#include <WiFi.h>
36#endif
37
38#include "../supla_lib_config.h"
39#include "netif_wifi.h"
40
41namespace Supla {
42class ESPWifi : public Supla::Wifi {
43 public:
44 ESPWifi(const char *wifiSsid = nullptr,
45 const char *wifiPassword = nullptr,
46 unsigned char *ip = nullptr)
47 : Wifi(wifiSsid, wifiPassword, ip) {
48 }
49
50
51 bool isReady() override {
52 return WiFi.status() == WL_CONNECTED;
53 }
54
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);
62 }
63
64 void setup() override {
65 // ESP8266: for some reason when hostname is longer than 30 bytes, Wi-Fi
66 // connection can't be esablished. So as a workaround, we truncate hostname
67 // to 30 bytes
68 hostname[30] = '\0';
69 if (!wifiConfigured) {
70 // ESP32 requires setHostname to be called before begin...
71 WiFi.setHostname(hostname);
72 wifiConfigured = true;
73#ifdef ARDUINO_ARCH_ESP8266
74 gotIpEventHandler =
75 WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event) {
76 (void)(event);
77 ESPWifi::printConnectionDetails();
78 });
79 disconnectedEventHandler = WiFi.onStationModeDisconnected(
80 [](const WiFiEventStationModeDisconnected &event) {
81 (void)(event);
82 SUPLA_LOG_INFO("WiFi station disconnected");
83 });
84#else
85 WiFiEventId_t event_gotIP = WiFi.onEvent(
86 [](WiFiEvent_t event, WiFiEventInfo_t info) {
87 ESPWifi::printConnectionDetails();
88 },
89 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
90
91 (void)(event_gotIP);
92
93 WiFiEventId_t event_disconnected = WiFi.onEvent(
94 [](WiFiEvent_t event, WiFiEventInfo_t info) {
95 SUPLA_LOG_INFO("WiFi Station disconnected");
96 // ESP32 doesn't reconnect automatically after lost connection
97 WiFi.reconnect();
98 },
99 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
100
101 (void)(event_disconnected);
102#endif
103 } else {
104 if (mode == Supla::DEVICE_MODE_CONFIG) {
105 WiFi.mode(WIFI_MODE_AP);
106 } else {
107 WiFi.mode(WIFI_MODE_STA);
108 }
109 SUPLA_LOG_INFO("WiFi: resetting WiFi connection");
110 DisconnectProtocols();
111 WiFi.disconnect();
112 }
113
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);
118
119 } else {
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");
145 }
146 }
147 WiFi.begin(ssid, password);
148 // ESP8266 requires setHostname to be called after begin...
149 WiFi.setHostname(hostname);
150 }
151
152 yield();
153 }
154
155 void disable() override {
156 }
157
158 // DEPRECATED, use setSSLEnabled instead
159 void enableSSL(bool value) {
160 setSSLEnabled(value);
161 }
162
163 void fillStateData(TDSC_ChannelState *channelState) override {
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);
173 }
174
175 bool getMacAddr(uint8_t *out) override {
176#ifdef ARDUINO_ARCH_ESP8266
177 WiFi.macAddress(out);
178#else
179#ifdef ESP_ARDUINO_VERSION_MAJOR
180#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
181 // Code for version 3.x
182 ::Network.macAddress(out);
183#else
184 // Code for version 2.x
185 WiFi.macAddress(out);
186#endif
187#endif
188#endif
189 return true;
190 }
191
192 void uninit() override {
193 WiFi.softAPdisconnect(true);
194 WiFi.disconnect(true);
195 }
196
197 uint32_t getIP() override {
198 return WiFi.localIP();
199 }
200
201 const char *getIntfName() const override {
202 return "Wi-Fi";
203 }
204
205 protected:
206 bool wifiConfigured = false;
207
208#ifdef ARDUINO_ARCH_ESP8266
209 WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
210#endif
211};
212
213}; // namespace Supla
214
215#endif // SRC_SUPLA_NETWORK_ESP_WIFI_H_
Definition ip_address.h:30
Definition network.h:36
Definition netif_wifi.h:28
Definition proto.h:2699