@@ -173,6 +173,9 @@ int failure_number_mqtt = 0; // number of failure connecting to MQTT
173173bool disc = true ; // Auto discovery with Home Assistant convention
174174#endif
175175unsigned long timer_led_measures = 0 ;
176+ static void * eClient = nullptr ;
177+ static bool mqttIsSecure = false ;
178+ static String mqttCert = " " ;
176179
177180#ifdef ESP32
178181# include < ArduinoOTA.h>
@@ -186,13 +189,8 @@ unsigned long timer_led_measures = 0;
186189void WiFiEvent (WiFiEvent_t event);
187190static bool esp32EthConnected = false ;
188191# endif
189- # ifdef SECURE_CONNECTION
190- # include < WiFiClientSecure.h>
191- WiFiClientSecure eClient;
192- # else
193- # include < WiFi.h>
194- WiFiClient eClient;
195- # endif
192+
193+ # include < WiFiClientSecure.h>
196194# include < WiFiMulti.h>
197195WiFiMulti wifiMulti;
198196# include < Preferences.h>
@@ -201,6 +199,7 @@ Preferences preferences;
201199# ifdef MDNS_SD
202200# include < ESPmDNS.h>
203201# endif
202+
204203#elif defined(ESP8266)
205204# include < ArduinoOTA.h>
206205# include < DNSServer.h>
@@ -209,26 +208,21 @@ Preferences preferences;
209208# include < ESP8266WiFiMulti.h>
210209# include < FS.h>
211210# include < WiFiManager.h>
212- # ifdef SECURE_CONNECTION
213- WiFiClientSecure eClient;
214- X509List caCert (certificate);
215- # else
216- WiFiClient eClient;
217- # endif
211+ X509List caCert;
218212ESP8266WiFiMulti wifiMulti;
219213# ifdef MDNS_SD
220214# include < ESP8266mDNS.h>
221215# endif
216+
222217#else
223218# include < Ethernet.h>
224- EthernetClient eClient;
225219#endif
226220
227221#define convertTemp_CtoF (c ) ((c * 1.8 ) + 32 )
228222#define convertTemp_FtoC (f ) ((f - 32 ) * 5 / 9 )
229223
230224// client link to pubsub mqtt
231- PubSubClient client (eClient) ;
225+ PubSubClient client;
232226
233227void revert_hex_data (const char * in, char * out, int l) {
234228 // reverting array 2 by 2 to get the data in good order
@@ -517,10 +511,10 @@ void connectMQTT() {
517511 failure_number_mqtt++; // we count the failure
518512 Log.warning (F (" failure_number_mqtt: %d" CR), failure_number_mqtt);
519513 Log.warning (F (" failed, rc=%d" CR), client.state ());
520- #if defined(SECURE_CONNECTION) && defined( ESP32)
521- Log.warning (F (" failed, ssl error code=%d" CR), eClient. lastError (nullptr , 0 ));
522- #elif defined(SECURE_CONNECTION) && defined( ESP8266)
523- Log.warning (F (" failed, ssl error code=%d" CR), eClient. getLastSSLError ());
514+ #if defined(ESP32)
515+ Log.warning (F (" failed, ssl error code=%d" CR), ((WiFiClientSecure*) eClient)-> lastError (nullptr , 0 ));
516+ #elif defined(ESP8266)
517+ Log.warning (F (" failed, ssl error code=%d" CR), ((WiFiClientSecure*) eClient)-> getLastSSLError ());
524518#endif
525519 digitalWrite (LED_INFO, LED_INFO_ON);
526520 delay (1000 );
@@ -590,9 +584,6 @@ void setup() {
590584# endif
591585
592586 setOTA ();
593- # ifdef SECURE_CONNECTION
594- setupTLS ();
595- # endif
596587#else // In case of arduino platform
597588
598589 // Launch serial for debugging purposes
@@ -617,6 +608,19 @@ void setup() {
617608 port = strtol (mqtt_port, NULL , 10 );
618609 Log.trace (F (" Port: %l" CR), port);
619610 Log.trace (F (" Mqtt server: %s" CR), mqtt_server);
611+ # if defined(ESP8266) || defined(ESP32)
612+ if (port == 8883 || port == 443 || mqttIsSecure) {
613+ eClient = new WiFiClientSecure;
614+ mqttIsSecure = true ;
615+ setupTLS ();
616+ } else {
617+ eClient = new WiFiClient;
618+ }
619+ # else
620+ eClient = new EthernetClient;
621+ # endif
622+
623+ client.setClient (*(Client*)eClient);
620624 client.setServer (mqtt_server, port);
621625#endif
622626
@@ -848,19 +852,24 @@ void setOTA() {
848852 ArduinoOTA.begin ();
849853}
850854
851- # ifdef SECURE_CONNECTION
852855void setupTLS () {
853- # if defined(NTP_SERVER)
854856 configTime (0 , 0 , NTP_SERVER);
855- # endif
856- # if defined(ESP32)
857- eClient.setCACert (certificate);
858- # elif defined(ESP8266)
859- eClient.setTrustAnchors (&caCert);
860- eClient.setBufferSizes (512 , 512 );
861- # endif
862- }
857+ WiFiClientSecure* sClient = (WiFiClientSecure*)eClient;
858+ if (mqttCert.length () > 0 ) {
859+ # if defined(ESP32)
860+ sClient ->setCACert (mqttCert.c_str ());
861+ } else {
862+ sClient ->setCACert (certificate);
863+ }
864+ # elif defined(ESP8266)
865+ caCert.append (mqttCert.c_str ());
866+ } else {
867+ caCert.append (certificate);
868+ }
869+ sClient ->setTrustAnchors (&caCert);
870+ sClient ->setBufferSizes (512 , 512 );
863871# endif
872+ }
864873#endif
865874
866875#if defined(ESPWifiManualSetup)
@@ -999,6 +1008,10 @@ void setup_wifimanager(bool reset_settings) {
9991008 strcpy (mqtt_pass, json[" mqtt_pass" ]);
10001009 if (json.containsKey (" mqtt_topic" ))
10011010 strcpy (mqtt_topic, json[" mqtt_topic" ]);
1011+ if (json.containsKey (" mqtt_broker_secure" ))
1012+ mqttIsSecure = json.get <bool >(" mqtt_broker_secure" );
1013+ if (json.containsKey (" mqtt_broker_cert" ))
1014+ mqttCert = json.get <const char *>(" mqtt_broker_cert" );
10021015 if (json.containsKey (" gateway_name" ))
10031016 strcpy (gateway_name, json[" gateway_name" ]);
10041017 } else {
@@ -1015,6 +1028,8 @@ void setup_wifimanager(bool reset_settings) {
10151028 WiFiManagerParameter custom_mqtt_user (" user" , " mqtt user" , mqtt_user, parameters_size);
10161029 WiFiManagerParameter custom_mqtt_pass (" pass" , " mqtt pass" , mqtt_pass, parameters_size * 2 );
10171030 WiFiManagerParameter custom_mqtt_topic (" topic" , " mqtt base topic" , mqtt_topic, mqtt_topic_max_size);
1031+ WiFiManagerParameter custom_mqtt_secure (" secure" , " mqtt secure" , " 1" , 1 , " type=\" checkbox\" " );
1032+ WiFiManagerParameter custom_mqtt_cert (" cert" , " mqtt broker cert" , mqttCert.c_str (), 1500 );
10181033 WiFiManagerParameter custom_gateway_name (" name" , " gateway name" , gateway_name, parameters_size * 2 );
10191034
10201035 // WiFiManager
@@ -1041,6 +1056,8 @@ void setup_wifimanager(bool reset_settings) {
10411056 wifiManager.addParameter (&custom_mqtt_port);
10421057 wifiManager.addParameter (&custom_mqtt_user);
10431058 wifiManager.addParameter (&custom_mqtt_pass);
1059+ wifiManager.addParameter (&custom_mqtt_secure);
1060+ wifiManager.addParameter (&custom_mqtt_cert);
10441061 wifiManager.addParameter (&custom_gateway_name);
10451062 wifiManager.addParameter (&custom_mqtt_topic);
10461063
@@ -1090,6 +1107,8 @@ void setup_wifimanager(bool reset_settings) {
10901107 strcpy (mqtt_pass, custom_mqtt_pass.getValue ());
10911108 strcpy (mqtt_topic, custom_mqtt_topic.getValue ());
10921109 strcpy (gateway_name, custom_gateway_name.getValue ());
1110+ mqttIsSecure = *custom_mqtt_secure.getValue ();
1111+ mqttCert = custom_mqtt_cert.getValue ();
10931112
10941113 // save the custom parameters to FS
10951114 if (shouldSaveConfig) {
@@ -1102,6 +1121,8 @@ void setup_wifimanager(bool reset_settings) {
11021121 json[" mqtt_pass" ] = mqtt_pass;
11031122 json[" mqtt_topic" ] = mqtt_topic;
11041123 json[" gateway_name" ] = gateway_name;
1124+ json[" mqtt_broker_secure" ] = mqttIsSecure;
1125+ json[" mqtt_broker_cert" ] = mqttCert;
11051126
11061127 File configFile = SPIFFS.open (" /config.json" , " w" );
11071128 if (!configFile) {
@@ -1623,12 +1644,11 @@ void receivingMQTT(char* topicOri, char* datacallback) {
16231644}
16241645
16251646#ifdef MQTT_HTTPS_FW_UPDATE
1626- # ifndef NTP_SERVER
1627- # error no NTP_SERVER defined
1628- # endif
16291647# include < WiFiClientSecure.h>
1648+
1649+ # include " Ota_github.h"
1650+
16301651# ifdef ESP32
1631- # include " Ota_github.h"
16321652# include " zzHTTPUpdate.h"
16331653# elif ESP8266
16341654# include < ESP8266httpUpdate.h>
@@ -1648,10 +1668,6 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
16481668 return ;
16491669 }
16501670
1651- # if MQTT_HTTPS_FW_UPDATE_USE_PASSWORD > 0
1652- # ifndef SECURE_CONNECTION
1653- # warning using a password with an unsecure MQTT connection will send it as clear text!!!
1654- # endif
16551671 const char * pwd = HttpsFwUpdateData[" password" ];
16561672 if (pwd) {
16571673 if (strcmp (pwd, ota_password) != 0 ) {
@@ -1662,7 +1678,6 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
16621678 Log.error (F (" No password sent" CR));
16631679 return ;
16641680 }
1665- # endif
16661681
16671682 Log.warning (F (" Starting firmware update" CR));
16681683
@@ -1683,38 +1698,46 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
16831698
16841699 } else {
16851700 WiFiClientSecure update_client;
1686- # ifdef SECURE_CONNECTION
1687- client.disconnect ();
1688- update_client = eClient;
1689- # else
1690- configTime (0 , 0 , NTP_SERVER);
1691- time_t now = time (nullptr );
1692- uint8_t count = 0 ;
1693- Log.trace (F (" Waiting for NTP time sync" CR));
1694- while ((now < 8 * 3600 * 2 ) && count++ < 60 ) {
1695- vTaskDelay (500 );
1696- now = time (nullptr );
1701+ if (mqttIsSecure) {
1702+ client.disconnect ();
1703+ update_client = *(WiFiClientSecure*)eClient;
1704+ } else {
1705+ configTime (0 , 0 , NTP_SERVER);
1706+ time_t now = time (nullptr );
1707+ uint8_t count = 0 ;
1708+ Log.trace (F (" Waiting for NTP time sync" CR));
1709+ while ((now < 8 * 3600 * 2 ) && count++ < 60 ) {
1710+ delay (500 );
1711+ now = time (nullptr );
1712+ }
1713+
1714+ if (count >= 60 ) {
1715+ Log.error (F (" Unable to update - invalid time" CR));
1716+ # if defined(ZgatewayBT) && defined(ESP32)
1717+ startProcessing ();
1718+ # endif
1719+ return ;
1720+ }
16971721 }
16981722
1699- if (count >= 60 ) {
1700- Log.error (F (" Unable to update - invalid time" CR));
1701- # if defined(ZgatewayBT) && defined(ESP32)
1702- startProcessing ();
1703- # endif
1704- return ;
1705- }
1706- # endif
17071723# ifdef ESP32
17081724 if (strstr (url, " github" ) != 0 ) {
17091725 update_client.setCACert (_github_cert);
17101726 } else {
17111727 update_client.setCACert (https_fw_server_cert);
17121728 }
1729+
17131730 update_client.setTimeout (12 );
17141731 httpUpdate.setFollowRedirects (HTTPC_STRICT_FOLLOW_REDIRECTS);
17151732 result = httpUpdate.update (update_client, url);
17161733# elif ESP8266
1717- update_client.setInsecure (); // TODO: replace with cert checking
1734+ if (strstr (url, " github" ) != 0 ) {
1735+ caCert.append (_github_cert);
1736+ } else {
1737+ caCert.append (https_fw_server_cert);
1738+ }
1739+
1740+ update_client.setTrustAnchors (&caCert);
17181741 update_client.setTimeout (12000 );
17191742 ESPhttpUpdate.setFollowRedirects (HTTPC_STRICT_FOLLOW_REDIRECTS);
17201743 result = ESPhttpUpdate.update (update_client, url);
0 commit comments