---
title: "002-STM32+W5500 Basic Control (Alibaba Cloud IoT Platform)-STM32+W5500 uses MQTT+SSL (mbedtls) encryption to access Alibaba Cloud IoT platform"
url: "https://maker.wiznet.io/praveen_k/projects/002STM32W5500-Basic-Control-Alibaba-Cloud-IoT-PlatformSTM32W5500-uses-MQTTSSL-mbedtls-encryption-to-access-Alibaba-Cloud-IoT-platform/"
markdown_url: "https://maker.wiznet.io/praveen_k/projects/002STM32W5500-Basic-Control-Alibaba-Cloud-IoT-PlatformSTM32W5500-uses-MQTTSSL-mbedtls-encryption-to-access-Alibaba-Cloud-IoT-platform/md"
type: "UCC: User Created Content"
author: "praveen_k"
original_author: "praveen_k"
original_url: "https://icode9.com/content-4-1325981.html"
published: "2022-02-08"
language: "en"
hardware: ["WIZnet W5500", "STM32"]
likes: 0
views: 296
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# 002-STM32+W5500 Basic Control (Alibaba Cloud IoT Platform)-STM32+W5500 uses MQTT+SSL (mbedtls) encryption to access Alibaba Cloud IoT platform

> 002-STM32+W5500 Basic Control (Alibaba Cloud IoT Platform)-STM32+W5500 uses MQTT+SSL (mbedtls) encryption to access Alibaba Cloud IoT platform

Original author: praveen_k (source: https://icode9.com/content-4-1325981.html)

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- **STM32** x 1

WIZnet parts: W5500 ([Datasheet](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/datasheet?utm_source=maker&utm_medium=project&utm_campaign=w5500), [product hub](https://maker.wiznet.io/products/w5500/))

## Article

## instruction

### In network communication, if SSL is not added, the information is exposed in the network communication in clear text.

### This section transplants the mbedtls library to allow W5500 to connect to the MQTT server in SSL mode.

### First, briefly explain SSL, in fact, the whole is TCP and server communication,

### It's just that after the TCP connection to the server, the encryption method and encryption password are negotiated with the server first.

### Then the data is encrypted and transmitted;

## test

### 1. Open the code in this section

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128155757041-1306758733.png)

### 2. Modify it to your own Alibaba Cloud device information

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207220632942-176365213.png)

### 3. Download to the microcontroller (connect the Internet cable)

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220121110408260-1076841797.png)

### 4. Observe the log print port to make sure the server is connected

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207220750158-111557801.png) ![](https://www.icode9.com/i/l/?n=20&i=blog/819239/202008/819239-20200824122023099-935631951.png)

## Program Description

### 1. In order to facilitate the use of the SSL function of mbedtls, I encapsulated the following

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207220914688-1922999053.png)

### 2. Let’s talk about a few functions that you need to implement. The bottom layer of mbedtls will call these functions.

#### Random number function and return timestamp function (not used if the certificate time is not verified)

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128160326379-1316842220.png)

#### Network reception processing function (the bottom layer of mbedtls will automatically call this function)

The way the bottom layer of SSL obtains data is to indicate how much data is needed, and then how much data is returned. So I store the data in the circular queue after receiving it, and then I give as much SSL as I want. ![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128160504504-869254498.png)

#### Network sending function (the bottom layer of mbedtls will automatically call this function)

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128160625052-251162029.png)

### 3. Use SSL to send data function and get data from SSL function

#### In fact, these two functions can be used directly, but I have set a layer of functions for the convenience of calling;

#### After completing the ssl negotiation, the data we send needs to call the function provided by mbedtls,?

#### Internally, our data will be encrypted and then sent out through TCP;?

#### Similarly, for the data received by TCP, we also need to use the function provided by mbedtls to obtain the data;

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128161040790-1830548210.png)

### 4. SSL initialization

#### The last two functions with red lines at the bottom are the tcp sending data function and tcp receiving data function we wrote above

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128161300203-2104520347.png)

### 5. Wait for the SSL handshake to complete

#### This function needs to be called in rotation after the TCP connection is successful. The SSL automatically implemented inside this function will return 0 as long as the SSL is completed.

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202201/819239-20220128161611772-2122990036.png)

## Now look at the specific use

### 1. After connecting to TCP, rotate training and wait for SSL to succeed

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207221048567-1489707387.png)

### 2. After the success, send the connection MQTT protocol

#### Pay attention, Alibaba Cloud stipulates that if you use SSL connection, you need to set the parameter securemode=2 of the client_id of mqtt

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207221151163-29862119.png) ![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207221401797-1195537372.png)

### 3. Get data from SSL

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207221441538-1799108148.png)

### 4. Of course, the place where the MQTT bottom layer sends data is also changed to use SSL to send

![](https://www.icode9.com/i/l/?n=22&i=blog/819239/202202/819239-20220207221517911-521652076.png)

## Epilogue

### In fact, it is actually still TCP communication, but the plaintext data is encrypted;

### After the TCP connection is made, the encryption method and password negotiated with the server are implemented internally using mbedtls.

### After negotiation, we can send and receive data normally, but only through the mbedtls internal function in the middle;

---

Source: https://maker.wiznet.io/praveen_k/projects/002STM32W5500-Basic-Control-Alibaba-Cloud-IoT-PlatformSTM32W5500-uses-MQTTSSL-mbedtls-encryption-to-access-Alibaba-Cloud-IoT-platform/
