knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

Connection problem to MQTT broker #560

Open Ramzi09 opened 5 years ago

Ramzi09 commented 5 years ago

Hi my arduino client can't connect to the public broker test.mosquitto.com. The Serial Monitor output: Connecting to MQTT... failed state : -2 Here is the code, where is the problem?I don't find it :

include

include

include

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};

const char* mqttServer = "test.mosquitto.com"; const int mqttPort = 1883; EthernetClient ethClient; PubSubClient mqttClient(ethClient);

void setup_Ethernet(){ // start the Ethernet connection: Serial.println("Initialize Ethernet with DHCP:"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); } else if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } //delay to let Etnernet shield initialize delay(1000); } //connection succesful Serial.println("Connection Successful"); Serial.println("--------------------");

}

void callback(char topic, byte payload, unsigned int length) { Serial.print("Messageved in topic: "); Serial.println(topic); Serial.print("Message :"); for(int i = 0; i < length; i++) { Serial.print((char)payload); } Serial.println(); }

void setup() { Serial.begin(9600); setup_Ethernet(); mqttClient.setServer(mqttServer,mqttPort); mqttClient.setCallback(callback); while(!mqttClient.connected()) { Serial.println("Connecting to MQTT..."); if (mqttClient.connect("arduinoClient")) { Serial.println("connected"); }else{ Serial.print("failed state : "); Serial.println(mqttClient.state()); delay(2000); } } mqttClient.publish("test/arduino","Hello Arduino world"); mqttClient.subscribe("test/arduino"); }

void loop() { mqttClient.loop(); }

JackGruber commented 5 years ago

Hi, have you a DNS entry for test.mosquitto.com on your Network?

knolleary commented 5 years ago

Having you tried with a desktop mqtt client or a different broker? I believe test.mosquitto.org has had some availability issues this week.

Ramzi09 commented 5 years ago

Thanks for replay, I used arduino uno with Ethernet shield as client. I tested the program with the public brokers like “iot.eclipse.org” or “test.mosquitto.org”.

The following information about my Network:
Ip address: 172.16.6.123 SubnetMask:255.255.248.0 Gateway:172.16.0.18 DNS:172.16.0.18

Ramzi09 commented 5 years ago

Is the problem in the availability of public broker? I tested several times, but it is always the same message failed -2

anklimov commented 5 years ago

I have the exactly same issue test.mosquitto.org working good with most another clients same time But adruino failed with connecting "Attempting MQTT connection to test.mosquitto.org:1883 user: ...failed, rc=5 try again in 5 seconds"

PS The cloudmqtt operating normally.