Securing ESP32 Home Automation System with MQTT over SSL/TLS for Voice-Controlled Appliances

Am integrating the Llama 3 AI model with an ESP32-based home automation system, using various sensors like the DHT22, PIR, and magnetic reed switches) and protocols like MQTT and Wi-Fi to manage appliances via voice commands? Additionally, how do u guys feel in leaving system reliability and security for homes at the hands of an Ai? My question is what's the best security measures to ensure over the MQTTover SSL and TLS so as not to have unauthorized users
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

client.setServer(mqtt_server, 1883);

dht.begin();
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

float temp = dht.readTemperature();
float hum = dht.readHumidity();

if (isnan(temp) || isnan(hum)) {
Serial.println("Error reading from DHT22 sensor");
return;
}

String tempString = String(temp);
String humString = String(hum);

client.publish("home/temperature", tempString.c_str());
client.publish("home/humidity", humString.c_str());
delay(2000);
}

void reconnect() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client")) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
delay(5000);
}
}
}
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

client.setServer(mqtt_server, 1883);

dht.begin();
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

float temp = dht.readTemperature();
float hum = dht.readHumidity();

if (isnan(temp) || isnan(hum)) {
Serial.println("Error reading from DHT22 sensor");
return;
}

String tempString = String(temp);
String humString = String(hum);

client.publish("home/temperature", tempString.c_str());
client.publish("home/humidity", humString.c_str());
delay(2000);
}

void reconnect() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client")) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
delay(5000);
}
}
}
1 Reply
wafa_ath
wafa_ath5d ago
Hi , i recommend using WiFiClientSecure for encrypted communication Also what port are listening from ? I recommend port 8883 for butter security
Want results from more Discord servers?
Add your server