Show wifi status with led

Hello
Is there a way to show the Wi-Fi status with an LED? Can it work separately from the void loop line? Because the delays in the program line sometimes disturb the state of the led blink.

Yes

If you mean the delay() function causes problems then don't use it

Please post your problem sketch using code tags when you do

This is my code
I want to add wifi status in this code

The device has a mode that checks the status of a new update every 10 seconds. Just checking the update will delay the LED flashing status


//Rev 1.5
//Uptime added
//Web OTA Added
//Ni read bug fix
//The reset operation was not performed when it was turned on without a sensor fixed
//Github SelfUpdate 

#include <SoftwareSerial.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>
#include <ESPAsyncTCP.h>
//#include <ESPAsyncWebServer.h>
//#include <AsyncElegantOTA.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <ESP8266Ping.h>



const String FirmwareVer={"1.1.0"}; 
#define URL_fw_Version ""
#define URL_fw_Bin ""
const char* host = "raw.githubusercontent.com";
const int httpsPort = 443;


#define WIFI_SSID "SSID"
#define WIFI_PASS "PASSWORD"
#define AP_SSID "ESP8266"
#define AP_PASS "magicword"
const char* ssid = WIFI_SSID;
const char* passphrase = WIFI_PASS;
const char* mqttServerName = "";
const int   mqttport = 1883;         
const char* mqttuser = "";
const char* mqttpass = "";
String prefix= "/angizeh/security_system/25/1" ;
const char* clientID = "Ang-NPK2";
const int redPin = 0;
IPAddress local_IP(192,168,43,10);
IPAddress gateway(192,168,43,1);
IPAddress subnet(255,255,255,0);
IPAddress primaryDNS(8,8,8,8);
IPAddress secondaryDNS(8,8,4,4);

//AsyncWebServer server(80);

WiFiClient wclient;                        
PubSubClient client(wclient);

#define RE 5
#define DE 4
#define PUB_DELAY 50


const byte ph[] = {0x02, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x38}; // Soil PH Sensor
byte ph_values[11];


const byte ni[] = {0x02, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xFF}; // Multi Sensor: Nitrogen
const byte phos[] = {0x02, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x3F};
const byte pot[] = {0x02, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xF3};
//const byte pot[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
const byte moist[] = {0x02, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x3C};
const byte temp[] = {0x02, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xFC};
const byte ec[] = {0x02, 0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xFD};

const byte orp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA}; 
byte orp_values[11];

const byte ph2[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA}; 
byte ph2_values[11];

