#include <TinyGPS++.h>
#define PIR_PIN 13 // PIR motion sensor connected to GPIO 13
// Define Nigeria's geographical boundaries
const float minLat = 4.25; // Minimum latitude
const float maxLat = 13.85; // Maximum latitude
const float minLon = 2.75; // Minimum longitude
const float maxLon = 14.65; // Maximum longitude
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
Serial.println("System Initialized...");
}
void loop() {
// Read PIR sensor state
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
Serial.println("Motion detected!");
// Generate random GPS coordinates within Nigeria
float randomLat = random(minLat * 1000000, maxLat * 1000000) / 1000000.0;
float randomLon = random(minLon * 1000000, maxLon * 1000000) / 1000000.0;
Serial.print("Random GPS Coordinates: ");
Serial.print(randomLat, 6);
Serial.print(", ");
Serial.println(randomLon, 6);
}
delay(1000);
}
// #include <HardwareSerial.h>
// #define PIR_PIN 13 // PIR motion sensor connected to GPIO 13
// #define RXD2 16 // GPS Module RX to ESP32 TX2
// #define TXD2 17 // GPS Module TX to ESP32 RX2
// HardwareSerial neogps(2);
// void setup() {
// Serial.begin(115200);
// neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);
// pinMode(PIR_PIN, INPUT);
// Serial.println("System Initialized...");
// }
// void loop() {
// // Read PIR sensor state
// int motion = digitalRead(PIR_PIN);
// if (motion == HIGH) {
// Serial.println("Motion detected!");
// }
// // Read GPS data
// while (neogps.available()) {
// char c = neogps.read();
// Serial.write(c);
// }
// delay(1000);
// }
// #define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
// #define BLYNK_TEMPLATE_NAME "IoT in GIS"
// #define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
// #include <WiFi.h>
// #include <BlynkSimpleEsp32.h>
// // ------ Motion Sensor Configuration ------
// const int motionSensorPin = 13;
// bool motionDetected = false;
// // ------ Blynk Timer Setup ------
// BlynkTimer timer;
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // Function Declarations
// void checkMotion();
// void sendRandomGPSData();
// void setup() {
// Serial.begin(115200);
// pinMode(motionSensorPin, INPUT);
// // Connect to WiFi and Blynk
// Serial.println("Connecting to WiFi...");
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// timer.setInterval(2000L, checkMotion);
// }
// void checkMotion() {
// if (digitalRead(motionSensorPin) == HIGH) {
// if (!motionDetected) {
// Serial.println("Motion detected! Generating random GPS coordinates...");
// motionDetected = true;
// sendRandomGPSData();
// }
// } else {
// motionDetected = false;
// }
// }
// void sendRandomGPSData() {
// float minLat = 4.2, maxLat = 13.9; // Nigeria's latitude range
// float minLon = 2.7, maxLon = 14.6; // Nigeria's longitude range
// float latitude = minLat + ((float)rand() / RAND_MAX) * (maxLat - minLat);
// float longitude = minLon + ((float)rand() / RAND_MAX) * (maxLon - minLon);
// Serial.print("Latitude: ");
// Serial.println(latitude, 6);
// Serial.print("Longitude: ");
// Serial.println(longitude, 6);
// Blynk.virtualWrite(V0, latitude);
// Blynk.virtualWrite(V1, longitude);
// }
// 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 <TinyGPS++.h>
// // ------ GPS Module Configuration ------
// #define GPS_BAUDRATE 9600 // Default baud rate for NEO-6M
// TinyGPSPlus gps;
// // ------ Motion Sensor Configuration ------
// const int motionSensorPin = 13; // PIR sensor pin (connected to GPIO 13)
// bool motionDetected = false;
// unsigned long lastMotionTime = 0; // Debouncing
// const long motionDebounceDelay = 500; // 500ms debounce
// // ------ Blynk Timer Setup ------
// BlynkTimer timer;
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST"; // Use Wokwi-GUEST for Wokwi simulation
// char pass[] = "";
// // ------ Function Declarations ------
// void checkMotion();
// void getGPSData();
// void setup() {
// Serial.begin(115200); // Initialize serial monitor
// Serial2.begin(GPS_BAUDRATE, SERIAL_8N1, 16, 17); // Use Serial2 for GPS (RX=16, TX=17)
// pinMode(motionSensorPin, INPUT_PULLUP); // Set motion sensor pin as input with internal pull-up
// // Connect to WiFi and Blynk
// Serial.println("Connecting to WiFi...");
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// timer.setInterval(2000L, checkMotion); // Check for motion every 2 seconds
// timer.setInterval(10000L, getGPSData); // Get GPS data every 10 seconds
// }
// void checkMotion() {
// int sensorValue = digitalRead(motionSensorPin);
// unsigned long currentTime = millis();
// if (sensorValue == LOW) { // Assuming LOW signal when motion is detected with INPUT_PULLUP
// if (!motionDetected && (currentTime - lastMotionTime > motionDebounceDelay)) {
// Serial.println("Motion detected!");
// motionDetected = true;
// lastMotionTime = currentTime; // Update last motion time
// getGPSData(); //Immediately get GPS data after motion
// Blynk.virtualWrite(V2, "Motion Detected!"); // Optionally send a notification to blynk
// }
// } else {
// motionDetected = false;
// Blynk.virtualWrite(V2, ""); // Clear notification
// }
// }
// // ------ Function to Get GPS Data ------
// void getGPSData() {
// if (gps.location.isValid()) {
// float latitude = gps.location.lat();
// float longitude = gps.location.lng();
// Serial.print("Latitude: ");
// Serial.println(latitude, 6);
// Serial.print("Longitude: ");
// Serial.println(longitude, 6);
// // Send GPS data to Blynk
// Blynk.virtualWrite(V0, latitude);
// Blynk.virtualWrite(V1, longitude);
// } else {
// Serial.println("Waiting for valid GPS data...");
// }
// }
// // ------ Continuous GPS Data Parsing ------
// void loop() {
// Blynk.run(); // Run Blynk tasks
// timer.run(); // Run timer tasks
// // Read GPS data continuously
// while (Serial2.available() > 0) {
// gps.encode(Serial2.read());
// }
// }
// #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 <TinyGPS++.h>
// // ------ GPS Module Configuration ------
// #define GPS_BAUDRATE 9600
// TinyGPSPlus gps;
// // ------ Motion Sensor Configuration ------
// const int motionSensorPin = 13;
// bool motionDetected = false;
// // ------ Blynk Timer Setup ------
// BlynkTimer timer;
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// // Function Declarations
// void checkMotion();
// void getGPSData();
// void setup() {
// Serial.begin(115200);
// Serial2.begin(GPS_BAUDRATE, SERIAL_8N1, 16, 17);
// pinMode(motionSensorPin, INPUT);
// // Connect to WiFi and Blynk
// Serial.println("Connecting to WiFi...");
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Serial.println("Connected to Blynk!");
// timer.setInterval(2000L, checkMotion);
// timer.setInterval(5000L, getGPSData);
// }
// void checkMotion() {
// if (digitalRead(motionSensorPin) == HIGH) {
// if (!motionDetected) {
// Serial.println("Motion detected! Sending location...");
// motionDetected = true;
// getGPSData();
// }
// } else {
// motionDetected = false;
// }
// }
// void getGPSData() {
// while (Serial2.available() > 0) {
// gps.encode(Serial2.read());
// }
// if (gps.location.isValid()) {
// float latitude = gps.location.lat();
// float longitude = gps.location.lng();
// Serial.print("Latitude: ");
// Serial.println(latitude, 6);
// Serial.print("Longitude: ");
// Serial.println(longitude, 6);
// Blynk.virtualWrite(V0, latitude);
// Blynk.virtualWrite(V1, longitude);
// } else {
// Serial.println("Waiting for valid GPS data...");
// }
// }
// void loop() {
// Blynk.run();
// timer.run();
// }