GreenHouse project

Hello, I am seeking assistance for my smart greenhouse project. I am looking for a way to ensure that the automations continue to function even when the ESP is not connected to Blynk. I have tried various methods such as timers and millis, but have been unsuccessful. I kindly request your help and would greatly appreciate a prompt response as I have a presentation tomorrow XD. Here is my current code:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";  

BlynkTimer timer;
bool Relay1 = 0;
bool Relay2 = 0;
bool Relay3 = 0;


#define moister A0
#define waterPump D6
#define ac D4
#define light D5
#define DHTPIN D2    
#define DHTTYPE DHT11    
DHT dht(DHTPIN, DHTTYPE);

void soilMoistureSensor() {
  int value = analogRead(moister);
  value = map(value, 0, 1023, 0, 100);
  value = (value - 100) * -1;

  Blynk.virtualWrite(V0, value);
}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Falha na leitura do sensor DHT!");
    return;
  }
  Blynk.virtualWrite(V2, h);  //V2 é para Humidade
  Blynk.virtualWrite(V3, t);  //V3 é para Temperatura
}

void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
  pinMode(ac, OUTPUT);
  pinMode(light, OUTPUT);
  digitalWrite(waterPump, HIGH);
  digitalWrite(ac, HIGH);
  digitalWrite(light, HIGH);
  

  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  timer.setInterval(1000L, soilMoistureSensor);
  timer.setInterval(3000L, sendSensor);
}

BLYNK_WRITE(V1) {
  Relay1 = param.asInt();

  if (Relay1 == 1) {
    digitalWrite(waterPump, LOW);
  } else {
    digitalWrite(waterPump, HIGH);
  }
}

BLYNK_WRITE(V4) {
  Relay2 = param.asInt();

  if (Relay2 == 1) {
    digitalWrite(ac, LOW);
  } else {
    digitalWrite(ac, HIGH);
  }
}

BLYNK_WRITE(V5) {
  Relay3 = param.asInt();

  if (Relay3 == 1) {
    digitalWrite(light, LOW);
  } else {
    digitalWrite(light, HIGH);
  }
}


  void loop() {
  if (WiFi.status() != WL_CONNECTED) { // verifica se não há conexão com a internet
    Serial.println("Sem conexão com a internet.");
    Blynk.disconnect(); // desconecta do servidor Blynk
    WiFi.disconnect(); // desconecta da rede WiFi
    delay(1000); // aguarda um segundo
    WiFi.begin(ssid, pass); // tenta se conectar novamente à rede WiFi
    while (WiFi.status() != WL_CONNECTED) { // aguarda até que haja conexão com a internet
      Serial.print(".");
      delay(1000);
    }
    Blynk.connect(); // reconecta ao servidor Blynk
  }

  Blynk.run();//Executa a biblioteca Blynk
  timer.run();//Executa o timer Blynk
  delay(2000);

  if (!WiFi.isConnected()) {
    // Código para controlar o ar condicionado e a luz caso não haja conexão com a internet

    float t = dht.readTemperature();
    
    if (t >= 27) {
      digitalWrite(ac, LOW);
    } else if (t <= 23){
      digitalWrite(ac, HIGH);
    }
    
    if (t <= 12) {
      digitalWrite(light, LOW);
    } else if (t >= 20) {
      digitalWrite(light, HIGH);
    }
    
    // Código para controlar a bomba de água caso não haja conexão com a internet

    int value = analogRead(moister);
    value = map(value, 0, 1023, 0, 100);
    value = (value - 100) * -1;

    if (value < 10) {
      digitalWrite(waterPump, LOW);
    } else if (value > 50) {
      digitalWrite(waterPump, HIGH);
    }
  }
}

@Rodrigo_patir 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.

Done

Okay. Offline Automations are only supported in the business subscription I believe.

But, you have quite a few issues with your code that would prevent it from working correctly offline.
I’d suggest that you stop using Blynk.begin and use Blynk.config and Blynk.connect instead.
You should also move away from Automations and do the processing in your code instead. I’m guessing you’d also need to use the time input widget as well, and possibly an external hardware RTC if you wanted 100% reliability in all offline situations.

Pete.

Thank you very much, Pete. I will see if I can do as you have instructed. :smile: