MQ-2 Smoke alarm system based on Raspberry Pi (Raspberry) platform and implementation of combined Zabbix monitoring (I.)

Source: Internet
Author: User

First, pre-preparation




Reach the goal:

Using Rapberry Pi to drive MQ-2 smoke Alarm module, the information is collected and extracted, and then Zabbix monitoring system to collect and process information collected.



1. Prepare the Raspberry Pi (Raspberry Pi) One

2.MQ-2 Harmful gas detection module

3. A number of DuPont lines

4.SD card and one card reader




Second, the introduction of each device


1. Raspberry Pi (Raspberry)


?? Raspberry Pi is a small British organization charity organization, the Raspberry Pi Foundation, a mini computer for computer amateurs, students, and small business users, preinstalled with the Linux operating system, very small, In the latest Raspberry Pi 3 Model B, the high-performance cup of the Qualcomm ARMv8 is equipped with 1GB RAM and the pin count up to 40.

    较上一代的优点:              1.首先处理器是新一代四核心Broadcom BCM2837 64位ARMv8 处理器,并且处理器速度最高可达1.2GHz,必要时还可以超频。</font>              2.第二是新添加了板载BCM43143 WiFi芯片,无需WiFi网络适配器。    


2.MQ-2 Hazardous gas Detection module


?? It is a gas leak detection device widely used in homes and factories for the detection of liquefied gas, methane, propane, butane, alcohol, hydrogen, smoke and other harmful gases.

  • There are four pins:
    VCC: input 5V positive current
    GND: Connect the power supply negative
    Do:ttl high-low level output
    AO: Analog Voltage output
  • TTL Output sensitivity adjustment:
    This potentiometer is only tuned for TTL, which is the do output sensitivity.
    The clockwise adjustment sensitivity increases, the counter-clockwise adjustment sensitivity reduces.
  • Has the following advantages:
    ??? Wide range of Detection
    ??? Highly sensitive, fast and appropriate recovery
    ??? Excellent stability, lifetime growth
    ??? Simple Drive Circuit???





Third, Raspberry PI deployment operating system


1. Overview


?? The vast majority of open-source Linux communities have operating systems for the Raspberry Pi arm architecture, such as the Debian-based Raspbian system and Ubuntu Mate system, which is the two most supportive system for Raspberry Pi, and also has a mirror source in the country, for a Development Board, This is probably the most important.
I personally strongly recommend the Raspbian system, which itself commands the same as the server side of the Debian Operation Command, but the greater advantage is: Many well-known domestic university sites have Raspbian system image source, it is convenient Because I started playing with the CentOS series of operating systems, more familiar with the command and other configuration methods, and the official CentOS site also has a dedicated Raspberry Pi-based system image of the arm architecture, but after installation you will find that the image source is very inconvenient 0.0.

2. Deploying the Raspbian System


? ? First, to the front I posted to the official online download system image files, point into the downloads, see there are two systems: "NOOBS" and "Raspbian", I chose Raspbian, and then see two versions of the image, which has "DESKTOP" on the Left is the operating system with the desktop, the opposite is the smallest installation of the system image, it is recommended that if you can easily get the monitor, to access through the HDML, then choose a desktop operating system, because I am here to complete the project, so do not choose the mirror with the desktop system.


? ? The official System mirroring interface:




3. Firing system


?? After downloading, through a software "Win32diskimager", to burn the system, in the device Select Reader display external device, image file Select just download, and extract the completed image files, and then click on Write, write operation, Because I am the smallest mirror system, so it takes only a few minutes to complete the system burning, you wait a moment, it is finished.


?? Burn software:



Note: In general, the Raspbian system does not have the SSH remote connection function, therefore wants to turn on the function, needs to enter the burning good mirror system
File (via card reader), create a blank text file called "ssh", do not need to change the suffix, then you can proceed to the next step


?? Plug in the network cable, through the router can know the IP assigned to start Raspbian, and then telnet through SSH.

Note here that the default user is "PI", the password is "raspberry", ssh remote connection "ssh [email protected]", enter the password can be


?? Following is a PI user connected to the Raspberry Pi:





4. Change the default login user


?? In general, we are the root of the user's identity to the operating system, so the right to greater, but also more convenient to do some ordinary users are difficult to do things, according to the following:

    • First switch the root user to activate the root user's password configuration option:

      [Email protected]:~ $ sudo passwd root
      Enter New UNIX Password:
      Retype new UNIX Password:
      Passwd:password updated successfully

    • Then unlock the root user:

      [Email protected]:~ $ sudo passwd--unlock root
      Passwd:password expiry information changed.

    • Finally, switch the root user:

      [Email protected]:~ $ su root
      Password:

    • However, since the Raspbian system defaults to "PI" user login to the Raspberry Pi, that is, even if the root user activated above, also cannot pass: "ssh [email protected]" to log in, so also need to do the following.