const byte temp2[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
byte temp2_values[11];

const byte res[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xC5, 0xCB};
byte res_values[11];

byte pot_values[11];
byte moist_values[11];
byte phos_values[11];
byte ni_values[11];
byte ec_values[11];
byte temp_values[11];


int cnt=10;
int cnt2=60;

int i = 0;
int statusCode;
long Day=0;
int Hour =0;
int Minute=0;
int Second=0;
int HighMillis=0;
int Rollover=0;

String esid;
String epass = "";
String content;
String st;
static unsigned long timer = millis();

bool testWifi(void);
void launchWeb(void);
void setupAP(void);
ESP8266WebServer server(80);


SoftwareSerial mod;

 
void setup()
{
  Serial.println(ESP.getFreeHeap());
  Serial.begin(115200); 
  mod.begin(9600, SWSERIAL_8N1, 14, 12, false, 256);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
   EEPROM.begin(512);
   delay(10);
   Serial.println("Startup");
 //config wifi with optional settings
  
//  if(!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS))
//  {
//    Serial.println("STA Failed to configure");
//  }
  // Read eeprom for ssid and password
   Serial.println("Reading EEPROM ssid");
 
 
  for (int i = 0; i < 32; ++i)
  {
    esid += char(EEPROM.read(i));
  }
  Serial.println();
  Serial.print("SSID: ");
  Serial.println(esid);
  Serial.println("Reading EEPROM pass");
 
  
  for (int i = 32; i < 96; ++i)
  {
    epass += char(EEPROM.read(i));
  }
  Serial.print("PASS: ");
  Serial.println(epass);
 
 
  WiFi.begin(esid.c_str(), epass.c_str());
  if (testWifi())
  {
    Serial.println("Succesfully Connected!!!");
    mqttconnect();
    return;
  }
  else
  {
    Serial.println("Turning the HotSpot On");
    launchWeb();
    setupAP();// Setup HotSpot
  }
 
  Serial.println();
  Serial.println("Waiting.");
  
  while ((WiFi.status() != WL_CONNECTED))
  {
    server.handleClient();
    if (millis() - timer > 600000){
      timer = millis();
       ESP.wdtEnable(0);
    while(1);
    }
  }
  ESP.restart();
  //mqttconnect();


//  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
//    request->send(200, "text/plain", "Hi! I am ESP8266.");
//  });
//
//  AsyncElegantOTA.begin(&server);    // Start ElegantOTA
//  server.begin();
//  Serial.println("HTTP server started");

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         //192.168.43.215

  
  
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  
}
 
void loop()
{  

   if ((WiFi.status() == WL_CONNECTED))
  {
    digitalWrite(redPin, HIGH);            // Fast Blink Red LED 
    delay(250);
    Serial.print("on");
    digitalWrite(redPin, LOW);
    delay(250);
uptime(); //Runs the uptime script located below the main loop and reenters the main loop
print_Uptime();
checkUpdate();   
ESP.wdtEnable(5000);
if (!client.connected()) {
 
  reconnect();
  } else {
 ArduinoOTA.handle();
 client.loop();
 String IP = WiFi.localIP().toString() ;
client.publish(toCharArray("/angizeh/security_system/25/1/IP:"), toCharArray(String(clientID) +"/"+WiFi.localIP().toString()));
client.publish(toCharArray("/angizeh/security_system/25/1/Uptime:"), toCharArray("Uptime:"+String(Day)+":"+String(Hour)+":"+String(Minute)+":"+String(Second)));
client.publish(toCharArray("/angizeh/security_system/25/1/MAC:"), toCharArray(String(WiFi.macAddress())));
client.publish(toCharArray("/angizeh/security_system/25/1/FirmwareVer:"), toCharArray(String(FirmwareVer)));
Serial.println("IP: " +IP);

multi_sensor_ph_detect();
multi_sensor_ni_detect();
multi_sensor_phos_detect();
multi_sensor_pot_detect();
multi_sensor_moist_detect();
multi_sensor_temp_detect();
multi_sensor_ec_detect();

multi_sensor_orp_detect();
multi_sensor_ph2_detect();
multi_sensor_temp2_detect();
multi_sensor_res_detect();

  }

 }
  else
  {
      Serial.println("no connection");
      checkWifi();

  }
   yield();
}

  
void multi_sensor_ph_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ph, sizeof(ph)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ph_values, 7);
     // Serial.print("Multi PH Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(ph_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (ph_values[0] == 0x02 && ph_values[1] == 0x03 && ph_values[2] == 0x02){
      float ph_reg3and4 = ((ph_values[3] << 8) + ph_values[4]);
      ph_reg3and4 = ph_reg3and4/100;
      if (ph_reg3and4 > -1 && ph_reg3and4 < 15){
        Serial.print("PH : "+(String)ph_reg3and4+"");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/PH"), toCharArray(String(ph_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.print("PH cant measure");
    break;
    }
  }
}


void multi_sensor_phos_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(phos, sizeof(phos)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(phos_values, 7);
     // Serial.print("Multi PHOS Values : ");
      for (byte i = 0; i < 7; i++)
      {
       // Serial.print(phos_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (phos_values[0] == 0x02 && phos_values[1] == 0x03 && phos_values[2] == 0x02){
      float phos_reg3and4 = ((phos_values[3] << 8) + phos_values[4]);
      if (phos_reg3and4 > -1 && phos_reg3and4 < 1000){
        Serial.print("PHOS : "+(String)phos_reg3and4+" mg/kg");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/PHOS"), toCharArray(String(phos_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.print("PHOS cant measure");
    break;
    }
  }
}

void multi_sensor_pot_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(pot, sizeof(pot)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(pot_values, 7);
      //Serial.print("Multi POT Values : ");
      for (byte i = 0; i < 7; i++)
      {
       // Serial.print(pot_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (pot_values[0] == 0x02 && pot_values[1] == 0x03 && pot_values[2] == 0x02){
      float pot_reg3and4 = ((pot_values[3] << 8) + pot_values[4]);
      if (pot_reg3and4 > -1 && pot_reg3and4 < 10000){
        Serial.print("POT : "+(String)pot_reg3and4+" mg/kg");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/POT"), toCharArray(String(pot_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.print("POT cant measure");
    break;
    }
  }
}

void multi_sensor_ec_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ec, sizeof(ec)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ec_values, 7);
     // Serial.print("Multi EC Values : ");
      for (byte i = 0; i < 7; i++)
      {
      //  Serial.print(ec_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (ec_values[0] == 0x02 && ec_values[1] == 0x03 && ec_values[2] == 0x02){
      float ec_reg3and4 = ((ec_values[3] << 8) + ec_values[4]);
      if (ec_reg3and4 > -1 && ec_reg3and4 < 10000){
        Serial.print("EC : "+(String)ec_reg3and4+" us/cm");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/EC"), toCharArray(String(ec_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.println("EC cant measure");
    break;
    }
  }
}


void multi_sensor_moist_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(moist, sizeof(moist)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(moist_values, 7);
      //Serial.print("Multi MOIST Values : ");
      for (byte i = 0; i < 7; i++)
      {
       // Serial.print(moist_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (moist_values[0] == 0x02 && moist_values[1] == 0x03 && moist_values[2] == 0x02){
      float moist_reg3and4 = ((moist_values[3] << 8) + moist_values[4]);
      moist_reg3and4 = moist_reg3and4/10;
      if (moist_reg3and4 > -1 && moist_reg3and4 < 105){
        Serial.print("MOIST : "+(String)moist_reg3and4+"%");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/MOIST"), toCharArray(String(moist_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.print("MOIST cant measure");
    break;
    }
  }
}

void multi_sensor_temp_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(temp, sizeof(temp)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(temp_values, 7);
       //Serial.print("Multi TEMP Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(temp_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (temp_values[0] == 0x02 && temp_values[1] == 0x03 && temp_values[2] == 0x02){
      float temp_reg3and4 = ((temp_values[3] << 8) + temp_values[4]);
      temp_reg3and4 = temp_reg3and4/10;
      if (temp_reg3and4 > -1 && temp_reg3and4 < 100){
        Serial.print("TEMP : "+(String)temp_reg3and4+"°C");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/Temperature"), toCharArray(String(temp_reg3and4)));
        Serial.println("Published");
        break;
      }
    }else{
      Serial.print("TEMP cant measure");
    break;
    }
  }
}

void multi_sensor_ni_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ni, sizeof(ni)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ni_values, 7);
       //Serial.print("Multi NI Values : ");
      for (byte i = 0; i < 7; i++)
      {
       //Serial.print(ni_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (ni_values[0] == 0x02 && ni_values[1] == 0x03 && ni_values[2] == 0x02){
       float ni_reg3and4 =((ni_values[3] << 8) + ni_values[4]);
        Serial.print("NI : "+(String)ni_reg3and4+" mg/kg");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/NI"), toCharArray(String(ni_reg3and4)));
        Serial.println("Published");
        break;
      
    }else{
      Serial.print("NI cant measure");
    break;
    }
  }
}



