Skip to content

Instantly share code, notes, and snippets.

@adlerweb
Last active October 4, 2023 12:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save adlerweb/807aee4a79a8dee043113d86172e7792 to your computer and use it in GitHub Desktop.
Save adlerweb/807aee4a79a8dee043113d86172e7792 to your computer and use it in GitHub Desktop.
#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());
espClient.setFingerprint(mqtt_fprint);
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);
}
@adlerweb
Copy link
Author

adlerweb commented Oct 6, 2019

Probier mal espClient.setFingerprint(mqtt_fprint); unter client.setServer(mqtt_server, mqtt_port); (Zeile 38) zu ergänzen. Das war alles was ich machen musste dass es funktioniert. Danke an: https://www.reddit.com/user/pointdexter33/ der mir dabei weiter geholfen hatte.

Hm, scheint durch ein Update neu zu sein, dass hatte der früher™ automatisch gemacht. Habe es oben mal ergänzt, danke für den Hinweis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment