Greenhouse

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <WebServer.h>

#include <SPI.h>

#include <WiFiManager.h>

#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5

uint8_t led1 = 13;
uint8_t led2 = 12;
uint8_t led3 = 14;
uint8_t led4 = 27;
uint8_t led5 = 26;
uint8_t led6 = 25;
uint8_t led7 = 33;

int PWM_LED1;
int PWM_LED2;
int PWM_LED3;
int PWM_LED4;
int PWM_LED5;
int PWM_LED6;
int PWM_LED7;



#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS); // hardware SPI
unsigned long delayTime;
float temperature, humidity, pressure, altitude;

char auth[] = "**";    
char ssid[] = ""; //WİFİ adınızı yazınız
char pass[] = ""; //şifrenizi yazınızp
BlynkTimer timer; 
void sendSensor()
{
  float h = bme.readHumidity();
  float t = bme.readTemperature(); 

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  Blynk.virtualWrite(V7, h);
  Blynk.virtualWrite(V8, t);
}

void setup() {
  WiFi.mode(WIFI_AP_STA);
  WiFi.beginSmartConfig();

  WiFi.mode(WIFI_STA);
  WiFiManager wm;

  bool res;
    
   res = wm.autoConnect("AutoConnectAP"); // anonymous ap

   if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
    } 
    else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
    }

  timer.setInterval(5*60*1000, connectionstatus);
    
  
  ledcAttachPin(led1, 1);
  ledcAttachPin(led2, 2);
  ledcAttachPin(led3, 3);
  ledcAttachPin(led4, 4);
  ledcAttachPin(led5, 5);
  ledcAttachPin(led6, 6);
  ledcAttachPin(led7, 7);

  ledcSetup(1, 5000, 5);
  ledcSetup(2, 5000, 5);
  ledcSetup(3, 5000, 5);
  ledcSetup(4, 5000, 5);
  ledcSetup(5, 5000, 5);
  ledcSetup(6, 5000, 5);
  ledcSetup(7, 9333, 9.3);



  Serial.begin(115200);
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Blynk.begin("**", ssid, pass);

  bool status;
  status = bme.begin(0x76);

  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  timer.setInterval(1000L, sendSensor);
  
}
BLYNK_WRITE(V0) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED1 = param.asInt();
  ledcWrite(1, PWM_LED1); 
}
BLYNK_WRITE(V1) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED2 = param.asInt();
  ledcWrite(2, PWM_LED2); 

}

BLYNK_WRITE(V2) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED3 = param.asInt();
  ledcWrite(3, PWM_LED3); 

}
BLYNK_WRITE(V3) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED4 = param.asInt();
  ledcWrite(4, PWM_LED4); 

}
BLYNK_WRITE(V4) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED5 = param.asInt();
  ledcWrite(5, PWM_LED5); 

}
BLYNK_WRITE(V5) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED6 = param.asInt();
  ledcWrite(6, PWM_LED6); 

}
BLYNK_WRITE(V6) //slider in Virtual Pin 0 (0...4095)
{
  PWM_LED7 = param.asInt();
  ledcWrite(7, PWM_LED7); 
}


void connectionstatus() {
  
  if ((WiFi.status() != WL_CONNECTED) ) {
    Serial.println("Reconnecting to WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.print(".");
    }
    Serial.println();
    Serial.println(WiFi.localIP());
    //Alternatively, you can restart your board
    //ESP.restart();
  } else {
    Serial.println("wifi OK");
  }

  if (!Blynk.connected() ) {
    Serial.println("Lost Blynk server connection");
    Blynk.connect();
  } else {
    Serial.println("Blynk OK");
  }
}



void loop() {

  //Blynk.virtualWrite(V1, String(humidity, 1));
  Blynk.run();
  timer.run();
}```

And?

Pete.

I’m sorry for my bad english .
My green house project is below.
I created it with the codes I found.
I wish I wanted to write with my own knowledge.
I can control leds, fans, dehumidifier, temperature, humidity, and pump.

I’m looking for your help to make the code better.

This code isn’t bad
What do you expect from us?

Write in your own language if you wish, we’ll use Google translate or Deepl.

The important thing is that you provide clear information about what you need help with.

Pete.

Sometimes internet drops are a problem.

I got your help.

and

Is there anything you think I should fix?

daha iyi bir hale getirmek istiyorum .

Is your device getting a good WiFi signal?

Pete.

WiFi da sorun yok ;

WiFiManager wm;
kodu doğru yerinde mi ?

I’m not sure about the WM code.
WiFi Manager is redundant if you use Edgent, but do you really need to keep changing the WiFi credentials?

Pete.

Salam,
Nə çətinliyin var?
Mən köməklik edə bilərəm.
WM i başqa kodlarla tənzimləməlisən.
Timer lə məsəlçün, müəyyən saniyədən bir checkconnection funksiyası yaratmaqla.