void multi_sensor_orp_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(orp, sizeof(orp)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(orp_values, 7);
     // Serial.print("Multi PH Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(ph_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    //if (ph_values[0] == 0x01 && ph_values[1] == 0x03 && ph_values[2] == 0x00){
    float orp_reg3and4 = word(orp_values[3],orp_values[4]);
     //Serial.println(ph_reg3and4);
      orp_reg3and4 = orp_reg3and4/10;
    //  if (orp_reg3and4 > -1 && orp_reg3and4 < 15){
        Serial.print("ORP : "+(String)orp_reg3and4+"");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/ORP"), toCharArray(String(orp_reg3and4)));
        Serial.println("Published");
        break;
 //     }
//    }else{
//      Serial.print("PH cant measure");
//    break;
//    }
//  
  }
}


void multi_sensor_ph2_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ph2, sizeof(ph2)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ph2_values, 7);
     // Serial.print("Multi PH Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(ph_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    //if (ph_values[0] == 0x01 && ph_values[1] == 0x03 && ph_values[2] == 0x00){
    float ph2_reg3and4 = word(ph2_values[3],ph2_values[4]);
     //Serial.println(ph_reg3and4);
      ph2_reg3and4 = ph2_reg3and4/100;
      if (ph2_reg3and4 > -1 && ph2_reg3and4 < 15){
        Serial.print("PH : "+(String)ph2_reg3and4+"");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/PH2"), toCharArray(String(ph2_reg3and4)));
        Serial.println("Published");
        break;
      }
//    }else{
//      Serial.print("PH cant measure");
//    break;
//    }
//  
  }
}

void multi_sensor_temp2_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(temp2, sizeof(temp2)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(temp2_values, 7);
       //Serial.print("Multi TEMP Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(temp_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
   // if (temp_values[0] == 0x02 && temp_values[1] == 0x03 && temp_values[2] == 0x02){
      int temp2_reg3and4 = word(temp2_values[3],temp2_values[4]);
      temp2_reg3and4 = temp2_reg3and4/10;
     // if (temp_reg3and4 > -1 && temp_reg3and4 < 100){
        Serial.print("TEMP : "+(String)temp2_reg3and4+"°C");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/Temperature2"), toCharArray(String(temp2_reg3and4)));
        Serial.println("Published");
        break;
    //  }
//    }else{
//      Serial.print("TEMP cant measure");
//    break;
//    }
  }
}

void multi_sensor_res_detect(){

  while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(res, sizeof(res)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(res_values, 7);
     // Serial.print("Multi EC Values : ");
      for (byte i = 0; i < 7; i++)
      {
      //  Serial.print(ec_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
   // if (ec_values[0] == 0x02 && ec_values[1] == 0x03 && ec_values[2] == 0x02){
      float res_reg3and4 = word(res_values[3],res_values[4]);
     // if (ec_reg3and4 > -1 && ec_reg3and4 < 10000){
        Serial.print("EC : "+(String)res_reg3and4+" us/cm");
        Serial.println();
        delay(100);
        client.publish(toCharArray("/angizeh/security_system/25/1/Resistance"), toCharArray(String(res_reg3and4)));
        Serial.println("Published");
        break;
     // }
//    }else{
//      Serial.println("EC cant measure");
//    break;
//    }
  }
}


boolean reconnect() {

   while (WiFi.status() != WL_CONNECTED){
    ESP.wdtFeed();            
    ESP.wdtDisable();                     
    delay(500);
    Serial.print(".");
    delay(500);
    cnt--;
        if(cnt==0){
    ESP.wdtDisable();
    #ifdef SOFT_WATCHDOG
    ESP.wdtEnable(0);
    #endif  
    while(1);  
  }
  }
  if (client.connect(clientID, mqttuser, mqttpass, toCharArray(prefix + "Will"), 1, true, toCharArray("Disconnected"), true)) {
    client.setCallback(onMessageArrived);
  }
  return client.connected();
}



void uptime(){

  //** Making Note of an expected rollover *****//   
if(millis()>=3000000000){ 
HighMillis=1;

}
//** Making note of actual rollover **//
if(millis()<=100000&&HighMillis==1){
Rollover++;
HighMillis=0;
}

long secsUp = millis()/1000;

Second = secsUp%60;

Minute = (secsUp/60)%60;

Hour = (secsUp/(60*60))%24;

Day = (Rollover*50)+(secsUp/(60*60*24));  //First portion takes care of a rollover [around 50 days]
                       
}
void print_Uptime(){

  
  Serial.print(F("Uptime: ")); // The "F" Portion saves your SRam Space
  Serial.print(Day);
  Serial.print(F("  Days  "));
  Serial.print(Hour);
  Serial.print(F("  Hours  "));
  Serial.print(Minute);
  Serial.print(F("  Minutes  "));
  Serial.print(Second);
  Serial.println(F("  Seconds"));
}

void checkUpdate(){
  static unsigned long timer1 = millis();
  if (millis() - timer1 > 60000){
    timer1 = millis();
   repeatedCall(); 
  } 
}

void FirmwareUpdate()
{  

  WiFiClientSecure client;
   client.setInsecure();
  if (!client.connect(host, httpsPort)) {
    Serial.println("Connection failed");
    return;
  }
  client.print(String("GET ") + URL_fw_Version + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      //Serial.println("Headers received");
      break;
    }
  }
  String payload = client.readStringUntil('\n');

  payload.trim();
  if(payload.equals(FirmwareVer) )
  {   
     Serial.println("Device already on latest firmware version "+FirmwareVer); 
  }
  else
  {
    Serial.println("New firmware detected");
    ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); 
    t_httpUpdate_return ret = ESPhttpUpdate.update(client, URL_fw_Bin);
        
    switch (ret) {
      case HTTP_UPDATE_FAILED:
        Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
        break;

      case HTTP_UPDATE_NO_UPDATES:
        Serial.println("HTTP_UPDATE_NO_UPDATES");
        break;

      case HTTP_UPDATE_OK:
        Serial.println("HTTP_UPDATE_OK");
        break;
    } 
  }
 }  
