当前位置:网站首页>[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code

[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code

2022-07-07 23:32:00 Little river god is tangled


Preface

       First, let's talk about what is dynamic registration , What's its use :
      1. Dynamic registration : After dynamic registration is enabled, the device does not need to burn the device certificate one by one / secret key , All devices under the same product can burn the same product key (ProductId and ProductSecret); It means that after the dynamic registration function is enabled ,STM32 When connected as a new device , There is no need to go to the cloud platform to get triples , Just modify the device name in the code to automatically create a new device on the cloud platform .
      2. Benefits of dynamic registration : The advantage of using dynamic registration is , You don't need to create new devices on your own cloud platform , We are STM32 Modifying the device name in the code can create the corresponding device on the Tencent cloud IOT development platform , And connect .

One 、 Open the dynamic registration function of the product

       Products after opening , You may not create a device , Just put the product ID and ProductSecret( Product key ) Just copy it .

 Insert picture description here

Two 、STM32 Code changes

       The modification of the project code is based on yesterday's project ( Please move to :【STM32+ESP8266 Connect Tencent cloud Internet of things development platform 2】STM32+ESP8266-01S Connect to Tencent cloud ), The source code of this time will also be open source for everyone .

1. Modify triples

       I put the macro of device triplet ESP8266.h In file .

#define PRODUCT_ID " ***** " // product ID  Modify it into your own product ID
#define DEVUICE_NAME "LED" // Equipment name   New equipment only needs to modify this 
#define DEVICE_SECRET " ******** "// Product key   Change it to your own product key 
#define PRODUCT_REGION "ap-guangzhou" // Geographical name 

2. Modify the connection code

       Previous static connection , It's a DeviceName( Equipment name )、PRODUCT_ID( product ID) and DEVICE_SECRET( Device key ) to 8266 Connect , The instructions are :

AT+TCDEVINFOSET=<tls_mode>,<product_id>,<device_name>,<device_secret>[,<product_region>]

Equipment level information , Dynamic registration belongs to product level , So we're going to use Product information setting instructions

AT+TCPRDINFOSET=<tls_mode>,<product_ID>,<product_secret>,<device_name>,<product_region>

The last parameter can be left blank , The default is used :“ap-guangzhou”, Use after setting Dynamic registration instructions :

AT+TCDEVREG
// Set product information 
 case 8:
      sprintf((char *)device_massage, "AT+TCPRDINFOSET=1,\"%s\",\"%s\",\"%s\"\r\n", PRODUCT_ID, DEVICE_SECRET,DEVUICE_NAME);
      HAL_UART_Transmit(&huart2, device_massage, strlen((const char *)device_massage), 5000);
      osDelay(500);
      break;
// Dynamic registration 
    case 9:
    case 10:
      HAL_UART_Transmit(&huart2, "AT+TCDEVREG\r\n", strlen("AT+TCMQTTDISCONN\r\n"), 5000); // Dynamic registration 
      osDelay(2000);
      break;
// Start connecting 
    case 11:
      HAL_UART_Transmit(&huart2, "AT+TCMQTTCONN=1,5000,240,0,1\r\n", strlen("AT+TCMQTTCONN=1,5000,240,0,1\r\n"), 5000);// Start connecting 
      osDelay(100);
      break;

You can register dynamically with other code unchanged ,

3、 ... and 、 Effect demonstration

 Insert picture description here
These three blog posts will be synchronized to the official blog posts of anxinco Technology !!
Source code :https://pan.baidu.com/s/1rOEzEuz9XeIe3tAPIz1d0g Extraction code :4sg4

原网站

版权声明
本文为[Little river god is tangled]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130559025627.html