STM32 + W5500 + Alibaba IoT Platform

Connect to Alibaba Cloud using w5500
ORIGINAL POST
By known
components
Hardware Components
STM32 32-bit Arm Cortex MCUs
X 1
1
W5500
X 1
1
details

alibaba_cloud_1.png like Like

premise:
I am not a major in the Internet of Things, and I have a little knowledge of network protocols (that is, I have never learned it). I recently debugged a stm32+w5500 development board. In order to learn knowledge, I rely onAlibaba Cloud IoT platform controls the LED switch function on the development board. (April 24, 2020)

1: Alibaba Cloud IoT platform:
https://www.aliyun.com/

The IoT platform is mainly to create a device, and the charge is relatively cheap (the device is created more than two yuan, and the resource package is less than ten yuan for 1 million minutes). The first time you log in to the website, there will be a step-by-step guide to create a document, so I won’t go into details here. When the three items of ProductKey, DeviceSecret, and DeviceName are obtained, the device creation ends.

**2: Introduction to w5500:
https://www.iwiznet.cn/2017/09/Here is an article on w5500 China’s official website blog. The function is also to connect to Alibaba Cloud, but it has been three years now. Alibaba Cloud interface or some configurations need to be changed.
w5500 driver:http://wizwiki.net/wiki/doku.php?id=products:w5500:driver

3: stm32 is related to w5500:
stm32 and w5500 use SPI communication, mode 0/3. Because I use stm32cube to generate code and use HAL library, so I only need to encapsulate two functions`

	void SPI_WriteByte(uint8_t Tdata)//SPI write one byte of data
	{
			 HAL_SPI_Transmit(&hspi2,&Tdata,1,0xffff);
			while(HAL_SPI_GetState(&hspi2) == HAL_SPI_STATE_BUSY_RX);
			SPI2->DR;
	}

	 //SPI reads one byte of data
	uint8_t SPI_ReadByte(void)
	{
			uint8_t Rcv=0;
			HAL_SPI_Receive(&hspi2,&Rcv,1,0XFFFF);
			return Rcv;
	}`

The next step is to realize the network operation of w5500. Don’t worry, we will divide it into three steps (note that due to the different versions of the w5500 driver, some function names may be different)
1. Set a fixed IP. You can use a computer to connect directly to the w5500 network cable for testing. If you connect to a router, please pay attention to prevent IP conflicts. Refer to another blogger’s blog here.https://blog.csdn.net/u014470361/article/details/79238437

Second, DHCP dynamic IP, in order to prevent IP conflicts here is changed to dynamic IP acquisition. Still the blog of the last bosshttps://blog.csdn.net/u014470361/article/details/79239729

Third, obtain the server IP through DNS. Link uphttps://blog.csdn.net/u014470361/article/details/79240054

