tuanpmt / ESP8266MQTTClient

Apache License 2.0
83 stars 41 forks source link

Support to MQTT, websockets, TSL #28

Open JorgeTorres01 opened 2 years ago

JorgeTorres01 commented 2 years ago

Hi,

I test the code "MQTTOverWebsocketClient" and this works fine on ports 8080 and 8090 of "test.mosquitto.org" (without encryption)

But I need to use MQTT over Websockets with TLS and authentication. I'm using the broker "test.mosquitto.org", port 8091, to test this. But I'm not succeeding.

How can I adapt the code to implement TLS?

I try add (the same of example "MQTTSecureClient") :

String fingerprint = "7E 36 22 01 F9 7E 99 2F C5 DB 3D BE AC 48 67 5B 5D 47 94 D2";

and add :

  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  mqtt.onSecure([](WiFiClientSecure *client, String host) {
        Serial.printf("Verify: %s\r\n", host.c_str());
        return client->verify(fingerprint.c_str(), host.c_str());
    });

but the connection is not established.

can someone help me find a solution to implement MQTT over WebSockets with TLS?

Thank you. Jorge

JorgeTorres01 commented 2 years ago

I share the code:

[ESP8266]

#include "ESP8266MQTTClient.h"
#include <ESP8266WiFi.h>

MQTTClient mqtt;

String fingerprint = "7E 36 22 01 F9 7E 99 2F C5 DB 3D BE AC 48 67 5B 5D 47 94 D2";

void setup() {
  Serial.begin(115200);  
  WiFi.begin("Redmi", "d8295353cd94");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  mqtt.onSecure([](WiFiClientSecure *client, String host) {
        Serial.printf("Verify: %s\r\n", host.c_str());
        return client->verify(fingerprint.c_str(), host.c_str());
    });

  //topic, data, data is continuing
  mqtt.onData([](String topic, String data, bool cont) {
    Serial.printf("Data received, topic: %s, data: %s\r\n", topic.c_str(), data.c_str());
    mqtt.unSubscribe("/qos0");
  });

  mqtt.onConnect([]() {
    Serial.printf("MQTT: Connected\r\n");
    mqtt.subscribe("Jorge_tests", 1);
  });

  //mqtt.begin("ws://rw:readwrite@test.mosquitto.org:8080/mqtt#ClientIDxyz");
  //mqtt.begin("ws://rw:readwrite@test.mosquitto.org:8090/mqtt#ClientIDxyz");
  mqtt.begin("ws://rw:readwrite@test.mosquitto.org:8091/mqtt#ClientIDxyz");

  mqtt.readConfigs();
}

void loop() {
  mqtt.handle();
}

On ESP8266MQTTClient.cpp, funtion handle() I need comment the line with _disconnected_cb(); to avoid crash of CPU

How can I adapt the code to implement TLS?

Thank you. Jorge