Developing a Smart Street Lamp Using NB-IoT BearPi
Scenario
Smart street lamps play an important role in the intelligent transformation of city roads. They save energy in public lighting, reduce traffic accidents caused by poor lighting, and contribute to many other aspects in our community. As a common public facility, street lamps can well exemplify how intelligence is transforming the world and implemented in our daily lives.
This topic describes how to build a smart street lamp 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). A smart street lamp detects and reports the illumination intensity to the IoTDA console. The LED light switch can be remotely controled on the IoTDA console.

Development Environment
- Hardware: BearPi-IoT development kit (including the NB-IoT card, NB-IoT module, smart street lamp function module, and a USB data cable), which you can purchase from the HUAWEI CLOUD Marketplace.
- 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
The following figure shows the end-to-end process of developing a smart street lamp.

In this scenario, a device interacts with the platform using LwM2M (NB-IoT card). The application displays property changes of the device and delivers commands to the device.

Introduction to the BearPi Development Board
The development board is a sensing device in the IoT architecture. This type of device usually includes a sensor, communications module, chip, and operating system. To improve scalability of the development board, the BearPi development board does not use a conventional onboard design. Instead, it uses replaceable sensor and communications module expansion boards. The communications module is an entrance and exit of data transmission. Common communications modules include NB-IoT, Wi-Fi, and 4G ones. A chip controls a device. The development board has a built-in low-power STM32L431 chip as the main control chip (MCU). The operating system is Huawei LiteOS, which provides various device-cloud interworking components.
To facilitate development and debugging, the development board uses the onboard ST-Link of the 2.1 version, as shown in Figure 1. It provides functions such as online debugging and programming, drag-and-drop download, and virtual serial port. An LCD screen with a resolution of 240 x 240 is installed at the center of the board to display sensor data and other debug logs. Below the LCD screen is the MCU.
There is a DIP switch in the upper right corner of the development board. When you set the switch to the AT-PC mode, use the serial port assistant on the computer to send AT commands to debug the communication module. When you set it to the AT-MCU mode, use the MCU to send AT commands to interact with the communication module and sends the collected sensor data to the cloud through the communication module.
Hardware Connection
- 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.
- Insert the photosensitive sensor 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 development languages, such as C, C++, and assembly language.
- Obtain the operating system information. For example, on Windows 10, enter pc in the Run window, and click Properties to view the system information.

- 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.

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

- 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 , 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
- Click Home on the toolbar at the bottom of the 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 the sample code (displayed after Step 2).
- Download is used to hard code to the MCU (displayed after Step 2).

- On the displayed page, click Create IoT Project. Enter the project name and project path, and select the hardware platform of the development board and a sample project template.


- Project Name: Enter a project name, for example, QuickStart.
- 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: Currently, the demo applies only to the STM32L431_BearPI hardware platform. Select STM32L431_BearPi.
- Create based on examples: In this example, select OC_streetlight_template. Otherwise, the programmed demo does not match the product model defined on the console and data cannot be reported. If you need to adapt to other scenarios such as smart smoke sensors, select the OC_smoke_template demo.
- Click OK.
Compiling and Programming 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 requirement on code modification and program it to the development board MCU to save the development time.
- Click Build on the toolbar at the bottom of the Visual Studio Code and wait until the system compilation is complete. A message is then displayed, indicating that the compilation is successful.

- 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.
- Click Download on the toolbar at the bottom of the Visual Studio Code and wait until the system programming is complete. A message is then displayed, indicating that the programming is successful.
If the programming 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 by following 4.a.
- (Optional) Install the ST-Link driver.
- 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: Alternatively, you can use an EXE file that adapts to your system version to install the ST-Link driver.
- 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.
- 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.
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 to improve the development efficiency. This section describes how to use AT commands to detect common problems of the communications module, for example, the device fails to go online or data fails to be reported.
- 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.
- Click Serial on the toolbar at the bottom of the Visual Studio Code.

- Select the port number obtained in 2. Set Baudrate to 9600, and click Open.

