Guest User

Untitled

a guest
Jun 30th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <PubSubClient.h>
  4.  
  5. const char* cfg_wifi_ssid = "freifunk-myk.de";
  6. const char* cfg_wifi_pwd = "";
  7.  
  8. const char* mqtt_server = "deinserver.local";
  9. const unsigned int mqtt_port = 1883;
  10. const char* mqtt_user = "admin";
  11. const char* mqtt_pass = "test123";
  12.  
  13. //echo | openssl s_client -connect localhost:1883 | openssl x509 -fingerprint -nooup
  14. const char* mqtt_fprint = "D9:B2:A5:BB:E1:E5:4D:50:23:E1:84:24:29:44:BA:7C:54:01:BB:08";
  15.  
  16. WiFiClientSecure espClient;
  17. PubSubClient client(espClient);
  18.  
  19. void setup() {
  20. // put your setup code here, to run once:
  21. Serial.begin(115200);
  22. Serial.println("TestMQTT");
  23.  
  24. WiFi.mode(WIFI_STA);
  25. WiFi.begin(cfg_wifi_ssid, cfg_wifi_pwd);
  26.  
  27. while (WiFi.status() != WL_CONNECTED) {
  28. delay(500);
  29. Serial.print(".");
  30. }
  31.  
  32. Serial.println("");
  33. Serial.println("WiFi connected");
  34. Serial.println("IP address: ");
  35. Serial.println(WiFi.localIP());
  36.  
  37. client.setServer(mqtt_server, mqtt_port);
  38. client.setCallback(callback);
  39.  
  40. while (!client.connected()) {
  41. Serial.print("Attempting MQTT connection...");
  42. verifyFingerprint();
  43. if (client.connect(WiFi.macAddress().c_str(), mqtt_user, mqtt_pass)) {
  44. Serial.println("connected");
  45. client.subscribe("/foo");
  46. }else{
  47. Serial.print("failed, rc=");
  48. Serial.print(client.state());
  49. Serial.println(" try again in 5 seconds");
  50. // Wait 5 seconds before retrying
  51. delay(5000);
  52. }
  53. }
  54. }
  55.  
  56. void callback(char* topic, byte* payload, unsigned int length) {
  57. Serial.print("Message arrived [");
  58. Serial.print(topic);
  59. Serial.print("] ");
  60.  
  61. char message[length + 1];
  62. for (int i = 0; i < length; i++) {
  63. message[i] = (char)payload[i];
  64. }
  65. message[length] = '\0';
  66. Serial.println(message);
  67. }
  68.  
  69. void verifyFingerprint() {
  70. if(client.connected() || espClient.connected()) return; //Already connected
  71.  
  72. Serial.print("Checking TLS @ ");
  73. Serial.print(mqtt_server);
  74. Serial.print("...");
  75.  
  76. if (!espClient.connect(mqtt_server, mqtt_port)) {
  77. Serial.println("Connection failed. Rebooting.");
  78. Serial.flush();
  79. ESP.restart();
  80. }
  81. if (espClient.verify(mqtt_fprint, mqtt_server)) {
  82. Serial.print("Connection secure -> .");
  83. } else {
  84. Serial.println("Connection insecure! Rebooting.");
  85. Serial.flush();
  86. ESP.restart();
  87. }
  88.  
  89. espClient.stop();
  90.  
  91. delay(100);
  92. }
  93.  
  94. unsigned int counter = 0;
  95.  
  96. void loop() {
  97. // put your main code here, to run repeatedly:
  98.  
  99. client.loop();
  100.  
  101. char buf[12];
  102. itoa(counter, buf, 11);
  103.  
  104. client.publish("/test", buf);
  105. counter++;
  106.  
  107. delay(1000);
  108. }
Add Comment
Please, Sign In to add comment