Run code without wifi connection

Hi. this is my script

#include <WiFi.h>
 #define ONBOARD_LED  2

const char* ssid     = "wifi";
const char* password = "password";

WiFiServer server(80);

String header;

IPAddress local_IP(192, 168, 240, 20);
IPAddress gateway(192, 168, 240, 1);

IPAddress subnet(255, 255, 254, 0);
IPAddress primaryDNS(8, 8, 8, 8);   
IPAddress secondaryDNS(8, 8, 4, 4); 

void setup() {
  pinMode(ONBOARD_LED,OUTPUT);
  Serial.begin(115200);

  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }
  
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}


void loop() {
  delay(1000);
  digitalWrite(ONBOARD_LED,HIGH);
  delay(100);
  digitalWrite(ONBOARD_LED,LOW);
}

problem is, that if esp32 is not connected to wifi, LED wont blink. how can I fix it?

Instead of waiting forever for something that won't happen, maybe break out of this loop after a number of attempts.

Count them and when the number is high enough, use the break; statement.

a7

Your while loop will never exit if there is no connection. You need to add some code to exit after some number of tries if you want to continue anyway.

Or just use a for loop of some number of iterations.

a7

might also work

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.