【 Abstract 】 The project of STM32 is at the end of the article, which can be downloaded.
I. Engineering configuration
1. MCU type is STM32L431RC
2, select OpenOCD, parameter -f interface/stlink-v2-1. CFG -f target/stm32l4x.cfg
3. Select LiteOS_Lab_STM32\targets\STM32L431_BearPi\GCC\appbuild for the output directory
Select targets\STM32L431_BearPi\GCC\Makefile
5, platform parameters: LiteOS_Lab_STM32\demos\ oc_MQtt_demo \ oc_MQTT_demo_static \ oc_MQTT_static.c
6. WIFI user name and password: LiteOS_Lab_STM32\ iOT_link \network\ tcpIP \ ESP8266_socket \ ESP8266_socket_IMP
7. Create products on the Internet of Things platform and choose MQTT method
Two, part of the code analysis
There are two code changes we need to make before compiling the burn program
The first place is to open the oc_MQTT_static. c file. We need to configure the parameters required for the device to connect to Huawei cloud and Internet of Things platform.
Modify line 51 CN_MQTT_EP_NOTE_ID to the device identification code used in huawei cloud, device access service and device registration, which can be viewed in the platform.
Change CN_MQTT_EP_DEVICE_ID to the device ID generated during device registration, and CN_MQTT_EP_PASSWD to the key generated during device registration.
The device ID and key can be viewed in the TXT file saved when the device is registered. If the device is forgotten or lost, you can register a new device.
The device ID can also be viewed in the device access service, and the key can be reset in the device access service.
The second place to open esp8266_socket_IMP point H file, we need to configure the development board to connect to wifi parameters,
Change the WIFI_SSID in line 43 to the name of the wifi hotspot created by itself, and WIFI_PASSWD to the password of the hotspot, which is used to connect the development board to wifi.
In this wifi hotspot, it is suggested to turn on the hotspot of your mobile phone. The data transmission of the development board is small and it will not cost too much traffic. You can also use the wifi of the router.
However, the wifi authentication mode can only be the user name and password, instead of the wifi authentication mode with portal authentication and 802.1x authentication.
Click the compile button to compile and wait patiently for the message that the compile succeeded.
Connect the development board to the computer with USB cable, turn on the hot spot or router of the mobile phone, click the burning button, and wait patiently for the prompt that the burning is successful. If the firing fails, check whether the preceding engineering configuration steps are correct, or try to reinsert and remove the USB cable. Then log in huawei Cloud Internet of Things platform, click the lower left corner to switch the page, and you can see that the device is online at all devices, and then you can see all products on the LCD screen of the development board. F1 selects the products, F2 adds them to the shopping cart, and F2 submits the order when F1 is located at Submit. You can also cancel the order, and the reported data shopping cart is empty and the order status is 0. Then we log in to the Internet of Things platform, click the online device, we can see the reported data, click view all attributes, we can see the data details.
Key code explanation
Config. mk LiteOS SDK configuration file, you can select or switch related functions such as protocol selection: MQTT/LWM2m/COAP, or configure encrypted/unencrypted
Makefiles define a set of rules and compilation options that the compiler uses to compile and link source files
Project.mk defines all the files that need to be migrated
Hardware contains the development board’s four sensors and drivers for the LCD display
Main. c The file in which the program entry function main resides
Iot Link folder, which contains all of the Lite O S SDK functionality code. The SDK will automatically add the source code of the corresponding function to participate in the compilation according to the user’s configuration in config.mk.
Oc_mqtt_static. c Main case program file, responsible for data reporting and command delivery business logic implementation.
The implementation of business logic in oc_mqtt_static.c
Oc_mqtt_demo_main is the case entry function of the vending machine. First, the LCD screen is initialized, the background is set to black, the font is set to green, and each item is displayed in a loop.
Display Submit and Cancel options, display the current position of the select button F1, display the current number of items in the cart,
Initialize key detection tasks and create semaphores for command response, data reporting tasks, and command delivery tasks
Two tasks are equivalent to two threads, which are automatically executed after they are created.
The key_detect task, when the F1 button is pressed, sequentially adjusts the asterisk to mark the current position of the F1 button.
Oc_mqtt_report_entry is a data report task. First define the JSON data structure required for the report, then configure the parameters of the Internet of Things platform, and connect the oc_MQTT_report_entry to huawei cloud platform.
You can see that the device is online on Huawei cloud platform, and then enter the loop body to continuously monitor whether the F2 button is pressed. When F2 is pressed and placed in the submit or Cancel position, the JSON field is assembled.
Put the name, data, data size, and data type of each field in the structure, concatenate it to the last field, and so on, to complete the assembly of all fields. And then convert them to JSON format,
After the OC_MQTT_report data is successfully reported, all order data will be emptied, the shopping cart will be emptied, the identifier asterisk of F1 button will return to the position of the first product, and finally the data created just now will be released.
When F2 is pressed, the F1 button will be placed at the commodity, and the commodity will be added to the shopping cart. The quantity and total price of the corresponding shopping cart will increase. Here, the five commodities on the left and the right will be judged separately.
Oc_mqtt_cmd_entry is a task that sends a command response. When receiving a command, the oc_MQTT_cmD_entry is modified according to the order of items in the command.
Definition of global variables, including constants used for LCD display and data reporting.
Common shortcut keys for IoT Studio
1, current file search Ctrl+F
Global search Ctrl+Shift+F
3. Undo Ctrl+Z and restore Ctrl+Y
4. Save Ctrl+S
Comment Ctrl+ backslash
6. Search for files with Ctrl+P
7, Copy Ctrl+C, paste Ctrl+V, cut Ctrl+X
8. Advance Alt+ right arrow
9. Back Alt+ left arrow
LiteOS_Lab_STM32.part2.rar
Author: Sunshine_666