From 7481b1f4f013b016cc6f08d02ee50d273a52479f Mon Sep 17 00:00:00 2001 From: rodriguezst <2828844+rodriguezst@users.noreply.github.com> Date: Fri, 10 Jun 2022 09:31:20 +0000 Subject: [PATCH] Implemented MQTT over TLS for WiFiDevice --- CMakeLists.txt | 1 + Network.cpp | 4 ++-- networkDevices/WifiDevice.cpp | 30 +++++++++++++++++++++++++----- networkDevices/WifiDevice.h | 9 ++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b8f3d2..a9816e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,7 @@ target_link_arduino_libraries(${PROJECT_NAME} PRIVATE core WiFi + WiFiClientSecure Update # WebServer DNSServer diff --git a/Network.cpp b/Network.cpp index 0339e76..61435fc 100644 --- a/Network.cpp +++ b/Network.cpp @@ -42,11 +42,11 @@ void Network::setupDevice(const NetworkDeviceType hardware) break; case NetworkDeviceType::WiFi: Serial.println(F("Network device: Builtin WiFi")); - _device = new WifiDevice(_hostname); + _device = new WifiDevice(_hostname, _preferences); break; default: Serial.println(F("Unknown network device type, defaulting to WiFi")); - _device = new WifiDevice(_hostname); + _device = new WifiDevice(_hostname, _preferences); break; } } diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 87633f1..ad940ba 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -1,15 +1,35 @@ #include #include "WifiDevice.h" #include "WiFiManager.h" +#include "../PreferencesKeys.h" -WifiDevice::WifiDevice(const String& hostname) -: NetworkDevice(hostname), - _mqttClient(_wifiClient) -{} +WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) +: NetworkDevice(hostname) +{ + String MQTT_CA = _preferences->getString(preference_mqtt_ca); + String MQTT_CRT = _preferences->getString(preference_mqtt_crt); + String MQTT_KEY = _preferences->getString(preference_mqtt_key); + + if(MQTT_CA.length() > 0) + { + _wifiClientSecure = new WiFiClientSecure(); + _wifiClientSecure->setCACert(MQTT_CA.c_str()); + if(MQTT_CRT.length() > 0 && MQTT_KEY.length() > 0) + { + _wifiClientSecure->setCertificate(MQTT_CRT.c_str()); + _wifiClientSecure->setPrivateKey(MQTT_KEY.c_str()); + } + _mqttClient = new PubSubClient(*_wifiClientSecure); + } else + { + _wifiClient = new WiFiClient(); + _mqttClient = new PubSubClient(*_wifiClient); + } +} PubSubClient *WifiDevice::mqttClient() { - return &_mqttClient; + return _mqttClient; } void WifiDevice::initialize() diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 4680210..d789386 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -1,13 +1,15 @@ #pragma once #include +#include +#include #include "NetworkDevice.h" #include "../SpiffsCookie.h" class WifiDevice : public NetworkDevice { public: - WifiDevice(const String& hostname); + WifiDevice(const String& hostname, Preferences* _preferences); virtual void initialize(); virtual void reconfigure(); @@ -20,7 +22,8 @@ public: virtual PubSubClient *mqttClient(); private: - WiFiClient _wifiClient; - PubSubClient _mqttClient; + WiFiClient* _wifiClient = nullptr; + WiFiClientSecure* _wifiClientSecure = nullptr; + PubSubClient* _mqttClient = nullptr; SpiffsCookie _cookie; };