Help Center> IoT Device Access> Best Practices> Device Access> Developing a Smart Smoke Detector Using NB-IoT BearPi

Developing a Smart Smoke Detector Using NB-IoT BearPi

Scenarios

Fires have caused great loss of lives and properties each year. As more independent smoke detectors have been put to use, their limitations become obvious. For example, users cannot monitor the working status of smoke detectors in real time or receive alarm information when they are absent.

On the contrary, NB-IoT smart smoke detectors overcome their disadvantages like difficult cabling, short battery lifespan, high maintenance costs, and inability to interact with property owners and firefighting departments. These smart smoke detectors use wireless communication and feature plug-and-play, no cabling, and easy installation.

This topic describes how to build a smart smoke detector solution in just 10 minutes based on Huawei one-stop development tool platform (the IoT Link plug-in on Visual Studio Code), covering the device (BearPi development kit) and the IoT platform (Huawei IoTDA).

Development Environment

  • Development board: BearPi-IoT development suite (including the NB-IoT card, NB-IoT module, smart smoke detector module, and USB data cable)
  • Software: Visual Studio Code, the IoT Link plug-in, HUAWEI CLOUD IoTDA service, and 64-bit Windows 7 or later (64-bit Windows 10 is used in the following demonstration.)

Development Process

Hardware Connection

  1. Insert the NB-IoT card into the SIM card slot of the NB-IoT expansion board. Ensure that the notch-end faces outwards, as shown in Figure 1.

  2. Insert the smoke density collection control board and NB-IoT expansion board into the development board. Ensure they are inserted in the correct direction. Use a USB data cable to connect the development board to the computer. If the screen displays information and the power indicator is on, the development board is powered on.

Installing the IoT Link Studio Plug-in

IoT Link Studio is an integrated development environment (IDE) developed for IoT devices. It provides one-stop development capabilities, such as compilation, programming, and debugging, and supports multiple programming languages, such as C, C++, and assembly language.

  1. Obtain the operating system information. For example, on Windows 10, enter pc in the Run window, and click Properties to view the system information.

  2. Visit https://code.visualstudio.com/. Download and install a Visual Studio Code version that best suits your computer system. This section uses 64-bit Windows 10 as an example.

  3. After Visual Studio Code is installed, in its plug-in store, search for IoT Link and install it.

  4. Perform the initial startup configuration.

    When the IoT Link Studio is started for the first time, it automatically downloads the latest SDK package and GCC dependency environment. Ensure that the network is available. Do not close the window during the installation. After the installation is complete, restart the Visual Studio Code for the plug-in to take effect.

    If a proxy is required, click in the lower left corner of the Visual Studio Code home page and choose Settings > Application > Proxy, and set Use the proxy support for extensions to on.

    If the SDK fails to download due to network access failure, manually download it and decompress it to the default installation path C:\Users\Username\.iotlink\sdk. If the installation path is not the default one, decompress and copy the manually downloaded IoT_LINK to the \.iotlink\sdk directory in the Visual Studio Code installation path.

Configuring an IoT Link Studio Project

  1. Click Home on the toolbar at the bottom of Visual Studio Code.

    • Home is used to manage the IoT Link project.
    • Serial is used to enter AT commands to check the status of the development board.
    • Build is used to compile sample code (displayed after a project is created).
    • Download is used to burn the compiled code to the MCU (displayed after a project is created).

  2. On the displayed page, click Create IoT Project, enter the project name and project directory, and select the hardware platform and sample project template of the developer board.

    • Project Name: Set this parameter as required, for example, Smoke.
    • Project Path: You can use the default installation path or select a path in a disk other than the system disk, for example, D:\.
    • Platform: The demo provided applies only to the STM32L431_BearPI hardware platform. Select STM32L431_BearPI.
    • Create based on examples: In this example, select OC_smoke_template demo. Otherwise, the demo does not match the product model defined on the console and data cannot be reported. Click OK.

Compiling and Burning Code

