Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MQ-2 smoke alarm based on Raspberry Pi platform

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. preliminary preparation

Achieve the goal:

   uses Rapberry Pi to drive MQ-2 smoke alarm module to collect and extract information.

1. Prepare a raspberry Raspberry Pi

2.MQ-2 harmful gas detection module

3. Some DuPont lines

A 4.SD card and a card reader

2. Introduction of each device

1. Raspberry pie (Raspberry Pi)

   Raspberry Pi is a British small organization charity The Raspberry Pi Foundation issued a mini computer for computer amateurs, students, small businesses and other users, pre-installed with the Linux operating system, the size is very small, in the latest model of Raspberry Pi 3 Model B, with Qualcomm's ARMv8 high-performance CUP, and includes 1GB RAM, the number of pins increased to 40.

Advantages over the previous generation: 1. First of all, the processor is a new generation of quad-core Broadcom BCM2837 64-bit ARMv8 processor, and the processor speed can reach up to 1.2GHz, and it can overclock if necessary. two。 The second is the addition of an on-board BCM43143 WiFi chip without the need for a WiFi network adapter. Raspberry Pi official website: https://www.raspberrypi.org/

2.MQ-2 harmful gas detection module

   is a gas leak detection device widely used in homes and factories, which is suitable for the detection of harmful gases such as liquefied gas, methane, propane, butane, alcohol, hydrogen, smoke, etc.

There are four pins:

VCC: input 5V positive current

GND: connect the negative pole of the power supply

DO:TTL high and low level output terminal

AO: analog voltage output TTL output sensitivity adjustment:

This potentiometer only adjusts the output sensitivity of TTL, that is, DO.

The sensitivity of clockwise adjustment increased and that of counterclockwise adjustment decreased. It has the following advantages:

    's wide range of detection

Highly sensitive and rapid corresponding recovery of    

    has excellent stability and life growth.

    simple drive circuit    

III. Raspberry Pi deployment operating system

1. Overview

The vast majority of     open source Linux communities have operating systems based on raspberry pie Arm architecture, such as Debian-based Raspbian system and Ubuntu mate system, which are the two most supportive systems for raspberry pie, and also have mirror sources in China, which is probably the most important for a development board.

    I personally strongly recommend the Raspbian system, its own command is actually the same as the server-side Debian operation command, but the greater advantage is: there are many well-known university websites in China have Raspbian system mirror source, which is very convenient Because at first I was playing with the Centos operating system, I am familiar with commands and other configuration methods, and there is also a system image based on Arm architecture developed specifically for Raspberry Pi on the official website of Centos, but after installation, you will find that the image source is very inconvenient.

two。 Deploy Raspbian system

    first, download the system image file on the official website I posted earlier, click DOWNLOADS, and see that there are two kinds of systems: "NOOBS" and "Raspbian". I chose Raspbian, and then I will see two versions of the image, of which "DESKTOP" on the left is the operating system with desktop, on the contrary, the one on the right is to minimize the installed system image. It is recommended that if you can easily get the monitor around you, you can access it through HDML. Then choose the operating system with the desktop, because my main purpose here is to complete the project, so I don't choose the image with the desktop system.

Screenshot of the official system image interface of    :

3. Firing system

After    download, through a software "Win32DiskImager", to burn the system, select the external devices displayed by the card reader in Device, Image File select the image file that has just been downloaded and decompressed, and then click Write to write, because I am a minimized mirror system, it only takes a few minutes to complete the system burning, you wait a moment, it will be finished.

Screenshot of    burning software:

Note: generally speaking, the Raspbian system does not have the SSH remote connection function, so to enable the function, you need to enter the recorded mirror system.

In the file (through the card reader), create a new blank text file named "ssh" without changing the suffix, and then you can proceed to the next step.

   plugs in the network cable, knows the IP assigned to the starting Raspbian through the router, and then logs in remotely through SSH.

It should be noted here that the default user is "pi", the password is "raspberry", and the SSH remote connection is "ssh pi@192.168.1.1". Just enter the password.

   below is a screenshot of a pi user connecting to raspberry pie:

4. Change the default login user

In general,    uses the operating system as a root user, which makes it more powerful and convenient for ordinary users to do things that are difficult for ordinary users to do. According to the following:

First switch the root user and activate the password configuration option for the root user:

Pi@raspberrypi:~ $sudo passwd root

