MakerMGCR555
Published © GPL3+

Flood alarm

A system for monitoring the environment and to give the alarm if there is danger of flooding before the catastrophic event.

IntermediateFull instructions provided3 hours137
Flood alarm

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
Seeed Studio Grove Soil Moisture Sensor
×1
Grove - Relay
Seeed Studio Grove - Relay
×1

Software apps and online services

Seeed Studio SenseCAP
Arduino IDE
Arduino IDE
mosquitto
MQTT
MQTT

Story

Read more

Schematics

Model

That's the way we will use this system. The Wio Terminal will receive signals from the moisture sensor periodically, and, if water is present (flooding), the board will start the pump using a relay.
The user will know the flooding situation in real time.

Code

Flood Monitoring

Arduino
The system monitor for water presence on the floor of my house. If there is water, using a relay, the system starts a pump to send water out.
/*
Use Wio Terminal and Moisture sensor to monitor flood
Use MQTT
Use topic "flood" 
*/


#include <rpcWiFi.h>
#include"TFT_eSPI.h"
#include <PubSubClient.h>

// Update these with values suitable for your network.
const char* ssid = "*********"; // WiFi Name
const char* password = "*********";  // WiFi Password
const char* mqtt_server = "192.168.1.***";  // MQTT Broker URL   broker.mqtt-dashboard.com

TFT_eSPI tft;
WiFiClient wioClient;
PubSubClient client(wioClient);
long lastMsg = 0;
char msg[50];
int soil_moisture;

void setup_wifi() {
  delay(10);
  tft.setTextSize(2);
  tft.setCursor((320 - tft.textWidth("Connecting to Wi-Fi..")) / 2, 120);
  tft.print("Connecting to Wi-Fi..");

  tft.print("Connecting to ");
  tft.println(ssid);
  WiFi.begin(ssid, password); // Connecting WiFi

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    tft.print(".");
  }

  tft.fillScreen(TFT_BLACK);
  tft.setCursor((320 - tft.textWidth("Connected!")) / 2, 120);
  tft.print("Connected!");

  tft.println("IP address: ");
  tft.println(WiFi.localIP()); // Display Local IP Address
}

void callback(char* topic, byte* payload, unsigned int length) {
  char buff_p[length];
  for (int i = 0; i < length; i++) {
    buff_p[i] = (char)payload[i];
  }
  buff_p[length] = '\0';
  String msg_p = String(buff_p);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor((320 - tft.textWidth("MQTT Message")) / 2, 90);
  tft.print("MQTT Message: " );
  tft.setCursor((320 - tft.textWidth(msg_p)) / 2, 120);
  tft.print(msg_p); // Print received payload
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    tft.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "WioTerminal";
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      tft.println("connected");
      // Once connected, publish an announcement...
      client.publish("flood", "hello world");
      // ... and resubscribe
      client.subscribe("flood");
      client.subscribe("pump");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // display initialization
  tft.begin();
  tft.fillScreen(TFT_BLACK);
  tft.setRotation(3);
  
  //  IO definition  
  pinMode(A0, INPUT);   // moisture sensor
  pinMode(PIN_WIRE_SCL, OUTPUT);  // relay

  Serial.println();
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883); // Connect the MQTT Server
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
    soil_moisture = analogRead(A0);
    if (soil_moisture > 200)
    {
        // Soil Moisture is too high, turning relay on.
        digitalWrite(PIN_WIRE_SCL, HIGH);
        client.publish("pump", "ON");
        delay(2000);
    }
    else
    {
        //Soil Moisture is ok, turning relay off
        digitalWrite(PIN_WIRE_SCL, LOW);
        client.publish("pump", "OFF");
        delay(2000);
    }
    client.publish("flood", String(soil_moisture).c_str());
  }
}

Credits

MakerMG

MakerMG

3 projects • 0 followers
CR555

CR555

2 projects • 6 followers

Comments