Skip to content

Commit

Permalink
Merge pull request #606 from adafruit/esp32-iface-updates
Browse files Browse the repository at this point in the history
Updates for the ESP32 network interface
  • Loading branch information
brentru authored Jul 25, 2024
2 parents 7121b05 + e539047 commit 9a75742
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 62 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ board_build.core = earlephilhower
board_build.filesystem_size = 0.5m
build_flags = -DUSE_TINYUSB
; Once https://github.com/platformio/platformio-core > 6.1.11 these can be removed
lib_ignore = WiFiNINA, Adafruit Zero DMA Library
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040

; ESP32-x Boards ;
Expand Down
97 changes: 54 additions & 43 deletions src/network_interfaces/Wippersnapper_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
* Copyright (c) Brent Rubell 2020-2024 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
Expand All @@ -25,7 +25,8 @@
#include "Arduino.h"
#include "WiFi.h"
#include "WiFiMulti.h"
#include <WiFiClientSecure.h>
#include <NetworkClient.h>
#include <NetworkClientSecure.h>
extern Wippersnapper WS;

/****************************************************************************/
Expand All @@ -44,7 +45,6 @@ class Wippersnapper_ESP32 : public Wippersnapper {
Wippersnapper_ESP32() : Wippersnapper() {
_ssid = 0;
_pass = 0;
_mqtt_client = new WiFiClientSecure;
}

/**************************************************************************/
Expand All @@ -53,8 +53,10 @@ class Wippersnapper_ESP32 : public Wippersnapper {
*/
/**************************************************************************/
~Wippersnapper_ESP32() {
if (_mqtt_client)
delete _mqtt_client;
if (_mqtt_client_secure)
delete _mqtt_client_secure;
if (_mqtt_client_insecure)
delete _mqtt_client_insecure;
}

/********************************************************/
Expand Down Expand Up @@ -156,18 +158,24 @@ class Wippersnapper_ESP32 : public Wippersnapper {
*/
/********************************************************/
void setupMQTTClient(const char *clientID) {
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0) {
_mqtt_client->setCACert(_aio_root_ca_prod);
} else if (strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
_mqtt_client->setCACert(_aio_root_ca_staging);
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0 ||
strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
_mqtt_client_secure = new NetworkClientSecure();
_mqtt_client_secure->setCACert(
strcmp(WS._config.aio_url, "io.adafruit.com") == 0
? _aio_root_ca_prod
: _aio_root_ca_staging);
WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client_secure, WS._config.aio_url, WS._config.io_port, clientID,
WS._config.aio_user, WS._config.aio_key);
} else {
_mqtt_client->setInsecure();
// Insecure connections require a NetworkClient object rather than a
// NetworkClientSecure object
_mqtt_client_insecure = new NetworkClient();
WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client_insecure, WS._config.aio_url, WS._config.io_port,
clientID, WS._config.aio_user, WS._config.aio_key);
}

// Construct MQTT client
WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client, WS._config.aio_url, WS._config.io_port, clientID,
WS._config.aio_user, WS._config.aio_key);
}

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

protected:
const char *_ssid; ///< WiFi SSID
const char *_pass; ///< WiFi password
WiFiClientSecure *_mqtt_client; ///< Pointer to a WiFi client object (TLS/SSL)
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
const char *_ssid; ///< WiFi SSID
const char *_pass; ///< WiFi password
NetworkClientSecure
*_mqtt_client_secure; ///< Pointer to a secure network client object
NetworkClient
*_mqtt_client_insecure; ///< Pointer to an insecure network client object
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network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v0+\n"
"EwOy59Hdm0PT/Er/84dDV0CSjdR/2XuZM3kpysSKLgD1cKiDA+IRguODCxfO9cyY\n"
"Ig46v9mFmBvyH04=\n"
"-----END CERTIFICATE-----\n"; ///< Root certificate for io.adafruit.us

const char *_aio_root_ca_prod =
Expand Down
44 changes: 26 additions & 18 deletions src/network_interfaces/ws_networking_pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
extern Wippersnapper WS;

Expand All @@ -42,7 +43,6 @@ class ws_networking_pico : public Wippersnapper {
ws_networking_pico() : Wippersnapper() {
_ssid = 0;
_pass = 0;
_mqtt_client = new WiFiClientSecure;
}

/**************************************************************************/
Expand All @@ -51,8 +51,10 @@ class ws_networking_pico : public Wippersnapper {
*/
/**************************************************************************/
~ws_networking_pico() {
if (_mqtt_client)
delete _mqtt_client;
if (_mqtt_client_secure)
delete _mqtt_client_secure;
if (_mqtt_client_secure)
delete _mqtt_client_secure;
}

/********************************************************/
Expand Down Expand Up @@ -154,19 +156,22 @@ class ws_networking_pico : public Wippersnapper {
*/
/********************************************************/
void setupMQTTClient(const char *clientID) {
// Set CA cert depending on the server we're connecting to
// compare WS._config.aio_url to "io.adafruit.com"
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0) {
_mqtt_client->setCACert(_aio_root_ca_prod);
} else if (strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
_mqtt_client->setCACert(_aio_root_ca_staging);
if (strcmp(WS._config.aio_url, "io.adafruit.com") == 0 ||
strcmp(WS._config.aio_url, "io.adafruit.us") == 0) {
_mqtt_client_secure = new WiFiClientSecure();
_mqtt_client_secure->setCACert(
strcmp(WS._config.aio_url, "io.adafruit.com") == 0
? _aio_root_ca_prod
: _aio_root_ca_staging);
WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client_secure, WS._config.aio_url, WS._config.io_port, clientID,
WS._config.aio_user, WS._config.aio_key);
} else {
_mqtt_client->setInsecure();
_mqtt_client_insecure = new WiFiClient();
WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client_insecure, WS._config.aio_url, WS._config.io_port,
clientID, WS._config.aio_user, WS._config.aio_key);
}

WS._mqtt = new Adafruit_MQTT_Client(
_mqtt_client, WS._config.aio_url, WS._config.io_port, clientID,
WS._config.aio_user, WS._config.aio_key);
}

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

protected:
const char *_ssid; ///< WiFi SSID
const char *_pass; ///< WiFi password
WiFiClientSecure *_mqtt_client; ///< Pointer to a secure MQTT client object
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode
const char *_ssid; ///< WiFi SSID
const char *_pass; ///< WiFi password
WiFiClient
*_mqtt_client_insecure; ///< Pointer to an insecure WiFi client object
WiFiClientSecure
*_mqtt_client_secure; ///< Pointer to a secure WiFi client object
WiFiMulti _wifiMulti; ///< WiFiMulti object for multi-network mode

const char *_aio_root_ca_staging =
"-----BEGIN CERTIFICATE-----\n"
Expand Down

0 comments on commit 9a75742

Please sign in to comment.