8
8
* please support Adafruit and open-source hardware by purchasing
9
9
* products from Adafruit!
10
10
*
11
- * Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
11
+ * Copyright (c) Brent Rubell 2020-2024 for Adafruit Industries.
12
12
*
13
13
* MIT license, all text here must be included in any redistribution.
14
14
*
25
25
#include " Arduino.h"
26
26
#include " WiFi.h"
27
27
#include " WiFiMulti.h"
28
- #include < WiFiClientSecure.h>
28
+ #include < NetworkClient.h>
29
+ #include < NetworkClientSecure.h>
29
30
extern Wippersnapper WS;
30
31
31
32
/* ***************************************************************************/
@@ -44,7 +45,6 @@ class Wippersnapper_ESP32 : public Wippersnapper {
44
45
Wippersnapper_ESP32 () : Wippersnapper() {
45
46
_ssid = 0 ;
46
47
_pass = 0 ;
47
- _mqtt_client = new WiFiClientSecure;
48
48
}
49
49
50
50
/* *************************************************************************/
@@ -53,8 +53,10 @@ class Wippersnapper_ESP32 : public Wippersnapper {
53
53
*/
54
54
/* *************************************************************************/
55
55
~Wippersnapper_ESP32 () {
56
- if (_mqtt_client)
57
- delete _mqtt_client;
56
+ if (_mqtt_client_secure)
57
+ delete _mqtt_client_secure;
58
+ if (_mqtt_client_insecure)
59
+ delete _mqtt_client_insecure;
58
60
}
59
61
60
62
/* *******************************************************/
@@ -172,18 +174,24 @@ class Wippersnapper_ESP32 : public Wippersnapper {
172
174
*/
173
175
/* *******************************************************/
174
176
void setupMQTTClient (const char *clientID) {
175
- if (strcmp (WS._config .aio_url , " io.adafruit.com" ) == 0 ) {
176
- _mqtt_client->setCACert (_aio_root_ca_prod);
177
- } else if (strcmp (WS._config .aio_url , " io.adafruit.us" ) == 0 ) {
178
- _mqtt_client->setCACert (_aio_root_ca_staging);
177
+ if (strcmp (WS._config .aio_url , " io.adafruit.com" ) == 0 ||
178
+ strcmp (WS._config .aio_url , " io.adafruit.us" ) == 0 ) {
179
+ _mqtt_client_secure = new NetworkClientSecure ();
180
+ _mqtt_client_secure->setCACert (
181
+ strcmp (WS._config .aio_url , " io.adafruit.com" ) == 0
182
+ ? _aio_root_ca_prod
183
+ : _aio_root_ca_staging);
184
+ WS._mqtt = new Adafruit_MQTT_Client (
185
+ _mqtt_client_secure, WS._config .aio_url , WS._config .io_port , clientID,
186
+ WS._config .aio_user , WS._config .aio_key );
179
187
} else {
180
- _mqtt_client->setInsecure ();
188
+ // Insecure connections require a NetworkClient object rather than a
189
+ // NetworkClientSecure object
190
+ _mqtt_client_insecure = new NetworkClient ();
191
+ WS._mqtt = new Adafruit_MQTT_Client (
192
+ _mqtt_client_insecure, WS._config .aio_url , WS._config .io_port ,
193
+ clientID, WS._config .aio_user , WS._config .aio_key );
181
194
}
182
-
183
- // Construct MQTT client
184
- WS._mqtt = new Adafruit_MQTT_Client (
185
- _mqtt_client, WS._config .aio_url , WS._config .io_port , clientID,
186
- WS._config .aio_user , WS._config .aio_key );
187
195
}
188
196
189
197
/* *******************************************************/
@@ -214,10 +222,13 @@ class Wippersnapper_ESP32 : public Wippersnapper {
214
222
const char *connectionType () { return " ESP32" ; }
215
223
216
224
protected:
217
- const char *_ssid; // /< WiFi SSID
218
- const char *_pass; // /< WiFi password
219
- WiFiClientSecure *_mqtt_client; // /< Pointer to a WiFi client object (TLS/SSL)
220
- WiFiMulti _wifiMulti; // /< WiFiMulti object for multi-network mode
225
+ const char *_ssid; // /< WiFi SSID
226
+ const char *_pass; // /< WiFi password
227
+ NetworkClientSecure
228
+ *_mqtt_client_secure; // /< Pointer to a secure network client object
229
+ NetworkClient
230
+ *_mqtt_client_insecure; // /< Pointer to an insecure network client object
231
+ WiFiMulti _wifiMulti; // /< WiFiMulti object for multi-network mode
221
232
222
233
const char *_aio_root_ca_staging =
223
234
" -----BEGIN CERTIFICATE-----\n "
0 commit comments