[Share] Huawei cloud JSON data reported to the vending machine Huawei Demo Things analytical platform based on STM32 chip Cubs sent by MQTT

Abstract STM32 project at the end of the article, free to download.

First, the project configuration

1, MCU type selection STM32L431RC

2, select the OpenOCD burner, the parameter -f interface / stlink-v2-1.cfg -f target / stm32l4x.cfg

3, the output directory selection LiteOS_Lab_STM32 \ targets \ STM32L431_BearPi \ GCC \ appbuild

4, the compiler Makefile select targets \ STM32L431_BearPi \ GCC \ Makefile

5, platform configuration parameters: LiteOS_Lab_STM32 \ demos \ oc_mqtt_demo \ oc_mqtt_demo_static \ oc_mqtt_static.c

6, WIFI user name and password configuration: LiteOS_Lab_STM32 \ iot_link \ network \ tcpip \ esp8266_socket \ esp8266_socket_imp.h

7, create a product on the Internet of Things platform, select MQTT way

Second, the code parsing part

We need to modify the code before compiling two burning program

At first open oc_mqtt_static.c file, we need to configure the device connection parameters Huawei cloud, networking platforms need,

Line 51 CN_MQTT_EP_NOTE_ID to modify the device identification code for use in a cloud Huawei, access device, registration device, can be viewed in the platform,

Modify the device ID registered is generated when the device CN_MQTT_EP_DEVICE_ID, CN_MQTT_EP_PASSWD apparatus to generate a registration key,

Device ID and keys stored in the device before registering txt file view, if forgotten or lost, you can re-register a new device,

Device ID can also view the access device, the access key may reset the device service.

The second point at the open esp8266_socket_imp h file, we need to link development board configuration parameters needed wifi,

Modify wifi hotspot name of the first 43 rows WIFI_SSID created for themselves, WIFI_PASSWD password for the hotspot, wifi link for the development board,

Here wifi hotspot own proposals phone open hotspots, development board data transmission is small, does not take much traffic, you can also use the wifi router,

But you can not have a portal authentication and 802.1x authentication wifi, can only be used for the authentication username and password of wifi.

Click the button to compile, compile, patiently waiting for a prompt compile successfully.

The development board connected via USB cable to your computer, open the phone or hotspot router, click the Burn button, patiently wait for the burning success. If the programming is unsuccessful, check the front steps of the project configuration is configured correctly, or try re-plug the USB cable. Things then login Huawei cloud platform, clicking on the left to switch pages, the equipment, all equipment can be seen at the device already online, and then on the LCD screen development board can see all the merchandise, F1 merchandise selection, F2 to ADD car, when F1 is located submit, F2 submit the order. You can also cancel your order at this time the data reported shopping cart is empty, order status is 0. Then we log of Things platform, click on the line, you will see the reported data, click View all attributes, you can see data details.

Explain the key code

config.mk LiteOS SDK profile can be selected or switches related functions, such as protocol selection: mqtt / lwm2m / coap, or configure the encryption / non-encryption

Makefile defines a set of rules and compile options, the compiler to compile, link to the source document based on these definitions

project.mk define all the files need transplants

Hardware includes four sensors and the driving of LCD display boards

File main.c program where the main entry point

iot link folder that contains all the features of the Lite OS SDK code. SDK according to the configurations in the config.mk, is automatically added to the source code corresponding to the function from build.

oc_mqtt_static.c main case file, is responsible for data reporting and orders issued under the business logic implementation.

oc_mqtt_static.c inside business logic implementations

oc_mqtt_demo_main is a vending machine case entry function, first do the LCD screen initialization, set the background to black and green font set to cycle through each item,

Submit display and Cancel option to display the current location selection F1 button displays the current number of commodities cart,

Initialization key detection task, semaphore is created for the command response, creates data reporting task, task creation command issued

Two tasks equivalent to two threads, created after, automatically.

key_detect task, when pressing the F1 key, the order of adjusting the position of an asterisk, to mark the location of the current F1 button resides.

oc_mqtt_report_entry data reporting task is first defined JSON data structure required reporting and configuration parameters thereof networked platforms, connected to the internet cloud Huawei, after the success of the configuration,

Huawei cloud can be seen on the device online platform, and then enters the loop, continuously monitor key presses F2, F2 is pressed when the submit or cancel position and is, for field assembly JSON,

The name of each field, data, data size, data type placed within the structure, spliced ​​to a field later, and so on, all fields complete assembly. They will then be converted into JSON format,

And then converted to a string for data reporting, data reporting oc_mqtt_report to be successful, clear all order data, empty the shopping cart, F1 key identification asterisk back to the first commodity position, and finally release the data you just created.

When F2 is pressed while, at the merchandise keys Fl, add the items to the shopping cart, the corresponding increase in total number and Cart, where the left and right commodities five separate determination.

oc_mqtt_cmd_entry command is issued in response to the task when receiving the command in the command sequence according to the commodity, be modified accordingly.

Define global variables, comprising a LCD display, the constants required for data reporting.

Third, IoT Studio tools commonly used shortcuts

1, the current file Ctrl + F search

2, global search Ctrl + Shift + F

3, Ctrl + Z Undo recovery Ctrl + Y

4. Save Ctrl + S

5. Notes Ctrl + backslash

6, search for files Ctrl + P

7, copy Ctrl + C, Paste Ctrl + V, Ctrl + X cut 

8, the process proceeds Alt + right arrow

9, Back Alt + left arrow

LiteOS_Lab_STM32.part1.rar

LiteOS_Lab_STM32.part2.rar

Author: Sunshine_666

Published 961 original articles · won praise 5343 · Views 710,000 +

Guess you like

Origin blog.csdn.net/devcloud/article/details/103935107