Enter new UNIX password:

Retype new UNIX password:

Passwd: password updated successfully

Then unlock the root user:

Pi@raspberrypi:~ $sudo passwd-- unlock root

Passwd: password expiry information changed.

Finally, switch root users:

Pi@raspberrypi:~ $su root

Password:

However, because the Raspbian system defaults to logging in to raspberry pie as a "pi" user, that is, even if the root user is activated above, it will not be able to log in through "ssh root@192.168.1.1", so the following operations are needed.

Access to the 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

The following figure of    shows the screenshot after changing the configuration:

5. Configure Mirror sourc

   configures the mirror source to facilitate the deployment of all kinds of software and solve the dependency.

Fortunately, the official list of software images is given:

   http://www.raspbian.org/RaspbianMirrors

   provides Raspbian software images on the websites of universities or educational institutions around the world, which is very convenient for fans who like to play raspberry pie. I have excerpted the image addresses provided by several universities for your reference:

Http://mirror.sysu.edu.cn/raspbian/raspbian Sun Yat-sen University Image Source (Southern users)

Http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ Mirror Source of Tsinghua University (northern users)

Http://mirrors.ustc.edu.cn/raspbian/raspbian/ University of Science and Technology Image Source (Central user)

Http://mirrors.cqu.edu.cn/Raspbian/raspbian/ Chongqing University Image (Midwest users)

   because I am relatively close to Beijing, so I chose the mirror source of Tsinghua University, which is fast and stable.

Enter the file: vim / etc/apt/sources.list

Comment on other statements, and add the following statement:

Deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian stretch main contrib non-free rpi

It should be noted that the source on the configuration is followed by a "stretch" field, which represents the ninth generation Debian system, and the other fields are: the seventh generation system "wheezy" and the eighth generation system "Jessie". Because the official website of Raspberry Pi generally downloads the latest system, and this generation system greatly expands and gives full play to Raspberry performance, it is recommended to install this version of the system.

   can see the version of the system through the command: "lsb_release-a", so be sure to check the system version before configuring the image file.

This is my system version:

6. Configure Infinite routing (WiFi)

   Raspberry has a built-in WiFi device, so you only need to write the WiFi name and password in the network configuration file, and every time you start the machine, you will read the file and automatically connect to WiFi.

Find the file and write: vim / etc/wpa_supplicant/wpa_supplicant.conf

Network= {

Ssid= "WiFi name"

Psk= "WiFi password"

}

7. Keyboard layout configuration

   because the Raspberry is of UK origin, so the keyboard layout is British, and many keys are different, so you need to change the keyboard layout. First, perform the following two actions:

Install the input method configuration before you can change the keyboard layout:

Apt install fcitx

Then enter:

Raspi-config

Enter the pseudo-graphical configuration interface, select the configuration "keyboard layout", select the US keyboard layout, save and exit, and restart.

IV. Deploy and apply GPIO libraries

1. Deploy Pip and GPIO

   in the Raspbian system, for Raspberry enthusiasts to customize their own board, so the choice of native support today's most mainstream programming language Python, whether Python2, or Python3 are installed, in order to facilitate the installation of GPIO library, it is strongly recommended to install pip tool, this tool is similar to apt and yum, can automatically solve the dependency when installing third-party libraries, very easy to use.

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

Install pip:

   apt install pip3

Note: if there is a problem and indicates that it cannot be installed, use the following command:

Apt-get install * pip-y

Install the GPIO module through 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 none of the above commands reported an error, and press the Table key to complete all built-in methods

Special note: why not install RPi.GPIO???

The RPi.GPIO library is a library that I saw on the official website of Raspberry that supports reading the status of "pip install RPi.GPIO O", but I executed the installation command according to many blogs on the Internet: "pip install RPi.GPIO", always reporting an error, or there will be an error when the module "import" is installed successfully, which is very difficult to understand. I thought it was the problem of the version, and I found that I installed the latest version, but I could not always bring up the library. I therefore do not have any good solution, accidentally found that the direct execution of the command, the installation of "pip installgpio" seems to have installed another version of the GPIO library, and can be called normally, I later did a test on the Centos7 virtual machine and found that it was possible, so I chose to use "gpio" instead of "RPi.GPIO".

The following is a screenshot of all available methods:

two。 Common usage of GPIO

