This shows you the differences between two versions of the page.
iothings:proiecte:2021:flood-detection [2022/01/27 23:25] alexandru.ionascu |
iothings:proiecte:2021:flood-detection [2022/01/28 08:41] (current) alexandru.ionascu |
||
---|---|---|---|
Line 1: | Line 1: | ||
==== Flood Detecton System ==== | ==== Flood Detecton System ==== | ||
<sub>Alexandru Ionascu - AAC 2021</sub> \\ | <sub>Alexandru Ionascu - AAC 2021</sub> \\ | ||
+ | <sub>[[https://youtu.be/t9QbCft8HMM | [Project Video]]]</sub> | ||
=== 1. Project Objective === | === 1. Project Objective === | ||
Line 7: | Line 8: | ||
=== 2. Project Description === | === 2. Project Description === | ||
- | #TODO | + | The system consists in two main components: **ESP32 board** and **HC-SR04 ultrasonic sensor**. \\ |
+ | ***ESP32** is the main component and has the following roles: | ||
+ | * to establish Wi-Fi connection that will allow to perform HTTP requests; | ||
+ | * to send POST requests to the Pushover API to trigger push notifications; | ||
+ | * to determine the distance to the floor based on received input from the ultrasonic sensor; \\ | ||
+ | ***HC-SR04** serves as the proximity sensor and has the following roles: \\ | ||
+ | * to provide informations about the distance to the floor; | ||
=== 3. Hardware Description === | === 3. Hardware Description === | ||
Line 13: | Line 20: | ||
* ** ESP32 Board ** | * ** ESP32 Board ** | ||
* The ESP32 Board controls the entire system. It establishes the Wi-Fi connection, it measures the water level based on the input received form the ultrasonic sensor and performs HTTP POST request if floor level is closer than 2cm. | * The ESP32 Board controls the entire system. It establishes the Wi-Fi connection, it measures the water level based on the input received form the ultrasonic sensor and performs HTTP POST request if floor level is closer than 2cm. | ||
- | {{ :iothings:proiecte:2021:esp32-board.jpg? 200 | ESP32 Wroom Board}} | + | {{ :iothings:proiecte:2021:esp32-board.jpg ? 300 | ESP32 Wroom Board}} |
* ** HC-SR04 Ultrasonic Sensor ** | * ** HC-SR04 Ultrasonic Sensor ** | ||
* The module is used to measure the distance from the sensor to the floor which helps the ESP32 Board to decide if the floor level has raised depending on the water lever. | * The module is used to measure the distance from the sensor to the floor which helps the ESP32 Board to decide if the floor level has raised depending on the water lever. | ||
- | * The concept behind the ultrasonic sensor is the following: when triggered, the device sends a ultrasonic wave forward; this wave is reflected on the nearest surface and is expected back to the ultrasonic sensor; the device rises a signal on the whole period of time between the sending of the wave and the receiving of the reflected wave; this signal represents the duration, therefore, to find the distance between the sensor and the nearest surface the formula d = v*t/2 is used (the 2 factor is a result of measuring the time for both the emitted and reflected waves). | + | * The concept behind the ultrasonic sensor is the following: by sending out a sound wave at a frequency above the range of human hearing, the transducer of the sensor acts as a microphone to receive and send the ultrasonic sound. The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the ultrasonic pulse. It sends an ultrasonic pulse out at 40kHz which travels through the air and if there is an obstacle or object, it will bounce back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated. |
- | * The ultrasonic sensor has the role to measure the level of water within the glass. This level is measured at every iteration of the program loop. | + | |
* The trigger signal is linked to the digital GPIO 5, the echo signal is linked to the GPIO 18, the VCC signal is linked to 5V and the ground signal is linked to GND. | * The trigger signal is linked to the digital GPIO 5, the echo signal is linked to the GPIO 18, the VCC signal is linked to 5V and the ground signal is linked to GND. | ||
- | {{ :iothings:proiecte:2021:hc-sr04.jpg? 200 | HC-SR04 Ultrasonic Distance Sensor}} | + | {{ :iothings:proiecte:2021:hc-sr04.jpg ? 300 | HC-SR04 Ultrasonic Distance Sensor}} |
+ | \\ | ||
+ | The diagram associated with the hardware will look like this: \\ | ||
+ | |||
+ | {{ :iothings:proiecte:2021:flood-diagram.png ? 500 | Hardware Diagram}} | ||
=== 4. Software Description === | === 4. Software Description === | ||
- | #TODO | + | |
+ | The code consists of two main functions: **setup** and **loop**. In the setup function, we will set the echo and trigger pins and we will connect to the local Wi-Fi network to perform further HTTP requests. Once the connection is established, in the loop function, we will measure the distance to the floor in centimeters by sending ulstrasonic impulses and if this is less than 2 centimeters, we will perform a POST request to the Pushover API. There will also be a 2s delay between requests to make avoid unnecessary push notifications. | ||
+ | |||
+ | The source code is listed below: | ||
+ | |||
+ | <file cpp main.cc> | ||
+ | #include <WiFi.h> | ||
+ | #include <HTTPClient.h> | ||
+ | |||
+ | #define SOUND_SPEED 0.034 | ||
+ | #define CM_TO_INCH 0.393701 | ||
+ | #define DISTANCE 2.0 | ||
+ | |||
+ | #define PUSHOVER_URL "https://api.pushover.net/1/messages.json" | ||
+ | |||
+ | const int trigPin = 5; | ||
+ | const int echoPin = 18; | ||
+ | |||
+ | const char* ssid = "********"; | ||
+ | const char* password = "********"; | ||
+ | |||
+ | long duration; | ||
+ | float distanceCm; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | pinMode(trigPin, OUTPUT); | ||
+ | pinMode(echoPin, INPUT); | ||
+ | |||
+ | WiFi.begin(ssid, password); | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(1000); | ||
+ | Serial.println("Connecting to WiFi..."); | ||
+ | } | ||
+ | |||
+ | Serial.println("Connected to the WiFi network"); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if ((WiFi.status() == WL_CONNECTED)) { | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | delayMicroseconds(2); | ||
+ | |||
+ | digitalWrite(trigPin, HIGH); | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | |||
+ | // Reads the echoPin, returns the sound wave travel time in microseconds | ||
+ | duration = pulseIn(echoPin, HIGH); | ||
+ | |||
+ | // Calculate the distance | ||
+ | distanceCm = (duration * SOUND_SPEED) / 2; | ||
+ | |||
+ | if (distanceCm < DISTANCE) { | ||
+ | HTTPClient http; | ||
+ | |||
+ | http.begin(PUSHOVER_URL); | ||
+ | http.addHeader("Content-Type", "application/x-www-form-urlencoded"); | ||
+ | |||
+ | String form = "device=device&user=********&title=IoT - ESP32&message=Flood!!&token=********"; | ||
+ | |||
+ | int httpCode = http.POST(form); | ||
+ | |||
+ | if (httpCode > 0) { | ||
+ | String payload = http.getString(); | ||
+ | |||
+ | Serial.println(httpCode); | ||
+ | Serial.println(payload); | ||
+ | } | ||
+ | |||
+ | http.end(); | ||
+ | |||
+ | delay(2000); | ||
+ | } else { | ||
+ | Serial.println("Error on HTTP request"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </file> | ||
=== 5. Issues and Solutions === | === 5. Issues and Solutions === | ||
- | #TODO | + | This project had some initial issues with getting the source code up on the board, but for some reasons it got fixed once we switched from MacOS to Linux. |
+ | Another problem would be testing this application in a real life case scenario because it won't be that easy to simulate flood home. | ||
- | === 6.Conclusions === | + | === 6. Resources and Documentation === |
- | #TODO | + | |
+ | [[https://techtutorialsx.com/2017/04/24/esp32-connecting-to-a-wifi-network/ | ESP32: Connecting to a WiFi network ]] \\ | ||
+ | [[https://techtutorialsx.com/2017/05/20/esp32-http-post-requests | ESP32: HTTP POST Requests ]] \\ | ||
+ | [[https://randomnerdtutorials.com/esp32-hc-sr04-ultrasonic-arduino | ESP32 with HC-SR04 Ultrasonic Sensor with Arduino IDE ]] \\ | ||