PicoW_HomeAssistant_Starter icon indicating copy to clipboard operation
PicoW_HomeAssistant_Starter copied to clipboard

Reconnect to router / HA after router power cycle.

Open JacobChrist opened this issue 2 years ago • 2 comments

Recently I've noticed that if my router is power cycled that the PicoPi either doesn't reconnect to the router or HA (not sure which yet). Not sure if this issue is best addressed by this project or the upstream arduino-home-assistant project.

JacobChrist avatar Oct 27 '23 18:10 JacobChrist

Entirely possible this implementation doesn't handle reconnects well, but impossible to say more without some basic investigation legwork done first. With the device connected to your machine so you can view the console, add some logging around the various network events. You can also enable debug logging for arduino-home-assistant:

#define ARDUINOHA_DEBUG

Add that BEFORE the a-h-a library is imported.

Then deliberately power cycle your router and see what happens.

daniloc avatar Oct 28 '23 02:10 daniloc

I have a solution to this issue, I'm not sure if its the best way to do it but it seems to be working. I modified Network.cpp so that is looks like this:

#include <WiFi.h>
#include "Network.h"
#include "Credentials.h"

uint8_t status;

void Network::connect() {
  do {

    Serial.print("Attempting to connect to WPA SSID: ");

    Serial.println(WIFI_SSID);

    // Connect to WPA/WPA2 network:

    status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //Set these credentials

    // wait to connect:

    delay(5000);

  } while (status != WL_CONNECTED);

  Serial.print("Connected to ");
  Serial.println(WIFI_SSID);
}

void Network::disconnect() {
  do {
    Serial.println("Attempting to disconnect from WiFi");
    status = WiFi.disconnect();
    delay(1000);
  } while (status != WL_DISCONNECTED);

  Serial.print("Disconnected from ");
  Serial.println(WIFI_SSID);
}

And loop() looks like this:

void loop() {
  uint8_t wifi_status = WiFi.status();
  if(wifi_status == WL_CONNECTED){
    integration.loop();
  }
  else{
    Network::disconnect();
    Network::connect();
  }
}

JacobChrist avatar Jun 23 '24 20:06 JacobChrist