If wifi not found code stops

I created a fan program to cool something and it works fine. But it turns out that if the WiFi network is not found, the program stops and the fan does not work. this is of course not good!

how can i avoid this ?
I’m a beginner so no very difficult answers :joy:
using esp8266 and arduino compiler

#define BLYNK_TEMPLATE_ID "TMPL4E4kcsNvu"
#define BLYNK_TEMPLATE_NAME "Fancontroller"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); 


 
#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 12

int x=0;
int y=0;
 
int fanPin = 16;
int dutyCycle = 0;
 
float temp = 0;
int threshold = 22;   //   start temp van de ventilator ********************************************************
 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
WidgetLED FAN(V0);
 
char auth[] = "K-h6TBagUtBxvNhHiY845I5zrrPpzt-j";

// char ssid[] = "";
// char pass[] = "";
char ssid[] = "";
char pass[] = "";
 
void setup()
{
 
  Serial.begin(115200);
  sensors.begin();
  pinMode(fanPin, OUTPUT);
 
  pinMode(LED_BUILTIN, OUTPUT); // led knipper
 
  analogWriteRange(100);
  analogWriteFreq(10000);
 
 Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);  //   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
 
 
BLYNK_WRITE(V7)
{
  threshold = param.asInt();
  Serial.print(" The Threshhold thresholdue is: ");
  Serial.println(threshold);
  Serial.println();
}
 
 
void controlFanSpeed(int fanSpeedPercent)
{
  analogWrite(fanPin, fanSpeedPercent);
 
  Serial.print("Fan Speed: ");
  Serial.print(fanSpeedPercent);
  Serial.println("%");
 
   
}
 
 
void loop()
 
{

 delay(1000);

 digitalWrite(LED_BUILTIN, HIGH);  // stuur led 8266 controlle aan

  Blynk.run();
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
 
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println("*C");
 
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print(" *C");

  lcd.setCursor(0, 3);
  lcd.print(x);


 
  Blynk.virtualWrite(V3, temp);
 
  if (temp >= threshold)
  {
    FAN.on();
    x++;

       if (x <=1) {
      controlFanSpeed(50);  // tijdelijk even naar 50%
      delay(2000);  
      }


    int fanSpeedPercent = map(temp, threshold, 30, 10, 100); // bij threshold  10% speed     bij 30 graden 100% speed  ***********************
    controlFanSpeed(fanSpeedPercent);
    Blynk.virtualWrite(V4, fanSpeedPercent);
  }
  else if (temp < threshold)
  {
    FAN.off();  
    lcd.setCursor(1, 3);
    lcd.print("    ");
    x=0;
    int fanSpeedPercent = 0;
    controlFanSpeed(fanSpeedPercent);
    Blynk.virtualWrite(V4, fanSpeedPercent);
    digitalWrite(LED_BUILTIN, LOW);  // stuur led 8266 controlle aan
    
  }
}

@frans Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Hi Pete… I changed it :blush:

Okay, a few things…

  1. Blynk.begin() is a blocking command. If it can’t connect to WiFi or the Blynk server then all code execution will halt at that point. If you want to have a non-blocking system then you’ll need to manually manage your WiFi connection then use Blynk.config() - possibly with an optional timeout - and Blynk.connect(). You’ll also need to avoid executing Blynk.run() if your device isn’t connected to Blynk, so you’ll need to place Blynk.run() in an if statement that checks for Blynk.connected().
    You’ll also need to have a timed function which occasionally checks if WiFi and/or Blynk are connected and attempts to re-connect if not. Without this then the device will never re-connect from “offline mode” without being rebooted.
    You’ll find some examples of how to do all of this if you search the forum.
  1. Your void loop() is a mess! You can’t have a cluttered loop or use delays with Blynk. You need to move most of this code into a separate function, and use a BlynkTimer to call that function on a regular basis.
    More info here…
    https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

  2. You need to get the value of V7 at startup, so you should be using a BLYNK_CONNNECTED() function with a Blynk.syncVirtual(V7) command in it.

Pete.

hi pete

Yes I know, I still have to check void loop, but first let’s see if I can get it to work the way I want

Blynk.begin() is a blocking command. If it can’t connect to WiFi or the Blynk server then all code execution will halt at that point.

I get it…but my knowledge is limited: is there any other cloud service that doesn’t work this way?

I’m not sure that you do get it.
You can use Blynk in a non-blocking way by following the steps I outlined above.
As far as looking for information about alternatives to Blynk, I don’t think the Blynk forum is the best place to ask for that info.

Pete.

Hello Peter.

I see more people have problems with this topic. It would be nice if there was an example somewhere of how to do it instead of just referring to it Blynk.config()

As I said before…

I guess searching the forum for the keywords I provided was too much effort?
If so then here’s a working example, but there are many more as well, if you make the effort to look…

Pete.