EasyIOT connection problem with PubSubClient

1 week 4 days ago #3979 by dlugipg
Hi

Welcome to everyone as this is my first post on this forum. I'm trying to connect to EasyIOT with my ESP8266 (Wemos), but everytime try to connect, I get rc=-2 (MQTT_CONNECTION_TIMEOUT). Tried to connect to other MQTT testing services and connection is fine, but to EasyIOT not. Do you have any idea what can be wrong? I tried already
#define MQTT_VERSION_3_1

instead of
#define MQTT_VERSION_3_1_1
.

Here is my code (pretty simple, just trying to connect to EasyIOT first:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define AP_SSID     "xxx"
#define AP_PASSWORD "xxx"  

const char* mqttUser = "yyy";
const char* mqttPass = "yyy";

// create MQTT object
const char* mqtt_server = "cloud.iot-playground.com";

#define DO_TOPIC        "/Sensor.Parameter1"

#define PIN_DO_1         D0  // DO pin1 
#define MODULE_ID_1     1


#define PIN_DO_2         D1  // DO pin2 
#define MODULE_ID_2     2


#define PIN_DO_3         D2  // DO pin3 
#define MODULE_ID_3     3


#define PIN_DO_4         D3  // DO pin4 
#define MODULE_ID_4     4

//MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);  
  WiFi.begin(AP_SSID, AP_PASSWORD);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(AP_SSID);
    
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  };

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  Serial.print("Settings of MQTT server..."); 
  Serial.println(mqtt_server);
  client.setServer(mqtt_server, 1883);
  

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

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

void reconnect() {
 
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection with ID: ");
    Serial.print(getClientId().c_str());
    // Attempt to connect

    if (client.connect(getClientId().c_str(), mqttUser, mqttPass)) {
      Serial.println(" -> connected");
      // ... and subscribe to topic
      client.subscribe("ledStatus");
    } else {
      Serial.print(" -> failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }

     
  }
}


String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}

String getClientId()
{
  //set client id
  // Generate client name based on MAC address and last 8 bits of microsecond counter
  String clientName;
  uint8_t mac[6];
  WiFi.macAddress(mac);
  clientName += macToStr(mac);
  clientName += "-";
  clientName += String(micros() & 0xff, 16);

  return clientName;
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.166 seconds