Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 -nooup
- 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);
- }
Add Comment
Please, Sign In to add comment