Skip to content
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

Reconnect to router / HA after router power cycle. #12

Open
JacobChrist opened this issue Oct 27, 2023 · 2 comments
Open

Reconnect to router / HA after router power cycle. #12

JacobChrist opened this issue Oct 27, 2023 · 2 comments

Comments

@JacobChrist
Copy link

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.

@daniloc
Copy link
Owner

daniloc commented Oct 28, 2023

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.

@JacobChrist
Copy link
Author

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();
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants