Unable to connect to the mqtt

#include<WiFi.h>
#include <WiFiManager.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <WiFiClientSecure.h>
#include <SSLClient.h>
#include "certificates.h" // This file must be regenerated
#include <SPIFFS.h>
#include <FS.h>
#include <DNSServer.h>
#include <WebServer.h>
#include <EnvironmentCalculations.h>
#include <BME280I2C.h>
#include <Wire.h>
#include"MapFloat.h"
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "CO2Sensor.h"
#include <AsyncTCP.h>
#include <U8x8lib.h>
#include<EEPROM.h>

const char* ddata;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

//Week Days
String weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//Month names
String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

WiFiManager wm;
WiFiManagerParameter custom_mqtt_server("mqttServer", "mqtt server", "", 40);
WiFiManager wifiManager;
WiFiClient espClient;
//WiFiClientSecure espClient; //This ClientSecure is for Gateway (Eg.pag.tayrix.com).
PubSubClient client(espClient);

void saveConfig();
void loadConfig();
void saveConfigCallback();
void initializeWifiManager();
void wifiInfo();
//Topics
const char* send_topic = "telemetry"; //publish 

//// Mqtt Credentials ////
char mqttServer[40] = "ditto.axalyn.com"; //
// string representation of the port
char port_str[40] = "30883";
// convert port string to integer
uint16_t mqttPort = atoi(port_str);
char mqttUserName[60] = "mqtt pwd;

char mqttPwd[40] = "DEVICE_PASSWORD"; //"cenaura@2024";  //
char clientID[40] = "ESP32Client";

String timestamp;

//parameters for using non-blocking delay
unsigned long previousMillis = 0;
const long interval = 5000;
unsigned long mtime = 0;

// TEST OPTION FLAGS
bool TEST_CP         = false; // always start the configportal, even if ap found
int  TESP_CP_TIMEOUT = 90; // test cp timeout
bool shouldSaveConfig = false;
bool TEST_NET        = true; // do a network test after connect, (gets ntp time)
bool ALLOWONDEMAND   = true; // enable on demand
int  ONDDEMANDPIN    = 0; // gpio for button
bool WMISBLOCKING    = false; // use blocking or non blocking mode

String msgStr = "";

int p;
bool a;

void setup() {

  Serial.begin(115200);
  EEPROM.begin(512);
  Wire.begin();
  pinMode(34, INPUT);

  // Initialize SPIFFS
  if (!SPIFFS.begin(true)) {
    Serial.println("SPIFFS Mount Failed");
    // Handle the failure appropriately, possibly halt or retry
    while (true) {
      delay(1000); // Delay indefinitely if SPIFFS fails to mount
    }
  }

  // Save configuration if needed
  if (shouldSaveConfig) {
    Serial.println("Saving updated configuration...");
    saveConfig();
  }

  loadConfig();   // Load configuration settings from SPIFFS or EEPROM

  initializeWifiManager();   // Set up WiFiManager and MQTT configuration

  wifiInfo();

  wifiManager.setTimeout(180);

  if (WiFi.status() == WL_CONNECTED) {

    timeClient.begin();
    timeClient.setTimeOffset(19800);

    // Enable mutual TLS with SSLClient
//    espClient.setCACert(rootCACertificate); // Use the CA certificates for the client

    client.setServer(mqttServer, mqttPort); //setting MQTT server
    client.setCallback(callback); //defining function which will be called when message is recieved.
    
  }
  else {
    Serial.println("No Wifi");
  }
}

unsigned long lastMsg = 0;
unsigned long lastMsg01 = 0;
unsigned long rstresp = 0;

void loop() {
  // Retry loop if WiFi connection is lost
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Attempting to reconnect to WiFi...");

    // Attempt to reconnect to Wi-Fi
    WiFi.reconnect();  // Try to reconnect to the last connected Wi-Fi network
    Serial.println("Reconnection failed, retrying...");
    delay(5000);  // Wait 5 seconds before retrying
    return;  // Skip the rest of the loop if not connected

    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Reconnected to WiFi."); \
    }
  }

  if (WiFi.status() == WL_CONNECTED) {
    timeClient.update();
    wm.process();

    if (!client.connected()) {
      reconnect();
    }

    unsigned long nowon = millis();
    if (nowon - lastMsg01 > 1000) {
      lastMsg01 = nowon;
    }

    unsigned long now1 = millis();
    if (now1 - lastMsg > 5000) {
      lastMsg = now1;
      if (client.connected()) {
        sendDataMQTT();
      }
    }
    client.loop();
  }

  unsigned long rstresp1 = millis();
  if (rstresp1 - rstresp > 500000) {
    Serial.println("....");
    rstresp = rstresp1;
    ESP.restart();
  }
}

I am experiencing an issue with connecting to the Ditto MQTT broker at ditto.axalyn.com on port 30883. Despite verifying that the server is reachable via ping and traceroute, I am unable to establish a connection to this port.

I have tried the following troubleshooting steps:

  • Verified server reachability with ping and traceroute.
  • Checked local firewall settings.
  • Attempted to connect using various tools (telnet, Netcat).

Could anyone please assist in confirming if port 30883 is open and accepting connections? Any additional guidance on resolving this issue would be greatly appreciated.

You will get faster and better help if you post more detail about your project, indicate a board used, and show the schematic as requested by the forum guidelines.

I am using Esp32 to send the following data to ditto mqtt broker, but I am unable to connect to the mqtt and when I check the credentials in mqtt explorer getting disconnected from the server.

void sendDataMQTT() {
  time_t epochTime = timeClient.getEpochTime();

  String formattedTime = timeClient.getFormattedTime();

  StaticJsonDocument<500> doc;
  JsonObject TXCC = doc.createNestedObject("txcc_es");
  TXCC["did"] = ESP.getChipModel();
  TXCC["ts"] = formattedTime;
  JsonObject TXCC_ES_rtd = TXCC.createNestedObject("ES_rtd");

  int E_AT = random(100, 200);
  TXCC_ES_rtd["AT_v"] = "{temp: 905, hum: 20}";
//  TXCC_ES_rtd["Name"] = "Hemanth";

  char buffer[1024];

  serializeJson(doc, buffer);
  client.publish("telemetry", buffer);
  serializeJson(doc, Serial);
  Serial.println();
  Serial.println("-----------------------");

}

Do you able to connect ESP32 to any other host outside your local network?

Yes its connecting to the local network.

The server is reachable via ping and traceroute, I am unable to establish a connection to this port.

Is ditto.axalyn.com also is in your local network?

Did you run ping and traceroute on ESP32 ? Or on PC?

Think about it - the problem may be that the network settings on the ESP and on the computer are different, so the computer can connect to an external host, but the board cannot.
That's why I'm asking again - have you tried connecting a ESP32 to some EXTERNAL site?

No I need to connect to Hono to send data and Hono will send to Ditto

I run ping and traceoute on PC not the Esp32 and when I try t connect to other sites its connecting.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.