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
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
}
}