IoT flood detection and avoidance system

Thread Starter

sh2000

Joined Jul 25, 2023
9
I’ve build my circuit and written my arduino code which is:
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 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.

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();
}
That was the code I tried to attempt but it’s not working.

Moderator edit: Added code tags.
 

MrChips

Joined Oct 2, 2009
34,700
It is just too easy to say "it's not working".

I think no one is going to go through your code and say "Hey, you made a mistake on line ###. Here is what you should have coded."

You need to do some trouble shooting yourself and then come back and say "It does --- when it does --- but fails to do ---- when ---".

Also, no one writes a huge program and expects it to work first time.
You write a small part and test it. Then you add one function at a time and test it.

The systematic way to create a program is to write code that runs without error on day 1!
I am not an Arduino user but I think this is a working program:
C:
void setup() {
}

void loop () {
}
 
Top