Enter file:
vim/etc/systemd/system/getty.target.wants/getty\ @tty1. Service

> found: execstart=-/sbin/agetty--noclear%I $TERM
> change to: execstart=-/sbin/agetty--autologin root--noclear%I $TERM



?? To change the configuration after:






5. Configure the image source

?? The image source is configured to facilitate the deployment of various types of software later, to resolve dependencies.
Fortunately, the official gives a list of software images:

?? http://www.raspbian.org/RaspbianMirrors

?? This includes the world's universities or educational institutions to provide the Raspbian software image, great convenience for fans who like to play the Raspberry Pi, I have an excerpt from a few universities to provide the mirror address for your reference:

Http://mirror.sysu.edu.cn/raspbian/raspbian Sun Yat-sen University Mirror Source (southern users)
http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/Tsinghua University Mirror Source (north user)
http://mirrors.ustc.edu.cn/raspbian/raspbian/Zhong ke Mirror source (central user)
http://mirrors.cqu.edu.cn/Raspbian/raspbian/Chongqing University Image (Midwest users)

?? Because I was closer to Beijing, so chose the Tsinghua Mirror source, fast and stable.

  • Access file: vim/etc/apt/sources.list
    > Note the other statements, plus the following statement:
    >deb Http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian Stretch main contrib Non-free RPI

  • Note: the source on the configuration, followed by a "Stretch" field, which represents the nineth generation of the Debian system, the other fields are: The Seventh generation System "Wheezy", Eighth generation system "Jessie", because in the raspberry PI's official website is generally download the latest system, and this generation of systems greatly expanded and played raspberry performance, so it is recommended to install this version of the system.
    ?? Through the command: "Lsb_release-a" can see the version of the system, so be sure to configure the image file, check the system version OH.



This is the version of my system:




6. Configure unlimited routing (WiFi)


?? Raspberry built-in WiFi device, so only in the network configuration file, the WiFi name and password to write in, then each boot machine will read the file, automatically connected to WiFi.

  • Find File and write: vim/etc/wpa_supplicant/wpa_supplicant.conf
    >network={
    ssid= "WiFi name"
    psk= "WiFi Password"
    }



7. Keyboard layout Configuration


?? Since Raspberry origin is British, so the keyboard layout is an English keyboard, many keys are different, so you need to change the keyboard layout, first perform the following two actions:

  • You can replace the keyboard layout by first installing the Input method configuration:
    >APT Install FCITX

  • Then enter:
    >raspi-config
    >> Enter the pseudo-graphical configuration interface, select Configure "keyboard Layout", select US keyboard layout, save and exit, restart.





Iv. deployment and application of GPIO libraries



1. Deploying Pip and Gpio


?? In the Raspbian system, in order to raspberry enthusiasts to customize their own board, so choose to natively support today's most mainstream programming language Python, whether it is Python2, or Python3 are installed, in order to facilitate the installation of Gpio library, It is highly recommended to install the PIP tool, which is similar to apt and Yum, and can automatically resolve dependencies when installing third-party libraries, which is very useful.


?? I'll use Python3 as the platform for code execution below, and here are all the Python tools included

  • Install PIP:
    ?? Apt Install PIP3
    > Note : If a problem occurs and the display fails to install, use the following command:
    >>apt-get Install *pip-y

  • To install the Gpio module via PIP:
    ?? PIP3 Install Gpio

  • Verify that the Gpio module is successfully installed and imported:
    ?? #python3
    ?? \>>>import Rpi.gpio as GPIO
    ?? \>>>gpio.setmode (GPIO). BOARD)

* found that the above command is not error, and press the table key, complete all the built-in methods




Special Note: Why not install Rpi.gpio???

        RPi.GPIO库是我在Raspberry官网上看到的支持I/O状态读取的一个库,但是我按照网上很多博客上查的,执行安装命令:“pip install RPi.GPIO”,总是报错,要么就是安装成功以后会在“import ”这个模块的时候,出现错误,很是费解,以为是版本的问题,发现自己安装的是最新版本,但总是调不起来这个库,我因此也没有什么好的解决方法,偶然发现,直接执行命令,安装“pip installgpio”貌似是安装了另一个版本的GPIO库,并且是可以正常调用的,我在之后在Centos7虚拟机上做了测试,发现也是可以的,那么我就选择用“gpio”代替“RPi.GPIO”。