- 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 command to detect the module communication, set the dialing test switch to AT-MCU mode so that the collected sensor data can be sent to the platform through the communication module after the console configuration.
In 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 MCU mode, the development board connects to the network through the SIM card inserted into the module to implement NB-IoT communication.
- The AT+CSQ<CR> command is used to check the network signal strength and SIM card status. Enter AT+CSQ<CR> and click Send. +CSQ:**,## is returned. In the preceding output, ** ranges from 10 to 31. A larger value indicates better signal quality. ## indicates the bit error rate, which ranges from 0 to 99. If the returned values are not within these ranges, check whether the antenna or SIM card is correctly installed.
Note: This section lists only two common AT commands for detecting the network status of the module. For more AT commands, see the instructions of the BearPi module.
Operations on the Console
After connecting the physical device and compiling and programming code, go to the IoTDA console to create a product, define a product model, develop a codec, and register the device.
- Create a product: Specify the protocol type, data format, manufacturer name, and device type of a product on the platform. In this example, create a smart street lamp product on the console based on the product features.
- Define a product model: A product model is a JSON file that describes device capabilities. It defines basic device properties and message formats for data reporting and command delivery. Defining a product model is to construct an abstract model of a device in the platform to enable the platform to understand the device properties. In this example, define a street lamp product model with light switch control, illumination intensity, and signal quality properties on the console.
- Develop a codec: A codec is called by the platform to convert data between the binary and JSON formats. The binary data reported by a device is decoded into the JSON format for the application to read, and the commands delivered by the application are encoded into the binary format for the device to understand and execute. Since the data format of smart street lamps is binary, a codec is needed to enable the platform to understand the data reported by the smart street lamp and to enable the smart street lamp to understand the commands delivered by the platform.
- Register the device: Register the BearPi smart street lamp with the platform.
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 generated during IoT capability building, a product model (profile), and a codec. In this example, create a smart street lamp product on the IoTDA console.
- Log in to the IoTDA console.
- On the navigation pane, click Products. Click the drop-down list box in the upper right corner, and select the resource space to which the new product belongs.
- Click Create Product in the upper right corner to create a product using CoAP. Set parameters and click Create.
Basic Information
Resource Space
Select the resource space to which the product belongs
Product Name
Enter a name, for example, BearPi_StreetLight.
Protocol
Select LwM2M over CoAP.
Data Type
Select Binary.
Manufacturer
Enter a name, for example, BearPi.
Define Product Model
Product Model
In this example, do not select this option. Instead, upload a developed product model later.
Industry
Select Smart city.
Device Type
Enter StreetLight.
- After the product is created, click View to access its details and perform subsequent operations.

Uploading a Product Model
A product model is a JSON file that describes device capabilities. It defines basic device properties and message formats for data reporting and command delivery. Defining a product model is to construct an abstract model of a device in the platform to enable the platform to understand the device function. A developed product model is provided for you to quickly experience the cloud migration process. If you want to go through the process of developing a product model, go to #EN-US_TOPIC_0184855964__li162991326191410.
Procedure
- On the Model Definition tab page of the product details page, click Import Local Profile.
- On the dialogue box displayed, upload the product model provided and click OK.

Developing a Codec
In the previous step, we have defined the functions of the product on the console, including the properties reported by the device and the commands that the device can understand. Since the data format of smart street lamps is binary, a codec is needed to enable the platform to understand the data reported by the smart street lamp and enable the smart street lamp to understand the commands delivered by the platform.
This section describes how to import codec offline to help you quickly experience the cloud migration process. For details about how to develop a codec, see #EN-US_TOPIC_0184855964__li957317871510.
- On the product details page, click the Codec Development tab page. Click Upload Codec, and upload the codec provided.

- Click OK.

Registering a Device
This section describes how to register a device integrated with the NB-IoT module, the BearPi smart street lamp in this example, to the platform.
- On the product details page, choose Online Debugging > Add Test Device to add an insecure NB-IoT device.
- In the dialog box displayed, set the parameters and click OK.

- Device Name: Customize a name.
- Node ID: Enter the IMEI of the device. The node ID will be carried by the device for 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.
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.
- The device is created. You can view the created device on the console.

Data Reporting
After the connection between the platform and the development board is set up, the BearPi smart street lamp reports the light sensor data every 2 seconds according to the code burnt to the development board. The reporting frequency can be customized in the demo based on service requirements. You can block the light with your hand to change the light intensity and view the real-time change of the light intensity data reported to the platform.
Note: Ensure that the dialing test switch of the development board is set to the AT-MCU mode.
- Log in to the IoTDA console. Choose .
- Select the device registered and click View to view the data reported to the platform.

Command Delivery
- Log in to the IoTDA console. Select the product created and click View to go to the product details page.
- On the Online Debugging tab page, click the device registered to access the debugging page.
- After setting the command parameters, click Send.
- The light on the BearPi board is on. Deliver the OFF command. The light is turned off.

