Hi, so I tried this code to run in my esp32, but the verify always tell that i couldn’t upload it because the compilation were error.
// Virtual Pin function in my console
V1 as humidity
V2 as temperature
V3 as relay
V4 as Slider Water Amount
V5 as to start the Watering Process
Is there any methods on how make this code more readable and simple ?
Thank’s in advance.
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// Replace with your Blynk Auth Token
char auth[] = "Your_Auth_Token_Here";
// Replace with your WiFi credentials
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
// Pin definitions
#define DHT_PIN 4
#define RELAY_PIN 12
// Define the type of DHT sensor (DHT11 or DHT22)
#define DHT_TYPE DHT22
// Create an instance of the DHT sensor library
DHT dht(DHT_PIN, DHT_TYPE);
// Variables to store the humidity and temperature readings
float humidity, temperature;
// Watering amount (adjustable with the slider)
int wateringAmount = 0;
// Blynk virtual pin for the humidity reading
#define VIRTUAL_PIN_HUMIDITY 1
// Blynk virtual pin for the temperature reading
#define VIRTUAL_PIN_TEMPERATURE 2
//Blynk virtual pin for the relay
#define VIRTUAL_PIN_RELAY 3
// Blynk virtual pin for the slider
#define VIRTUAL_PIN_SLIDER 4
// Blynk virtual pin for the button
#define VIRTUAL_PIN_BUTTON 5
void setup() {
// Start the serial communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Connect to Blynk
Blynk.begin(auth, WiFi.localIP());
Serial.println("Connected to Blynk");
// Initialize the DHT sensor
dht.begin();
// Set the relay pin as an output
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
// Read the humidity and temperature from the DHT sensor
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// Check if the read was successful
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor");
return;
}
// Send the humidity and temperature readings to Blynk
Blynk.virtualWrite(VIRTUAL_PIN_HUMIDITY, humidity);
Blynk.virtualWrite(VIRTUAL_PIN_TEMPERATURE, temperature);
// Read the value of the slider from Blynk
wateringAmount = Blynk.getValue(VIRTUAL_PIN_SLIDER).asInt();
Serial.print("Watering amount: ");
Serial.println(wateringAmount);
// Read the value of the button from Blynk
if (Blynk.getValue(V
.virtualRead(VIRTUAL_PIN_BUTTON)) {
// If the button is pressed, turn on the relay for the specified watering amount
Serial.println("Starting watering process");
digitalWrite(RELAY_PIN, HIGH);
delay(wateringAmount * 1000);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Finished watering process");
}
Blynk.run();
}