The following is a brief introduction to the more commonly used GPIO usage:

1. Import GPIO module import RPi.GPIO as GPIO 2. Set the GPIO pin to BOARD mode GPIO.setmode (GPIO.BOARD) 3. Set the GPIO pin channel as the input GPIO.setup (pin,GPIO.IN) 4. The setting is initialized to high level GPIO.setup (pin,GPIO.OUT,initial=GPIO.HIGH) 5. Software implementation up / down: GPIO.setup (pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup (pin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) 6. Edge detection, this function is to listen to a pin GPIO.add_event_detect (channel, GPIO.RISING) 7. Clear all resources GPIO.cleanup ()

The pin diagram of Raspberry Pi is shown below:

There is a point on   : "GPIO pins are set to BOARD mode." there is another way to define pins. In RPi.GPIO, both GPIO pin numbers on raspberry pie are supported. The first number is the BOARD number, which corresponds to the physical pin number on the raspberry pie board. The advantage of using this number is that the hardware will always be available without having to worry about the version of the raspberry pie. There is no need to rewrite code when replacing a version or system.

The second numbering of    is the BCM rule, which works at a lower level, which corresponds to the channel number in Broadcom's system-on-chip system. When using a pin, the user needs to find the corresponding rule between the channel number and the physical pin number. For different versions of raspberry pie, the script file may not be universal.

For example, the "GPIO 16" and "36" pins in the image above refer to the same one, while the former is defined by BCM and the latter is defined by "BOARD".

V. Application practice

The connection between 1.Raspberry Pi and MQ-2

What    needs to know here is that the GPIO library of Python can only accept changes in the level of pins, so you only need to connect three pins: 5V power supply, ground wire, and DO interface. I choose to define the pin with the "BOARD" rule, that is, the No. 36 pin is connected to the DO port of MQ-2. Carefully correspond to the pin diagram given above. Here is the picture after I have connected it.

   MQ-2 connection:

   Raspberry connection:

two。 Module testing

   then starts to write Python code to test the connection and whether the function of the module is intact. According to several GPIO library methods and simple logic introduced above, write the following code:

#! / usr/bin/env python3import RPi.GPIO as GPIO # imports the library and sets the alias import timeCHANNEL=36 # to determine the pin port. GPIO.setmode (GPIO.BOARD) # Select the pin system according to the real position. Here we select the BOARDGPIO.setup (CHANNEL,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # initialization pin and set pin 36 as the input pull-down resistor, because the lead level is uncertain during initialization, so this setting is used to ensure accuracy. (but you can not write "pull_up_down=GPIO.PUD_DOWN") # main program with exception handling try: while True: # execute a while dead loop status=GPIO.input (CHANNEL) # detect the input high and low level status of pin 36 # print (status) # Real-time print the level state if status= = True: # if it is high Indicate that MQ-2 is normal, and print "OK" print ("normal") else: # if it is low, MQ-2 detects harmful gases And print "dangerous" print ('dangerous gas detected!') time.sleep (0.1) # sleep for 0.1 seconds, and then perform while loop except KeyboardInterrupt: # exception handling. When the keyboard Ctrl+C is detected, the > script GPIO.cleanup () # starts the execution program after cleaning up the remnants of the run:

\ # chmod + x test.py\ #. / test.py

   will then jump out of the word "OK" every 0.1 seconds, and when it reaches the MQ-2 alarm through the lighter, it will pop up a "DANGEROUS", indicating that harmful gases have been detected. Here is a picture of my experimental results:

   shows that the module is working properly and can detect harmful gases, and there is basically nothing wrong with this program. If you need to stop later, just press "Ctrl+C" to stop the script.

Be careful

   in Python2, because of syntax changes, when there is Chinese in the script, add "# encoding=utf-8" at the beginning; and the "print ()" method has no parentheses, only quotation marks, and exception handling may be followed by different syntax, which requires the reader's attention.

   actually combines Zabbix for data monitoring and statistics, so there is no need for output, so here is just for the effect of intuitive, and add the "print ()" statement, in order to achieve the purpose, I will put it in the Cron planning task, in a boot time, start to execute this program in the background, and there is no output, but will record the level of the pin in real time.

   as to how to get this value and how to store it, I will study later, here may encounter the problem of secondary development of Zabbix.

I will finish the     -Zabbix project in the next few weeks, to be continued.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report