Wifi loss stops my Voltage measurment

Wifi loss stops my Voltage measurment. It is a Problem in the Philipinnes. It can be for hours in worst case. Reconnect my Wifi works fine !! All following code stops working until Wifi is back. I need all code working permanent , also if Wifi is Offline.
Thanks in advance for any help.

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
//------------------------------------------
// Thingspeak
#include <ThingSpeakWriter_asukiaaa.h>
#define WRITE_API_KEY "2CHEZXKCV2FQFYDM"
ThingSpeakWriter_asukiaaa channelWriter(WRITE_API_KEY);
//------------------------------------------
#include <INA3221.h>
INA3221 ina_0(INA3221_ADDR40_GND);
//-----------------------------------------
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 20, 4);
LiquidCrystal_I2C lcd(0x25, 20, 4);
//------------------------------------------------------------------
// TIMER for K2-3

// K2
const float BB1 = 100.0;
const float BB1a = 250.0;
float countdown2 = 100.0;
float countdown2a = 250.0;
float V2 = 12.70;   // K2 ON
float V2a = 13.20;  // K2 OFF
//---------------------------------------
// K3
const float C1 = 100.0;
const float C1a = 250.0;
float countdown3 = 100.0;
float countdown3a = 250.0;
float V3 = 12.70;   // K3 ON
float V3a = 13.20;  // K3 OFF
//------------------------------------------------------------------
//Counter k1 - 3 int
static int count13 = 0;
static int count27 = 0;
static int count25 = 0;
//------------------------------------------------------------------
const char* ssid = "722027";
const char* password = "240833348";
//------------------------------------------------------------------
// Needed for OTA
AsyncWebServer server(80);
//------------------------------------------------------------------
// INA3221
float busvoltage1 = 0;
float val2 = 0;  // LIPO 1

float busvoltage2 = 0;
float val5 = 0;  // LIPO 2
//-------------------------------------------------------------------------
int vin1 = 0;  //Procent % Lead Acid Battery //int
int vin2 = 0;  //Procent % Lipo Battery // int
//----------------------------------------------------------------------
#define ONBOARD_LED 2
//------------------------------
const int buttonPin23 = 23;  //       Manually Lipo4 zu  Lead Acid aktivieren K1
const int buttonPin18 = 18;  //       not used
int RelayControl33 = 32;     //       LED RED - Buzzer
const int buttonPin19 = 19;  //       Manual Arduino RESET
const int buttonPin16 = 16;  //       not used
const int buttonPin12 = 12;  //       not used
int k1 = 13;                 // K1    Lipo4 to Leadacid ON/OFF
int k2 = 27;                 // K2    not used
int k3 = 25;                 // K3    Fridge ON/OFF 230V normal Power
int k4 = 26;                 // K4    Fridge ON/OFF Inverter
//--------------------------------------------------------------------------------------------
//                                    Reset Function Arduino by AH by 998
void (*resetFunc)(void) = 0;  //      declare reset fuction at address 0
//---------------------------------------------------------------------------------------------
//                                            SMILY
byte a[8] = {  //SMILE
  B00000,
  B00000,
  B01010,
  B00000,
  B10001,
  B01110,
  B00000,
  B00000
};

byte b[8] = {  //unhappy
  B00000,
  B01010,
  B00000,
  B00000,
  B01110,
  B10001,
  B00000,
  B00000
};
byte c[8] = {  // !
  B00000,
  B00100,
  B00100,
  B00100,
  B00100,
  B00100,
  B00000,
  B00100
};

