Arduino + W5100 - reconnect issue

Hello guys, I need a help with my project… Using Arduino + W5100 module.

When there´s a power outage… my Arduino + W5100 reconnects faster than my router… so I don’t have internet connection at this time and connection to MQTT server fails. So it won’t reconnect anymore. I have to arrive home and reset Arduino… so it will connect nicely.

Do you have any toughts on this?! Any routine for automatic reconnection to mqtt server?! Any logs needed?

Br,
Fernando

what do you mean by this?
the cayenne MQTT library tries to re-connect automatically whenever there is loss of internet connection.
You can add #define CAYENNE_DEBUG and check your serial monitor for output.

I mean Power outage in the neiighborhood… energy shutdown… so… Arduino/w5100 will boot faster than my router… router takes time to get an internet connection… the Arduino will be freezed trying to reconnect… will get the logs and paste here… will simulate turning the router off/on…

I’ve got the log.

Look how weird it is after the router reboot. Arduino/W5100 fails DHCP and gets a weird IP (wrong subnetwork) and then fails the connections.

After I power cycle the board again it gets the proper IP and starts to work again.

Any toughts on this?

#########################################################

WORKING PROPERLY - IP 192.168.0.14

#########################################################

[0] MAC: FE-39-E8-6-BA-B6
[4628] IP: 192.168.0.14
[4628] Connecting to 52.4.184.212:1883
[5226] Connected
[13663] Channel 4, pin 4, value 1
[16226] Channel 4, pin 4, value 0
[18677] Channel 7, pin 7, value 1
[22593] Channel 7, pin 7, value 0
[24842] Channel 6, pin 6, value 1
[26169] Channel 6, pin 6, value 0

#########################################################

NOT WORKING PROPERLY - DHCP FAILS IP 192.168.1.11 ??

#########################################################

[0] MAC: FE-39-E8-6-BA-B6
[60450] DHCP failed, retrying
[119261] IP: 192.168.1.11
[119262] Connecting to 52.4.184.212:1883
[151190] Network connect failed
[154000] Network connect failed
[156809] Network connect failed
[159618] Network connect failed
[162427] Network connect failed
[165236] Network connect failed
[168046] Network connect failed

#########################################################

WORKING PROPERLY - IP 192.168.0.14

#########################################################

[0] MAC: FE-39-E8-6-BA-B6
[6580] IP: 192.168.0.14
[6581] Connecting to 52.4.184.212:1883
[38507] Network connect failed
[39839] Connected
[131429] Channel 4, pin 4, value 1

can you post the entire serial output? First keep your router ON (device online), then turn it OFF (device offline) and turn it ON again (should make it online).

So here we go…

1st log as you asked… connected… then router restarts… it disconnects and connects again properly… It seems to be reconnecting nicely…

[0] MAC: FE-39-E8-6-BA-B6
[8638] IP: 192.168.0.14
[8638] Connecting to 52.4.184.212:1883
[9193] Connected
[31525] Channel 4, pin 4, value 1
[35255] Channel 4, pin 4, value 0
[38249] Channel 7, pin 7, value 1
[40722] Channel 7, pin 7, value 0
[42508] Channel 6, pin 6, value 1
[43956] Channel 6, pin 6, value 0
[89444] Disconnected
[89444] Connecting to 52.4.184.212:1883
[91253] Network connect failed
[94062] Network connect failed
[96872] Network connect failed
[99681] Network connect failed
[102490] Network connect failed
[105299] Network connect failed
[108108] Network connect failed
[110918] Network connect failed
[113727] Network connect failed
[116537] Network connect failed
[119346] Network connect failed
[122156] Network connect failed
[124964] Network connect failed
[127773] Network connect failed
[130582] Network connect failed
[133392] Network connect failed
[136202] Network connect failed
[138925] Connected
[209401] Channel 4, pin 4, value 0
[224846] Channel 4, pin 4, value 1
[228685] Channel 4, pin 4, value 0
[233145] Channel 6, pin 6, value 1
[235758] Channel 6, pin 6, value 0

The problem is in this other simulation… I shut off Arduino and router together… Then power up Arduino… then after 2 minutes the router… looks like it gets a wrong IP after a DHCP fail… and keeps failing… until I power cycle the Arduino again…

[0] MAC: FE-39-E8-6-BA-B6
[60450] DHCP failed, retrying
[121001] DHCP failed, retrying
[131641] IP: 192.168.1.11
[131642] Connecting to 52.4.184.212:1883
[163570] Network connect failed
[166380] Network connect failed
[169189] Network connect failed
[171999] Network connect failed
[174808] Network connect failed
[177616] Network connect failed
[180426] Network connect failed
[183235] Network connect failed
[186045] Network connect failed
[188854] Network connect failed
[191664] Network connect failed
[194472] Network connect failed
[197281] Network connect failed
[200090] Network connect failed
[202899] Network connect failed
[205709] Network connect failed
[208518] Network connect failed
[211328] Network connect failed
[214136] Network connect failed
[216946] Network connect failed
[219755] Network connect failed
[222565] Network connect failed
[0] MAC: FE-39-E8-6-BA-B6
[7581] IP: 192.168.0.14
[7581] Connecting to 52.4.184.212:1883
[39507] Network connect failed
[40852] Connected
[55430] Channel 4, pin 4, value 1
[59156] Channel 4, pin 4, value 0

