"The core of this system is a Multi-Sensor Fusion Algorithm. To minimize false positives (like accidental pocket movement), the Raspberry Pi processes real-time data from four distinct sensors. An alert is only triggered if the system detects a Sum of Anomalies >= 2."
Anomaly Detection Criteria:
LDR: Light levels drop below the calibrated dark threshold (Pocket Removal).
Capacitive Touch: Loss of physical contact with the user's leg/fabric.
Accelerometer: Sudden G-force spike exceeding 2.5G (The Snatch).
Gyroscope: Rotational velocity exceeding 300°/sec (The Twist).
import RPi.GPIO as GPIO
import time, serial, threading, smbus2, json, pynmea2
import paho.mqtt.client as mqtt
# GPIO & Pin Setup
LDR_PIN, TOUCH_PIN, BUZZ_PIN = 18, 17, 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(LDR_PIN, GPIO.IN)
GPIO.setup(TOUCH_PIN, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(BUZZ_PIN, GPIO.OUT)
buzzer = GPIO.PWM(BUZZ_PIN, 1000)
buzzer.start(0)
# MPU6050 & MQTT Config
bus = smbus2.SMBus(1)
MQTT_TOPIC = "iot-pickpocket/final/gps"
client = mqtt.Client()
client.connect("broker.hivemq.com", 1883, 60)
client.loop_start()
def get_accel_mag():
try:
# Reading raw Accel data (X, Y, Z)
ax = (bus.read_byte_data(0x68, 0x3B) << 8 | bus.read_byte_data(0x68, 0x3C)) / 16384.0
ay = (bus.read_byte_data(0x68, 0x3D) << 8 | bus.read_byte_data(0x68, 0x3E)) / 16384.0
az = (bus.read_byte_data(0x68, 0x3F) << 8 | bus.read_byte_data(0x68, 0x40)) / 16384.0
return (ax**2 + ay**2 + az**2)**0.5
except: return 1.0
def send_bt_alert():
try:
with serial.Serial('/dev/rfcomm0', 9600, timeout=1) as bt:
bt.write(b'A') # Send Alert signal to Wristband
except: pass
# Main Monitoring Loop
alert_mode = False
while True:
mag = get_accel_mag()
light_anom = GPIO.input(LDR_PIN) == 0 # Light detected
touch_anom = GPIO.input(TOUCH_PIN) == 1 # Loss of contact
motion_anom = mag > 2.5 # Sudden snatch
if (light_anom + touch_anom + motion_anom) >= 2:
print("THEFT DETECTED!")
send_bt_alert()
alert_mode = True
time.sleep(5) # Cooldown
time.sleep(0.1)
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
SoftwareSerial BT(2, 3); // RX, TX
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
BT.begin(9600);
pinMode(9, OUTPUT); // Vibration Motor
pinMode(4, INPUT_PULLUP); // Find Phone Switch
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
showStatus("READY");
}
void loop() {
// 1. Listen for Alert from Phone
if (BT.available()) {
if (BT.read() == 'A') {
triggerAlert();
}
}
// 2. Handle 'Find Phone' Switch
if (digitalRead(4) == LOW) {
BT.write('F'); // Send Find signal back to Pi
showStatus("FINDING...");
delay(1000);
showStatus("READY");
}
}
void triggerAlert() {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(10, 20);
display.print("!!THEFT!!");
display.display();
for(int i=0; i<5; i++) {
digitalWrite(9, HIGH); delay(300);
digitalWrite(9, LOW); delay(200);
}
showStatus("READY");
}
void showStatus(String txt) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(30, 25);
display.print(txt);
display.display();
}
"The tracking system publishes real-time coordinates using the NMEA 0183 standard. The data is parsed on the Raspberry Pi and sent as a JSON payload to the cloud dashboard."
Broker: broker.hivemq.com
Topic: iot-pickpocket/final/gps
Visualization: Integration with Leaflet.js map for real-time pin tracking.
Github: Link