# Client.state() failed with state -2

**URL:** <https://discourse.nodered.org/t/client-state-failed-with-state-2/79646>\
**Category:** General\
**Created:** [8 July 2023 13:55 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646 "2023-07-08T13:55:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [8 July 2023 13:55 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/1 "2023-07-08T13:55:49Z")

</div>

I have an ESP32-CAM module that I want to run in my greenhouse too far away for the WiFi to reach. I added a WiFi extender that gives me a new SSID of EXISTING\_SSID\_EXT. Things work ok when I fire the module up close to the EXISTING\_SSID but when I locate it in the greenhouse it fails to connect to MQTT with the Failed with state -2 message. I know it is connected to the EXISTING\_SSID\_EXT because I can see the serial messages. It has multiple tries but fails every time. Is it failing because it previously connected on the EXISTING\_SSID and wont let it connect under the new SSID?  
So just to be clear - it works when connected to EXISTING\_SSID but it does not connect to MQTT when reconnected to EXISTING\_SSID\_EXT

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 July 2023 14:11 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/2 "2023-07-08T14:11:40Z")

</div>

I am a bit confused about exactly what the problem is. As you describe it the problem seems to be to do with the CAM module connecting to the MQTT broker. Is that right? if so it doesn't appear to be anything to do with node-red. If so that doesn't mean no-one here can help, I just want to make sure I understand the problem.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [8 July 2023 15:50 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/3 "2023-07-08T15:50:28Z")

</div>

So the sequence of events is this. Power up module in the house and it connects to the EXISTING\_SSID fine. It connects to MQTT fine. I can demand photos and that's fine. All ok. Now when I move the module to the greenhouse, it now connects via the WiFi extender to EXISTING\_SSID\_EXT, which has the same password and it indicates what the new IP address is. So I know it connects to the WiFi ok. But, monitoring the serial output from the module, it then indicates it is trying to connect to MQTT but fails with a code -2. So, it basically sits there doing retries at connecting to MQTT. So, nothing works as regards taking pictures.  
So, yes the problem is connecting to the MQTT broker.  
I haven't tried cycling the power to the Pi running the MQTT broker as yet. It's at a remote location so not easy to do now that I have come away from there.  
I wondered if the fact I had connected with a user name on one IP address would mean I couldn't log on via the extender with the same user name. I wondered if I had to 'disconnect' from MQTT as that user before connecting again.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 July 2023 20:33 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/4 "2023-07-08T20:33:46Z")

</div>

Which broker are you using? Look at the broker log, that may tell you something.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [9 July 2023 05:39 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/5 "2023-07-09T05:39:31Z")

</div>

Mosquitto. I don't know how to look at the log. I'll google it. Thanks.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [9 July 2023 08:33 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/6 "2023-07-09T08:33:56Z")

</div>

I think I have found a way of looking at he log using

sudo tail -n20 -f /var/log/mosquitto/mosquitto.log

I have brought the module to a different location with a different SSID and I get the following

```auto
1688880762: New connection from 192.168.1.123 on port 1883.
1688880762: New client connected from 192.168.1.123 as bc_cam4 (c1, k15).
1688881794: Client bc_cam4 has exceeded timeout, disconnecting.
1688881794: Socket error on client bc_cam4, disconnecting.

```

That sequence repeats over and over.  
I am over at the remote location tomorrow and will try it over there whilst monitoring the MQTT log.  
Not sure what the (c1,k15) means in the above.  
Is there a way of finding the reason for disconnect?  
Thanks again

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 July 2023 09:39 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/7 "2023-07-09T09:39:13Z")

</div>