can you share the code you are using?

Here it is:

#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>  
#include "DHT.h"

char username[] = "";
char password[] = "";
char clientID[] = "";

#define CAYENNE_DEBUG 
#define CAYENNE_PRINT Serial
#define SENSOR_PIN 0
#define VIRTUAL_THERMISTOR 1
#define VIRTUAL_DHT11_TEMP 2
#define VIRTUAL_DHT11_HUM 3
#define VALVE 4
#define VIRTUAL_VALVE 4
#define DHTPIN 5
#define DHTTYPE DHT11   // DHT 22  (AM2302), AM2321
#define GARAGE 6
#define VIRTUAL_GARAGE 6
#define QUARTINHO 7
#define VIRTUAL_QUARTINHO 7

const float resistance = 2200; // Resistance of the resistor 

DHT dht(DHTPIN, DHTTYPE);
Thermistor thermistor(SENSOR_PIN, resistance);

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
  dht.begin();
  pinMode(VALVE,OUTPUT);
  pinMode(GARAGE,OUTPUT);
  pinMode(QUARTINHO,OUTPUT);
  
}

void loop()
{
	Cayenne.loop();


}

 CAYENNE_IN(VIRTUAL_VALVE)

{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_VALVE, VALVE, value);
  // Write the value received to the digital pin.
  digitalWrite(VALVE, value);
}


CAYENNE_IN(VIRTUAL_GARAGE)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(GARAGE, HIGH);
  }
  else {
    digitalWrite(GARAGE, LOW);
  }
}


CAYENNE_IN(VIRTUAL_QUARTINHO)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(QUARTINHO, HIGH);
  }
  else {
    digitalWrite(QUARTINHO, LOW);
  }
}


CAYENNE_OUT(VIRTUAL_THERMISTOR)
{
	// This command writes the temperature in Celsius to the Virtual Channel.
	Cayenne.celsiusWrite(VIRTUAL_THERMISTOR, thermistor.getCelsius());
}


CAYENNE_OUT(VIRTUAL_DHT11_TEMP)
{
  float t = dht.readTemperature();
  Cayenne.celsiusWrite(VIRTUAL_DHT11_TEMP, t);
}

CAYENNE_OUT(VIRTUAL_DHT11_HUM)
{
  float h = dht.readHumidity();
  Cayenne.virtualWrite(VIRTUAL_DHT11_HUM, h);
}

in your arduino cayenne library open CayenneDefines.h (src/CayenneUtils/CayenneDefines.h).
in this file is this same #define CAYENNE_DOMAIN "mqtt.mydevices.com"

Hello Shramik,

this is the only way I’ve found to connect to your MQTT server. Whe I use DNS it won’t resolve it and fails the connection. When I change to the resolved IP address in the CayenneDefines.h it works and connect to the server.

Br,

on your router open the DHCP setting and enable DNS. Also try changing the DNS server to 8.8.8.8

In your setup you could add a loop that won’t exit until you get a valid IP address. I don’t have an example right now, but let me know if you can’t figure it out.

@dart73 is the issue solved?

My Cayenne is not connecting anymore. I suppose IP address for mqtt.mydevices.com changed so I’ll have change Cayenne domain on CayenneDefines and fix the DNS issue on the router and give a new try.

Previously I was connecting succesfully to IP 52.4.184.212 but now when I resolve the DNS is pointing to 52.200.177.238… so looks like it changed…

Is this IP always being changed?

tagging @jburhenn here

Sounds like some issue with DHCP on your network. For instance, you might have some other DHCP server on the network that is assigning the incorrect IP as suggested here:

And here’s another link with some DHCP troubleshooting:

So you could try and figure out if any device on your network is acting as a DHCP server, or if your router has some issue with its DHCP settings. Alternatively you could just use a static IP for your Arduino, like this:

Cayenne.begin(username, password, clientID, IPAddress(192, 168, 0, 14));

And yes, the mqtt.mydevices.com IP is dynamic to it will change like you are seeing.

1 Like

Guys, some good news here.

I had to troubleshoot my router. There was some configuration issue with DHCP. It was conflicting with the WAN router DHCP so after this fix and Google DNS configuration on router it started to solve the _mqtt.mydevices.com DNS. Also tried some spontaneous reboot on router, arduino/router and Arduino and it seems to be reconnecting nicely.

I will keep monitoring it for the next couple of days but I think we can close this topic now.

Thanks for the support. I really appreciate it.

2 Likes

Glad to hear it’s working now. Let us know if you run into any other issues.