Skip to content

Commit 9a75742

Browse files
authored
Merge pull request #606 from adafruit/esp32-iface-updates
Updates for the ESP32 network interface
2 parents 7121b05 + e539047 commit 9a75742

File tree

3 files changed

+81
-62
lines changed

3 files changed

+81
-62
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ board_build.core = earlephilhower
107107
board_build.filesystem_size = 0.5m
108108
build_flags = -DUSE_TINYUSB
109109
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
110-
lib_ignore = WiFiNINA, Adafruit Zero DMA Library
110+
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
111111
lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040
112112

113113
; ESP32-x Boards ;

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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
*
@@ -25,7 +25,8 @@
2525
#include "Arduino.h"
2626
#include "WiFi.h"
2727
#include "WiFiMulti.h"
28-
#include <WiFiClientSecure.h>
28+
#include <NetworkClient.h>
29+
#include <NetworkClientSecure.h>
2930
extern 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
/********************************************************/
@@ -156,18 +158,24 @@ class Wippersnapper_ESP32 : public Wippersnapper {
156158
*/
157159
/********************************************************/
158160
void setupMQTTClient(const char *clientID) {
159-
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0) {
160-
_mqtt_client->setCACert(_aio_root_ca_prod);
161-
} else if (strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
162-
_mqtt_client->setCACert(_aio_root_ca_staging);
161+
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0 ||
162+
strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
163+
_mqtt_client_secure = new NetworkClientSecure();
164+
_mqtt_client_secure->setCACert(
165+
strcmp(WS._config.aio_url, "io.adafruit.com") == 0
166+
? _aio_root_ca_prod
167+
: _aio_root_ca_staging);
168+
WS._mqtt = new Adafruit_MQTT_Client(
169+
_mqtt_client_secure, WS._config.aio_url, WS._config.io_port, clientID,
170+
WS._config.aio_user, WS._config.aio_key);
163171
} else {
164-
_mqtt_client->setInsecure();
172+
// Insecure connections require a NetworkClient object rather than a
173+
// NetworkClientSecure object
174+
_mqtt_client_insecure = new NetworkClient();
175+
WS._mqtt = new Adafruit_MQTT_Client(
176+
_mqtt_client_insecure, WS._config.aio_url, WS._config.io_port,
177+
clientID, WS._config.aio_user, WS._config.aio_key);
165178
}
166-
167-
// Construct MQTT client
168-
WS._mqtt = new Adafruit_MQTT_Client(
169-
_mqtt_client, WS._config.aio_url, WS._config.io_port, clientID,
170-
WS._config.aio_user, WS._config.aio_key);
171179
}
172180