> [@gajjer](#):
>
> Is there a way of finding the reason for disconnect?

The reason is that too long has elapsed since the broker last heard from the client. Note that it does not disconnect until 17 minutes after it connected, the timestamps are in seconds. If you edit /etc/mosquitto/mosquitto.conf and add the lines

```auto
log_timestamp true
log_timestamp_format %Y-%m-%d %H:%M:%S

```

and restart mosquitto:  
`sudo systemctl restart mosquitto`  
then it will show human readable timestamps which may help to see what is going on.

Is 192.168.1.123 the failing device? If so then it is successfully connecting according to the broker.

If you also add to the conf file  
`log_type all`  
then I think you will get a vast amount of extra log info, which may be helpful.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 July 2023 10:00 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/8 "2023-07-09T10:00:09Z")

</div>

1. what are you using to code the esp32-cam?
2. do you have code that choses the best wifi?
3. do you have code that watches for an MQTT disconnect and will reconnect?

If you are using the Arduino IDE, take a look at the example at [ESP32 MQTT Publish Subscribe with Arduino IDE | Random Nerd Tutorials](https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/)

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [9 July 2023 16:56 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/9 "2023-07-09T16:56:32Z")

</div>

Thanks for that. That log is not for the problem SSID. That is what happens at home. I am going to the remote location tomorrow and will try it with the problem SSID. I now have some tools to look at what is happening. I have included some Serial.print statements to help me too. I'm using an old Samsung phone with Terminal on it to monitor the output. Should be able to get a scope on it too.  
Thanks. That looks really helpful

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [9 July 2023 17:05 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/10 "2023-07-09T17:05:27Z")

</div>

I'm using Arduino IDE and yes it uses WifiMulti.h. The code is based on that RandomNerdTutorials program and works for other modules on the same SSID. However, none of the other modules are connected to the \_EXT SSID, so this is a first. The WiFi extender allows me to use the module down the end of the garden. I have of course included the extender SSID and Password in the ist for WiFiMulti. It connects to the WiFi but not to MQTT.  
I may have confused the information by listing the log for the problem module - but connected on a different network. I will get the log for the problem network tomorrow.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [10 July 2023 10:56 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/11 "2023-07-10T10:56:40Z")

</div>

Ok. Here is what happened at the remote location. First modified the mosquitto.conf file as described above. However, mosquitto would not start again because of any error. So edited it back out again. No big deal just the format. Must check on why though.  
Next added the line log\_type all and this was most useful. I have yet to check through the log to see what is going on.  
While reprogramming the ESP32-CAM module, I added a short piece of wire to the antenna on the pcb. 6.25mm in fact. While monitoring the ESP module, situated in the greenhouse, using the serial output monitor, I noticed that there had been a considerable improvement int the signal strength of the WiFi from the router. Almost the same as from the WiFi extender at about -70dB. So, I did the easy option and turned off the extender. Then when I rebooted, it logged into the router directly and immediately managed to connect to MQTT. Sorry if this w=is the cowards way out, but I'm all for simplicity!  
All I can say is that going via the extender, it did log into the router successfully but it failed multiple times to connect to MQTT. I have a log and will search through it for a clew but nothing leapt out at me.  
Thank you for your interest and your assistance. I have learned yet more.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [10 July 2023 16:07 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/12 "2023-07-10T16:07:48Z")

</div>

Back home again and I have had a look at the log. I can't see any reference to a request to connect from the module in question. Nor is there an entry that indicates a rejection. So puzzled.  
Is there a way of seeing what clients are connected to mosquitto?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 July 2023 16:23 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/13 "2023-07-10T16:23:26Z")

</div>

If there isn't any mention of the device in the mosquitto log then the connection messages didn't get through. Are you sure that the module can see the machine running mosquitto? Try adding some ping requests into the module code.  
Can you ping the module from the machine running mosquitto?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 July 2023 17:22 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/14 "2023-07-10T17:22:12Z")

</div>

