Commit 733a7b7f authored by Eric Duminil's avatar Eric Duminil
Browse files

MQTT encryption. Hackish

parent 478c4896
...@@ -21,16 +21,15 @@ namespace config { ...@@ -21,16 +21,15 @@ namespace config {
const unsigned long wait_after_fail = 900; // [s] Wait 15 minutes after an MQTT connection fail, before trying again. const unsigned long wait_after_fail = 900; // [s] Wait 15 minutes after an MQTT connection fail, before trying again.
} }
#if MQTT_ENCRYPTED #if defined(ESP32)
# if defined(ESP32)
# include <WiFiClientSecure.h> # include <WiFiClientSecure.h>
# endif
WiFiClientSecure espClient;
#else
WiFiClient espClient;
#endif #endif
PubSubClient mqttClient(espClient); //TODO: Find better way. Only one of both will be used
WiFiClientSecure secureClient;
WiFiClient espClient;
PubSubClient mqttClient;
namespace mqtt { namespace mqtt {
unsigned long last_sent_at = 0; unsigned long last_sent_at = 0;
...@@ -44,11 +43,16 @@ namespace mqtt { ...@@ -44,11 +43,16 @@ namespace mqtt {
void initialize(const char *sensorId) { void initialize(const char *sensorId) {
json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}"); json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}");
snprintf(publish_topic, sizeof(publish_topic), "CO2sensors/%s", sensorId); snprintf(publish_topic, sizeof(publish_topic), "CO2sensors/%s", sensorId);
#if MQTT_ENCRYPTED
if (config::mqtt_encryption) {
// The sensor doesn't check the fingerprint of the MQTT broker, because otherwise this fingerprint should be updated // The sensor doesn't check the fingerprint of the MQTT broker, because otherwise this fingerprint should be updated
// on the sensor every 3 months. The connection can still be encrypted, though: // on the sensor every 3 months. The connection can still be encrypted, though:
espClient.setInsecure(); // If not available for ESP32, please update Arduino IDE / PlatformIO secureClient.setInsecure(); // If not available for ESP32, please update Arduino IDE / PlatformIO
#endif mqttClient.setClient(secureClient);
} else {
mqttClient.setClient(espClient);
}
mqttClient.setServer(config::mqtt_server, config::mqtt_port); mqttClient.setServer(config::mqtt_server, config::mqtt_port);
sensor_console::defineIntCommand("mqtt", setMQTTinterval, F("60 (Sets MQTT sending interval, in s)")); sensor_console::defineIntCommand("mqtt", setMQTTinterval, F("60 (Sets MQTT sending interval, in s)"));
...@@ -110,7 +114,7 @@ namespace mqtt { ...@@ -110,7 +114,7 @@ namespace mqtt {
Serial.print(F("MQTT - Attempting connection to ")); Serial.print(F("MQTT - Attempting connection to "));
Serial.print(config::mqtt_server); Serial.print(config::mqtt_server);
Serial.print(MQTT_ENCRYPTED ? F(" (Encrypted") : F(" (Unencrypted")); Serial.print(config::mqtt_encryption ? F(" (Encrypted") : F(" (Unencrypted"));
Serial.print(F(", port ")); Serial.print(F(", port "));
Serial.print(config::mqtt_port); Serial.print(config::mqtt_port);
Serial.print(F(") ")); Serial.print(F(") "));
......
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
#include <stdint.h> // For uint32_t & uint16_t #include <stdint.h> // For uint32_t & uint16_t
#include "config.h" #include "config.h"
#if !defined(MQTT_ENCRYPTED)
# define MQTT_ENCRYPTED true // Old config files might not define it, and encryption was on by default.
#endif
namespace mqtt { namespace mqtt {
extern char last_successful_publish[]; extern char last_successful_publish[];
......
...@@ -102,6 +102,10 @@ namespace web_config { ...@@ -102,6 +102,10 @@ namespace web_config {
Builder<IntTParameter<uint16_t>>("mqtt_timestep").label("MQTT timestep").defaultValue( Builder<IntTParameter<uint16_t>>("mqtt_timestep").label("MQTT timestep").defaultValue(
MQTT_SENDING_INTERVAL).min(0).step(1).defaultValue(300).placeholder("[s]").build(); MQTT_SENDING_INTERVAL).min(0).step(1).defaultValue(300).placeholder("[s]").build();
#if !defined(MQTT_ENCRYPTED)
# define MQTT_ENCRYPTED true // Old config files might not define it, and encryption was on by default.
#endif
CheckboxTParameter mqttEncryptionParam = CheckboxTParameter mqttEncryptionParam =
Builder<CheckboxTParameter>("mqtt_encryption").label("Encrypt MQTT?").defaultValue(MQTT_ENCRYPTED).build(); Builder<CheckboxTParameter>("mqtt_encryption").label("Encrypt MQTT?").defaultValue(MQTT_ENCRYPTED).build();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment