88 * please support Adafruit and open-source hardware by purchasing
99 * products from Adafruit!
1010 *
11- * Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
11+ * Copyright (c) Brent Rubell 2020-2024 for Adafruit Industries.
1212 *
1313 * MIT license, all text here must be included in any redistribution.
1414 *
2525#include " Arduino.h"
2626#include " WiFi.h"
2727#include " WiFiMulti.h"
28- #include < WiFiClientSecure.h>
28+ #include < NetworkClient.h>
29+ #include < NetworkClientSecure.h>
2930extern Wippersnapper WS;
3031
3132/* ***************************************************************************/
@@ -44,7 +45,6 @@ class Wippersnapper_ESP32 : public Wippersnapper {
4445 Wippersnapper_ESP32 () : Wippersnapper() {
4546 _ssid = 0 ;
4647 _pass = 0 ;
47- _mqtt_client = new WiFiClientSecure;
4848 }
4949
5050 /* *************************************************************************/
@@ -53,8 +53,10 @@ class Wippersnapper_ESP32 : public Wippersnapper {
5353 */
5454 /* *************************************************************************/
5555 ~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;
5860 }
5961
6062 /* *******************************************************/
@@ -172,18 +174,24 @@ class Wippersnapper_ESP32 : public Wippersnapper {
172174 */
173175 /* *******************************************************/
174176 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 );
179187 } 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 );
181194 }
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 );
187195 }
188196
189197 /* *******************************************************/
@@ -214,10 +222,13 @@ class Wippersnapper_ESP32 : public Wippersnapper {
214222 const char *connectionType () { return " ESP32" ; }
215223
216224protected:
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
221232
222233 const char *_aio_root_ca_staging =
223234 " -----BEGIN CERTIFICATE-----\n "
0 commit comments