Pay attention here, this big guy got “www.baidu.com”, if you successfully get Baidu’s ip, then you need to pay attention to the next step.
We need to obtain the iP of the domain name “{ProductKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com” of our device on the IoT platform
 cn-shanghai 2
Use cn-shanghai because it is the East China 2 node.
But you will find that you can’t get the ip. This may be because of Alibaba Cloud updates or my network firewall. So I used another method in the w5500 blog. Just Ping directly. . . . . . . You will notice a slight difference from the manual interface. (The domain name has changed. I don’t have a trace of Internet knowledge so I don’t know why). But use this ip assignment directly.

4: MQTT protocol:
Is the MQTT protocol a TCP protocol? Although it is not clear, there are ready-made libraries for direct use!
MQTT code source code download address: (http://www.eclipse.org/paho/)

The above files can be found in the blog file of w5500, the most important part of which is the void MQTT_CON_ALI(void) function, which uses the MQTT protocol to connect to Alibaba Cloud and send The spliced ​​message is as follows:
Refer to the MQTT device access manual in Alibaba Cloud and calculate the parameters of the device connection. For example, the part in the box in the following procedure is the configuration of the parameters of the MQTT connection with Alibaba Cloud in this routine , The details are as follows:
clientId = 192.168.207.115 //You can write whatever you want
deviceName = MQTT1 //Modify according to the name on your platform
productKey = TKKMt4nMF8U //Modify according to the name on your platform
timestamp = 789 (milliseconds value) // unchanged
signmethod = hmacsha1 (algorithm type) // unchanged
deviceSecret = secret //Modify according to the name on your platform

The encrypted part can be generated using the websitehttp://encode.chahuo.com/



Replace the fields in the above figure at the corresponding position in the tcp_client.c file, and we can perform the connection test!
Keep yourself online by interrupting the timer to ping the server every few seconds.
5: Cjson related:
The basic format of json is “variable name”: “variable value”, and the variable value is another json format value. That is to say, the variable value can be json, string, or number. . . for example:
{“method”:
“thing.service.property.set”,“id”:“1098258095”,“params”:
{“NightLightSwitch”:0},“version”:“1.0.0”}
This is an instruction to control the switch, where the variable name “params” is {“NightLightSwitch”:0},”version”:”1.0.0″}, in order to get the switch Value We also need to know the value of “NightLightSwitch”, so the code is implemented like this:

		char parsetext[] =  {"method":
										"thing.service.property.set","id":"1098258095","params":
										{"NightLightSwitch":0},"version":"1.0.0"} // Initially define a string for JSON parsing
       								           }//This is just a demonstration, so no escape characters are added. The actual test needs to add 

		cJSON  *json = NULL;  
		char   *out = NULL;                        // root
	     json = cJSON_Parse(parsetext); // Process the string into a JSON object
		 if (!json) // If there is an error in conversion, exit with an error
        {
              printf("Error before: [%s]n", cJSON_GetErrorPtr());
        }
       else
       {
              /* print the json */
                             out = cJSON_Print(json); // first string print prints out the JSON object "according to the format"
               //printf("%sn", out); 
	  }
	 cJSON  *jver = NULL;
	  jver = cJSON_GetObjectItem(json,"params"); //Get the object whose key value is "params" in JSON format
	 cJSON* tmp = cJSON_GetObjectItem(jver, "LightSwitch");
	  int value = tmp->valueint; // read the value of version, version value is an integer
	printf(" LightSwitch: %drn",value );

For details, see ten articles dedicated to json parsing written by this big manhttps://blog.csdn.net/NB_vol_1/article/details/50454841

6: Alibaba Cloud issued, stm32 upload process: **


