This is an old revision of the document!
Alexandru Ionascu - AAC 2021
This project aims to create a system that notifies owner on his smartphone about a possible flood via push notifications. This will be achieved by measuring the distance from the floor to the ultrasonic sound sensor and if it's less than 2 centimeters, it will trigger a push notification by sending a POST request to the Pushover API.
#TODO
The hardware has two main components:
The diagram associated with the hardware will look like this:
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.
The source code is listed below:
#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=ukszyvbax7eob3goo9h159xs75ixvw&title=IoT - ESP32&message=Flood!!&token=atq3xjuehksriumq9s23hcyw5b3f7g"; int httpCode = http.POST(form); if (httpCode > 0) { String payload = http.getString(); Serial.println(httpCode); Serial.println(payload); } http.end(); delay(10000); } else { Serial.println("Error on HTTP request"); } } }
#TODO
#TODO