├── LICENSE ├── README.md ├── add_data.php ├── connect.php ├── fire.py ├── gas.py ├── index.php └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sheikh Nawab Arzoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Smart Alert System** 2 | ![](https://img.shields.io/badge/Release-V1.0.0-blue.svg) ![](https://img.shields.io/badge/Build-Stable-green.svg) ![](https://img.shields.io/badge/License-MIT-red.svg) ![](https://img.shields.io/badge/By-Sheikh%20Nawab%20Arzoo-red.svg?style=social&logo=appveyor) 3 | 4 | ------------ 5 | 6 | It's a alert system which will provide real-time alert on fire or LPG gas Leak in your house and it will monitor the house temperature and Methane ppm concentration in air via a Web portal. If the sensor records more than 60 degree temperature or 1000 ppm *(you can modify on script)* of methane concentration then it would send directly sms alert to the registered mobile number. 7 | 8 | ##### Requirements: 9 | 1. Raspberry Pi 10 | 2. ADC121C_MQ4 (Measure Methane Concentration in PPM) 11 | 3. DHT-11 (Measure Temperature) 12 | 4. GSM module (To send SMS) 13 | 14 | ##### Prerequisite: 15 | First raspbian os needs to be installed on SD card then run the following codes 16 | ```` 17 | sudo apt-get update 18 | sudo apt-get upgrade 19 | sudo apt-get install python-pip 20 | sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev 21 | sudo python -m pip install --upgrade pip setuptools wheel 22 | sudo pip install cffi 23 | sudo pip install smbus-cffi 24 | sudo pip install Adafruit_DHT 25 | git clone https://github.com/sakearzoo/Smart_Alert_System.git && cd Smart_Alert_System 26 | chmod +x fire.py 27 | chmod +x gas.py 28 | chmod +x main.py 29 | ```` 30 | 31 | After then you need to modify the `connect.php` file with proper credentials, Add your phone number to `fire.py` and `gas.py` and finally update the `main.py` with proper website address. 32 | 33 | ##### Now all is Set !! 34 | 35 | To run the script you need to enter th following code: 36 | ```` 37 | Sudo ./main.py 38 | or 39 | sudo python main.py 40 | ```` 41 | 42 | --------- -------------------------------------------------------------------------------- /add_data.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /connect.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fire.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import serial 3 | import RPi.GPIO as GPIO 4 | import os, time 5 | 6 | GPIO.setmode(GPIO.BOARD) 7 | 8 | # Enable Serial Communication 9 | port = serial.Serial("/dev/ttyUSB3", baudrate=9600, timeout=1) 10 | 11 | # Transmitting AT Commands to the Modem 12 | # '\r\n' indicates the Enter key 13 | 14 | port.write('AT'+'\r\n') 15 | rcv = port.read(10) 16 | print rcv 17 | time.sleep(1) 18 | 19 | port.write('ATE0'+'\r\n') # Disable the Echo 20 | rcv = port.read(10) 21 | print rcv 22 | time.sleep(1) 23 | 24 | port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode 25 | rcv = port.read(10) 26 | print rcv 27 | time.sleep(1) 28 | 29 | port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications 30 | rcv = port.read(10) 31 | print rcv 32 | time.sleep(1) 33 | 34 | # Sending a message to a particular Number 35 | # Eneter your number in place of 1234567890 36 | 37 | port.write('AT+CMGS="1234567890"'+'\r\n') 38 | rcv = port.read(10) 39 | print rcv 40 | time.sleep(1) 41 | 42 | port.write('Emergency Fire Alert Please check sensor log for more info'+'\r\n') # Message 43 | rcv = port.read(10) 44 | print rcv 45 | 46 | port.write("\x1A") # Enable to send SMS 47 | for i in range(10): 48 | rcv = port.read(10) 49 | print rcv 50 | -------------------------------------------------------------------------------- /gas.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import serial 3 | import RPi.GPIO as GPIO 4 | import os, time 5 | 6 | GPIO.setmode(GPIO.BOARD) 7 | 8 | # Enable Serial Communication 9 | port = serial.Serial("/dev/ttyUSB3", baudrate=9600, timeout=1) 10 | 11 | # Transmitting AT Commands to the Modem 12 | # '\r\n' indicates the Enter key 13 | 14 | port.write('AT'+'\r\n') 15 | rcv = port.read(10) 16 | print rcv 17 | time.sleep(1) 18 | 19 | port.write('ATE0'+'\r\n') # Disable the Echo 20 | rcv = port.read(10) 21 | print rcv 22 | time.sleep(1) 23 | 24 | port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode 25 | rcv = port.read(10) 26 | print rcv 27 | time.sleep(1) 28 | 29 | port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications 30 | rcv = port.read(10) 31 | print rcv 32 | time.sleep(1) 33 | 34 | # Sending a message to a particular Number 35 | # Eneter your number in place of 1234567890 36 | 37 | port.write('AT+CMGS="1234567890"'+'\r\n') 38 | rcv = port.read(10) 39 | print rcv 40 | time.sleep(1) 41 | 42 | port.write('Emergency Gas leak Alert Please check sensor log for more info'+'\r\n') # Message 43 | rcv = port.read(10) 44 | print rcv 45 | 46 | port.write("\x1A") # Enable to send SMS 47 | for i in range(10): 48 | rcv = port.read(10) 49 | print rcv 50 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | Raspberry Pi Sensor Log 11 | 32 | 33 | 34 | 35 |

Raspberry Pi Sensor Log

36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | '; 71 | echo ' '.$row["id"].''; 72 | echo ' '.$row["date"].''; 73 | echo ' '.$row["temperature"].''; 74 | echo ' '.$row["humidity"].''; 75 | echo ' '.$row["ppm"].''; 76 | 77 | echo ''; 78 | } 79 | ?> 80 |
IDDateTemperatureHumidityMethane Concentration
81 | 82 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import board 4 | import adafruit_dht 5 | import smbus 6 | import time 7 | import math 8 | 9 | # Get I2C bus 10 | bus = smbus.SMBus(1) 11 | 12 | # Initial the dht device, with data pin connected to: 13 | dhtDevice = adafruit_dht.DHT22(board.D18) 14 | 15 | Measure_RL = 20 16 | MQ_Sample_Time = 5 17 | Measure_RoInCleanAir = 4.4 18 | 19 | # I2C address of the device 20 | ADC121C_MQ4_DEFAULT_ADDRESS = 0x50 21 | 22 | # ADC121C_MQ4 Register Map 23 | ADC121C_MQ4_REG_CONVERSION = 0x00 # Conversion Result Register 24 | ADC121C_MQ4_REG_ALERT_STATUS = 0x01 # Alert Status Register 25 | ADC121C_MQ4_REG_CONFIG = 0x02 # Configuration Register 26 | ADC121C_MQ4_REG_LOW_LIMIT = 0x03 # Alert Low Limit Register 27 | ADC121C_MQ4_REG_HIGH_LIMIT = 0x04 # Alert High Limit Register 28 | ADC121C_MQ4_REG_HYSTERESIS = 0x05 # Alert Hysteresis Register 29 | ADC121C_MQ4_REG_LOWCONV = 0x06 # Lowest Conversion Register 30 | ADC121C_MQ4_REG_HIGHCONV = 0x07 # Highest Conversion Register 31 | 32 | # ADC121C_MQ4 Configuration Register 33 | ADC121C_MQ4_CONFIG_CYCLE_TIME_DIS = 0x00 # Automatic Conversion Mode Disabled, 0 ksps 34 | ADC121C_MQ4_CONFIG_CYCLE_TIME_32 = 0x20 # Tconvert x 32, 27 ksps 35 | ADC121C_MQ4_CONFIG_CYCLE_TIME_64 = 0x40 # Tconvert x 64, 13.5 ksps 36 | ADC121C_MQ4_CONFIG_CYCLE_TIME_128 = 0x60 # Tconvert x 128, 6.7 ksps 37 | ADC121C_MQ4_CONFIG_CYCLE_TIME_256 = 0x80 # Tconvert x 256, 3.4 ksps 38 | ADC121C_MQ4_CONFIG_CYCLE_TIME_512 = 0xA0 # Tconvert x 512, 1.7 ksps 39 | ADC121C_MQ4_CONFIG_CYCLE_TIME_1024 = 0xC0 # Tconvert x 1024, 0.9 ksps 40 | ADC121C_MQ4_CONFIG_CYCLE_TIME_2048 = 0xE0 # Tconvert x 2048, 0.4 ksps 41 | ADC121C_MQ4_CONFIG_ALERT_HOLD_CLEAR = 0x00 # Alerts will self-clear 42 | ADC121C_MQ4_CONFIG_ALERT_FLAG_NOCLEAR = 0x10 # Alerts will not self-clear 43 | ADC121C_MQ4_CONFIG_ALERT_FLAG_DIS = 0x00 # Disables alert status bit in the Conversion Result register 44 | ADC121C_MQ4_CONFIG_ALERT_FLAG_EN = 0x08 # Enables alert status bit in the Conversion Result register 45 | ADC121C_MQ4_CONFIG_ALERT_PIN_DIS = 0x00 # Disables the ALERT output pin 46 | ADC121C_MQ4_CONFIG_ALERT_PIN_EN = 0x04 # Enables the ALERT output pin 47 | ADC121C_MQ4_CONFIG_POLARITY_LOW = 0x00 # Sets the ALERT pin to active low 48 | ADC121C_MQ4_CONFIG_POLARITY_HIGH = 0x01 # Sets the ALERT pin to active high 49 | 50 | class ADC121C_MQ4(): 51 | def data_config(self): 52 | """Select the Configuration Register data from the given provided values""" 53 | DATA_CONFIG = (ADC121C_MQ4_CONFIG_CYCLE_TIME_32 | ADC121C_MQ4_CONFIG_ALERT_HOLD_CLEAR | ADC121C_MQ4_CONFIG_ALERT_FLAG_DIS) 54 | bus.write_byte_data(ADC121C_MQ4_DEFAULT_ADDRESS, ADC121C_MQ4_REG_CONFIG, DATA_CONFIG) 55 | 56 | time.sleep(0.1) 57 | """Read data back from ADC121C_MQ4_REG_CONVERSION(0x00), 2 bytes 58 | raw_adc MSB, raw_adc LSB""" 59 | data = bus.read_i2c_block_data(ADC121C_MQ4_DEFAULT_ADDRESS, ADC121C_MQ4_REG_CONVERSION, 2) 60 | 61 | # Convert the data to 12-bits 62 | self.raw_adc = (data[0] & 0x0F) * 256.0 + data[1] 63 | 64 | def measure_rsAir(self): 65 | """Calculate the sensor resistance in clean air from raw_adc""" 66 | vrl = self.raw_adc * (5.0 / 4096.0) 67 | self.rsAir = ((5.0 - vrl) / vrl) * Measure_RL 68 | 69 | def measure_Ro(self): 70 | """Calculate Rs/Ro ratio from the resistance Rs & Ro""" 71 | Measure_Ro = 0.0 72 | for i in range(0, MQ_Sample_Time): 73 | Measure_Ro += self.rsAir 74 | time.sleep(0.1) 75 | Measure_Ro = Measure_Ro / MQ_Sample_Time 76 | Measure_Ro = Measure_Ro / Measure_RoInCleanAir 77 | return Measure_Ro 78 | 79 | 80 | def measure_Rs(self): 81 | Measure_Rs = 0.0 82 | for i in range(0, MQ_Sample_Time): 83 | Measure_Rs += self.rsAir 84 | time.sleep(0.1) 85 | Measure_Rs = Measure_Rs / MQ_Sample_Time 86 | return Measure_Rs 87 | 88 | def measure_ratio(self): 89 | self.ratio = self.measure_Rs() / self.measure_Ro() 90 | print "Ratio = %.3f "%self.ratio 91 | 92 | def calculate_ppm(self): 93 | """Calculate the final concentration value""" 94 | a = -0.42 95 | b = 2.30 96 | ppm = math.exp(((math.log(self.ratio, 10)) - b) / a) 97 | 98 | return {'p' : ppm} 99 | 100 | from ADC121C_MQ4 import ADC121C_MQ4 101 | adc121c_mq4 = ADC121C_MQ4() 102 | 103 | while True : 104 | adc121c_mq4.data_config() 105 | adc121c_mq4.measure_rsAir() 106 | adc121c_mq4.measure_Rs() 107 | adc121c_mq4.measure_Ro() 108 | adc121c_mq4.measure_ratio() 109 | data = adc121c_mq4.calculate_ppm() 110 | print "Methane concentration : %.3f ppm" %(data['p']) 111 | b 112 | try: 113 | temperature_c = dhtDevice.temperature 114 | temperature_f = temperature_c * (9 / 5) + 32 115 | humidity = dhtDevice.humidity 116 | print("Temp: {:.1f} F / {:.1f} C Humidity: {}% " 117 | .format(temperature_f, temperature_c, humidity)) 118 | 119 | except RuntimeError as error: 120 | # Errors happen fairly often, DHT's are hard to read, just keep going 121 | print(error.args[0]) 122 | 123 | print " ********************************* " 124 | 125 | # In place of abcd.com please enter your website 126 | 127 | urllib2.urlopen("http://www.abcd.com/add_data.php?temperature="+temperature_c+"&humidity="+humidity+"&ppm="+data['p']).read() 128 | 129 | # It will compare the temperature and send sms via gsm module 130 | if temperature_c > 60 : 131 | subprocess.Popen("/home/pi/fire.py") 132 | 133 | # Please calibrate the sensor and set the approximate ppm value in place of 1000 134 | if ppm > 1000 : 135 | subprocess.Popen("/home/pi/gas.py") 136 | 137 | time.sleep(1) 138 | --------------------------------------------------------------------------------