#include <WiFi.h>
#include <ThingSpeak.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* pass = ""; // Replace with your WiFi password
// ThingSpeak credentials
unsigned long myChannelNumber = 2887753; // Replace with your ThingSpeak channel number
const char* myWriteAPIKey = "EH7PODUNRPP7LC7H"; // Replace with your ThingSpeak API key
// Pin Definitions
const int pirPin = 4; // PIR Sensor on GPIO 4
const int trigPin = 12; // Ultrasonic Sensor Trig on GPIO 12
const int echoPin = 14; // Ultrasonic Sensor Echo on GPIO 14
const int potPin = 34; // Potentiometer on GPIO 34 (simulates sound sensor)
const int ldrPin = 35; // LDR Module A0 on GPIO 35
const int ledPin = 2; // LED on GPIO 2
const int buzzerPin = 15; // Buzzer on GPIO 15
// Threshold Values
const int motionThreshold = HIGH; // PIR detects motion (HIGH)
const int distanceThreshold = 50; // Ultrasonic distance in cm
const int soundThreshold = 500; // Sound threshold
const int lightThreshold = 500; // Light threshold
WiFiClient client;
void setup() {
Serial.begin(115200); // Initialize Serial Monitor
// Initialize pins
pinMode(pirPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(potPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Connect to WiFi
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected.");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Read sensor data
int motion = digitalRead(pirPin); // PIR Sensor (HIGH = motion detected)
long distance = getDistance(); // Ultrasonic Sensor (distance in cm)
int sound = 200; // Potentiometer (simulates sound, 0–4095)
int light = analogRead(ldrPin); // LDR Module (0–4095, lower = darker)
// Detect intruder type
const char* intruderType = detectIntruderType(motion, distance, sound, light);
// Print sensor values and intruder type to Serial Monitor
Serial.print("Motion: ");
Serial.println(motion);
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Sound: ");
Serial.println(sound);
Serial.print("Light: ");
Serial.println(light);
Serial.print("Intruder Type: ");
Serial.println(intruderType);
// Check for animal intrusion
if (strcmp(intruderType, "Animal") == 0) {
digitalWrite(ledPin, HIGH); // Turn on LED
tone(buzzerPin, 1000); // Activate buzzer
Serial.println("Animal Intrusion Detected!"); // Print to Serial Monitor
} else {
digitalWrite(ledPin, LOW); // Turn off LED
noTone(buzzerPin); // Deactivate buzzer
}
// Send data to ThingSpeak
ThingSpeak.setField(1, motion); // Field 1: Motion detection
ThingSpeak.setField(2, distance); // Field 2: Distance measurement
ThingSpeak.setField(3, sound); // Field 3: Simulated sound level
ThingSpeak.setField(4, light); // Field 4: Light intensity
ThingSpeak.setField(5, (strcmp(intruderType, "Human") == 0) ? 1 : 0); // Field 5: Human (1) or Animal (0)
int statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Problem writing data. HTTP error code: " + String(statusCode));
}
delay(15000); // Wait for 15 seconds before sending the next data
}
// Function to calculate distance using ultrasonic sensor
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert duration to distance in cm
}
// Function to detect intruder type (Human or Animal)
const char* detectIntruderType(int motion, long distance, int sound, int light) {
if (motion == motionThreshold) {
if (distance < distanceThreshold && sound > soundThreshold && light > lightThreshold) { // Close, loud, and bright
return "Human";
} else if (distance >= distanceThreshold && sound <= soundThreshold && light <= lightThreshold) { // Far, quiet, and dark
return "Animal";
}
}
return "Unknown";
}