|
| 1 | +# ESP-MQTT SSL Sample application (mutual authentication) |
| 2 | + |
| 3 | +(See the README.md file in the upper level 'examples' directory for more information about examples.) |
| 4 | + |
| 5 | +This example connects to the broker test.mosquitto.org using ssl transport with client certificate and as a demonstration subscribes/unsubscribes and send a message on certain topic. |
| 6 | + |
| 7 | +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. |
| 8 | + |
| 9 | +## How to use example |
| 10 | + |
| 11 | +### Hardware Required |
| 12 | + |
| 13 | +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. |
| 14 | + |
| 15 | +### Configure the project |
| 16 | + |
| 17 | +``` |
| 18 | +make menuconfig |
| 19 | +``` |
| 20 | + |
| 21 | +* Set serial port under Serial Flasher Options. |
| 22 | + |
| 23 | +* Set ssid and password for the board to connect to AP. |
| 24 | + |
| 25 | +* Generate your client keys and certificate |
| 26 | + |
| 27 | +Navigate to the main directory |
| 28 | + |
| 29 | +``` |
| 30 | +cd main |
| 31 | +``` |
| 32 | + |
| 33 | +Generate a client key and a CSR. When you are generating the CSR, do not use the default values. At a minimum, the CSR must include the Country, Organisation and Common Name fields. |
| 34 | + |
| 35 | +``` |
| 36 | +openssl genrsa -out client.key |
| 37 | +openssl req -out client.csr -key client.key -new |
| 38 | +``` |
| 39 | + |
| 40 | +Paste the generated CSR in the [Mosquitto test certificate signer](https://test.mosquitto.org/ssl/index.php), click Submit and copy the downloaded `client.crt` in the `main` directory. |
| 41 | + |
| 42 | +Please note, that the supplied files `client.crt` and `client.key` in the `main` directory are only placeholders for your client certificate and key (i.e. the example "as is" would compile but would not connect to the broker) |
| 43 | + |
| 44 | +### Build and Flash |
| 45 | + |
| 46 | +Build the project and flash it to the board, then run monitor tool to view serial output: |
| 47 | + |
| 48 | +``` |
| 49 | +make -j4 flash monitor |
| 50 | +``` |
| 51 | + |
| 52 | +(To exit the serial monitor, type ``Ctrl-]``.) |
| 53 | + |
| 54 | +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. |
| 55 | + |
| 56 | +## Example Output |
| 57 | + |
| 58 | +``` |
| 59 | +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 |
| 60 | +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE |
| 61 | +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 |
| 62 | +I (4164) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED |
| 63 | +I (4174) MQTTS_EXAMPLE: sent publish successful, msg_id=41464 |
| 64 | +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=17886 |
| 65 | +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=42970 |
| 66 | +I (4184) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=50241 |
| 67 | +I (4314) MQTTS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 |
| 68 | +I (4484) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 |
| 69 | +I (4484) MQTTS_EXAMPLE: sent publish successful, msg_id=0 |
| 70 | +I (4684) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 |
| 71 | +I (4684) MQTTS_EXAMPLE: sent publish successful, msg_id=0 |
| 72 | +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 |
| 73 | +I (4884) MQTTS_EXAMPLE: MQTT_EVENT_DATA |
| 74 | +TOPIC=/topic/qos0 |
| 75 | +DATA=data |
| 76 | +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 |
| 77 | +I (5194) MQTTS_EXAMPLE: MQTT_EVENT_DATA |
| 78 | +TOPIC=/topic/qos0 |
| 79 | +DATA=data |
| 80 | +``` |
| 81 | + |
0 commit comments