#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#include "DHT.h"
#define DHTPIN 18
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int trigPin = 19;
const int echoPin = 5;
//define sound speed in cm/us
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
float Temperature;
float Humidity;
float Temp_Fahrenheit;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SECRET_SSID "Wokwi-GUEST" // replace MySSID with your WiFi network name
#define SECRET_PASS "" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 2287631 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "7AA29RKMLP66H2CX" // replace XYZ with your channel write API Key
WiFiClient client;
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
int piezoPin = 26;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(27, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("hello buzz!");
pinMode(piezoPin, OUTPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
//put your main code here,to run repeatly: Serial.println("Hello,ESP32!");
int a=digitalRead(27);
Serial.println(a);
if(a==HIGH)
{
Serial.println("motion detected");
tone(piezoPin, 1000, 500);
delay(5000);
}
if(a==LOW)
{
Serial.println("motion not detected");
noTone(piezoPin);
}
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distanceCm = duration * 0.034 / 2;
//Convert to inches
distanceInch = distanceCm * CM_TO_INCH; // Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text display.setCursor(0,0);
display.setCursor(0,0); // Start at top-left corner
display.println(F("Distance:"));
display.print(distanceCm);
display.println(F("cm"));
display.print(distanceInch);
display.println(F("inch"));
display.display();
delay(2000);
int pirStatus = digitalRead(piezoPin);
//Sensor Readings
Humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
Temperature = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
Temp_Fahrenheit= dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(Humidity) ||isnan(Temperature) ||isnan(Temp_Fahrenheit)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//display temperature and humidity to Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(Humidity);
Serial.print(F("% Temperature: "));
Serial.print(Temperature);
Serial.print(F("°C Temp_Fahrenheit: "));
Serial.print(Temp_Fahrenheit);
Serial.println(F("°F "));
delay(1000);
// display temperature to OLED Display
display.setCursor(0,0);
display.clearDisplay();
display.setTextSize(1.75);
display.setCursor(0,0);
display.print("Temp Monitoring");
display.setTextSize(1);
display.setCursor(0,16);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,26);
display.print(Temperature);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 41);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 51);
display.print(Humidity);
display.print(" %");
display.display();
delay(20000);
// set the fields with the values
ThingSpeak.setField(1, distanceCm);
ThingSpeak.setField(2, distanceInch);
ThingSpeak.setField(3, pirStatus);
ThingSpeak.setField(4, Temperature);
ThingSpeak.setField(5, Humidity );
ThingSpeak.setField(6, Temp_Fahrenheit);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}