/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
#include "DHT.h"
const int walltrigPin = 5;
const int wallechoPin = 18;
const int stairtrigPin = 19;
const int stairechoPin = 21;
const int stairlengthtrigPin = 15;
const int stairlengthechoPin = 2;
const int pitholelengthtrigPin = 12;
const int pitholelengthechoPin = 14;
#define navButtonPin 22 // ESP32 pin GIOP16, which connected to button
#define sosButtonPin 23 // ESP32 pin GIOP16, which connected to button
#define DHTPIN 27 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
//water level
#define POWER_PIN 26 // ESP32 pin GIOP17 connected to sensor's VCC pin
#define SIGNAL_PIN 25 // ESP32 pin GIOP36 (ADC0) connected to sensor's signal pin
int value = 0; // v
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22
#define BUZZER_PIN 13 // ESP32 GIOP17 pin connected to Buzzer's pin
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float walldistanceCm;
float walldistanceInch;
float stairdistanceCm;
float stairdistanceInch;
float stairlengthdistanceCm;
float stairlengthdistanceInch;
float pitholelengthdistanceCm;
float pitholelengthdistanceInch;
int navbutton_state; // the current state of button
int sosbutton_state; // the current state of button
int nav_last_button_state; // the previous state of button
int sos_last_button_state; // the previous state of button
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(walltrigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(wallechoPin, INPUT); // Sets the echoPin as an Input
pinMode(stairtrigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(stairechoPin, INPUT); // Sets the echoPin as an Input
pinMode(stairlengthtrigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(stairlengthechoPin, INPUT); // Sets the echoPin as an Input
pinMode(pitholelengthtrigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(pitholelengthechoPin, INPUT); // Sets the echoPin as an Input
pinMode(navButtonPin, INPUT); // set ESP32 pin to input pull-up mode
pinMode(sosButtonPin, INPUT); // set ESP32 pin to input pull-up mode
dht.begin();
navbutton_state = digitalRead(navButtonPin);
sosbutton_state = digitalRead(sosButtonPin);
// pinMode(POWER_PIN, OUTPUT); // configure pin as an OUTPUT
//digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
pinMode(BUZZER_PIN, OUTPUT); // set ESP32 pin to output mode
}
void loop() {
nav_last_button_state = navbutton_state; // save the last state
navbutton_state = digitalRead(navButtonPin); // read new state
if (nav_last_button_state == HIGH && navbutton_state == LOW) {
Serial.println("Nav button is pressed");
// toggle state of LED
}
sos_last_button_state = sosbutton_state; // save the last state
sosbutton_state = digitalRead(sosButtonPin); // read new state
if (sos_last_button_state == HIGH && sosbutton_state == LOW) {
Serial.println("SOS button is pressed");
// toggle state of LED
}
//wall
// Clears the walltrigPin
digitalWrite(walltrigPin, LOW);
delayMicroseconds(2);
// Sets the walltrigPin on HIGH state for 10 micro seconds
digitalWrite(walltrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(walltrigPin, LOW);
// Reads the wallechoPin, returns the sound wave travel time in microseconds
duration = pulseIn(wallechoPin, HIGH);
// Calculate the distance
walldistanceCm = duration * SOUND_SPEED/2;
// Convert to inches
walldistanceInch = walldistanceCm * CM_TO_INCH;
if (walldistanceCm >= 160) {
// Serial.println("The Buzzer is being");
// Serial.print("Wall Distance (cm): ");
// Serial.println(walldistanceCm);
// Serial.print(" Wall Distance (inch): ");
// Serial.println(walldistanceInch);
tone(BUZZER_PIN, 250, 25);
}
else
if (walldistanceCm <= 160) {
// Serial.println("The Buzzer is unpressed");
noTone(BUZZER_PIN);
}
// Prints the distance in the Serial Monitor
//stairs
digitalWrite(stairtrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin1 on HIGH state for 10 micro seconds
digitalWrite(stairtrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(stairtrigPin, LOW);
// Reads the echoPin1, returns the sound wave travel time in microseconds
duration = pulseIn(stairechoPin, HIGH);
// Calculate the distance
stairdistanceCm = duration * SOUND_SPEED/2;
// Convert to inches
stairdistanceInch = stairdistanceCm * CM_TO_INCH;
if (stairdistanceCm >= 130) {
// Serial.println("The Buzzer is being");
// Prints the distance in the Serial Monitor
// Serial.print("Stair Distance (cm): ");
// Serial.println(stairdistanceCm);
//Serial.print("Stair Distance (inch): ");
// Serial.println(stairdistanceInch);
tone(BUZZER_PIN, 250, 25);
}
else
if (stairdistanceCm <= 130) {
// Serial.println("The Buzzer is unpressed");
noTone(BUZZER_PIN);
}
//stairs length
digitalWrite(stairlengthtrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin1 on HIGH state for 10 micro seconds
digitalWrite(stairlengthtrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(stairlengthtrigPin, LOW);
// Reads the stairlengthechoPin, returns the sound wave travel time in microseconds
duration = pulseIn(stairlengthechoPin, HIGH);
// Calculate the distance
stairlengthdistanceCm = duration * SOUND_SPEED/2;
// Convert to inches
stairlengthdistanceInch = stairlengthdistanceCm * CM_TO_INCH;
if (stairlengthdistanceCm >= 100) {
// Serial.println("The Buzzer is being");
// Prints the distance in the Serial Monitor
// Serial.print("Stair Distance Length (cm): ");
// Serial.println(stairlengthdistanceCm);
// Serial.print("Stair Distance (inch): ");
// Serial.println(stairlengthdistanceInch);
tone(BUZZER_PIN, 250, 25);
}
else
if (stairlengthdistanceCm <= 100) {
// Serial.println("The Buzzer is unpressed");
noTone(BUZZER_PIN);
}
//pithole length
digitalWrite(pitholelengthtrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin1 on HIGH state for 10 micro seconds
digitalWrite(pitholelengthtrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(pitholelengthtrigPin, LOW);
// Reads the stairlengthechoPin, returns the sound wave travel time in microseconds
duration = pulseIn(pitholelengthechoPin, HIGH);
// Calculate the distance
pitholelengthdistanceCm = duration * SOUND_SPEED/2;
// Convert to inches
pitholelengthdistanceInch = pitholelengthdistanceCm * CM_TO_INCH;
if (pitholelengthdistanceCm <= 90) {
// Serial.println("The Buzzer is being");
// Prints the distance in the Serial Monitor
// Serial.print("Pit hole Distance Length (cm): ");
// Serial.println(pitholelengthdistanceCm);
// Serial.print("Pit hole Distance (inch): ");
// Serial.println(pitholelengthdistanceInch);
tone(BUZZER_PIN, 250, 25);
}
else
if (pitholelengthdistanceCm >= 100) {
//Serial.println("The Buzzer is unpressed");
noTone(BUZZER_PIN);
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
/* Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
digitalWrite(POWER_PIN, HIGH); // turn the sensor ON
delay(10); // wait 10 milliseconds
value = analogRead(SIGNAL_PIN); // read the analog value from sensor
digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
Serial.print("The water sensor value: ");
Serial.println(value);*/
delay(1000);
}