now with TLS and mqtt login

This commit is contained in:
KS_HTK 2024-08-07 07:59:46 +02:00
parent 1107bb5d30
commit 8191aacd31
2 changed files with 17 additions and 4 deletions

View file

@ -3,4 +3,7 @@ build_flags =
-D WIFI_SSID=\"wifi ssid\"
-D WIFI_PASS=\"wifi password\"
-D MQTT_SERVER=\"mqtt broker address\"
-D MQTT_PORT=mqtt port number
-D MQTT_PORT=mqtt port number
-D MQTT_USER=\"mqttuser\"
-D MQTT_PASS=\"mqttpass\"
-D CA_CERT=\"-----BEGIN CERTIFICATE-----\nYOUR_CERT_HERE\n-----END CERTIFICATE-----\n\"

View file

@ -1,4 +1,5 @@
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
// Update these with your WiFi credentials
@ -7,11 +8,16 @@ const char* password = WIFI_PASS;
// Update these with your MQTT Broker details
const char* mqtt_server = MQTT_SERVER;
const unsigned int mqtt_port = MQTT_PORT; // Default port
const unsigned int mqtt_port = MQTT_PORT;
const char* mqtt_user = MQTT_USER;
const char* mqtt_pass = MQTT_PASS;
// Certificate
const char* ca_cert = CA_CERT
const char* mqtt_topic = "backdoor/control";
WiFiClient espClient;
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup_wifi() {
@ -26,6 +32,10 @@ void setup_wifi() {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Load certificate
X509List cert(ca_cert);
espClient.setTrustAnchors(&cert);
}
void callback(char* topic, byte* payload, unsigned int length) {
@ -54,7 +64,7 @@ void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client", mqtt_user, mqtt_pass)) {
Serial.println("connected");
// Subscribe
client.subscribe(mqtt_topic);