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 // TODO(klew): add handling of custom local ip
65 void setup() override {
66 // ESP8266: for some reason when hostname is longer than 30 bytes, Wi-Fi
67 // connection can't be esablished. So as a workaround, we truncate hostname
68 // to 30 bytes
69 hostname[30] = '\0';
70 if (!wifiConfigured) {
71 // ESP32 requires setHostname to be called before begin...
72 WiFi.setHostname(hostname);
73 wifiConfigured = true;
74#ifdef ARDUINO_ARCH_ESP8266
75 gotIpEventHandler =
76 WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event) {
77 (void)(event);
78 ESPWifi::printConnectionDetails();
79 });
80 disconnectedEventHandler = WiFi.onStationModeDisconnected(
81 [](const WiFiEventStationModeDisconnected &event) {
82 (void)(event);
83 SUPLA_LOG_INFO("WiFi station disconnected");
84 });
85#else
86 WiFiEventId_t event_gotIP = WiFi.onEvent(
87 [](WiFiEvent_t event, WiFiEventInfo_t info) {
88 ESPWifi::printConnectionDetails();
89 },
90 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
91
92 (void)(event_gotIP);
93
94 WiFiEventId_t event_disconnected = WiFi.onEvent(
95 [](WiFiEvent_t event, WiFiEventInfo_t info) {
96 SUPLA_LOG_INFO("WiFi Station disconnected");
97 // ESP32 doesn't reconnect automatically after lost connection
98 WiFi.reconnect();
99 },
100 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
101
102 (void)(event_disconnected);
103#endif
104 } else {
105 if (mode == Supla::DEVICE_MODE_CONFIG) {
106 WiFi.mode(WIFI_MODE_AP);
107 } else {
108 WiFi.mode(WIFI_MODE_STA);
109 }
110 SUPLA_LOG_INFO("WiFi: resetting WiFi connection");
111 DisconnectProtocols();
112 WiFi.disconnect();
113 }
114
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);
119
120 } else {
121 SUPLA_LOG_INFO("WiFi: establishing connection with SSID: \"%s\"", ssid);
122 WiFi.mode(WIFI_MODE_STA);
123 WiFi.begin(ssid, password);
124 // ESP8266 requires setHostname to be called after begin...
125 WiFi.setHostname(hostname);
126 }
127
128 yield();
129 }
130
131 void disable() override {
132 }
133
134 // DEPRECATED, use setSSLEnabled instead
135 void enableSSL(bool value) {
136 setSSLEnabled(value);
137 }
138
139 void fillStateData(TDSC_ChannelState *channelState) override {
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);
149 }
150
151 bool getMacAddr(uint8_t *out) override {
152#ifdef ARDUINO_ARCH_ESP8266
153 WiFi.macAddress(out);
154#else
155#ifdef ESP_ARDUINO_VERSION_MAJOR
156#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
157 // Code for version 3.x
158 ::Network.macAddress(out);
159#else
160 // Code for version 2.x
161 WiFi.macAddress(out);
162#endif
163#endif
164#endif
165 return true;
166 }
167
168 void uninit() override {
169 WiFi.softAPdisconnect(true);
170 WiFi.disconnect(true);
171 }
172
173 protected:
174 bool wifiConfigured = false;
175
176#ifdef ARDUINO_ARCH_ESP8266
177 WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
178#endif
179};
180
181}; // namespace Supla
182
183#endif // SRC_SUPLA_NETWORK_ESP_WIFI_H_
Definition network.h:36
Definition netif_wifi.h:28
Definition proto.h:2575