bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
550 stars 210 forks source link

Cannot connect to MQTT-server (problem due to not showing correct local IP?) #49

Open jontebotas opened 8 years ago

jontebotas commented 8 years ago

Hi! I am using a ESP8266-01 to enable wifi for my Arduino Uno. The ESP-01 is connected via the RX/TX on the Uno, and i am using SoftSerial for debugging (via an Arduino USB2Serial). The ESP-01 is powered from a separate power source.

I manage to connect to my AP (wifi), but not to my MQTT broker (mosquitto on a raspberry pi). I get the following error in mosquitto:

"Socket error on client (null), disconnecting"

In the serial monitor, i get that the MQTT-connection fails (RC: -2)

The essential parts of my code: Declarations etc:

#include <WiFiEsp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>

WiFiEspClient espClient;
PubSubClient client(espClient);

char* ssid = "XXXX";
char* password = "XXXXXX";
int status = WL_IDLE_STATUS;   // the Wifi radio's status

const char* mqtt_server = "server";
const char* mqtt_username = "user";
const char* mqtt_password = "password";
const char* mqtt_client_name = "cliendID";

My setup-loop (i'm using a macro to enable/disable serial.println for debugging)

void setup() {

  soft.begin(9600); // SoftSerial for debugging
  Serial.begin(115200); //ESP8266-01 

  delay(1000);
  WiFi.init(&Serial);

  delay(1000);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    DEBUG_PRINTLNT("WiFi shield not present");
    // don't continue
    while (true);
  }
  delay(100);

  while ( status != WL_CONNECTED) {
    DEBUG_PRINT("Attempting to connect to WPA SSID: ");
    DEBUG_PRINT(ssid);
    DEBUG_PRINTLNT(".");
    status = WiFi.begin(ssid, password);
    delay(10000); 
    DEBUG_PRINT("SSID: ");
    DEBUG_PRINTLNT(WiFi.SSID());
    DEBUG_PRINT("Wifi.status: ");
    DEBUG_PRINTLNT(status);
    DEBUG_PRINT("IP: ");
    DEBUG_PRINTLNT(WiFi.localIP());
    DEBUG_PRINT("Firmware version: ");
    DEBUG_PRINTLNT(WiFi.firmwareVersion());
  }

  //CONNECT TO MQTT BROKER
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

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

The reconnect() function:

void reconnect() { 
  while (!client.connected()) {
    soft.println(F("Attempting MQTT connection..."));
    // Attempt to connect
    if (client.connect(mqtt_client_name, mqtt_username, mqtt_password,mqtt_topic_arduino_status,0,true,"offline")) {
      mqttSubscribe(mqtt_topic_r1_status);
      mqttSubscribe(mqtt_topic_r2_status);   
    } else {
      soft.print(F("Failed, rc=")); //using the softserial for debugging...
      soft.print(client.state());
      soft.println(F(", will try again in 5 seconds."));
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

I have found one strange thing: In the mosquitto-logs my Uno/ESP-01 have the IP: 10.0.1.16 (which is also the IP that is shown when checking the connected clients in my AP). However, when i call the function WiFi.localIP() in my Arduino sketch, it says that my local IP is "10.0.1.116". Can this be the reason to why my MQTT-connection fails? I haven't found a way to get around it though...

Any ideas on how to solve this?

Thankful for any help!

jontebotas commented 8 years ago

I have also tested to assign a static IP to my ESP-01 in my router, but I still get the same problem as above.

When bypassing the Uno to communicate with the ESP-01 directly via AT commands in the serial monitor, I manage to connect to my AP - and this time i get the correct IP (my static IP). This tells me the problem lies in my sketch or in WifEsp (or my use of it)?

diegommarino commented 8 years ago

Hey, don't know if it may help, but I made a MQTT example and post it in this issue #37 . Hit me back if you need help.