I’ve build my circuit and written my arduino code which is:
I want to use the esp32 for serial communication and to integrate it with BLYNK sot that I’m able to see the values of temp, humidity and water level on the app. Also, I want to have a button on the app where I’m able to control the relay.
I’m unsure of what the code could be for the esp32 or any modification with my arduino code I should make.
That was the code I tried to attempt but it’s not working.
Moderator edit: Added code tags.
C:
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTTYPE DHT11
// Define pins
const int relayPin = A2;
const int trigPin = 3;
const int echoPin = 4;
const int buzzerPin = 8;
const int dhtPin = 2;
const float threshold = 15.0;
DHT dht(dhtPin, DHT11);
LiquidCrystal lcd(9, 10, 7, 11, 12, 13); // Initializing the LCD
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(relayPin, LOW);
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
float distance = readUltrasonicDistance();
float humidity = dht.readHumidity();
float temp = dht.readTemperature();
int waterLevel = map(distance, 0, 30, 100, 0); // Assuming 0cm = 100% and 30cm = 0%
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp);
lcd.print((char)223); // Degree symbol
lcd.print("C H:");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Water Lvl:");
lcd.print(waterLevel);
lcd.print("%");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Water Level: ");
Serial.print(waterLevel);
Serial.println(" %");
if (distance > threshold) {
digitalWrite(relayPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(1000); // Sound buzzer for 1 second
digitalWrite(buzzerPin, LOW); // Turn off buzzer
} else {
digitalWrite(relayPin, LOW);
digitalWrite(buzzerPin, LOW);
}
delay(2000); // Wait for 2 seconds before the next loop
}
float readUltrasonicDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.0343) / 2;
return distance;
}
I’m unsure of what the code could be for the esp32 or any modification with my arduino code I should make.
C:
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkSSID";
char pass[] = "YourPassword";
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
}
BLYNK_WRITE(V1) { //Button on V1
int pinValue = param.asInt();
if (pinValue == 1) {
Serial.println("RELAY_ON");
} else {
Serial.println("RELAY_OFF");
}
}
void requestDataFromArduino() {
Serial.println("GET_DATA");
delay(100); // give some time for Arduino to respond
if (Serial.available()) {
String data = Serial.readStringUntil('\n');
float temperature, humidity, distance;
sscanf(data.c_str(), "TEMP:%f,HUM:%f,DIST:%f", &temperature, &humidity, &distance);
Blynk.virtualWrite(V2, temperature); // Send temperature to V2
Blynk.virtualWrite(V3, humidity); // Send humidity to V3
Blynk.virtualWrite(V4, distance); // Send distance (water level) to V4
}
}
BLYNK_READ(V2) { // When the Blynk app requests data for V2
requestDataFromArduino();
}
BLYNK_READ(V3) { // For V3
requestDataFromArduino();
}
BLYNK_READ(V4) { // For V4
requestDataFromArduino();
}
Moderator edit: Added code tags.