![Serial assistant display](https://img-blog.csdnimg.cn/20200424135225536.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4, size_16,color_FFFFFF,t_70
So far, stm32+w5500 as a client can accept server instructions and make corresponding operations. The following is a feedback on whether the execution is successful or not.

alibaba_cloud_1.png like Like

premise:
I am not a major in the Internet of Things, and I have a little knowledge of network protocols (that is, I have never learned it). I recently debugged a stm32+w5500 development board. In order to learn knowledge, I rely onAlibaba Cloud IoT platform controls the LED switch function on the development board. (April 24, 2020)

1: Alibaba Cloud IoT platform:
https://www.aliyun.com/

The IoT platform is mainly to create a device, and the charge is relatively cheap (the device is created more than two yuan, and the resource package is less than ten yuan for 1 million minutes). The first time you log in to the website, there will be a step-by-step guide to create a document, so I won’t go into details here. When the three items of ProductKey, DeviceSecret, and DeviceName are obtained, the device creation ends.

**2: Introduction to w5500:
https://www.iwiznet.cn/2017/09/Here is an article on w5500 China’s official website blog. The function is also to connect to Alibaba Cloud, but it has been three years now. Alibaba Cloud interface or some configurations need to be changed.
w5500 driver:http://wizwiki.net/wiki/doku.php?id=products:w5500:driver

3: stm32 is related to w5500:
stm32 and w5500 use SPI communication, mode 0/3. Because I use stm32cube to generate code and use HAL library, so I only need to encapsulate two functions`

	void SPI_WriteByte(uint8_t Tdata)//SPI write one byte of data
	{
			 HAL_SPI_Transmit(&hspi2,&Tdata,1,0xffff);
			while(HAL_SPI_GetState(&hspi2) == HAL_SPI_STATE_BUSY_RX);
			SPI2->DR;
	}

	 //SPI reads one byte of data
	uint8_t SPI_ReadByte(void)
	{
			uint8_t Rcv=0;
			HAL_SPI_Receive(&hspi2,&Rcv,1,0XFFFF);
			return Rcv;
	}`

The next step is to realize the network operation of w5500. Don’t worry, we will divide it into three steps (note that due to the different versions of the w5500 driver, some function names may be different)
1. Set a fixed IP. You can use a computer to connect directly to the w5500 network cable for testing. If you connect to a router, please pay attention to prevent IP conflicts. Refer to another blogger’s blog here.https://blog.csdn.net/u014470361/article/details/79238437

Second, DHCP dynamic IP, in order to prevent IP conflicts here is changed to dynamic IP acquisition. Still the blog of the last bosshttps://blog.csdn.net/u014470361/article/details/79239729

Third, obtain the server IP through DNS. Link uphttps://blog.csdn.net/u014470361/article/details/79240054

Pay attention here, this big guy got “www.baidu.com”, if you successfully get Baidu’s ip, then you need to pay attention to the next step.
We need to obtain the iP of the domain name “{ProductKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com” of our device on the IoT platform
 cn-shanghai 2
Use cn-shanghai because it is the East China 2 node.
But you will find that you can’t get the ip. This may be because of Alibaba Cloud updates or my network firewall. So I used another method in the w5500 blog. Just Ping directly. . . . . . . You will notice a slight difference from the manual interface. (The domain name has changed. I don’t have a trace of Internet knowledge so I don’t know why). But use this ip assignment directly.

4: MQTT protocol:
Is the MQTT protocol a TCP protocol? Although it is not clear, there are ready-made libraries for direct use!
MQTT code source code download address: (http://www.eclipse.org/paho/)

The above files can be found in the blog file of w5500, the most important part of which is the void MQTT_CON_ALI(void) function, which uses the MQTT protocol to connect to Alibaba Cloud and send The spliced ​​message is as follows:
Refer to the MQTT device access manual in Alibaba Cloud and calculate the parameters of the device connection. For example, the part in the box in the following procedure is the configuration of the parameters of the MQTT connection with Alibaba Cloud in this routine , The details are as follows:
clientId = 192.168.207.115 //You can write whatever you want
deviceName = MQTT1 //Modify according to the name on your platform
productKey = TKKMt4nMF8U //Modify according to the name on your platform
timestamp = 789 (milliseconds value) // unchanged
signmethod = hmacsha1 (algorithm type) // unchanged
deviceSecret = secret //Modify according to the name on your platform

The encrypted part can be generated using the websitehttp://encode.chahuo.com/



Replace the fields in the above figure at the corresponding position in the tcp_client.c file, and we can perform the connection test!
Keep yourself online by interrupting the timer to ping the server every few seconds.
5: Cjson related:
The basic format of json is “variable name”: “variable value”, and the variable value is another json format value. That is to say, the variable value can be json, string, or number. . . for example:
{“method”:
“thing.service.property.set”,“id”:“1098258095”,“params”:
{“NightLightSwitch”:0},“version”:“1.0.0”}
This is an instruction to control the switch, where the variable name “params” is {“NightLightSwitch”:0},”version”:”1.0.0″}, in order to get the switch Value We also need to know the value of “NightLightSwitch”, so the code is implemented like this:

		char parsetext[] =  {"method":
										"thing.service.property.set","id":"1098258095","params":
										{"NightLightSwitch":0},"version":"1.0.0"} // Initially define a string for JSON parsing
       								           }//This is just a demonstration, so no escape characters are added. The actual test needs to add 

		cJSON  *json = NULL;  
		char   *out = NULL;                        // root
	     json = cJSON_Parse(parsetext); // Process the string into a JSON object
		 if (!json) // If there is an error in conversion, exit with an error
        {
              printf("Error before: [%s]n", cJSON_GetErrorPtr());
        }
       else
       {
              /* print the json */
                             out = cJSON_Print(json); // first string print prints out the JSON object "according to the format"
               //printf("%sn", out); 
	  }
	 cJSON  *jver = NULL;
	  jver = cJSON_GetObjectItem(json,"params"); //Get the object whose key value is "params" in JSON format
	 cJSON* tmp = cJSON_GetObjectItem(jver, "LightSwitch");
	  int value = tmp->valueint; // read the value of version, version value is an integer
	printf(" LightSwitch: %drn",value );

For details, see ten articles dedicated to json parsing written by this big manhttps://blog.csdn.net/NB_vol_1/article/details/50454841

6: Alibaba Cloud issued, stm32 upload process: **


![Serial assistant display](https://img-blog.csdnimg.cn/20200424135225536.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4, size_16,color_FFFFFF,t_70
So far, stm32+w5500 as a client can accept server instructions and make corresponding operations. The following is a feedback on whether the execution is successful or not.

COMMENTS

Please Login to comment
  Subscribe  
Notify of
POSTED BY
Reusable S/W