173181
/********************************************************/
@@ -198,37 +206,40 @@ class Wippersnapper_ESP32 : public Wippersnapper {
198206
const char *connectionType() { return "ESP32"; }
199207

200208
protected:
201-
const char *_ssid; ///< WiFi SSID
202-
const char *_pass; ///< WiFi password
203-
WiFiClientSecure *_mqtt_client; ///< Pointer to a WiFi client object (TLS/SSL)
204-
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
209+
const char *_ssid; ///< WiFi SSID
210+
const char *_pass; ///< WiFi password
211+
NetworkClientSecure
212+
*_mqtt_client_secure; ///< Pointer to a secure network client object
213+
NetworkClient
214+
*_mqtt_client_insecure; ///< Pointer to an insecure network client object
215+
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
205216

206217
const char *_aio_root_ca_staging =
207218
"-----BEGIN CERTIFICATE-----\n"
208-
"MIIEZTCCA02gAwIBAgIQQAF1BIMUpMghjISpDBbN3zANBgkqhkiG9w0BAQsFADA/\n"
209-
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
210-
"DkRTVCBSb290IENBIFgzMB4XDTIwMTAwNzE5MjE0MFoXDTIxMDkyOTE5MjE0MFow\n"
211-
"MjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxCzAJBgNVBAMT\n"
212-
"AlIzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuwIVKMz2oJTTDxLs\n"
213-
"jVWSw/iC8ZmmekKIp10mqrUrucVMsa+Oa/l1yKPXD0eUFFU1V4yeqKI5GfWCPEKp\n"
214-
"Tm71O8Mu243AsFzzWTjn7c9p8FoLG77AlCQlh/o3cbMT5xys4Zvv2+Q7RVJFlqnB\n"
215-
"U840yFLuta7tj95gcOKlVKu2bQ6XpUA0ayvTvGbrZjR8+muLj1cpmfgwF126cm/7\n"
216-
"gcWt0oZYPRfH5wm78Sv3htzB2nFd1EbjzK0lwYi8YGd1ZrPxGPeiXOZT/zqItkel\n"
217-
"/xMY6pgJdz+dU/nPAeX1pnAXFK9jpP+Zs5Od3FOnBv5IhR2haa4ldbsTzFID9e1R\n"
218-
"oYvbFQIDAQABo4IBaDCCAWQwEgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8E\n"
219-
"BAMCAYYwS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V7\n"
236+
"s8ubknRzs+3C/Bm19RFOoiPpDkwvyNfvmQ14XkyqqKK5oZ8zhD32kFRQkxa8uZSu\n"
237+
"h4aTImFxknu39waBxIRXE4jKxlAmQc4QjFZoq1KmQqQg0J/1JF8RlFvJas1VcjLv\n"
238+
"YlvUB2t6npO6oQjB3l+PNf0DpQH7iUx3Wz5AjQCi6L25FjyE06q6BZ/QlmtYdl/8\n"
239+
"ZYao4SRqPEs/6cAiF+Qf5zg2UkaWtDphl1LKMuTNLotvsX99HP69V2faNyegodQ0\n"
240+
"LyTApr/vT01YPE46vNsDLgK+4cL6TrzC/a4WcmF5SRJ938zrv/duJHLXQIku5v0+\n"
241+
"EwOy59Hdm0PT/Er/84dDV0CSjdR/2XuZM3kpysSKLgD1cKiDA+IRguODCxfO9cyY\n"
242+
"Ig46v9mFmBvyH04=\n"
232243
"-----END CERTIFICATE-----\n"; ///< Root certificate for io.adafruit.us
233244

234245
const char *_aio_root_ca_prod =

src/network_interfaces/ws_networking_pico.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "Adafruit_MQTT.h"
2424
#include "Adafruit_MQTT_Client.h"
2525
#include "Arduino.h"
26+
#include <WiFiClient.h>
2627
#include <WiFiClientSecure.h>
2728
extern Wippersnapper WS;
2829

@@ -42,7 +43,6 @@ class ws_networking_pico : public Wippersnapper {
4243
ws_networking_pico() : Wippersnapper() {
4344
_ssid = 0;
4445
_pass = 0;
45-
_mqtt_client = new WiFiClientSecure;
4646
}
4747

4848
/**************************************************************************/
@@ -51,8 +51,10 @@ class ws_networking_pico : public Wippersnapper {
5151
*/
5252
/**************************************************************************/
5353
~ws_networking_pico() {
54-
if (_mqtt_client)
55-
delete _mqtt_client;
54+
if (_mqtt_client_secure)
55+
delete _mqtt_client_secure;
56+
if (_mqtt_client_secure)
57+
delete _mqtt_client_secure;
5658
}
5759

5860
/********************************************************/
@@ -154,19 +156,22 @@ class ws_networking_pico : public Wippersnapper {
154156
*/
155157
/********************************************************/
156158
void setupMQTTClient(const char *clientID) {
157-
// Set CA cert depending on the server we're connecting to
158-
// compare WS._config.aio_url to "io.adafruit.com"
159-
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0) {
160-
_mqtt_client->setCACert(_aio_root_ca_prod);
161-
} else if (strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
162-
_mqtt_client->setCACert(_aio_root_ca_staging);
159+
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0 ||
160+
strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
161+
_mqtt_client_secure = new WiFiClientSecure();
162+
_mqtt_client_secure->setCACert(
163+
strcmp(WS._config.aio_url, "io.adafruit.com") == 0
164+
? _aio_root_ca_prod
165+
: _aio_root_ca_staging);
166+
WS._mqtt = new Adafruit_MQTT_Client(
167+
_mqtt_client_secure, WS._config.aio_url, WS._config.io_port, clientID,
168+
WS._config.aio_user, WS._config.aio_key);
163169
} else {
164-
_mqtt_client->setInsecure();
170+
_mqtt_client_insecure = new WiFiClient();
171+
WS._mqtt = new Adafruit_MQTT_Client(
172+
_mqtt_client_insecure, WS._config.aio_url, WS._config.io_port,
173+
clientID, WS._config.aio_user, WS._config.aio_key);
165174
}
166-
167-
WS._mqtt = new Adafruit_MQTT_Client(
168-
_mqtt_client, WS._config.aio_url, WS._config.io_port, clientID,
169-
WS._config.aio_user, WS._config.aio_key);
170175
}
171176

172177
/********************************************************/
@@ -197,10 +202,13 @@ class ws_networking_pico : public Wippersnapper {
197202
const char *connectionType() { return "Pico"; }
198203

199204
protected:
200-
const char *_ssid; ///< WiFi SSID
201-
const char *_pass; ///< WiFi password
202-
WiFiClientSecure *_mqtt_client; ///< Pointer to a secure MQTT client object
203-
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
205+
const char *_ssid; ///< WiFi SSID
206+
const char *_pass; ///< WiFi password
207+
WiFiClient
208+
*_mqtt_client_insecure; ///< Pointer to an insecure WiFi client object
209+
WiFiClientSecure
210+
*_mqtt_client_secure; ///< Pointer to a secure WiFi client object
211+
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
204212

205213
const char *_aio_root_ca_staging =
206214
"-----BEGIN CERTIFICATE-----\n"

0 commit comments

Comments
 (0)