1# ESP-MQTT MQTT over WSS Sample application 2(See the README.md file in the upper level 'examples' directory for more information about examples.) 3 4This example connects to the broker mqtt.eclipseprojects.io over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. 5(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) 6 7It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. 8 9## How to use example 10 11### Hardware Required 12 13This 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* Open the project configuration menu (`idf.py menuconfig`) 18* 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. 19* When using Make build system, set `Default serial port` under `Serial flasher config`. 20 21Note how to create a PEM certificate for mqtt.eclipseprojects.io: 22 23PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipseprojects.io. 24In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). 25``` 26echo "" | openssl s_client -showcerts -connect mqtt.eclipseprojects.io:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem 27``` 28Please note that this is not a general command for downloading a root certificate for an arbitrary host; 29this command works with mqtt.eclipseprojects.io as the site provides root certificate in the chain, which then could be extracted 30with text operation. 31 32### Build and Flash 33 34Build the project and flash it to the board, then run monitor tool to view serial output: 35 36``` 37idf.py -p PORT flash monitor 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 (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 48I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE 49I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 50I (4164) MQTTWSS_EXAMPLE: MQTT_EVENT_CONNECTED 51I (4174) MQTTWSS_EXAMPLE: sent publish successful, msg_id=41464 52I (4174) MQTTWSS_EXAMPLE: sent subscribe successful, msg_id=17886 53I (4174) MQTTWSS_EXAMPLE: sent subscribe successful, msg_id=42970 54I (4184) MQTTWSS_EXAMPLE: sent unsubscribe successful, msg_id=50241 55I (4314) MQTTWSS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 56I (4484) MQTTWSS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 57I (4484) MQTTWSS_EXAMPLE: sent publish successful, msg_id=0 58I (4684) MQTTWSS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 59I (4684) MQTTWSS_EXAMPLE: sent publish successful, msg_id=0 60I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 61I (4884) MQTTWSS_EXAMPLE: MQTT_EVENT_DATA 62TOPIC=/topic/qos0 63DATA=data 64I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 65I (5194) MQTTWSS_EXAMPLE: MQTT_EVENT_DATA 66TOPIC=/topic/qos0 67DATA=data 68``` 69 70 71 72