> [@gajjer](#):
>
> Is there a way of seeing what clients are connected to mosquitto?

No. But I found this in a Google search:

You can get mosquitto to log each time a subscription request is which made which you could then use to build a list. Edit the mosquito.conf file (at /etc/mosquitto/mosquitto.conf on a raspberry Pi) and add:

```auto
log_type subscribe, unsubscribe

```

[UPDATE]  
This caused mosquito to fail on a restart but when I look at the moquitto log I see the IP's of devices.

```auto
pi@mqttpizw:~ $ sudo tail -n20 -f /var/log/mosquitto/mosquitto.log
1689011078: New client connected from 192.168.48.62 as nodered_19d0f4885bdfc1ab (c1, k60).
1689011078: New connection from 192.168.48.63 on port 1883.
1689011078: New client connected from 192.168.48.63 as nodered_b783de428beccebf (c1, k60).
1689011078: New connection from 192.168.48.63 on port 1883.
1689011078: New client connected from 192.168.48.63 as nodered_c9cfcdbe726abf5f (c1, k60).
1689011092: Client ESP8266Client-a04d has exceeded timeout, disconnecting.
1689011092: Socket error on client ESP8266Client-a04d, disconnecting.
1689011095: New connection from 192.168.48.3 on port 1883.
1689011095: New client connected from 192.168.48.3 as be3298b6-0c50-46d8-aa4d-87292e6f8121 (c1, k60).
1689011095: New connection from 192.168.48.61 on port 1883.
1689011095: New client connected from 192.168.48.61 as 1114ca6e-57cb-4430-ad96-7dcba71ddb60 (c1, k60).
1689011095: New connection from 192.168.48.221 on port 1883.
1689011095: New client connected from 192.168.48.221 as 99a87d45-111a-411c-9ab0-7ae54a73df67 (c1, k60).
1689011095: New connection from 192.168.48.70 on port 1883.
1689011095: New client connected from 192.168.48.70 as aa2a945b-f740-4288-8641-de2adab16147 (c1, k60).
1689011095: New connection from 192.168.48.62 on port 1883.
1689011095: New client connected from 192.168.48.62 as b60cfbf8-2291-4b07-be61-29f31c57ef66 (c1, k60).
1689011097: New connection from 192.168.48.63 on port 1883.
1689011097: New client connected from 192.168.48.63 as c0e2d183-95d4-408b-9312-6b0bb9dec5c8 (c1, k60).
1689011143: New connection from 192.168.48.229 on port 1883.

```

which might be helpful to you.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [10 July 2023 17:59 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/15 "2023-07-10T17:59:45Z")

</div>

That sounds like a useful thing to be able to do - if I knew how! So I presume on the Pi running mosquitto, do I send a ping command to the ip address of the module?  
Not sure what the code in the module would look like to send a ping.  
All these things sound like useful things to do.  
Thanks

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [10 July 2023 18:02 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/16 "2023-07-10T18:02:16Z")

</div>

Yes that sounds like an easy thing to try. At present I am getting pages of messages because log\_type is set to all.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [10 July 2023 18:03 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/17 "2023-07-10T18:03:48Z")

</div>

I can't try these things for about a week. I am told I have to go on holiday! Rats. Women don't understand these things!  
Thanks guys

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [11 July 2023 01:34 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/18 "2023-07-11T01:34:17Z")

</div>

Not sure if it could be related but most of the WiFi extenders I have used change the MAC address reported for connected devices.

So your DHCP server may be be allocating a different IP to the ESP or even blocking it if you have MAC filtering setup.

---

<div class="post-metadata">

**Author:** ![gajjer](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@gajjer](https://discourse.nodered.org/u/gajjer)\
**Post date:** [11 July 2023 07:13 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/19 "2023-07-11T07:13:40Z")

</div>

Thanks for the suggestion. I'll have a look.

---

<div class="post-metadata">

**Author:** ![manglemender](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@manglemender](https://discourse.nodered.org/u/manglemender)\
**Post date:** [13 July 2023 11:32 UTC](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646/20 "2023-07-13T11:32:05Z")

</div>

I also have an ESP32 in my greenhouse for monitoring soil moisture levels that are sent back to node-red via MQTT. I gave up with wifi extensions as they were just too problematic and have now installed a mesh network that covers the entire property.

[Next page](https://discourse.nodered.org/t/client-state-failed-with-state-2/79646.md?page=2)
