#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;
// ------ 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
Serial1.begin(GPS_BAUDRATE, SERIAL_8N1, 23, 22); // GPS module on RX=23, TX=22
pinMode(motionSensorPin, INPUT); // Set motion sensor pin as 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); // Check for motion every 2 seconds
}
void checkMotion() {
if (digitalRead(motionSensorPin) == HIGH) {
if (!motionDetected) { // Only trigger once per motion event
Serial.println("Motion detected!");
motionDetected = true;
getGPSData(); // Get GPS data when motion is detected
}
} else {
motionDetected = false; // Reset motion flag when no motion
}
}
// ------ 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
// Continuously read GPS data
while (Serial1.available() > 0) {
gps.encode(Serial1.read());
}
}
// #define BLYNK_TEMPLATE_ID "TMPL4mhud2_gw"
// #define BLYNK_TEMPLATE_NAME "IoT in GIS"
// #define BLYNK_AUTH_TOKEN "zuLUQu7kWSfAjKSJVo9IZeexKPoU0co4"
// // ------ WiFi Credentials ------
// char ssid[] = "Wokwi-GUEST";
// char pass[] = "";
// #include <HardwareSerial.h>
// #define PIR_PIN 13 // PIR motion sensor output pin
// HardwareSerial gpsSerial(1); // Use Serial1 for GPS communication
// void setup() {
// Serial.begin(115200); // Serial monitor
// gpsSerial.begin(9600, SERIAL_8N1, 16, 17); // GPS module on Serial1 (RX=16, TX=17)
// pinMode(PIR_PIN, INPUT);
// Serial.println("System Initialized");
// }
// void loop() {
// // Read PIR sensor
// int motionDetected = digitalRead(PIR_PIN);
// if (motionDetected) {
// Serial.println("Motion detected!");
// }
// // Read GPS data
// while (gpsSerial.available()) {
// char c = gpsSerial.read();
// Serial.write(c); // Forward GPS data to serial monitor
// }
// delay(500); // Reduce CPU usage
// }