#define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
#define BLYNK_TEMPLATE_NAME "IoT in GIS"
#define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int motionSensorPin = 13;
const int ledPin = 2;
const int buzzerPin = 4;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
bool motionDetected = false;
BlynkTimer timer;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup() {
Serial.begin(115200);
pinMode(motionSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, HIGH);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("Device ON");
display.display();
WiFi.begin(ssid, pass);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(2000L, checkMotion);
}
void checkMotion() {
if (digitalRead(motionSensorPin) == HIGH && !motionDetected) {
Serial.println("Motion detected!");
motionDetected = true;
tone(buzzerPin, 1000, 500);
Blynk.virtualWrite(V2, "Motion Detected!");
sendRandomLocation();
} else if (digitalRead(motionSensorPin) == LOW) {
motionDetected = false;
Blynk.virtualWrite(V2, "");
}
}
void sendRandomLocation() {
float randomLat = 4.0 + (random(0, 1000) / 1000.0) * 10.0;
float randomLon = 3.0 + (random(0, 1000) / 1000.0) * 12.0;
Serial.printf("Lat: %.6f, Lon: %.6f\n", randomLat, randomLon);
display.clearDisplay();
display.setCursor(0, 10);
display.println("Motion Detected!");
display.setCursor(0, 30);
display.printf("Lat: %.6f", randomLat);
display.setCursor(0, 40);
display.printf("Lon: %.6f", randomLon);
display.display();
Blynk.virtualWrite(V0, randomLat);
Blynk.virtualWrite(V1, randomLon);
}
void loop() {
Blynk.run();
timer.run();
}
// #define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
// #define BLYNK_TEMPLATE_NAME "IoT in GIS"
// #define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
// #include <WiFi.h>
// #include <BlynkSimpleEsp32.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// // ------ Pin Definitions ------
// const int motionSensorPin = 13; // PIR sensor input pin
// const int ledPin = 2; // ON indicator LED
// const int buzzerPin = 4; // Buzzer output pin
// // ------ OLED Display ------
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// // ------ Motion Detection ------
// bool motionDetected = false;
// BlynkTimer timer; // Built-in timer in Blynk library
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // ------ Function Prototypes ------
// void checkMotion();
// void sendRandomLocation();
// void initializeOLED();
// void connectToWiFiAndBlynk();
// void setup() {
// Serial.begin(115200);
// pinMode(motionSensorPin, INPUT);
// pinMode(ledPin, OUTPUT);
// pinMode(buzzerPin, OUTPUT);
// // Indicate device power with LED
// digitalWrite(ledPin, HIGH);
// // Initialize OLED Display
// initializeOLED();
// // Connect to WiFi and Blynk
// connectToWiFiAndBlynk();
// // Set a timer to check motion every 2 seconds
// timer.setInterval(2000L, checkMotion);
// }
// void initializeOLED() {
// if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("OLED initialization failed!"));
// while (1); // Halt if OLED cannot initialize
// }
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(WHITE);
// display.setCursor(0, 10);
// display.println("Device ON");
// display.display();
// }
// void connectToWiFiAndBlynk() {
// Serial.println("Connecting to WiFi...");
// WiFi.begin(ssid, pass);
// // Retry connection if WiFi is not connected
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
// Serial.println("Trying to connect to WiFi...");
// }
// Serial.println("Connected to WiFi!");
// // Connect to Blynk
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// }
// void checkMotion() {
// if (digitalRead(motionSensorPin) == HIGH) {
// if (!motionDetected) { // Trigger only once per motion event
// Serial.println("Motion detected!");
// motionDetected = true;
// // Activate buzzer with tone function
// tone(buzzerPin, 1000, 500); // 1 kHz for 500ms
// // Send "Motion Detected" message to Blynk
// Blynk.virtualWrite(V2, "Motion Detected!");
// // Generate and send random location
// sendRandomLocation();
// }
// } else {
// motionDetected = false; // Reset motion flag
// // Clear "Motion Detected" message on Blynk when no motion
// Blynk.virtualWrite(V2, "");
// }
// }
// void sendRandomLocation() {
// // Nigeria's latitude and longitude boundaries
// const float minLat = 4.0, maxLat = 14.0;
// const float minLon = 3.0, maxLon = 15.0;
// // Generate random latitude and longitude within the defined boundaries
// float randomLat = minLat + ((float)esp_random() / UINT32_MAX) * (maxLat - minLat);
// float randomLon = minLon + ((float)esp_random() / UINT32_MAX) * (maxLon - minLon);
// // Output the coordinates to Serial Monitor
// Serial.printf("Latitude: %.10f, Longitude: %.10f\n", randomLat, randomLon);
// // Display the coordinates on OLED
// display.clearDisplay();
// display.setCursor(0, 10);
// display.println("Motion Detected!");
// display.setCursor(0, 30);
// display.printf("Lat: %.10f", randomLat);
// display.setCursor(0, 40);
// display.printf("Lon: %.10f", randomLon);
// display.display();
// // Send to Blynk (ensure exact values without approximation)
// Blynk.virtualWrite(V0, randomLat);
// Blynk.virtualWrite(V1, randomLon);
// }
// void loop() {
// Blynk.run();
// timer.run(); // Run the timer to check motion
// }
// #define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
// #define BLYNK_TEMPLATE_NAME "IoT in GIS"
// #define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
// #include <WiFi.h>
// #include <BlynkSimpleEsp32.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// // ------ Pin Definitions ------
// const int motionSensorPin = 13; // PIR sensor input pin
// const int ledPin = 2; // ON indicator LED
// const int buzzerPin = 4; // Buzzer output pin
// // ------ OLED Display ------
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// // ------ Motion Detection ------
// bool motionDetected = false;
// BlynkTimer timer; // Built-in timer in Blynk library
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // ------ Function Prototypes ------
// void checkMotion();
// void sendRandomLocation();
// void initializeOLED();
// void connectToWiFiAndBlynk();
// void setup() {
// Serial.begin(115200);
// pinMode(motionSensorPin, INPUT);
// pinMode(ledPin, OUTPUT);
// pinMode(buzzerPin, OUTPUT);
// // Indicate device power with LED
// digitalWrite(ledPin, HIGH);
// // Initialize OLED Display
// initializeOLED();
// // Connect to WiFi and Blynk
// connectToWiFiAndBlynk();
// // Set a timer to check motion every 2 seconds
// timer.setInterval(2000L, checkMotion);
// }
// void initializeOLED() {
// if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("OLED initialization failed!"));
// while (1); // Halt if OLED cannot initialize
// }
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(WHITE);
// display.setCursor(0, 10);
// display.println("Device ON");
// display.display();
// }
// void connectToWiFiAndBlynk() {
// Serial.println("Connecting to WiFi...");
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// }
// void checkMotion() {
// if (digitalRead(motionSensorPin) == HIGH) {
// if (!motionDetected) { // Trigger only once per motion event
// Serial.println("Motion detected!");
// motionDetected = true;
// // Activate buzzer with tone function
// tone(buzzerPin, 1000, 500); // 1 kHz for 500ms
// // Send "Motion Detected" message to Blynk
// Blynk.virtualWrite(V2, "Motion Detected!");
// // Generate and send random location
// sendRandomLocation();
// }
// } else {
// motionDetected = false; // Reset motion flag
// // Clear "Motion Detected" message on Blynk when no motion
// Blynk.virtualWrite(V2, "");
// }
// }
// void sendRandomLocation() {
// // Nigeria's latitude and longitude boundaries
// const float minLat = 4.0, maxLat = 14.0;
// const float minLon = 3.0, maxLon = 15.0;
// // Generate random latitude and longitude within the defined boundaries
// float randomLat = minLat + ((float)esp_random() / UINT32_MAX) * (maxLat - minLat);
// float randomLon = minLon + ((float)esp_random() / UINT32_MAX) * (maxLon - minLon);
// // Output the coordinates to Serial Monitor
// Serial.printf("Latitude: %.10f, Longitude: %.10f\n", randomLat, randomLon);
// // Display the coordinates on OLED
// display.clearDisplay();
// display.setCursor(0, 10);
// display.println("Motion Detected!");
// display.setCursor(0, 30);
// display.printf("Lat: %.10f", randomLat);
// display.setCursor(0, 40);
// display.printf("Lon: %.10f", randomLon);
// display.display();
// // Send to Blynk (ensure exact values without approximation)
// Blynk.virtualWrite(V0, randomLat);
// Blynk.virtualWrite(V1, randomLon);
// }
// void loop() {
// Blynk.run();
// timer.run(); // Run the timer to check motion
// }
// #define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
// #define BLYNK_TEMPLATE_NAME "IoT in GIS"
// #define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
// #include <WiFi.h>
// #include <BlynkSimpleEsp32.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// // ------ Pin Definitions ------
// const int motionSensorPin = 13; // PIR sensor
// const int ledPin = 2; // ON indicator LED
// const int buzzerPin = 4; // Buzzer
// // ------ OLED Display ------
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// // ------ Motion Detection ------
// bool motionDetected = false;
// BlynkTimer timer;
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // ------ Function Prototypes ------
// void checkMotion();
// void sendRandomLocation();
// void setup() {
// Serial.begin(115200);
// pinMode(motionSensorPin, INPUT);
// pinMode(ledPin, OUTPUT);
// pinMode(buzzerPin, OUTPUT);
// // Turn on LED to indicate the device is ON
// digitalWrite(ledPin, HIGH);
// // Initialize OLED Display
// if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("SSD1306 initialization failed!"));
// while (1);
// }
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(WHITE);
// display.setCursor(0, 10);
// display.println("Device ON");
// display.display();
// // Connect to WiFi and Blynk
// Serial.println("Connecting to WiFi...");
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// // Set timer to check for motion every 2 seconds
// timer.setInterval(2000L, checkMotion);
// }
// void checkMotion() {
// if (digitalRead(motionSensorPin) == HIGH) {
// if (!motionDetected) { // Trigger only once per motion event
// Serial.println("Motion detected!");
// motionDetected = true;
// // Activate buzzer with tone function
// tone(buzzerPin, 1000, 500); // 1 kHz sound for 500ms
// // Send "Motion Detected" message to Blynk
// Blynk.virtualWrite(V2, "Motion Detected!");
// sendRandomLocation(); // Generate and send location
// }
// } else {
// motionDetected = false; // Reset motion flag when no motion
// // Clear "Motion Detected" message on Blynk when no motion
// Blynk.virtualWrite(V2, "");
// }
// }
// void sendRandomLocation() {
// // Nigeria's latitude and longitude boundaries
// float minLat = 4.0, maxLat = 14.0;
// float minLon = 3.0, maxLon = 15.0;
// // Generate random latitude and longitude
// float randomLat = minLat + ((float)esp_random() / UINT32_MAX) * (maxLat - minLat);
// float randomLon = minLon + ((float)esp_random() / UINT32_MAX) * (maxLon - minLon);
// // Output the coordinates to Serial Monitor
// Serial.printf("Latitude: %.10f, Longitude: %.10f\n", randomLat, randomLon);
// // Display on OLED
// display.clearDisplay();
// display.setCursor(0, 10);
// display.println("Motion Detected!");
// display.setCursor(0, 30);
// display.printf("Lat: %.10f", randomLat);
// display.setCursor(0, 40);
// display.printf("Lon: %.10f", randomLon);
// display.display();
// // Send to Blynk (ensuring exact values without approximation)
// Blynk.virtualWrite(V0, randomLat);
// Blynk.virtualWrite(V1, randomLon);
// }
// void loop() {
// Blynk.run();
// timer.run();
// }