//Installation of Dependancies needed by the ESP32
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
//Setting the OLED Displays resolution
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Setting the DHT22's pin and needed Libraries
#include "DHTesp.h"
const int DHT_PIN = 4;
DHTesp dhtSensor;
Adafruit_MPU6050 mpu;
//Setting the Pins for the Motion Sensor
int inputPin = 2;
int val = 0;
void setup() {
Serial.begin(115200);
//Pins for the HCSR04
pinMode(17, OUTPUT);
pinMode(16, INPUT);
//DHT22 Setup
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//Starting the OLED Display, on failure show "SSD1306 allocation Failed", on success skip
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(1000);
display.clearDisplay();
//Starting the MPU6050 and confirming status on boot
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
//Setting the cursor and color options for the OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BIN);
display.setCursor(0, 0);
display.clearDisplay();
display.println("Please enter your wanted data:");
display.display();
delay(1000);
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void hcsr04() {
long duration;
int distance;
//Setting pins for the HCSR04
digitalWrite(17, LOW);
delay(2);
digitalWrite(17, HIGH);
delay(10);
digitalWrite(17, LOW);
duration = pulseIn(16, HIGH);
distance = duration * 0.034 / 2;
display.clearDisplay();
display.setCursor(0, 0);
//Displaying HCSR04 data in Serial and OLED
display.print("Distance: ");
Serial.print("Distance: ");
display.print(distance);
Serial.print(distance);
display.print("cm");
Serial.println("cm");
display.display();
delay(2000);
}
void dht22() {
//Retrieving temperature and humidity values from the DHT22
TempAndHumidity data = dhtSensor.getTempAndHumidity();
display.clearDisplay();
display.setCursor(0, 0);
//Displaying the values from the DHT22 in the Serial and OLED
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
display.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
display.println("Humidity: " + String(data.humidity, 1) + "%");
display.display();
delay(1000);
}
void mpu6050() {
//Retriving data from the MPU6050
sensors_event_t event;
mpu.getAccelerometerSensor()->getEvent(&event);
display.clearDisplay();
display.setCursor(0, 0);
//Displaying the data from the MPU6050
Serial.print("[");
Serial.print(millis());
Serial.print("] X: ");
Serial.print(event.acceleration.x);
Serial.print(", Y: ");
Serial.print(event.acceleration.y);
Serial.print(", Z: ");
Serial.print(event.acceleration.z);
Serial.println(" m/s^2");
display.print("[");
display.print(millis());
display.print("] X: ");
display.print(event.acceleration.x);
display.print(", Y: ");
display.print(event.acceleration.y);
display.print(", Z: ");
display.print(event.acceleration.z);
display.println(" m/s^2");
display.display();
delay(500);
}
void motionSensor() {
//Setting the pins for the Motion Sensor
int inputPin = 2;
int pirState = LOW;
int val = 0;
val = digitalRead(inputPin);
display.clearDisplay();
display.setCursor(0, 0);
//Displaying first portion of message
display.println("Motion: ");
Serial.println("Motion: ");
//Displaying second half, showing if there was movement or not
if (val == HIGH) {
if (pirState == HIGH) {
display.println("Detected!");
Serial.println("Detected!");
} else {
if (pirState == LOW) {
display.println("Not Detected!");
Serial.println("Not Detected!");
}
}
}
//if (pirState == HIGH) {
//display.println("Detected!");
//Serial.println("Detected!");
//} else {
//display.println("Not Detected!");
//Serial.println("Not Detected!");
//}
display.display();
}
void ntcTemp() {
const float BETA = 3950;
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temperature: ");
display.print(celsius);
display.print(" °C");
display.display();
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
display.display();
delay(1000);
}
void photoresistor() {
pinMode(34, INPUT);
display.clearDisplay();
display.setCursor(0, 0);
Serial.print("Room: ");
display.print("Room: ");
if (digitalRead(34 == LOW)) {
Serial.print("Light!");
display.print("Light!");
} else {
Serial.print("Dark!");
display.print("Dark!");
}
display.display();
delay(100);
}
void loop() {
if(Serial.available()>0){
String command = Serial.readStringUntil('\n');
if(command=="distance"){
hcsr04();
} else if (command == "temp" || command == "humi") {
dht22();
} else if (command == "accelerometer") {
mpu6050();
} else if (command == "motion") {
motionSensor();
} else if (command == "temp2") {
ntcTemp();
} else if (command == "brightness") {
photoresistor();
}
}
delay(1000);
}