In the provided demo, the information for connecting to the HUAWEI CLOUD IoT platform has been configured. You can directly compile code with no code modification and burn it to the development board MCU.

  1. Click Build on the toolbar at the bottom of Visual Studio Code and wait until the compilation is complete. A message is then displayed, indicating that the compilation is successful.

  2. Use a USB data cable to connect the BearPi development board to the computer. Set the dialing test switch in the upper right corner of the board to the AT-MCU mode on the right.
  3. Click Download on the toolbar at the bottom of Visual Studio Code and wait until the burning is complete. A message is then displayed, indicating that the burning is successful.

    If the burning fails, the possible cause is that the development board does not have a driver and cannot communicate with the computer through the serial port. In this case, perform 2 to check whether the ST-Link driver is installed. If the driver is not installed, download and install the ST-Link driver.

  4. (Optional) Install the ST-Link driver.

    1. Visit the ST website and download the ST-Link driver, or directly use the provided driver.zip package. Double-click the stlink_winusb_install.bat file to start automatic installation. This section uses Windows 10-64bit ST-Link 2.0.1 as an example.

      Note: You can also use an EXE file that adapts to your system version to install the ST-Link driver.

    2. Open the device manager on the computer to check whether the driver is installed. If the information shown in the following figure is displayed, the driver is installed.

Creating a Product

A product is a collection of devices with the same capabilities or features. In addition to physical devices, a product includes product information, product models (profiles), and codecs generated during IoT capability building.

  1. Log in to the IoTDA console.
  2. In the navigation pane, click Products. Click the drop-down list box above the product list, and select the resource space to which the new product belongs.
  3. Click Create Product in the upper right corner to create a product using LwM2M over CoAP. Set the parameters and click Create.

    Set Basic Info

    Resource Space

    Select the resource space to which the product belongs

    Product Name

    Enter a name, for example, BearPi_Smoke.

    Protocol

    Select LwM2M over CoAP.

    Data Type

    Select Binary.

    Manufacturer

    Enter a name, for example, BearPi.

    Define Product Model

    Product Model

    Do not select Use product model. Customize a product model in subsequent steps.

    Industry

    Select Smart City.

    Device Type

    Enter Smoke.

  4. After the product is created, click View to access its details and perform subsequent operations.

Defining a Product Model

On the Model Definition tab page, click Custom Model to add a service for the product.

Table 1 describes the service defined in the product model.

Table 1 Device service list

Service

Description

Enter Smoke.

Detects smoke density in real time.

Table 2 and Table 3 describe the service capabilities.

Table 2 Smoke

Capability Description

Property Name

Data Type

Data Range

Properties

Smoke value

int

0 - 65535

Table 3 Smoke commands

Capability Description

Command Name

Command Field

Field Name

Data Type

Data Length

Enumeration

Commands

Smoke control beep

Command delivery

beep

string

3

ON,OFF

Response field

beep_state

int

/

/

Adding the Smoke Service
  1. In the Add Service dialog box, set Service to Smoke, specify Description, and click OK.
  2. Under Properties/Commands, click Add Property, enter related information, and click OK.

  3. Under Properties/Commands, click Add Command, and enter the command name Smoke_Control_Beep.

  4. Click Add Input Parameter and Add Output Parameter respectively, enter related information, and click OK.
    Figure 1 Adding an input parameter
    Figure 2 Adding the response parameter Beep_State

Developing a Codec

  1. On the Product Details page, click Online Develop on the Codec Development tab page.
  2. Click Add Message.

    Develop the codec based on the operations provided in this section and ensure that the fields are added in the order specified in this section.

  3. Add the Smoke message. A configuration example is as follows:

    • Message Name: Enter Smoke.
    • Message Type: Select Data reporting.

    1. In the Add Message dialog box, click Add Field. In the dialog box displayed, select Tagged as address field to add the address field messageId, and click OK.

    2. Click Add Field, add the Smoke_Value field, enter related information, and click OK.
      • Field Name: Enter Smoke_Value.
      • Data Type: Select int16s.

    3. In the Add Message dialog box, click OK to complete the configuration of Smoke_Value.

  4. Add the Smoke_Control_Beep message. A configuration example is as follows:

    • Message Name: Enter Smoke_Control_Beep.
    • Message Type: Select Command delivery.
    • Add Response Field: Select this option.

    1. In the Add Message dialog box, click Add Field. In the dialog box displayed, select Tagged as address field to add the address field messageId, set Default value to 0x9, and click OK.

    2. Click Add Field. In the dialog box displayed, select Tagged as response ID field to add the address field mid, and click OK.

    3. Click Add Field. In the dialog box displayed, add the Beep field, and click OK.
      • Field Name: Enter Beep.
      • Data Type: Select string.
      • Length: Enter 3.

    4. Click Add Response Field. In the dialog box displayed, select Tagged as address field to add the address field messageId, set Default value to 0xa, and click OK.

    5. Click Add Response Field. In the dialog box displayed, select Tagged as response ID field to add the address field mid, and click OK.

    6. Click Add Response Field. In the dialog box displayed, select Tagged as command execution state field to add the address field errcode, and click OK.

    7. Click Add Response Field. In the dialog box displayed, add the Beep_State field, and click OK.
      • Field Name: Enter Beep_State.
      • Data Type: Select int8u.
      • Length: Enter 1.

    8. In the Add Message dialog box, click OK to complete the configuration of Smoke_Control_Beep.

  5. Map the property fields, command fields, and response fields in Product Model on the right with the fields in the data reporting message, command delivery message, and command response.

  6. Click Save and then Deploy to deploy the codec on the platform.

