BitBastelei #290 - MQTT (Protokoll, Mosquitto, ESP8266, HomeAssistant, TLS)
(265 MB) 00:38:36
2018-07-01 10:00 🛈In den letzten Jahren hat sich im Bereich der herstellerübergreifenden Hausautomation MQTT als Protokoll verbreitet. Schauen wir mal wie das Protokoll funktioniert, wie wir selbst einen Server aufsetzen und diesen absichern und letztentlich wie wir mit ESP8266 und HomeAssistant einen eigenen Sensor implementieren können.
Inhalt:
- 00:00 Das Protokoll
- 05:17 Installation von Mosquitto
- 07:52 TLS und Passwörter mit Mosquitto
- 22:28 MQTT mit HomeAssistant
- 25:03 MQTT mit ESP8266/Arduino
Links:
- Anleitung von Auxnet
- Fingerprint-Befehl: echo | openssl s_client -connect localhost:1883 | openssl x509 -fingerprint -noout
Demo-Quellcode:
https://gist.github.com/adlerweb/807aee4a79a8dee043113d86172e7792
#include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <PubSubClient.h> const char* cfg_wifi_ssid = "freifunk-myk.de"; const char* cfg_wifi_pwd = ""; const char* mqtt_server = "deinserver.local"; const unsigned int mqtt_port = 1883; const char* mqtt_user = "admin"; const char* mqtt_pass = "test123"; //echo | openssl s_client -connect localhost:1883 | openssl x509 -fingerprint -noout const char* mqtt_fprint = "D9:B2:A5:BB:E1:E5:4D:50:23:E1:84:24:29:44:BA:7C:54:01:BB:08"; WiFiClientSecure espClient; PubSubClient client(espClient); void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("TestMQTT"); WiFi.mode(WIFI_STA); WiFi.begin(cfg_wifi_ssid, cfg_wifi_pwd); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); while (!client.connected()) { Serial.print("Attempting MQTT connection..."); verifyFingerprint(); if (client.connect(WiFi.macAddress().c_str(), mqtt_user, mqtt_pass)) { Serial.println("connected"); client.subscribe("/foo"); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); char message[length + 1]; for (int i = 0; i < length; i++) { message[i] = (char)payload[i]; } message[length] = '\0'; Serial.println(message); } void verifyFingerprint() { if(client.connected() || espClient.connected()) return; //Already connected Serial.print("Checking TLS @ "); Serial.print(mqtt_server); Serial.print("..."); if (!espClient.connect(mqtt_server, mqtt_port)) { Serial.println("Connection failed. Rebooting."); Serial.flush(); ESP.restart(); } if (espClient.verify(mqtt_fprint, mqtt_server)) { Serial.print("Connection secure -> ."); } else { Serial.println("Connection insecure! Rebooting."); Serial.flush(); ESP.restart(); } espClient.stop(); delay(100); } unsigned int counter = 0; void loop() { // put your main code here, to run repeatedly: client.loop(); char buf[12]; itoa(counter, buf, 11); client.publish("/test", buf); counter++; delay(1000); }