Skip to content

Commit d2a5fac

Browse files
committed
feat(mqtt): add APIs for mqtt ssl functionality
internal: 2c667eef
1 parent 5f058d0 commit d2a5fac

File tree

7 files changed

+602
-197
lines changed

7 files changed

+602
-197
lines changed

VERSION

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ gwen:
1111
wps: ff84a8b
1212

1313
gitlab:
14+
driver: 7bee5263
1415
espconn: 3a998034
1516
freertos: a9985a9c
1617
lwip: 1651e055
17-
driver: 7bee5263
1818
mbedtls: 1ac9f1f4
19-
ssl: eefb383a
19+
mqtt: 6c098065
20+
nopoll: 31f0ea07
2021
openssl: 1669353f
21-
nopoll: 31f0ea07
22+
ssl: eefb383a

examples/mqtt_demo/README.md

+163-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,168 @@
1-
# Paho MQTT demo
1+
# ESP8266 MQTT Client Demo
22

3-
This example shows how to use the Eclipse Paho MQTT as an example of ESP8266 RTOS SDK. In this demo, the following functions can be realized: MQTT publish, subscribe and ping.
3+
## 1. Introduction
44

5-
1. Config SSID and PASSWORD of the Wi-Fi AP to be connected in user_config.h
5+
This MQTT demo is based on the Eclipse Paho MQTT library, and demonstrates a working MQTT client actions(subscribe, publish, ping). Using this MQTT demo, you can connect to the MQTT broker, subscribe to a topic and publish messages to the predefined topic.
66

7-
2. Config MQTT Broker to be connected in MQTTEcho.c
7+
Also, this demo will ping the MQTT broker in the defined interval if no sending or receiving action happens. And we add some APIs to realize the SSL functionality, these SSL APIs provide the one-way certification and two-way certification.
88

9-
3. Export SDK_PATH and BIN_PATH, run gen_misc.sh to compile, then download and run the demo
9+
## 2. Configuration
1010