// will store last time LED was updated
 void repeatedCall(){

  
//   static unsigned long Timer = millis();
//    if (millis() - Timer > 10000) 
//    {
//      // save the last time you blinked the LED
//      Timer = millis();
      FirmwareUpdate();
   // }
 }


 bool testWifi(void)
{

  int c = 0;
  Serial.println("Waiting for Wifi to connect");
  while ( c < 20 ) {
    if (WiFi.status() == WL_CONNECTED)
    {
      return true;
    }
    delay(500);
    Serial.print("*");
    c++;
  }
  Serial.println("");
  Serial.println("Connect timed out, opening AP");
  return false;
}

void launchWeb()
{

  Serial.println("");
  if (WiFi.status() == WL_CONNECTED)
    Serial.println("WiFi connected");
  Serial.print("Local IP: ");
  Serial.println(WiFi.localIP());
  Serial.print("SoftAP IP: ");
  Serial.println(WiFi.softAPIP());
  createWebServer();
  // Start the server
  server.begin();
  Serial.println("Server started");
}
 
void setupAP(void)
{

   WiFi.mode(WIFI_AP_STA);
   // Begin WiFi
  WiFi.begin(esid.c_str(), epass.c_str());
  delay(100);
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0)
    Serial.println("no networks found");
  else
  {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i)
    {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
      delay(10);
    }
  }
  Serial.println("");
  st = "<ol>";
  for (int i = 0; i < n; ++i)
  {
    // Print SSID and RSSI for each network found
    st += "<li>";
    st += WiFi.SSID(i);
    st += " (";
    st += WiFi.RSSI(i);
 
    st += ")";
    st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
    st += "</li>";
  }
  st += "</ol>";
  delay(100);
  WiFi.softAP(AP_SSID,AP_PASS);
  Serial.println("softap");
  launchWeb();
  Serial.println("over");
}
 
