1# ESP-MQTT SSL example with PSK verification
2
3(See the README.md file in the upper level 'examples' directory for more information about examples.)
4
5This example connects to a local broker configured to PSK authentication
6
7## How to use example
8
9### Hardware Required
10
11This example can be executed on any ESP32 board, the only required interface is WiFi (or ethernet) to connect to a MQTT
12broker with preconfigured PSK verification method.
13
14#### Mosquitto settings
15In case of using mosquitto broker, here is how to enable PSK authentication in `mosquitto.config`,
16```
17psk_hint hint
18psk_file path_to_your_psk_file
19allow_anonymous true
20```
21Note: Last line enables anonymous mode, as this example does not use mqtt username and password.
22
23PSK file then has to contain pairs of hints and keys, as shown below:
24```
25hint:BAD123
26```
27
28Important note: Keys are stored as text hexadecimal values in PSK file, while the example code stores key as plain binary
29as required by MQTT API. (See the example source for details: `"BAD123" -> 0xBA, 0xD1, 0x23`)
30
31### Configure the project
32
33* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system)
34* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details.
35* When using Make build system, set `Default serial port` under `Serial flasher config`.
36
37### Build and Flash
38
39
40(To exit the serial monitor, type ``Ctrl-]``.)
41
42See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
43
44## Example Output
45
46```
47I (2160) example_connect: Ethernet Link Up
48I (4650) example_connect: Connected to Ethernet
49I (4650) example_connect: IPv4 address: 192.168.0.1
50I (4650) MQTTS_EXAMPLE: [APP] Free memory: 244792 bytes
51I (4660) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
52D (4670) MQTT_CLIENT: MQTT client_id=ESP32_c6B4F8
53D (4680) MQTT_CLIENT: Core selection disabled
54I (4680) MQTTS_EXAMPLE: Other event id:7
55D (4680) esp-tls: host:192.168.0.2: strlen 13
56D (4700) esp-tls: ssl psk authentication
57D (4700) esp-tls: handshake in progress...
58D (4720) MQTT_CLIENT: Transport connected to mqtts://192.168.0.2:8883
59I (4720) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
60D (4720) MQTT_CLIENT: mqtt_message_receive: first byte: 0x20
61D (4730) MQTT_CLIENT: mqtt_message_receive: read "remaining length" byte: 0x2
62D (4730) MQTT_CLIENT: mqtt_message_receive: total message length: 4 (already read: 2)
63D (4740) MQTT_CLIENT: mqtt_message_receive: read_len=2
64D (4750) MQTT_CLIENT: mqtt_message_receive: transport_read():4 4
65D (4750) MQTT_CLIENT: Connected
66I (4760) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED
67D (4760) MQTT_CLIENT: mqtt_enqueue id: 4837, type=8 successful
68D (4770) OUTBOX: ENQUEUE msgid=4837, msg_type=8, len=18, size=18
69D (4770) MQTT_CLIENT: Sent subscribe topic=/topic/qos0, id: 4837, type=8 successful
70I (4780) MQTTS_EXAMPLE: sent subscribe successful, msg_id=4837
71D (4790) MQTT_CLIENT: mqtt_enqueue id: 58982, type=8 successful
72D (4790) OUTBOX: ENQUEUE msgid=58982, msg_type=8, len=18, size=36
73D (4800) MQTT_CLIENT: Sent subscribe topic=/topic/qos1, id: 58982, type=8 successful
74I (4810) MQTTS_EXAMPLE: sent subscribe successful, msg_id=58982
75```
76
77