11-
4. MQTT client will connect with the MQTT Broker, subscribe to the topic "ESP8266/sample/sub", and will publish messages
12-
to the topic "ESP8266/sample/pub"
11+
Some basic configurations need to be done before starting this demo and are listed in the include/user_config.h.
12+
13+
* Wi-Fi SSID & Password
14+
* MQTT Broker Address(can be a domain name) & MQTT Port
15+
>Note: There is a publically accessible sandbox server for the Eclipse IoT projects available at iot.eclipse.org, please get some reference information from the website: https://iot.eclipse.org/getting-started
16+
17+
## 3. Description
18+
19+
### 3.1 MQTT-Normal
20+
21+
This section describes the mqtt informations and API for MQTT client without SSL functionality.
22+
23+
#### 3.1.1 MQTT Info
24+
25+
For this MQTT demo, mqtt-related informations are defined in the mqtt_client_thread(), and they are listed below.
26+
27+
* two buffers(i.e. sendbuf[80] & readbuf[80]) to store packets to be sent and received
28+
* MQTTVersion, ClientID, KeepAliveInterval, etc are defined using **MQTTPacket_connectData_initializer**
29+
* Command_timeout is defined as 30s, and you can use this value as default
30+
* The subscribe topic is defined as "ESP8266/sample/sub"
31+
* The subscribe message handler is "void messageArrived(MessageData* data)"
32+
* The publish topic is defined as "ESP8266/sample/pub"
33+
* The published message's QoS type is QoS2
34+
35+
These informarions are only defined as a demonstration, you can change them appropriately according to your own requirements.
36+
37+
#### 3.1.2 Major API
38+
39+
1.Platform-Related
40+
41+
* NetworkInit(): used to initialize **Network** structure, which includes read/write functions, etc.
42+
* NetworkConnect(): used to create socket and connect to the MQTT broker
43+
44+
2.MQTT-Related
45+
46+
* MQTTClientInit(): used to initialize **MQTTClient** structure, which includes MQTT client information
47+
* MQTTStartTask(): a task used to perform MQTT **keep alive**
48+
* MQTTConnect(): used to perform MQTT connect
49+
* MQTTSubscribe(): used to subscribe to a topic
50+
* MQTTPublish(): used to publish messages to a topic
51+
52+
### 3.2 MQTT-SSL
53+
54+
This section describes the mqtt informations and API for MQTT client with SSL functionality enabled.
55+
56+
#### 3.2.1 MQTT Info
57+
58+
The aforementioned informations in the **MQTT Info** section of **MQTT-Normal** are also used for MQTT-SSL. As for SSL functionality, some more information will be needed and are listed below in the "Added-Info" section.
59+
60+
1.Existed-Info
61+
62+
This section is the same with the **MQTT Info** section of **MQTT-Normal**.
63+
64+
2.Added-Info
65+
66+
* May need header files of CA (and client certificate & key) included in the include/ directory
67+
* May need length of the CA (and client certificate & key) files
68+
* Need a **ssl_ca_crt_key_t** structure initialized using the CA (and client certificate & key) files
69+
70+
#### 3.2.2 Major API
71+
72+
When SSL is enabled, the Platform-related API are different with **MQTT-Normal** section.
73+
74+
1.Platform-related
75+
76+
* NetworkInitSSL(): used to initialize **Network** structure, which includes SSL read/write functions, etc.
77+
* NetworkConnectSSL(): used to create socket and connect to the MQTT broker with SSL enabled
78+
79+
2.MQTT-Related
80+
81+
This section is the same with the "MQTT-Related" section of "MQTT-Normal".
82+
83+
#### 3.2.3 SSL Special
84+
85+
For SSL functionality, three certification ways may be used: no certification, one-way certification and two-way certification. The specific configurations for each of them are described below:
86+
87+
1.No Certification
88+
89+
* No CA file and client certificate & key files need to be included
90+
* Define a **ssl_ca_crt_key_t** structure
91+
* Set the **cacrt**, **cert** and **key** parameters within the structure to be **NULL**
92+
* Recommend to set the **verify_mode** parameter to **SSL_VERIFY_NONE**
93+
* Set the **method** parameter to **TLSv1_1_client_method()** or **TLSv1_2_client_method()**
94+
* Set the **frag_len** parameter with a value between **2048** and **8192**
95+
96+
2.One-way Certification
97+
98+
* CA file shall be included, also length of the CA file shall be provided
99+
* Define a **ssl_ca_crt_key_t** structure
100+
* Set the **cacrt** parameter within the structure to the array in the CA file
101+
* Set the **cacrt_len** parameter to length of the CA file
102+
* Set the **verify_mode** parameter to **SSL_VERIFY_PEER**
103+
* Set the **method** parameter to **TLSv1_1_client_method()** or **TLSv1_2_client_method()**
104+
* Set the **frag_len** parameter with a value between **2048** and **8192**
105+
106+
3.Two-way Certification
107+
108+
* CA file and client certificate & key files shall be included
109+
* Also length of the CA file and client certificate & key files shall be provided
110+
* Define a **ssl_ca_crt_key_t** structure
111+
* Set the **cacrt** parameter within the structure to the array in the CA file
112+
* Set the **cacrt_len** parameter to length of the CA file
113+
* Set the **cert** parameter within the structure to the array in the client certificate file
114+
* Set the **cert_len** parameter to length of the client certificate file
115+
* Set the **key** parameter within the structure to the array in the client key file
116+
* Set the **key_len** parameter to length of the client key file
117+
* Set the **verify_mode** parameter to **SSL_VERIFY_PEER**
118+
* Set the **method** parameter to **TLSv1_1_client_method()** or **TLSv1_2_client_method()**
119+
* Set the **frag_len** parameter with a value between **2048** and **8192**
120+
121+
>Note: two-way certification is decided by the SSL Server side, so on the client side we just provide all the files needed by the two-way certification.
122+
123+
#### 3.2.4 SSL Demo
124+
125+
The following shows a simple demo of the MQTT client SSL functionality, and only the different places compared with MQTT-Normal demo are displayed. The names of CA file, client certificate & key files are just a demonstration, changing these properly according to your own files.
126+
127+
```c
128+
#include "openssl/ssl.h"
129+
#include "CA.h"
130+
#include "cert.h"
131+
#include "key.h"
132+
133+
ssl_ca_crt_key_t ssl_cck;
134+
135+
#define SSL_CA_CERT_KEY_INIT(s,a,b,c,d,e,f) ((ssl_ca_crt_key_t *)s)->cacrt = a;\
136+
((ssl_ca_crt_key_t *)s)->cacrt_len = b;\
137+
((ssl_ca_crt_key_t *)s)->cert = c;\
138+
((ssl_ca_crt_key_t *)s)->cert_len = d;\
139+
((ssl_ca_crt_key_t *)s)->key = e;\
140+
((ssl_ca_crt_key_t *)s)->key_len = f;
141+
142+
static void mqtt_client_thread(void *pvParameters)
143+
{
144+
......
145+
NetworkInitSSL(&network);
146+
......
147+
SSL_CA_CERT_KEY_INIT(&ssl_cck, ca_crt, ca_crt_len, client_crt, client_crt_len, client_key, client_key_len);
148+
149+
if ((rc = NetworkConnectSSL(&network, address, MQTT_PORT, &ssl_cck, TLSv1_1_client_method(), SSL_VERIFY_NONE, 8192)) != 1) {
150+
printf("Return code from network connect ssl is %d\n", rc);
151+
}
152+
......
153+
}
154+
```
155+
156+
## 4. Compiling & Execution
157+
158+
Once all the aforementioned works are done, we can compile and download the MQTT client (SSL) demo, and a few more steps will be needed.
159+
160+
* Export SDK_PATH & BIN_PATH, and run gen_misc.sh to compile and generate binary files
161+
* Download the binary files to flash and run, also you can use UART console to watch the output log
162+
163+
All these being done, the MQTT client demo will:
164+
165+
* Connect to the MQTT Broker
166+
* Subscribe to the topic "ESP8266/sample/sub"
167+
* Publish messages to the topic "ESP8266/sample/pub" every 1 seconds
168+
* MQTT keep alive interval is 60s, so if no sending and receiving actions happended during this interval, ping request will be sent and ping response is expected to be received.

examples/mqtt_demo/include/user_config.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#ifndef __USER_CONFIG_H__
2626
#define __USER_CONFIG_H__
2727

28-
#define SSID "TEST001"
29-
#define PASSWORD "1234567890"
28+
#define SSID "espressif" /* Wi-Fi SSID */
29+
#define PASSWORD "1234567890" /* Wi-Fi Password */
30+
31+
#define MQTT_BROKER "iot.eclipse.org" /* MQTT Broker Address*/
32+
#define MQTT_PORT 1883 /* MQTT Port*/
3033

3134
#endif
3235

0 commit comments

Comments
 (0)