void createWebServer()
{

 {
    server.on("/", []() {
      String content;
      IPAddress ip = WiFi.softAPIP();
      String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
      content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
      content += "<form action=\"/scan\" method=\"POST\"><input type=\"submit\" value=\"scan\"></form>";
      content += ipStr;
      content += "<p>";
      content += st;
      content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
      content += "</html>";
      server.send(200, "text/html", content);
    });
    server.on("/scan", []() {
      String content;
      //setupAP();
      IPAddress ip = WiFi.softAPIP();
      String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
 
      content = "<!DOCTYPE HTML>\r\n<html>go back";
      server.send(200, "text/html", content);
    });
 
    server.on("/setting", []() {
      String content;
      String qsid = server.arg("ssid");
      String qpass = server.arg("pass");
      if (qsid.length() > 0 && qpass.length() > 0) {
        Serial.println("clearing eeprom");
        for (int i = 0; i < 96; ++i) {
          EEPROM.write(i, 0);
        }
        Serial.println(qsid);
        Serial.println("");
        Serial.println(qpass);
        Serial.println("");
 
        Serial.println("writing eeprom ssid:");
        for (int i = 0; i < qsid.length(); ++i)
        {
          EEPROM.write(i, qsid[i]);
          Serial.print("Wrote: ");
          Serial.println(qsid[i]);
        }
        Serial.println("writing eeprom pass:");
        for (int i = 0; i < qpass.length(); ++i)
        {
          EEPROM.write(32 + i, qpass[i]);
          Serial.print("Wrote: ");
          Serial.println(qpass[i]);
        }
        EEPROM.commit();
 
        content = "{\"Success\":\"saved to eeprom... reset to boot into new wifi\"}";
        statusCode = 200;
        ESP.reset();    // when you call this nothing will be sent back to the browser afterwards
      } else {
        content = "{\"Error\":\"404 not found\"}";
        statusCode = 404;
        Serial.println("Sending 404");
      }
      server.sendHeader("Access-Control-Allow-Origin", "*");
      server.send(statusCode, "application/json", content);
 
    });
  } 
}

void mqttconnect(){

    Serial.print("///////////////");
  client.setServer(mqttServerName, mqttport);
  client.setCallback(onMessageArrived);

   while (!client.connected()){
    

    delay(1000);
    Serial.println("Connecting To Server ...");
    if (client.connect(clientID, mqttuser, mqttpass, toCharArray(prefix + "/Will"), 1, true, toCharArray("Bye"), true))
    {
      Serial.println("Connected To Server");
      
    }  
   
    else 
    {
      cnt--;
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
       if(cnt==0){
      ESP.wdtDisable();
      #ifdef SOFT_WATCHDOG
      ESP.wdtEnable(0);
      #endif  
      while(1);
  }
  delay(1000);
       
   }
}
}


void checkWifi (){

   while (WiFi.status() != WL_CONNECTED){
    ESP.wdtFeed();            
    ESP.wdtDisable();                     
    delay(500);
    Serial.print(".");
    delay(500);
    cnt--;
    if(cnt==0){
    ESP.wdtDisable();
    #ifdef SOFT_WATCHDOG
    ESP.wdtEnable(0);
    #endif  
    while(1);
  }
}
}

char* toCharArray(String str) {

  return &str[0];
}

void onMessageArrived(char* t, byte* m, unsigned int length) {

  
  String message = "";
  String topic = "";
  topic = String(t);
  message = String((char*)m);
  message = message.substring(0, length);

  if (message == "HELLO") {
    Serial.println("Hello client");
    client.publish(toCharArray(prefix + "/server"), toCharArray("HELLO"));
    Serial.println("Hello server");
  }
}

This possibly means your code could be better designed.

The ideal is to have everything running in loop... if you write the code correctly this is possible and still be able to do multiple things at once. Can you post the code that you would like to light the LED in we may be able to suggest some options.

Need to study the BlinkWithoutDelay example in the IDE.
So you can replace delay() with millis() timing.
Leo..

Thanks guys for your advice, I was able to do it by optimizing the code and reducing the delay in the code.

So did you use millis() for timing.
delay() is beginners stuff that needs to (almost) disappear in advanced coding.
delay() also does strange things (sleep) with an ESP8266.
Leo..

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