title: Unable to connect to Thingsboard running on localhost server using mosquitto/mqtt
body: <p>I am unable to connect to thingsboard running on localhost through MQTT. My arduino code worked well with Thingsboard's demo server. It connected succesfully and was able to see values under Latest Telemetry. While running locally, I am getting </p>
  
  <pre><code>"Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]"
  </code></pre>
  
  <p>error in arduino serial monitor.</p>
  
  <p>I have installed tb-gateway and mosquitto broker. Following are the changes done:</p>
  
  <p>tb-gateway.yml</p>
  
  <pre><code>    path: storage
      bufferSize: 1000
    connection:
      host: "127.0.0.1"
      port: 1882
      retryInterval: 3000
  </code></pre>
  
  <p>mqtt-config.json</p>
  
  <pre><code>{
    "host": "127.0.0.1",
    "port": 1883,
    "ssl": false,
    "retryInterval": 3000,
  }
  </code></pre>
  
  <p>thingsboard.yml</p>
  
  <pre><code> mqtt:
   # Enable/disable mqtt transport protocol.
   enabled: "${MQTT_ENABLED:true}"
   bind_address: "${MQTT_BIND_ADDRESS:0.0.0.0}"
   bind_port: "${MQTT_BIND_PORT:1882}"
   timeout: "${MQTT_TIMEOUT:10000}"
   netty:    
  </code></pre>
  
  <p>Here is my arduino code :</p>
  
  <pre><code>#include &lt;Wire.h&gt;
  
  #include &lt;PubSubClient.h&gt;
  #include &lt;ArduinoJson.h&gt;
  
  #include &lt;WiFi.h&gt;
  #include &lt;WiFiClient.h&gt;
  
  // MPU6050 Slave Device Address
  const uint8_t MPU6050SlaveAddress = 0x68;
  
  // Select SDA and SCL pins for I2C communication 
  const uint8_t scl = 22;
  const uint8_t sda = 21;
  
  #define TOKEN "oPfC3R8p3LLKJ4lBU9u2" 
  
  #define WIFI_AP ""     
  #define WIFI_PASSWORD ""
  char thingsboardServer[] = "127.0.0.1";
  unsigned long lastSend;
  int status = WL_IDLE_STATUS;
  
  WiFiClient wifiClient;
  PubSubClient client(wifiClient);
  
  // sensitivity scale factor respective to full scale setting provided in         
  datasheet 
  const uint16_t AccelScaleFactor = 16384;
  const uint16_t GyroScaleFactor = 131;
  
  
  int16_t AccelX, AccelY, AccelZ, Temperature;
  
  void setup() {
    Serial.begin(115200);
    InitWiFi();
    client.setServer( thingsboardServer, 1883 );
    Wire.begin(sda, scl);
    MPU6050_Init();
    lastSend = 0;
  }
  
  void loop() {
    if ( !client.connected() ) {
      reconnect();
    }
  
     if ( millis() - lastSend &gt; 1000 ) { // Update and send only after 1 seconds
      getAndSendData();
      lastSend = millis();
    }
  
    delay(10);
    client.loop();
  }    
  
  void getAndSendData(){
    double Ax, Ay, Az, T, Gx, Gy, Gz;
    Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
  
    //divide each with their sensitivity scale factor
    Ax = (double)AccelX/AccelScaleFactor;
    Ay = (double)AccelY/AccelScaleFactor;
    Az = (double)AccelZ/AccelScaleFactor;
    T = (double)Temperature/340+36.53; //temperature formula
  
  
    Serial.println("Collecting  Data ");
  
    Serial.print("Ax: "); Serial.print(Ax);
    Serial.print(" Ay: "); Serial.print(Ay);
    Serial.print(" Az: "); Serial.print(Az);
    Serial.print(" T: "); Serial.print(T);
  
  
    // Prepare a JSON payload string
    String payload = "{";
    payload += "\"Ax\":"; payload += String(Ax); payload += ",";
    payload += "\"Ay\":"; payload += String(Ay); payload += ",";
    payload += "\"Az\":"; payload += String(Az); payload += ",";
    payload += "\"temperature\":"; payload += String(T);
    payload += "}";
  
    // Send payload
    char attributes[100];
    payload.toCharArray( attributes, 100 );
    client.publish( "v1/devices/me/telemetry", attributes );
    Serial.println( attributes );
    Serial.println("%. Sent to Thingsboard.");
  
  }
  
  void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
    Wire.beginTransmission(deviceAddress);
    Wire.write(regAddress);
    Wire.write(data);
    Wire.endTransmission();
  }
  
  // read all 14 register
  void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
    Wire.beginTransmission(deviceAddress);
    Wire.write(regAddress);
    Wire.endTransmission();
    Wire.requestFrom(deviceAddress, (uint8_t)14);
    AccelX = (((int16_t)Wire.read()&lt;&lt;8) | Wire.read());
    AccelY = (((int16_t)Wire.read()&lt;&lt;8) | Wire.read());
    AccelZ = (((int16_t)Wire.read()&lt;&lt;8) | Wire.read());
    Temperature = (((int16_t)Wire.read()&lt;&lt;8) | Wire.read());
  
  }
  
  //configure MPU6050
  void MPU6050_Init(){
    delay(150);
     .......
  }
  
  void InitWiFi()
  {
    Serial.println("Connecting to AP ...");
        WiFi.begin(WIFI_AP, WIFI_PASSWORD);
   .......
  }
  
  
   void reconnect() {
   // Loop until we're reconnected
   while (!client.connected()) {
   status = WiFi.status();
   if ( status != WL_CONNECTED) {
     WiFi.begin(WIFI_AP, WIFI_PASSWORD);
     while (WiFi.status() != WL_CONNECTED) {
       delay(500);
       Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
  Serial.print("Connecting to ThingsBoard node ...");
  // Attempt to connect (clientId, username, password)
  if ( client.connect("fa5c6fe0-05cc-11e9-bfc6-27e4379bb947", TOKEN, 
    NULL,NULL,NULL,NULL,NULL,NULL) ) {
    Serial.println( "[DONE]" );
  } else {
    Serial.print( "[FAILED] [ rc = " );
    Serial.print( client.state() );
    Serial.println( " : retrying in 5 seconds]" );
    // Wait 5 seconds before retrying
    delay( 5000 );
    }
   }
  } 
  </code></pre>
  
  <p>Please let me know in case I am missing any configuration or doing something wrong.</p>