The following are all available methods:




2. Common GPIO Usage



Below is a brief introduction to the more commonly used gpio usages:

1.导入GPIO模块        import RPi.GPIO as GPIO  

2.将GPIO引脚设置为BOARD模式        

3.设置GPIO引脚通道 作为输入        GPIO.setup(pin,GPIO.IN)  

4.设置初始化为高电平        

5.软件实现上/下拉:        GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)        GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

6.边缘检测,该函数是对一个引脚进行监听        

7.清除所有资源        GPIO.cleanup()    



The pin diagram for the Raspberry Pi is given below:



?? Here is a place to point out: "Gpio pin set to board mode", there is another way to define the pin, in the Rpi.gpio, while supporting the Raspberry Pi two gpio pin number. The first number is the board number, which corresponds to the physical PIN number on the Raspberry Pi board. The advantage of using this number is that the hardware will always be available without worrying about the version of the Raspberry Pi. There is no need to rewrite the code when replacing the version or system.

?? The second number is the BCM rule, which is the lower-level way of working, which corresponds to the channel number in the Broadcom system. When using a PIN, the user needs to find the corresponding rule between the channel number and the physical PIN number. For different Raspberry Pi versions, the script files you write may not be generic.
For example, the "GPIO 16" and "36" pins refer to the same, while the corresponding ones are defined by BCM, which is defined by "BOARD".




V. Application and Practice



1.Raspberry connection between Pi and MQ-2


?? What you need to know here is that Python's Gpio library can only accept changes in the high and low levels of the pin, so just wiring three pins is good: 5V power, ground, and do interface, I choose to "BOARD" rule definition pin, that is, 36th pin connection MQ-2 do port, Carefully corresponding to the above-given PIN diagram, the following is the picture I received after:


?? MQ-2 Connection:




?? Raspberry Connection:




2. Module testing


?? The next step is to write the Python code to test the wiring, and whether the module is functioning properly, according to a few Gpio library methods described above, and simple logic, write the following code:

#! /usr/bin/env python3import RPi.GPIO as GPIO # 导入库,并进行别名的设置import timeCHANNEL=36 # 确定引脚口。按照真实的位置确定GPIO.setmode(GPIO.BOARD) # 选择引脚系统,这里我们选择了BOARDGPIO.setup(CHANNEL,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)#初始化引脚,将36号引脚设置为输入下拉电阻,因为在初始化的时候不确定的的引电平,因此这样设置是用来保证精准,(但是也可以不写“pull_up_down=GPIO.PUD_DOWN”)# 带有异常处理的主程序try:         while True: # 执行一个while死循环          status=GPIO.input(CHANNEL) # 检测36号引脚口的输入高低电平状态          #print(status) # 实时打印此时的电平状态          if status == True: # 如果为高电平,说明MQ-2正常,并打印“OK”                        print ( ‘ 正常 ‘ )                 else:    # 如果为低电平,说明MQ-2检测到有害气体,并打印“dangerous”                        print ( ‘ 检测到危险气体 ! ! ! ‘ )           time.sleep(0.1) # 睡眠0.1秒,以后再执行while循环except KeyboardInterrupt: # 异常处理,当检测按下键盘的Ctrl+C,就会退出这个>脚本            GPIO.cleanup() # 清理运行完成后的残余

    • To start executing the program:
      \#chmod +x test.py\#./test.py



      ?? Then will jump out of the word "OK", every 0.1 seconds, when through the lighter to reach the MQ-2 alarm, will pop up a "dangerous", indicating the detection of harmful gas, the following map, my experimental results:



?? The description module is working properly, can detect harmful gas, and this program is basically no problem, after the need to stop, press "CTRL + C" to stop the script to operate just fine.

Attention
?? In Python2, because of grammatical changes, when the script has Chinese in the case, add "# Encoding=utf-8" at the beginning, and "Print ()" method is without parentheses, only quotation marks, and exception processing may have a syntax is not the same, it is necessary for the reader to pay attention to.



?? Actual in the back combined with Zabbix data monitoring and statistics, is not required to output, so here just for the effect of intuitive, and add in the "print ()" statement, in order to achieve the goal, I will put it in the cron scheduled tasks, in a boot, the background to start the program, And there is no output, but the high and low levels of the pins are recorded in real time.

?? For how to get to this value and how to store it, I'll look at it later, and here's a two-time development issue that you might encounter with Zabbix.








??????????????????? --------Zabbix Project Part I will be ready in the next few weeks, not to be continued ....

MQ-2 Smoke alarm system based on Raspberry Pi (Raspberry) platform and implementation with Zabbix monitoring (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.