[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Time:2022-5-3

Noise detection system

In addition to the function description in the following figure, please refer to step 3 in this case. After the hardware connection is completed, we suggest you use the “one minute cloud experience” function to experience the actual operation effect of this case in advance.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

brief introduction

In today’s society, noise pollution has nearly affected all aspects of people’s daily work and life. The noise pollution in the office will affect the work efficiency of employees, and the noise pollution in life will affect everyone’s sleep and emotion, thus affecting our physical and mental health.

The purpose of the noise detection system is to detect the noise of the office and living environment and give an alarm when the noise exceeds the set peak value. According to the alarm, people in the office can avoid disturbing others by reducing the voice of telephone calls and discussions, or taking the initiative to the conference room; The family can also make corresponding countermeasures, such as closing doors and windows, contacting the property or the police, etc.

This course introduces how to build an indoor noise detection and alarm system.

Noise standard

  1. Relatively quiet: 30 ~ 40 dB
  2. Affect sleep and rest: more than 50 dB
  3. Affect work efficiency: more than 70 dB
  4. Seriously affect hearing or cause other diseases: live in an environment above 90 dB for a long time

In order to detect the sound in the indoor environment, the schematic diagram of this scene is shown in the figure below. The behavior of scene design is as follows:

  1. Measure the ambient sound through the sound sensor
  2. When the sound is lower than 90 dB, lights of different colors will be lit according to different sound levels:
    • Less than 40 dB bright green
    • 50 ~ 70 dB bright blue
    • More than 70 dB bright red
  1. Turn on the buzzer when the sound is greater than 90 dB

prepare

This case only needs the following hardware:

  • One esp32
  • One mk002508 sound sensor
  • One RGB three color LED lamp
  • One buzzer
  • Some problems of DuPont line
  • One micro USB data cable

Hardware wiring diagram

The hardware wiring diagram is shown in the figure below:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Internet of things platform development

Open public instance

For readers who use the Internet of things platform for the first time, they need to open examples to use the functions of the Internet of things platform. Here you can use free public instances for development.

  inInternet of things platformSelect “East China 2 Shanghai” in the upper left corner and click “public instance” to open.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After opening the function of the Internet of things platform, you need to complete the following four steps to complete the setting of cloud devices:

  1. Create cloud products
  2. Create product attributes (object model)
  3. Create cloud devices (get triples)
  4. Multi device scenario linkage setting

Create cloud products

  click “public instance” in the above figure to enterConsoleProduct creation. Then, click the create product button, as shown in the figure below.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

On the new product setting page, as shown in the figure below, set the “product name”, select the “user-defined category”, select the node type as “direct connected device”, select the networking mode as “Wi Fi”, select the data format as “ICA standard data format”, and select the default setting for the inspection type and authentication method. You can also add a description of this product on the “product description” page according to the developer’s own needs.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After selection, click the “confirm” button to complete the product creation. After returning to the “products” page, you can see that the products of the “sound detection device” just created will appear in the product category table, as shown in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Create product attributes (object model)

Click the “view” button in the figure above to see the product information, topic list, function definition, data analysis and other settings related to the product. Click the “function definition” tab to see the equipment object model definition.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

The identifier is the identifier that needs to be used in the message that the device side reports the device attribute status, and it will be recorded by the Internet of things platform only when the attribute content reported by the device meets the data value range in the “data definition”, otherwise it will be filtered out by the Internet of things platform as an illegal attribute.

In this section, we choose to create a custom object model to create the object model information required by the system. Click the “edit draft” button in the above figure. Then, select the add custom function according to the steps in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Then select the sound decibel value, turn on the green light, turn on the red light, turn on the blue light and turn on the buzzer according to the figure below.

  • Sound decibel value

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

*Red light on:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

*Green light on:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

*Turn on the blue light:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

*On buzzer:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After the product and its object model are created, click the “publish” button and follow the system prompts step by step to publish the object model attributes just created to the product:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Create cloud devices (get triples)

Click “manage equipment” on the product list page to enter the equipment management page.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Click the “add device” button on the “device” page, as shown in the figure below.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Set “devicename” on the “add device” page, where developers can fill in the device name they want to set, or let the system automatically generate the device name without filling in anything, as shown in the figure below.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After adding the device, click the “view” button to see the details of the device.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

There are two pieces of equipment information that need to be matched with the development of the equipment end:

  1. Triplet
  2. Object model attribute information

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Get device triples

As shown in the above figure, click the “view” button to see the triple information of the device. The triple is the unique identifier associated with the IOT device and the IOT cloud device. When the device is connected to the cloud, the triple information and the cloud will be used for authentication. After authentication, the cloud will think that the device has been activated and online.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

View device attribute information

In the “object model data” tab of the equipment detail information page, you can see all attribute information of the equipment, equipment time reporting and equipment service call, as shown in the following figure. When the IOT device reports the device attribute according to the identifier corresponding to the device attribute, the attribute values such as “sound decibel value” and “light red light” in this picture will display the latest attribute information of the device.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

The process of creating products and equipment is designed according to the idea of object-oriented. Creating products can be regarded as creating a new class, in which the object model is the object of the class, and creating equipment is the instantiation of the class.

Cloud object model

In the object model of this product, there are four bool type attributes named “light red light”, “light green light”, “light blue light” and “turn on buzzer”. The true and false of these four attributes correspond to whether to light red light, green light, blue light and turn on buzzer respectively.

Multi device scenario linkage setting

Control logic can be realized in two ways:

  1. Use the rule engine function of the Internet of things platform
  2. Use the business logic function of IOT studio

Next, this section will introduce how to use the rule engine of the Internet of things platform to complete the setting of this control logic.

Follow the steps shown in the following figure to create scene linkage rules:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Click “create rule” and fill in the name of the scene rule. Here we first create the first rule of “green light when the sound is lower than 40 dB”.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After the rule is created, wait a few seconds and the system will automatically jump to the rule editing page. Here, you need to configure the following two steps to create a complete rule:

  1. When the “trigger” is set as the “sound decibel value” attribute of the sound detection device is lower than 40 dB, the execution of this rule is sent, as shown in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  1. Set the property of “execute action” and “turn on the green light for the control device”.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After clicking save, the scene linkage rules of “turn on the air conditioner when the temperature is too high” will be created.

Create another three scene linkage rules in the same way:

  • “50-70 DB light blue”, as shown in the figure below

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

*”More than 70 dB lights up red”, as shown in the figure below

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  • “Turn on the buzzer for more than 90 dB”, as shown in the figure below

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After creating these rules, click the start button behind the rules on the “scenario linkage” page, and all rules will run.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Device side development

development environment

  please ensure that the esp32 development environment has been built before proceeding to the next step. For details, please refer toEsp32 development environmentDescription of.

Create solution

Click the “quick start” button in the figure below(

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

)The welcome page of Haas studio will pop up. Please click the “create project” button.

In the following pop-up box, after setting the project name (“noise_detector”) and workspace path, select nodemcu32s as the hardware type, and click “create now” to create a solution for Python light application.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

downloadEquipment code of this caseAnd unzip it, then copy all the files and overwrite noise_ The original file in the detector project directory. Where main The contents of the PY script are shown in the following figure:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Then on main After the content in py, you need to complete the following three code modifications.

For a detailed description of Python script, please refer to the text notes embedded in the script

  1. Modify router name and password  modify noise_ In the detector project, main The values of WiFi Sid and WiFi password in py are the name and password of the router that the reader actually wants to connect to (please note that the name and password should be placed in the middle of the “” symbol).
#Wi Fi SSID and password settings

Get after modification_ wifi_ WLAN in status function The connect (WiFi SID, WiFi password) statement will connect to the router set by the reader.

  1. Modify device side triples

Modify main. In humiture project The values of productkey, devicename and devicesecret in py are the triple information of Internet of things devices created by readers, as shown in the following figure:

#Triple information
  1. Modify the identifier used by the device to report sound information

Main in humiture project The following code in py realizes the function of uploading sound size to the cloud. Sounddecibelvalue is the identifier used when reporting the sound value.

#Upload the sound volume to the Internet of things platform

Ensure that this identifier is the same as the attribute identifier in the IOT model of IOT products, as shown in the following figure:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

   The following are the key code paragraphs added in this section:

  • Four new variables are added to control the GPIO corresponding to LED and buzzer
#Led buzzer control variable
  • New functions are added to process cloud information and control GPIO interfaces
#Set props event receiving function (when the cloud platform sends attributes to the device)

Operation results

After pushing this script to esp32, run this script, and the esp32 serial port will periodically print the following logs. Of which:

  • “Internet of things platform successfully connected” means successfully connected to the Internet of things platform
  • “Uploading data:” is followed by the sound size attribute information published by the device to the cloud, where the value after sounddecibelvalue is the sum of the sound size, in decibels.
  • After the device side parses that the control instruction sent from the cloud contains “{” light_up_green_led “: 1}”, it will print “turn on the green light” and turn on the green LED
  • After the device side parses that the control instruction sent from the cloud contains “{” light_up_blue_led “: 1}”, it will print “turn on the blue light” and turn on the blue LED
  • After the device side parses that the control instruction sent from the cloud contains “{” light_up_red_led “: 1}”, it will print “light red” and light the red LED
  • After the device side parses that the control instruction sent from the cloud contains “{” switch_on_buzzer “: 1}”, it will print “turn on buzzer” and turn on the buzzer
==== python file check /data/pyamp/main. py ====

The actual effect of the development board is as follows:

  • When the sound is lower than 40 dB, you can see the words “green light on” appear in the log, accompanied by the green LED light on
  • When the sound is between 50 and 70 dB, you can see the words “turn on the blue light” in the log, accompanied by the blue LED light
  • When the sound is higher than 70 dB, you can see the words “turn on the red light” in the log, accompanied by the red LED light
  • When the humidity is higher than 90 dB, you can see the words “turn on the red light” and “turn on the buzzer” in the log. With the red LED light on, the buzzer turns on

Internet of things platform end device information viewing

After the system of the IOT device is started successfully and connected to the IOT platform, the corresponding device status on the IOT platform will change from “inactive status” to “online”, and the attribute value reported by the device to the IOT platform will be displayed on the data tab of the IOT model.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

At this time, if the sound around the development board changes, the object model data of the Internet of things platform will be updated to the latest attribute value reported by the device.

Internet of things application development

If the noise data can be displayed intuitively in the form of web pages, users can identify the law of noise by viewing the current and historical data of noise, which is helpful to draw effective control methods and countermeasures. At the same time, relevant personnel can also view the data from a long distance to obtain intuitive understanding and understanding.

Central control large screen target

The main goal of this course is to design a web application with a large master control screen, which can view the following information in real time:

  1. Real time information of sound changes in the past 3 hours
  2. Real time status display of LED lamp and buzzer
  3. Manually control the switch status of LED lamp and buzzer

Sound monitoring system

The following figure is a typical flow chart of Internet of things application development. Next, this section introduces how to develop the web application of large control screen in the flower cultivation system according to this flow.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

New “normal project”

  openIOT studio official website, create a blank project in project management, as shown in the figure below. Name this project “sound monitoring system”.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

New web app

After the “ordinary project” is created, a new web application named “real-time monitoring of indoor sound volume” is created on the front page of the new project.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After the web application is successfully created, it will enter the application interface design page.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  click the “component” button icon in the red box above to see the list of available components. Refer to for the description of each componentIOT studio component description

Page design

Here we use three components:

  • One real-time curve is used to display the historical change curve and real-time data of sound
  • Three indicators display and control the current status of the LED
  • One switch displays and controls the status of the buzzer

Drag the three components to the middle canvas area and adjust the component layout appropriately. Then drag the text component to add text notes for the component, as shown in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Associated products and devices

At this time, go back to the home page of “product monitoring” as shown in the figure below

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

The process of related products is as follows:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

The process of associated equipment is as follows:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

After the product and equipment are associated, the attributes of the component and equipment can be associated.

Associated data source

The associated data source is divided into the following three steps, and the screenshot of each step is as follows:

  • Related products
  • Associated equipment
  • Association attribute

The specific operation steps are as follows:

  1. Select the “switch” component and click “configure data source” on the right.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  1. Select target product

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  1. Select target device

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

  1. Select the on buzzer attribute

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

In the same way, select the “green light”, “blue light” and “red light” attributes of the same device for the other three “indicator” components.

Next, you need to set the data source for the “real-time curve” component. Also, after clicking the component, click the configuration data source on the right. Because the sound volume measurement and LED light / buzzer control in this scene are completed through an Internet of things device, select “single device multi attribute” in this page. Then select the product created in this scene, as shown in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Select the same device, as shown in the following figure.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

This scene displays the temperature information and humidity information of Camellia growing environment, so these two attributes should be selected in the “data item”.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Finally, select the time length of real-time temperature and humidity data to be displayed. Here, select 3 hours, as shown in the figure below.

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Release Online

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

Before publishing, you can click “Preview” in the figure above to view the actual running effect of the application. The actual operation effect is shown in the figure below:

[design reference] the noise detection system built by esp32 + Haas Python keeps away from noise and enjoys tranquility

In this way, we have completed the development of a sound monitoring system from the measurement of device sound size, device control, Internet of things cloud platform development and Internet of things application development.

Come here to explore more interesting cases

Haas development framework Haas building block scheme enables ecological developers to quickly find the solutions you need, hardware motherboards and peripherals, as well as various application components.https://haas.iot.aliyun.com/solution

Recommended Today

Why is reids fast

1. What is redis? Redis is completely open source and complies with the BSD protocol. It is a high-performance key value database. Redis is also one of the most popular NoSQL databases at present. It contains a variety of data structures, supports network, is memory based, and has an optional key value pair storage database […]