-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I always fail to connect. #138
Comments
|
I got a little more question, if i set connection in Arduino and doing different task than publish or subscribe. Will it disconnect automatically? |
|
Hi, you'll have to provide a clearer description, with code, of what works and doesn't work for you. What hardware are you using? The client will remain connected as long as you call the client.loop function regularly. Otherwise the server will disconnect the client due to inactivity. |
|
Problem Message : Attempting MQTT connection...failed, rc=-2 try again in 5 seconds **Hardware _: Arduino Uno + Ethernet Shield Code : (Modified the ip address to that of test.mosquitto.org )#include <SPI.h> // Update these with values suitable for your network. void callback(char* topic, byte* payload, unsigned int length) { EthernetClient ethClient; void reconnect() { void setup() client.setServer(server, 1883); Ethernet.begin(mac, ip); void loop() |
|
@shrobon Have you tried verifying your basic setup using the |
|
@knolleary , Yes I have tried doing that . The WebClient works perfectly well . |
|
@knolleary . You can close the issue . There were a couple of problems from my end . You can include this in your readme since many stumble with this problem . |
|
hi there sorry for reviving this post but i have been with this problem for 2 weeks ! and here i read that there is a webclient example i try it but doesnt works for me :( i add an screenshot to show you what happen, i am using an arduino ethernet on a DHCP network can someone help my with that ? or tell me where i can find some help ? PD: sorry for my bad english LOOOTS OF THANKS FOR YOU POST GIVE ME HOPE !! |
|
@vicatcu yeah thanks for your answer i use that example and i remember that my network is not a dhcp network, i am on a static ip network ! (sorry about that :P ) now the example give me back the ip of my arduino in the monitor serie 9600 but i already have problems with my mqtt sketch #include <PubSubClient.h> #include <SPI.h> #include <Ethernet.h> #include <LiquidCrystal.h> LiquidCrystal lcd(9, 8, 6, 5, 3, 2); byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xDA, 0xA0 }; byte server[] = { 10,1,4,36 }; //byte ip[] = { 10, 2, 23, 163 }; IPAddress ip(10, 2, 23, 163); EthernetClient ethClient; //PubSubClient client(server , 1883 , callback ,ethClient); void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Payload: "); Serial.write(payload, length); Serial.println(); //clear LCD screen lcd.clear(); //set cursor position on the LCD at column 0, line 0 then display Office Temp lcd.setCursor(0,0); lcd.print("mqtt"); //set cursor position on the LCD at column 0, line 1 lcd.setCursor(0,1); //write the payload to the LCD display lcd.write(payload, length); } PubSubClient client(server, 1883, callback, ethClient); void reconnect() { void setup(){ //Print Arduino IP address to serial for debugging Serial.print("IP = "); Serial.println(Ethernet.localIP()); lcd.begin(16, 2); lcd.print("mqtt Siquei"); lcd.setCursor(0,1); lcd.print(Ethernet.localIP()); if(4>3){ client.publish("mqtt/2","LCD Display Online"); Serial.println("conexion exitosa"); client.subscribe("mqtt/2"); } client.publish("mqtt/2","LCD Display Online"); Serial.println("fallo la conexion"); client.subscribe("mqtt/2"); } } void loop() ` |
|
@panzho if you monitor the traffic coming from your Arduino Ethernet (e.g. using WireShark or something) I bet things would become clear. I would guess wildly that the IP address of your server is wrong - double check it, and try reversing the octets in your sketch. |
|
@vicatcu thanks for your patience i know my english is horrible this little part of my life... its called happiness xD i think the problem was that i was trying to connect to a dhcp network but my network is a static one the code for that is byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xDA, 0xA0 }; Ethernet.begin(mac, ip, myDns, gateway, subnet); this if for STATIC ip networks byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xDA, 0xA0 }; ethernet.begin(mac,ip); this one is for DHCP networks |
|
@panzho i have the same problem u had and i am running my broker on a raspberry pi how can i solve it |
|
Dont Use: byte server[] = { 10,1,4,36 }; USE: IPAddress server(10, 1, 4, 36); IN: client.setServer(server, 1883); Worked for me. |
|
This seems to be a common problem when using ESP8266 + Mosquitto broker. I also had a perfectly working set-up with multiple ESPs and they suddenly started showing the same "failed rc=-2" error (WifiClient can not connect to the broker) out of nowhere. Once this error starts showing, there is seemingly no way to recover. I even tried different MQTT libraries and ESP platforms. The solution is very unexpected but simple: Explicitly add a before I could not believe it myself. But it actually fixed the problem across ALL my MQT sensors. There simply seems to be an invalid state of the network stack that prevents PubSub's WifiClient to connect. |
|
@hotkeymuc interesting - good discovery thanks. Need to find somewhere to capture this as I'm sure it'll come up! |
|
@knolleary Please add hotkeymuc's solution somewhere visible (in the (esp8266/32) examples perhaps?)! |
|
nice job, that solved my problem too. I add the command just before and it did the job. Now, it is working like a charm !!!!!! Thanks |
Console response still facing issue after adding WiFi.mode(WIFI_STA); |
Resolve rc=-2 (network issues) while connecting to a local MQTT broker by forcing station mode of the WiFi (WIFI_STA). Fix inspired by: knolleary/pubsubclient#138 (comment) Signed-off-by: Leon Anavi <leon@anavi.org>
|
@knolleary I've just got involved with ESP and had the same issue with connecting to the MQTT server. I was using Mosquitto on the local machine. Cloud MQTT was working fine as was Mos testing server. I thought I might have needed to somehow 'register' topics before I subcribed to them as I am new to all things IoT. I was trying AT Command Tester and bunch of other things trying to track the issue. I spent far too long trying to track down info for this without realizing I shouldn't be using "127.0.0.1" or "Localhost" on port 1883 but that I should be using the IP of the local machine. I'd be willing wager a fizzy drink that there are more than a few people who receive the -2 error have the same issue. |
must have statement for current Arduino Framework, see also knolleary/pubsubclient#138 (comment)
must have statement for current Arduino Framework, see also knolleary#138 (comment)
|
I am not able to connect to the mqtt server I have made in Window. } void reconnect() // Allow the hardware to sort itself out if (mqttClient.connect("myClientID")) { } } |
It's 2022 and this is still the fix. Thanks! |
|
same here today. only this works: nodemcu v3
|
|
hello every one, i have the same problem but I don't know where to add it can comeone help? |
Hi, In my code, I added the line But Same error persists. My MQTT server is actively running on port 1883. And Also I connected to the wifi. This is my code: Kindly help me resolve this issue. Thanks in advance! |



I always fail to connect when I implement your basic example with my code. This will happen when I put put reconnect method in the void loop but when I tried to change the position to void setup(), it's work but not perfectly which mean when I put in different position in void setup(), somehow it make my program freeze. BTW, I put small delay before it will connect to and the code that I get is -2 which mean the connection fail. Do u have any suggestion?
The text was updated successfully, but these errors were encountered: