├── README.md └── alert.py /README.md: -------------------------------------------------------------------------------- 1 | # Earthquake_Alert -------------------------------------------------------------------------------- /alert.py: -------------------------------------------------------------------------------- 1 | import smbus 2 | import math 3 | import time 4 | import requests 5 | 6 | power_mgmt_1 = 0x6b 7 | power_mgmt_2 = 0x6c 8 | 9 | def read_byte(adr): 10 | return bus.read_byte_data(address, adr) 11 | 12 | def read_word(adr): 13 | high = bus.read_byte_data(address, adr) 14 | low = bus.read_byte_data(address, adr+1) 15 | val = (high << 8) + low 16 | return val 17 | 18 | def read_word_2c(adr): 19 | val = read_word(adr) 20 | if val >= 0x8000: 21 | return -((65535 - val) + 1) 22 | else: 23 | return val 24 | 25 | def dist(a, b): 26 | return math.sqrt((a * a) + (b * b)) 27 | 28 | def get_y_rotation(x, y, z): 29 | radians = math.atan2(x, dist(y, z)) 30 | return -math.degrees(radians) 31 | 32 | def get_x_rotation(x, y, z): 33 | radians = math.atan2(y, dist(x, z)) 34 | return math.degrees(radians) 35 | 36 | bus = smbus.SMBus(1) 37 | address = 0x68 # MPU6050 I2C address 38 | 39 | bus.write_byte_data(address, power_mgmt_1, 0) 40 | 41 | 42 | previous_accel_magnitude = 0 43 | threshold = 0.4 # Adjust this threshold as needed 44 | 45 | while True: 46 | time.sleep(0.1) 47 | 48 | 49 | accel_xout = read_word_2c(0x3b) 50 | accel_yout = read_word_2c(0x3d) 51 | accel_zout = read_word_2c(0x3f) 52 | 53 | accel_xout_scaled = accel_xout / 16384.0 54 | accel_yout_scaled = accel_yout / 16384.0 55 | accel_zout_scaled = accel_zout / 16384.0 56 | 57 | 58 | accel_magnitude = dist(accel_xout_scaled, accel_yout_scaled) 59 | 60 | 61 | if abs(accel_magnitude - previous_accel_magnitude) > threshold: 62 | print("Earthquake detected!") 63 | r = requests.post("https://api.pushover.net/1/messages.json", data = { 64 | "token": "your_token", 65 | "user": "user", 66 | "message": "Earthquake Alert"}, 67 | files = {"attachment": ("ek_logo.jpeg", open("ek_logo.jpeg", "rb"), "image/jpeg")}) 68 | 69 | 70 | previous_accel_magnitude = accel_magnitude 71 | 72 | time.sleep(0.5) 73 | --------------------------------------------------------------------------------