Registering a Device

This section describes how to register a non-secure NB-IoT module.

  1. On the product details page, click Add Test Device on the Online Debugging tab page
  2. In the Add Test Device dialog box, select Physical device, and enter the device name and node ID.

    • Device Name: Customize a name, for example, Test.
    • Node ID: Enter the IMEI of the device. The node ID will be carried by the device for device access authentication. You can view the node ID on the NB-IoT module. You can also set the dialing test switch to the AT-PC mode, select the STM port, set the baud rate to 9600, and run the AT+CGSN=1 command to obtain the IMEI. Copy the IMEI to the console.

      Note: After obtaining the IMEI and registering the device, set the dialing test switch of the development board to the AT-MCU mode because the development board connects to the network through the NB-IoT card only in MCU mode.

    • Registration Mode: Select Unencrypted.

Reporting Data

After the development board is connected to the platform, it reports the smoke density.

  1. Log in to the IoTDA console. In the navigation pane, choose Devices > All Devices.
  2. Select the device registered and click View to view the data reported to the platform.

Delivering a Command

  1. Log in to the IoTDA console. In the navigation pane, click Products. On the page displayed, select the created product, and click View to access its details.
  2. On the Online Debugging tab page, click the registered device to access the debugging page.
  3. After setting the command parameters, click Send. The smoke detector buzzes. After you send the OFF command, the smoke detector stops buzzing.

(Optional) Configuring a Device Linkage Rule

  1. In the navigation pane, choose Rules > Device Linkage, and click Create Rule in the upper right corner.
  2. Create a device linkage rule based on the table below.

    Parameter

    Description

    Rule Name

    Specify the name of the rule to create, for example, Smoke_Beep.

    Activate Now

    Select Activate Now.

    When Effective

    Select Always effective.

    Description

    Enter a description of the rule, for example, "The smoke detector buzzes when the smoke density is higher than 600."

    Set Trigger Conditions

    1. Click Add Condition.
    2. Select Triggered upon specified device and select the device added in 2.
    3. Select Smoke for Service Type, Smoke_Value for Property, > for Operation, and 600 for Value. Click Trigger Mode. In the dialog box displayed, set Trigger Suppression to Yes and Data Validity Period (s) to 300, click OK.

    Set Actions

    1. Click Add Action.
    2. Select Deliver Commands, and select the device created in 2.
    3. Select Smoke for Service Type, and Smoke_Control_Beep for Command. Click Configure Parameter. In the dialog box displayed, set Beap to ON, and click OK.

(Optional) Verification

Spray cooling agents around the smoke detector to simulate the smoke density. When the smoke density is greater than 600, the smoke detector buzzes.

Locating Module Communication Problems Using AT Commands

When IoT Link is connected to the platform, you can use AT commands to quickly locate the connectivity problem between the module and the cloud. This section describes how to use AT commands to detect common problems of the communication module, for example, the device fails to go online or data fails to report.

  1. Connect the BearPi development board to the computer and ensure that the driver has been installed. Set the dialing test switch in the upper right corner of the board to the AT-PC mode.
  2. Click Serial on the toolbar at the bottom of Visual Studio Code.

  3. Select the port obtained in 2, set the baud rate to 9600, and click Open.

  4. Enter AT+CGATT? and click Send. If +CGATT:1 is returned, the network attach is successful, indicating that the NB-IoT network is normal. If +CGATT:0 is returned, the network attach fails, indicating that the NB-IoT network is abnormal. In this case, check whether the SIM card is correctly inserted or contact the carrier to check the network status.

    After using the AT commands to detect the module communication, set the dialing test switch to the AT-MCU mode so that the collected data can be sent to the platform through the communication module after the console configuration.

    In the AT-PC mode, the development board communicates with the serial port of the computer, and AT commands are used to read and write data such as the status of the development board. In the AT-MCU mode, the development board connects to the network through the SIM card inserted into the module to implement NB-IoT communications.t