//-------------------------------------------
void connectWiFi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    }
}
//------------------------------------------------------------------------------------------------

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void setup() {

  Serial.begin(115200);
  Wire.begin();
  connectWiFi();

  lcd1.init();
  lcd1.init();  // Start lcd1 Display(Top one)
  lcd1.backlight();
  //-----------------------------------
  lcd.init();
  lcd.init();  // Start lcd1 Display(bottom one)
  lcd.backlight();
  //-----------------------------------
  lcd1.createChar(0, a);  // SMILY
  lcd1.createChar(1, b);  // UNHAPPY
  lcd1.createChar(2, c);  // !!!!!!!
  delay(500);             //100
  //------------------------------------
  // OTA
  AsyncElegantOTA.begin(&server);  // Start AsyncElegantOTA
  server.begin();
  lcd.setCursor(0, 0);
  lcd.print("OTP HTTP Server OK");
  //-------------------------------------
  ina_0.begin();
  //------------------------------------
  // Set Relays status in lcd after reboot
  lcd1.setCursor(0, 3);
  lcd1.print("K1=");
  lcd1.setCursor(3, 3);
  lcd1.write(byte(0));  // SMILE
  //-------------------------------------
  lcd1.setCursor(5, 3);
  lcd1.print("K2=");
  lcd1.setCursor(8, 3);
  lcd1.write(byte(0));  // SMILE
  //------------------------------------
  lcd1.setCursor(10, 3);
  lcd1.print("K3=");
  lcd1.setCursor(13, 3);
  lcd1.print("I");  // INVERTER OFF
  //------------------------------------
  lcd1.setCursor(15, 3);
  lcd1.print("K4=");
  lcd1.setCursor(18, 3);
  lcd1.print("X");  // GRID OFF
  //-----------------------------------
  //Display counter K1-3 by start
  lcd.setCursor(0, 3);
  lcd.print("K1: ");
  lcd.setCursor(3, 3);
  lcd.print(count13);
  //---------------------------
  lcd.setCursor(7, 3);
  lcd.print("K2: ");
  lcd.setCursor(10, 3);
  lcd.print(count27);
  //--------------------------
  lcd.setCursor(14, 3);
  lcd.print("K3: ");
  lcd.setCursor(17, 3);
  lcd.print(count25);
  //-----------------------------------
  pinMode(ONBOARD_LED, OUTPUT);  //ESP32 LED"
  //-----------------------------------------------------------------------------------------
  //      Switches
  pinMode(buttonPin12, INPUT_PULLUP);  //   not used
  pinMode(buttonPin16, INPUT_PULLUP);  //   not used
  pinMode(buttonPin23, INPUT_PULLUP);  //   Manually Lipo4 zu Lead acid aktivieren
  pinMode(buttonPin18, INPUT_PULLUP);  //   not used
  pinMode(buttonPin19, INPUT_PULLUP);  //   Manual Arduino RESET
  //------------------------------------------------------------------------------------------
  //      Smily K1 to K4 as output definiert
  pinMode(25, OUTPUT);  // K3 Fridge ON/OFF 230V normal Power
  pinMode(27, OUTPUT);  // K2 Tv-Fan-Wifi-Security
  pinMode(13, OUTPUT);  // K1 Lipo4 to Leadacid ON/OFF
  pinMode(32, OUTPUT);  // Alarm LED to pin 32
  //----------------------------------------------
  //By start all relays OFF
  digitalWrite(25, HIGH);  // K3(LED on) by start Arduino - Fridge ON/OFF INVERTER (I)
  digitalWrite(27, HIGH);  // K2(LED off) by start Arduino - K2 Inverter on/OFF ATS2
  digitalWrite(13, HIGH);  // K1(LED off) by start Arduino - K1 Lipo4 to Leadacid ON/OFF
  //------------------------------------------------------------------------------------------
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop() {
  //-------------------------------------------------------------------------------------------
  if (WiFi.status() != WL_CONNECTED) {
    lcd.setCursor(0, 1);
    lcd.print("               ");
    lcd.setCursor(0, 1);
    lcd.print("WiFi lost.....");
    connectWiFi();
  }
  if (WiFi.status() == WL_CONNECTED) {
    lcd.setCursor(0, 1);
    lcd.print("               ");
    lcd.setCursor(0, 1);
    lcd.print(WiFi.localIP());
  }
  //-------------------------------------------------------------------------------------------
  delay(500);
  digitalWrite(ONBOARD_LED, HIGH);
  delay(500);
  digitalWrite(ONBOARD_LED, LOW);
  //-----------------------------------------------------------------------------------------
  // INA3221
  val2 = ina_0.getVoltage(INA3221_CH1);  // TV
  val5 = ina_0.getVoltage(INA3221_CH2);  // FRIDGE
  //----------------------------------------------------------------------------------------
  // Lipo4-1 - Battery %

  if (val5 >= 14.6) {
    vin2 = 100;
  } else if (val5 >= 13.6) {
    vin2 = 100;
  } else if (val5 >= 13.4) {
    vin2 = 99;
  } else if (val5 >= 13.35) {
    vin2 = 95;
  } else if (val5 >= 13.3) {
    vin2 = 90;
  } else if (val5 >= 13.25) {
    vin2 = 80;
  } else if (val5 >= 13.2) {
    vin2 = 70;
  } else if (val5 >= 13.15) {
    vin2 = 45;
  } else if (val5 >= 13.1) {
    vin2 = 40;
  } else if (val5 >= 13.05) {
    vin2 = 35;
  } else if (val5 >= 13.0) {
    vin2 = 30;
  } else if (val5 >= 12.95) {
    vin2 = 25;
  } else if (val5 >= 12.9) {
    vin2 = 20;
  } else if (val5 >= 12.8) {
    vin2 = 17;
  } else if (val5 >= 12.50) {
    vin2 = 14;
  } else if (val5 >= 12.25) {
    vin2 = 12;
  } else if (val5 >= 12.00) {
    vin2 = 9;
  } else if (val5 >= 11.0) {
    vin2 = 5;
  } else if (val5 >= 10.0) {
    vin2 = 0;
  }
  //------------------------------------------------------------------------------------------
  // Lipo4-2 - Battery %

  if (val2 >= 14.6) {
    vin1 = 100;
  } else if (val2 >= 13.6) {
    vin1 = 100;
  } else if (val2 >= 13.4) {
    vin1 = 99;
  } else if (val2 >= 13.35) {
    vin1 = 95;
  } else if (val2 >= 13.3) {
    vin1 = 90;
  } else if (val2 >= 13.25) {
    vin1 = 80;
  } else if (val2 >= 13.2) {
    vin1 = 70;
  } else if (val2 >= 13.15) {
    vin1 = 45;
  } else if (val2 >= 13.1) {
    vin1 = 40;
  } else if (val2 >= 13.05) {
    vin1 = 35;
  } else if (val2 >= 13.0) {
    vin1 = 30;
  } else if (val2 >= 12.95) {
    vin1 = 25;
  } else if (val2 >= 12.9) {
    vin1 = 20;
  } else if (val2 >= 12.8) {
    vin1 = 17;
  } else if (val2 >= 12.50) {
    vin1 = 14;
  } else if (val2 >= 12.25) {
    vin1 = 12;
  } else if (val2 >= 12.00) {
    vin1 = 9;
  } else if (val2 >= 11.0) {
    vin1 = 5;
  } else if (val2 >= 10.0) {
    vin1 = 0;
  }
  //------------------------------------------------------------------------------------------
  //         Lipo4 Battery Voltage + Charge status (TV)
  lcd1.setCursor(0, 1);
  lcd1.print("Lipo-2:");
  lcd1.setCursor(8, 1);
  lcd1.print(val5);
  lcd1.setCursor(15, 1);
  lcd1.print(vin2);
  //lcd1.setCursor(17, 0);
  lcd1.print("%");
  //--------------------------------------------------
  //         Lipo4 Battery Voltage + Charge status (Fridge)
  lcd1.setCursor(0, 0);
  lcd1.print("Lipo-1:");
  lcd1.setCursor(8, 0);
  lcd1.print(val2);
  lcd1.setCursor(15, 0);
  lcd1.print(vin1);
  //lcd1.setCursor(17, 1);
  lcd1.print("%");
  //--------------------------------------------------------------------------------------------
  //        Manual Arduino RESET
  if (digitalRead(buttonPin19) == LOW) {  //
    resetFunc();                          //call reset
  }
  //-------------------------------------------------------------------------------------------
  // Fridge (INVERTER) On K3 (LED ON) Relay 1 NO
  if (val5 < V3 && countdown3 > 0 && digitalRead(25) == HIGH) {
    countdown3 -= 1.0;
  }
  if (countdown3 == 0 && val5 > V3) {
    countdown3 = C1;
    countdown3a = C1a;
  }
  if (val5 >= V3 && val5 < V3a) {
    countdown3 = C1;
    countdown3a = C1a;
  }
  if (val5 < V3 && countdown3 == 0 && digitalRead(25) == HIGH) {
    countdown3a -= 1.0;
  }
  if (countdown3a == 0) {
    digitalWrite(25, LOW);
    lcd1.setCursor(13, 3);
    lcd1.print("G");  // INVERTER OFF
  }
  if (val5 >= V3a) {
    digitalWrite(25, HIGH);
    lcd1.setCursor(13, 3);
    lcd1.print("I");  // INVERTER ON
    countdown3 = C1;
    countdown3a = C1a;
  }

  //lcd.setCursor(0, 2);
  //lcd.print("T3: ");
  //lcd.setCursor(3, 2);
  //lcd.print(int(countdown3));
  //lcd.setCursor(3, 2);
  //lcd.print("    ");
  //lcd.setCursor(3, 2);
  //lcd.print(int(countdown3));

  //lcd.setCursor(10, 2);
  //lcd.print("T3a: ");
  //lcd.setCursor(14, 2);
  //lcd.print(int(countdown3a));
  //lcd.setCursor(14, 2);
  //lcd.print("    ");
  //lcd.setCursor(14, 2);
  //lcd.print(int(countdown3a));
  //---------------------------------------------------------------------------------------------------------------------------------------------------------
  //            K2 Actually Cut off Inverter to activate ADS to switch to Grid
  // K2 Actually Cut off Inverter to activate ADS to switch to Grid
  if (val2 < V2 && countdown2 > 0 && digitalRead(27) == HIGH) {
    countdown2 -= 1.0;
  }
  if (countdown2 == 0 && val2 > V2) {
    countdown2 = BB1;
    countdown2a = BB1a;
  }
  if (val2 >= V2 && val2 < V2a) {
    countdown2 = BB1;
    countdown2a = BB1a;
  }
  if (val2 < V2 && countdown2 == 0 && digitalRead(27) == HIGH) {
    countdown2a -= 1.0;
  }
  if (countdown2a == 0) {
    digitalWrite(27, LOW);
    lcd1.setCursor(5, 3);
    lcd1.print("K2=");
    lcd1.setCursor(8, 3);
    lcd1.write(byte(1));  // UNHAPPY
  }
  if (val2 >= V2a) {
    digitalWrite(27, HIGH);
    lcd1.setCursor(5, 3);
    lcd1.print("K2=");
    lcd1.setCursor(8, 3);
    lcd1.write(byte(0));  // SMILE
    countdown2 = BB1;
    countdown2a = BB1a;
  }
  //lcd.setCursor(0, 1);
  //lcd.print("T2: ");
  //lcd.setCursor(3, 1);
  //lcd.print(int(countdown2));
  //lcd.setCursor(3, 1);
  //lcd.print("    ");
  //lcd.setCursor(3, 1);
  //lcd.print(int(countdown2));

  //lcd.setCursor(10, 1);
  //lcd.print("T2a: ");
  //lcd.setCursor(14, 1);
  //lcd.print(int(countdown2a));
  //lcd.setCursor(14, 1);
  //lcd.print("    ");
  //lcd.setCursor(14, 1);
  //lcd.print(int(countdown2a));
  //---------------------------------------------------------------------------------------------------------------------------------------------------------
  // K1 Lipo-1 to Lipo-2 ON/OFF Automatic
  if (val2 >= 14.5) {
    digitalWrite(13, LOW);  // ON
    lcd1.setCursor(0, 3);
    lcd1.print("K1=");
    lcd1.setCursor(3, 3);
    lcd1.write(byte(1));  // UNHAPPY
  }
  if (val2 <= 13.80) {
    digitalWrite(13, HIGH);  // OFF
    lcd1.setCursor(0, 3);
    lcd1.print("K1=");
    lcd1.setCursor(3, 3);
    lcd1.write(byte(0));  // HAPPY
  }
  //if (digitalRead(buttonPin23) == HIGH) { // MANUALL ON
  //digitalWrite(13, LOW);
  //lcd1.setCursor(0, 3);
  //lcd1.print("K1=");
  //lcd1.setCursor(3, 3);
  //lcd1.write(byte(1));  // UNHAPPY
  //} else {
  //digitalWrite(13, HIGH);             // MANUALL OFF
  //lcd1.setCursor(0, 3);
  //lcd1.print("K1=");
  //lcd1.setCursor(3, 3);
  //lcd1.write(byte(0));  // HAPPY
  //}
  //----------------------------------------------------------------------------------
  // Alarm Lipo4-1 Battery HIGH
  if (val5 > 14.70) {
    digitalWrite(32, HIGH);
    delay(1000);
    digitalWrite(32, LOW);
    delay(1000);
  }
  if (val5 < 14.60) {
    digitalWrite(32, LOW);
  }
  //---------------------------------
  // Alarm Lipo4-1 Battery LOW
  if (val5 < 12.00) {
    digitalWrite(32, HIGH);
    delay(1000);
    digitalWrite(32, LOW);
    delay(3000);
  }
  if (val5 > 12.10) {
    digitalWrite(32, LOW);
  }
  //---------------------------------------
  // Alarm Lipo-2 Battery HIGH
  if (val2 > 14.70) {
    digitalWrite(32, HIGH);
    delay(1000);
    digitalWrite(32, LOW);
    delay(3000);
  }
  if (val2 < 14.60) {
    digitalWrite(32, LOW);
  }
  //---------------------------------
  // Alarm Lipo-2 Battery LOW
  if (val2 < 12.00) {
    digitalWrite(32, HIGH);
    delay(1000);
    digitalWrite(32, LOW);
    delay(3000);
  }
  if (val2 > 12.00) {
    digitalWrite(32, LOW);
  }
  //----------------------------------------------------------------------------------
  // Counter Status k1-k3 wenn ausgelöst

  // count the number of times digitalWrite(13, LOW) goes LOW
  static int prev13 = HIGH;
  if (digitalRead(13) == LOW) {
    if (prev13 == HIGH) {
      count13++;
      lcd.setCursor(0, 3);
      lcd.print("K1: ");
      lcd.setCursor(3, 3);
      lcd.print(count13);
    }
    prev13 = LOW;
  } else {
    prev13 = HIGH;
  }

  // count the number of times digitalWrite(27, HIGH) goes HIGH
  static int prev27 = HIGH;
  if (digitalRead(27) == LOW) {
    if (prev27 == HIGH) {
      count27++;
      lcd.setCursor(7, 3);
      lcd.print("K2: ");
      lcd.setCursor(10, 3);
      lcd.print(count27);
    }
    prev27 = LOW;
  } else {
    prev27 = HIGH;
  }

  // count the number of times digitalWrite(25, HIGH) goes HIGH
  static int prev25 = HIGH;
  if (digitalRead(25) == LOW) {
    if (prev25 == HIGH) {
      count25++;
      lcd.setCursor(14, 3);
      lcd.print("K3: ");
      lcd.setCursor(17, 3);
      lcd.print(count25);
    }
    prev25 = LOW;
  } else {
    prev25 = HIGH;
  }
  //-------------------------------------------------------------------------------------
  // THINGSPEAK APP + HOMEPAGE
  bool k1a = digitalRead(13) == HIGH;
  int value1 = k1a ? 0 : 5;  // Set value based on digital state for K1

  bool k2a = digitalRead(27) == HIGH;
  int value2 = k2a ? 0 : 5;  // Set value based on digital state for K2

  bool k3a = digitalRead(25) == HIGH;
  int value3 = k3a ? 0 : 5;  // Set value based on digital state for K3

  bool k4a = digitalRead(26) == HIGH;
  int value4 = k4a ? 0 : 5;  // Set value based on digital state for K4

  channelWriter.setField(1, String(val2));
  channelWriter.setField(2, String(val5));
  channelWriter.setField(3, String(value1));
  channelWriter.setField(4, String(value2));
  channelWriter.setField(5, String(value3));
  channelWriter.setField(6, String(value4));
  int result = channelWriter.writeFields();
  //------------------------------------------------------------------------------------
}  // Program END

Your code looks nice, blocking but nice and well annotated. I took a quick look suspect you are getting caught up in your first line of code in your main loop. You have" if (WiFi.status() == WL_CONNECTED) {...} kyou need to add } elsse { before the closing "}" what you want it to do. I am not a coding expert so this is just an educated guess on my part.

