Skip to content

hive broker not connecting in SSL mode聽#24

Description

@muscodev

error

[ 49637][V][PsychicMqttClient.cpp:414] _onMqttEvent(): [馃悪] Event dispatched from event loop base=MQTT_EVENTS, event_id=7
[ 49638][I][PsychicMqttClient.cpp:443] _onMqttEvent(): [馃悪] Other event id:7
mqtt connecting..mqtt connecting..mqtt connecting..[ 51099][V][PsychicMqttClient.cpp:414] _onMqttEvent(): [馃悪] Event dispatched from event loop base=MQTT_EVENTS, event_id=0
[ 51100][I][PsychicMqttClient.cpp:587] _onError(): [馃悪] MQTT_EVENT_ERROR
[ 51103][V][PsychicMqttClient.cpp:414] _onMqttEvent(): [馃悪] Event dispatched from event loop base=MQTT_EVENTS, event_id=2
[ 51104][I][PsychicMqttClient.cpp:467] _onDisconnect(): [馃悪] MQTT_EVENT_DISCONNECTED

is it bcz of SSL protocol version ?

#include <Arduino.h>
#include <WiFi.h>
#include <PsychicMqttClient.h>
#include "esp_log.h"
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>


const char ssid[] = "ssid"; 
const char pass[] = "pass"; 


// extern const uint8_t _binary_src_certs_bin_start[] asm("_binary_certs_ca_certs_der_start");
// extern const uint8_t _binary_src_certs_bin_end[]   asm("_binary_certs_ca_certs_der_end");

// Create UDP instance
WiFiUDP ntpUDP;

// Create NTP Client (IST offset = 19800 sec = +5:30)
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000);

void setTimeFromNTP() {
  Serial.println("Syncing time...");

  timeClient.begin();

  // Try until time is received
  while (!timeClient.update()) {
    timeClient.forceUpdate();
    delay(200);
  }

  // Extract UNIX time from NTPClient
  unsigned long epochTime = timeClient.getEpochTime();

  // Convert to struct tm
  struct tm tm;
  gmtime_r((time_t *)&epochTime, &tm);

  // Apply to ESP32 system time
  struct timeval now = { .tv_sec = epochTime };
  settimeofday(&now, NULL);

  Serial.print("Time synced: ");
  Serial.println(timeClient.getFormattedTime());
  
}


/**
 * Create a PsychicMqttClient object
 */
PsychicMqttClient mqttClient;

void setup()
{
    Serial.begin(115200);
    esp_log_level_set("*", ESP_LOG_VERBOSE);
    /**
     * Connect to the WiFi network with the given ssid and pass.
     */
    WiFi.begin(ssid, pass);
  
    Serial.printf("Connecting to WiFi %s .", ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(500);
    }
    setTimeFromNTP(); // <-- Sets ESP32 RTC time  
    Serial.printf("\r\nConnected, IP address: %s \r\n", WiFi.localIP().toString().c_str());

    /**
     * Connect to the open MQTT broker broker.hivemq.com.
     */

    mqttClient.setServer("mqtts://f6c34904deb94fae916044a6f9929162.s1.eu.hivemq.cloud:8883");
    mqttClient.setCredentials("user","pass");
    // mqttClient.setServer("mqtt://user1:pass1@192.168.1.3:1883");
    // mqttClient.setCACertBundle(_binary_src_certs_bin_start, _binary_src_certs_bin_end - _binary_src_certs_bin_start);
    mqttClient.attachArduinoCACertBundle();
    /**
     * Lambda callback function for onTopic Event Handler
     *
     * Subscribes to the topic "{MAC-Address}/simple" with QoS 0.
     * The lambda callback function will be called when a message is received.
     */
    String topic = String(ESP.getEfuseMac()) + "/simple";

    mqttClient.onTopic(topic.c_str(), 0, [&](const char *topic, const char *payload, int retain, int qos, bool dup)
                       {
                        /**
                         * Using a lambda callback function is a very convenient way to handle the 
                         * received message. The function will be called when a message is received. 
                         * 
                         * The parameters are:
                         * - topic: The topic of the received message
                         * - payload: The payload of the received message
                         * - retain: The retain flag of the received message
                         * - qos: The qos of the received message
                         * - dup: The duplicate flag of the received message
                         * 
                         * It is important not to do any heavy calculations, hardware access, delays or 
                         * blocking code in the callback function.
                        */
                        Serial.printf("Received Topic: %s\r\n", topic);
                        Serial.printf("Received Payload: %s\r\n", payload); });

    /**
     * Connect to the MQTT broker
     */

    

    mqttClient.connect();

    /**
     * Wait blocking until the connection is established
     */
    while (!mqttClient.connected())
    {
        delay(500);
        Serial.print("mqtt connecting..");
           
        
    }

    /**
     * Publish a message to the topic "{MAC-Address}/simple" with QoS 0 and retain flag 0.
     *
     * You can only publish messages after the connection is established.
     */    
    mqttClient.publish("lc/", 0, 0, "Hello World!");

}

void loop()
{
    /**
     * Nothing to do here, the onTopic callback function will be called when a message is received.
     */
      time_t now;
    time(&now);
      Serial.printf("Current time: %s", ctime(&now));
      delay(1000);       
    mqttClient.publish("lc/", 0, 0, "Hello World!");

}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions