now with TLS and mqtt login
This commit is contained in:
parent
1107bb5d30
commit
8191aacd31
2 changed files with 17 additions and 4 deletions
|
@ -4,3 +4,6 @@ build_flags =
|
||||||
-D WIFI_PASS=\"wifi password\"
|
-D WIFI_PASS=\"wifi password\"
|
||||||
-D MQTT_SERVER=\"mqtt broker address\"
|
-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\"
|
16
src/main.c++
16
src/main.c++
|
@ -1,4 +1,5 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiClientSecure.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
// Update these with your WiFi credentials
|
// Update these with your WiFi credentials
|
||||||
|
@ -7,11 +8,16 @@ const char* password = WIFI_PASS;
|
||||||
|
|
||||||
// Update these with your MQTT Broker details
|
// Update these with your MQTT Broker details
|
||||||
const char* mqtt_server = MQTT_SERVER;
|
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";
|
const char* mqtt_topic = "backdoor/control";
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClientSecure espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
void setup_wifi() {
|
void setup_wifi() {
|
||||||
|
@ -26,6 +32,10 @@ void setup_wifi() {
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Load certificate
|
||||||
|
X509List cert(ca_cert);
|
||||||
|
espClient.setTrustAnchors(&cert);
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
@ -54,7 +64,7 @@ void reconnect() {
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("Attempting MQTT connection...");
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect("ESP8266Client")) {
|
if (client.connect("ESP8266Client", mqtt_user, mqtt_pass)) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// Subscribe
|
// Subscribe
|
||||||
client.subscribe(mqtt_topic);
|
client.subscribe(mqtt_topic);
|
||||||
|
|
Loading…
Reference in a new issue