Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for secure MQTT connections with TLS #106

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions EleksTubeHAX_pio/src/Mqtt_client_ips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
*/

#include "Mqtt_client_ips.h"
#include "WiFi.h" // for ESP32
#include <WiFi.h> // for ESP32
#include <PubSubClient.h> // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client
#include <ArduinoJson.h>
#include "TempSensor.h"
#include "TFTs.h"
#include "Backlights.h"
#include "Clock.h"
#ifdef MQTT_USE_TLS
#include <WiFiClientSecure.h> // for secure WiFi client

WiFiClientSecure espClient;
#else
WiFiClient espClient;
#endif
PubSubClient MQTTclient(espClient);

#define concat2(first, second) first second
#define concat3(first, second, third) first second third
#define concat4(first, second, third, fourth) first second third fourth

WiFiClient espClient;
PubSubClient MQTTclient(espClient);

#define MQTT_STATE_ON "ON"
#define MQTT_STATE_OFF "OFF"

Expand Down Expand Up @@ -320,6 +325,54 @@ void MqttReportState(bool force)
#endif
}

#ifdef MQTT_USE_TLS
bool loadCARootCert()
{
const char *filename = "/mqtt-ca-root.pem";
Serial.println("Loading CA Root Certificate");

// Check if the PEM file exists
if (!SPIFFS.exists(filename))
{
Serial.println("ERROR: File not found mqtt-ca-root.pem");
return false;
}

// Open the PEM file in read mode
File file = SPIFFS.open(filename, "r");
if (!file)
{
Serial.println("ERROR: Failed to open mqtt-ca-root.pem");
return false;
}

// Get the size of the file
size_t size = file.size();
if (size == 0)
{
Serial.println("ERROR: Empty mqtt-ca-root.pem");
file.close();
return false;
}

// Use the loadCA() method to load the certificate directly from the file stream
bool result = espClient.loadCACert(file, size);

file.close();

if (result)
{
Serial.println("CA Root Certificate loaded successfully");
}
else
{
Serial.println("ERROR: Failed to load CA Root Certificate");
}

return result;
}
#endif

void MqttStart()
{
#ifdef MQTT_ENABLED
Expand All @@ -330,7 +383,15 @@ void MqttStart()
MQTTclient.setServer(MQTT_BROKER, MQTT_PORT);
MQTTclient.setCallback(callback);
MQTTclient.setBufferSize(2048);
#ifdef MQTT_USE_TLS
bool result = loadCARootCert();
if (!result)
{
return; // load certificate failed -> do not continue
}
#endif

Serial.println("");
Serial.println("Connecting to MQTT...");
if (MQTTclient.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD))
{
Expand All @@ -341,14 +402,15 @@ void MqttStart()
{
if (MQTTclient.state() == 5)
{
Serial.println("Connection not allowed by broker, possible reasons:");
Serial.println("Error: Connection not allowed by broker, possible reasons:");
Serial.println("- Device is already online. Wait some seconds until it appears offline");
Serial.println("- Wrong Username or password. Check credentials");
Serial.println("- Client Id does not belong to this username, verify ClientId");
}
else
{
Serial.print("Not possible to connect to Broker Error code:");
Serial.println("Error: Not possible to connect to Broker!");
Serial.print("Error code:");
Serial.println(MQTTclient.state());
}
return; // do not continue if not connected
Expand Down
1 change: 1 addition & 0 deletions EleksTubeHAX_pio/src/Mqtt_client_ips.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define mqtt_client_H_

#include "GLOBAL_DEFINES.h"
#include <FS.h>

extern bool MqttConnected;

Expand Down
7 changes: 5 additions & 2 deletions EleksTubeHAX_pio/src/_USER_DEFINES - empty.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
#define GEOLOCATION_API_KEY "__enter_your_api_key_here__"

// ************* MQTT config *************
// #define MQTT_ENABLED // enable general MQTT support
// #define MQTT_ENABLED // enable general MQTT support
#define MQTT_SAVE_PREFERENCES_AFTER_SEC 60 // auto save config X seconds after last MQTT message received

// --- MQTT Home Assistant settings ---
// You will either need a local MQTT broker to use MQTT with Home Assistant (e.g. Mosquitto) or use an internet-based broker with Home Assistant support.
Expand Down Expand Up @@ -69,7 +70,9 @@
#define MQTT_USERNAME "__enter_your_username_here__" // Username from Smartnest
#define MQTT_PASSWORD "__enter_your_api_key_here__" // Password from Smartnest or API key (under MY Account)
#define MQTT_CLIENT "__enter_your_device_id_here__" // Device Id from Smartnest
#define MQTT_SAVE_PREFERENCES_AFTER_SEC 60
// #define MQTT_USE_TLS // Use TLS for MQTT connection. Setting a root CA certificate is needed!
// Don't forget to copy the correct certificate file into the 'data' folder and rename it to mqtt-ca-root.pem!
// Example CA cert (Let's Encrypt CA cert) can be found in the 'data - other graphics' subfolder in the root of this repo

// ************* Optional temperature sensor *************
// #define ONE_WIRE_BUS_PIN 4 // DS18B20 connected to GPIO4; comment this line if sensor is not connected
Expand Down
4 changes: 2 additions & 2 deletions EleksTubeHAX_pio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ void setup()
// Setup MQTT
tfts.setTextColor(TFT_YELLOW, TFT_BLACK);
tfts.print("MQTT start...");
Serial.print("MQTT start...");
Serial.println("MQTT start...");
MqttStart();
tfts.println("Done!");
Serial.println("Done!");
Serial.println("MQTT start Done!");
tfts.setTextColor(TFT_WHITE, TFT_BLACK);

#ifdef GEOLOCATION_ENABLED
Expand Down
31 changes: 31 additions & 0 deletions data - other graphics/mqtt-ca-root.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQY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