This is the end-to-end development of a smart street lamp by using the NB-IoT BearPi development board.
Reference
- Developing a Product Model
On the Model Definition tab page of the product details page, click Custom Model to add services of the product.
Table 1 describes the service defined in the product model.
Table 1 Device service list ServiceID
Service Description
Button
Real-time button detection
LED
LED light control
Sensor
Real-time light intensity detection
Connectivity
Real-time signal quality detection
The following table lists the service capabilities.
Table 2 Button Capability Description
Attribute Name
Data Type
Data Range
Attribute list
toggle
int
0 - 65535
Table 3 LED command list Capability Description
Command Name
Command Parameter
Parameter Name
Type
Data Length
Enumeration
Command list
Set_Led
Command
led
string
3
ON,OFF
Response
light_state
string
3
ON,OFF
Table 4 Sensor Capability Description
Attribute Name
Data Type
Data Range
Attribute list
luminance
int
0 - 65535
Table 5 Connectivity Capability Description
Attribute Name
Data Type
Data Range
Attribute list
SignalPower
int
-140 - -44
ECL
int
0 - 2
SNR
int
-20 - 30
CellID
int
0 - 65535
- Adding the Button Service
- On the Model Definition tab page, click Add Service. Set Service to Button, specify Description, and click OK.
- Under Properties/Commands, click Add Property, enter related information, and click OK.

- Adding the LED Service
- On the Model Definition tab page, click Add Service. Set Service to LED. Specify Description, and click OK.
- Under Properties/Commands, click Add Command, and enter the command name Set_Led.

- Click Add Input Parameter and Add Output Parameter respectively, enter related information, and click OK. Figure 2 Adding the input parameter led
Figure 3 Adding the output parameter light_state
- Adding the Sensor Service
- On the Model Definition tab page, click Add Service. Set Service to Sensor. Specify Description, and click OK.
- Under Properties/Commands, click Add Property, enter related information, and click OK.

- Adding the Connectivity Service
- On the Model Definition tab page, click Add Service. Set Service to Connectivity. Specify Description, and click OK.
- Under Properties/Commands, click Add Property to add SignalPower, ECL, SNR, and CellID, respectively. Enter related information, and click OK. Figure 4 Adding property SignalPower
Figure 5 Adding property ECL
Figure 6 Adding property SNR
Figure 7 Adding property CellID
- Adding the Button Service
- Developing a Codec
- On the Product Details page, click Online Develop on the Codec Development tab page.
- 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.

- Add the Report_Connectivity message. A configuration example is as follows:
- Message Name: Enter Report_Connectivity.
- Message Type: Select Data reporting.
- Add Response Field: Select this option.
- Response: Retain the default value AAAA0000.

- 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.

- Click Add Field, add the SignalPower field, enter related information, and click OK.
- Field Name: Enter SignalPower.
- Data Type: Select int16s, which means a 16-bit signed integer.

- Click Add Field, add the ECL field, enter related information, and click OK.
- Field Name: Enter ECL.
- Data Type: Select int16s, which means a 16-bit signed integer.

- Click Add Field, add the SNR field, enter related information, and click OK.
- Field Name: Enter SNR.
- Data Type: Select int16s, which means a 16-bit signed integer.

- Click Add Field, add the CellID field, enter related information, and click OK.
- Field Name: Enter CellID.
- Data Type: Select int32s, which means a 32-bit signed integer.

In the Add Message dialog box, click OK to complete the configuration of Report_Connectivity.
- Add the Report_Toggle message. A configuration example is as follows:
- Message Name: Enter Report_Toggle.
- Message Type: Select Data reporting.
- Add Response Field: Select this option.
- Response: Retain the default value AAAA0000.

- 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.

- Click Add Field, add the toggle field, enter related information, and click OK.
- Field Name: Enter toggle.
- Data Type: Select int16u, which means a 16-bit unsigned integer.

In the Add Message dialog box, click OK to complete the configuration of Report_Toggle.
- Add the Report_Sensor message. A configuration example is as follows:
- Message Name: Enter Report_Sensor.
- Message Type: Select Data reporting.

- 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.

- Click Add Field, add the data field, enter related information, and click OK.
- Field Name: Enter data.
- Data Type: Select int16u, which means a 16-bit unsigned integer.

In the Add Message dialog box, click OK to complete the configuration of Report_Sensor.
- Add the Set_Led message. A configuration example is as follows:
- Message Name: Enter Set_Led.
- Message Type: Select Command delivery.
- Add Response Field: Select this option.

- 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.

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

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

- In the Add Message dialog box, click Add Response Field. In the dialog box displayed, select Tagged as address field to add the address field messageId, and click OK.
- Click Add Response Field. In the dialog box displayed, select Tagged as response ID field, and click OK.
- 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.

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

In the Add Message dialog box, click OK to complete the configuration of Set_Led.
- 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.




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

Last Article: Device Access
Next Article: Developing a Smart Smoke Detector Using NB-IoT BearPi



Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.