Adafruit Library
Wippersnapper_ESP8266.h
Go to the documentation of this file.
1 
17 #ifndef WIPPERSNAPPER_ESP8266_H
18 #define WIPPERSNAPPER_ESP8266_H
19 
20 #ifdef ARDUINO_ARCH_ESP8266
21 #include "Adafruit_MQTT.h"
22 #include "Adafruit_MQTT_Client.h"
23 #include "ESP8266WiFi.h"
24 #include "ESP8266WiFiMulti.h"
25 #include "Wippersnapper.h"
26 
27 class Wippersnapper_ESP8266;
28 extern Wippersnapper_ESP8266 WS;
29 
30 /* NOTE - As of 07/21/26, this library no longer supports "Secure MQTT"
31  * (TLS/SSL) on the ESP8266 due to new automatic certificate updates that
32  * require a new certificate every 199 days (or less) -
33  * https://forums.adafruit.com/viewtopic.php?t=224362 If you would like a secure
34  * Adafruit IO Arduino project, please switch to using the modern ESP32 (and
35  * related models) instead of the ESP8266.
36  */
37 
38 /******************************************************************************/
43 /******************************************************************************/
44 class Wippersnapper_ESP8266 : public Wippersnapper {
45 
46 public:
47  /**************************************************************************/
59  /**************************************************************************/
60  Wippersnapper_ESP8266() : Wippersnapper() {
61  _ssid = 0;
62  _pass = 0;
63  _wifi_client = new WiFiClient;
64  WiFi.persistent(false);
65  WiFi.mode(WIFI_STA);
66  }
67 
68  /**************************************************************************/
72  /**************************************************************************/
73  ~Wippersnapper_ESP8266() {
74  if (_wifi_client)
75  delete _wifi_client;
76  if (_mqtt)
77  delete _mqtt;
78  }
79 
80  /**********************************************************/
88  /**********************************************************/
89  void set_ssid_pass(const char *ssid, const char *ssidPassword) {
90  _ssid = ssid;
91 
92  // set the AP password
93  // check if ssidPassword was "" in secrets.json
94  if ((ssidPassword != NULL) && (strlen(ssidPassword) == 0)) {
95  _pass = NULL; // Set as NULL for open networks
96  } else {
97  _pass = ssidPassword;
98  }
99  }
100 
101  /**********************************************************/
106  /**********************************************************/
107  void set_ssid_pass() {
108  _ssid = WS._config.network.ssid;
109  _pass = WS._config.network.pass;
110  }
111 
112  /***********************************************************/
117  /***********************************************************/
118  bool check_valid_ssid() {
119  // Set WiFi to station mode and disconnect from an AP if it was previously
120  // connected
121  WiFi.mode(WIFI_STA);
122  WiFi.disconnect();
123  delay(100);
124 
125  // Perform a network scan, including hidden networks
126  int n = WiFi.scanNetworks(/*async=*/false, /*show_hidden=*/true);
127  if (n == 0) {
128  WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
129  return false;
130  }
131 
132  bool foundNetwork = false;
133  bool foundHidden = false;
134 
135  WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
136  for (uint8_t i = 0; i < n; i++) {
137  if (WiFi.SSID(i).length() == 0) {
138  // hidden networks don't broadcast their SSID
139  foundHidden = true;
140  } else if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) {
141  foundNetwork = true;
142  } else if (!foundNetwork && WS._isWiFiMulti) {
143  // multi network mode
144  for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
145  if (strlen(WS._multiNetworks[j].ssid) > 0 &&
146  strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
147  foundNetwork = true;
148  }
149  }
150  }
151  if (WiFi.SSID(i).length() == 0) {
152  WS_DEBUG_PRINT("<hidden>");
153  } else {
154  WS_DEBUG_PRINTVAR(WiFi.SSID(i));
155  }
156  WS_DEBUG_PRINT(" (");
157  uint8_t BSSID[WL_MAC_ADDR_LENGTH];
158  memcpy(BSSID, WiFi.BSSID(i), WL_MAC_ADDR_LENGTH);
159  for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
160  if (m != 0)
161  WS_DEBUG_PRINT(":");
162  WS_DEBUG_PRINTHEX(BSSID[m]);
163  }
164  WS_DEBUG_PRINT(") ");
165  WS_DEBUG_PRINTVAR(WiFi.RSSI(i));
166  WS_DEBUG_PRINT("dB (ch");
167  WS_DEBUG_PRINTVAR(WiFi.channel(i))
168  WS_DEBUG_PRINTLN(")");
169  }
170 
171  // Hidden networks only reveal their SSID in reply to a directed probe
172  if (!foundNetwork && foundHidden) {
173  foundNetwork = scan_hidden_ssid(_ssid);
174  if (!foundNetwork && WS._isWiFiMulti) {
175  for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS && !foundNetwork; j++) {
176  foundNetwork = scan_hidden_ssid(WS._multiNetworks[j].ssid);
177  }
178  }
179  }
180 
181  if (!foundNetwork) {
182  WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
183  }
184  return foundNetwork;
185  }
186 
187  /***********************************************************/
194  /***********************************************************/
195  bool scan_hidden_ssid(const char *ssid) {
196  if (ssid == NULL || strlen(ssid) == 0)
197  return false;
198 
199  WS_DEBUG_PRINT("Scanning for hidden WiFi network: ");
200  WS_DEBUG_PRINTLNVAR(ssid);
201  WS.feedWDT();
202  int n = WiFi.scanNetworks(/*async=*/false, /*show_hidden=*/true,
203  /*channel=*/0, (uint8 *)ssid);
204  WS.feedWDT();
205  for (int i = 0; i < n; i++) {
206  // directed scans only report APs answering to `ssid`
207  if (WiFi.SSID(i).length() == 0 ||
208  strcmp(WiFi.SSID(i).c_str(), ssid) == 0) {
209  WS_DEBUG_PRINT("Found hidden WiFi network on ch");
210  WS_DEBUG_PRINTLNVAR(WiFi.channel(i));
211  return true;
212  }
213  }
214  return false;
215  }
216 
217  /********************************************************/
222  /********************************************************/
223  void getMacAddr() {
224  uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
225  WiFi.macAddress(mac);
226  memcpy(WS._macAddr, mac, sizeof(mac));
227  }
228 
229  /********************************************************/
234  /********************************************************/
235  int32_t getRSSI() { return WiFi.RSSI(); }
236 
237  /*******************************************************************/
243  /*******************************************************************/
244  void setupMQTTClient(const char *clientID) {
245  if (WS._config.io_port == 8883)
246  WS._config.io_port = 1883;
247  WS._mqtt = new Adafruit_MQTT_Client(
248  _wifi_client, WS._config.aio_url, WS._config.io_port, clientID,
249  WS._config.aio_user, WS._config.aio_key);
250  }
251 
252  /********************************************************/
257  /********************************************************/
259  switch (WiFi.status()) {
260  case WL_CONNECTED:
261  return WS_NET_CONNECTED;
262  case WL_CONNECT_FAILED:
263  return WS_NET_CONNECT_FAILED;
264  case WL_IDLE_STATUS:
265  return WS_IDLE;
266  default:
267  return WS_NET_DISCONNECTED;
268  }
269  }
270 
271  /*******************************************************************/
276  /*******************************************************************/
277  const char *connectionType() { return "ESP8266"; }
278 
279 protected:
280  const char *_ssid = NULL;
281  const char *_pass = NULL;
282  WiFiClient *_wifi_client;
283  ESP8266WiFiMulti _wifiMulti;
284 
285  /**************************************************************************/
289  /**************************************************************************/
290  void _connect() {
291 
292  if (WiFi.status() == WL_CONNECTED)
293  return;
294 
295  if (strlen(_ssid) == 0) {
296  _status = WS_SSID_INVALID;
297  } else {
298  WiFi.setAutoReconnect(false);
299  // Attempt connection
300  _disconnect();
301  delay(100);
302  // ESP8266 MUST be in STA mode to avoid device acting as client/server
303  WiFi.mode(WIFI_STA);
304  _status = WS_NET_DISCONNECTED;
305  delay(100);
306 
307  if (WS._isWiFiMulti) {
308  // multi network mode
309  for (int i = 0; i < WS_MAX_ALT_WIFI_NETWORKS; i++) {
310  if (strlen(WS._multiNetworks[i].ssid) > 0 &&
311  (_wifiMulti.existsAP(WS._multiNetworks[i].ssid) == false)) {
312  // doesn't exist, add it
313  _wifiMulti.addAP(WS._multiNetworks[i].ssid,
314  WS._multiNetworks[i].pass);
315  }
316  }
317  }
318 
319  // add default network
320  if (_wifiMulti.existsAP(_ssid) == false) {
321  _wifiMulti.addAP(_ssid, _pass);
322  }
323 
324  long startRetry = millis();
325  WS_DEBUG_PRINTLN("CONNECTING");
326 
327  while (_wifiMulti.run(5000) != WL_CONNECTED &&
328  millis() - startRetry < 10000) {
329  // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
330  yield();
331  }
332 
333  if (WiFi.status() == WL_CONNECTED) {
334  _status = WS_NET_CONNECTED;
335  } else {
336  _status = WS_NET_DISCONNECTED;
337  }
338 
339  WS.feedWDT();
340  }
341  }
342 
343  /**************************************************************************/
347  /**************************************************************************/
348  void _disconnect() {
349  WiFi.disconnect();
350  delay(500);
351  }
352 };
353 
354 #endif // ARDUINO_ARCH_ESP8266
355 #endif // WIPPERSNAPPER_ESP8266_H
virtual void setupMQTTClient(const char *clientID)
Sets up the MQTT client session.
Definition: Wippersnapper.cpp:187
#define WS_DEBUG_PRINT(...)
Prints debug output.
Definition: Wippersnapper.h:63
#define WS_DEBUG_PRINTHEX(...)
Prints in hexadecimal.
Definition: Wippersnapper.h:78
#define WS_DEBUG_PRINTVAR(...)
Prints variable (any type)
Definition: Wippersnapper.h:72
#define WS_DEBUG_PRINTLNVAR(...)
Prints variable with newline.
Definition: Wippersnapper.h:75
secretsConfig _config
Definition: Wippersnapper.h:432
#define WL_MAC_ADDR_LENGTH
MAC address length - from RP2040 BSP.
Definition: Wippersnapper_Networking.h:20
ws_status_t
Definition: Wippersnapper.h:219
virtual bool check_valid_ssid()
Performs a scan of local WiFi networks.
Definition: Wippersnapper.cpp:232
Class that provides storage and functions for the Adafruit IO Wippersnapper interface.
Definition: Wippersnapper.h:311
virtual void _connect()
Connects to wireless network.
Definition: Wippersnapper.cpp:146
#define WS_MAX_ALT_WIFI_NETWORKS
Maximum number of alternative networks.
Definition: Wippersnapper.h:280
virtual void getMacAddr()
Sets the network interface&#39;s unique identifier, typically the MAC address.
Definition: Wippersnapper.cpp:165
virtual void set_ssid_pass()
Sets the device&#39;s wireless network credentials from the secrets.json configuration file...
Definition: Wippersnapper.cpp:222
void feedWDT()
Feeds the WDT to prevent hardware reset.
Definition: Wippersnapper.cpp:2645
bool _isWiFiMulti
Definition: Wippersnapper.h:434
networkConfig _multiNetworks[3]
Definition: Wippersnapper.h:433
#define WS_DEBUG_PRINTLN(...)
Prints line from debug output.
Definition: Wippersnapper.h:66
Wippersnapper_AIRLIFT WS
Global instance of Wippersnapper_WiFi.
Definition: Wippersnapper.cpp:38
uint8_t _macAddr[6]
Definition: Wippersnapper.h:426
Adafruit_MQTT * _mqtt
Definition: Wippersnapper.h:430
virtual ws_status_t networkStatus()
Returns the network&#39;s connection status.
Definition: Wippersnapper.cpp:197
virtual int32_t getRSSI()
Gets the network&#39;s RSSI.
Definition: Wippersnapper.cpp:175
virtual void _disconnect()
Disconnect Wippersnapper MQTT session and network.
Definition: Wippersnapper.cpp:155