#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h> // Library for DS1307 RTC
#include <WiFi.h>
#include <HTTPClient.h>
// Pin definitions
const int pirPin = 14; // PIR sensor connected to GPIO 14
const int buzzerPin = 15; // Buzzer connected to GPIO 15
const int ledPin = 4; // LED connected to GPIO 4
// I2C address of the LCD
const int i2cAddress = 0x27; // Change this according to your LCD address
// ThingSpeak parameters
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const String channelID = "GZCK9W91Z0STRAVC"; // Replace with your ThingSpeak channel ID
const char *server = "api.thingspeak.com";
// Set the LCD dimensions (16 columns and 2 rows)
LiquidCrystal_I2C lcd(i2cAddress, 16, 2);
// Define RTC object
RTC_DS1307 rtc;
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
lcd.init();
lcd.backlight();
// Initialize RTC
Wire.begin(); // Initialize I2C communication
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Connect to WiFi
connectToWiFi();
}
void loop() {
// Read motion sensor
int motionState = digitalRead(pirPin);
// Check motion
if (motionState == HIGH) {
Serial.println("Motion detected!");
digitalWrite(buzzerPin, HIGH);
// Display "Motion Detected!" along with the current time
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
lcd.setCursor(0, 1);
lcd.print("Time:");
lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME));
// Send data to ThingSpeak with value "1"
sendToThingSpeak(1);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(5000);
} else {
// Display current time on LCD when no motion detected
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(0, 1);
lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME));
// Control LED based on time
int hour = now.hour();
if (hour >= 18 || hour < 6) {
digitalWrite(ledPin, HIGH); // Turn on LED at night (6 PM to 6 AM)
} else {
digitalWrite(ledPin, LOW);
}
// Send data to ThingSpeak with value "0"
sendToThingSpeak(0);
delay(1000); // Delay for a second
}
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("...");
}
Serial.println("WiFi connected");
}
void sendToThingSpeak(int value) {
WiFiClient client;
HTTPClient http;
// Construct URL with data to send
String url = "/update?api_key=";
url += channelID;
url += "&field1=";
url += value;
Serial.print("Sending data to ThingSpeak: ");
Serial.println(url);
// Make HTTP GET request
if (http.begin(client, server, 80, url)) {
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("Data sent successfully");
} else {
Serial.print("Error sending data to ThingSpeak. HTTP response code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Unable to connect to ThingSpeak");
}
}