i changed the code part. But it doesnt make a difference. Voltage meassurment still stops.
Maybe you can give a advice how to change this.

void loop() {
  //-------------------------------------------------------------------------------------------
  if (WiFi.status() != WL_CONNECTED) {
    lcd.setCursor(0, 1);
    lcd.print("               ");
    lcd.setCursor(0, 1);
    lcd.print("WiFi lost.....");
    connectWiFi();
  } else {
    //if (WiFi.status() == WL_CONNECTED) {
    lcd.setCursor(0, 1);
    lcd.print("               ");
    lcd.setCursor(0, 1);
    lcd.print(WiFi.localIP());
  }

i am not sure but it seems the problem is the WHILE in following code. The problem if i take out WHILE the wifi doesnt reconnect.

void connectWiFi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }
}

You need to remove the while because that will wait for as long as the WI-Fi is down.

What you should do is just check the Wi-Fi status and then move on each time through your main loop function. Set a flag and use that to bypass anything that would normal access the Wi-Fi.

1 Like

can you please give me example with the flag . by the way if i remove the While the wifi doesnt reconnect

Then try:-

void connectWiFi() {
long unsigned int timeStart = millis();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED ||  millis() - timeStart  > 2000) {
    delay(100);
  }
}

That will slow you code down by only two seconds as it sees if it can connect again.

You have a flag already it is WiFi.status()

Your code doesnt work. Voltage meassument stops.And no reconnect

Ok you are on your own then, sorry.