Securing a connection between ESP8266 and an MQTT broker

If you don't want to have your own broker, but you want a secure MQTT connection you can use a cloud MQTT instance, such as

First create an account on and confirm your email address. After that you can log in to and get your private assigned topic. It is an eight character code, such as c5c05211, and this code needs to precede all of your topics as follows:


No matter if you publish or subscribe. The following code connects to the Wi-Fi and then establishes a secure connection to the cloud MQTT over port 8883. Every message sent to the broker is received back like a loopback.

Included ...

Get ESP8266 Home Automation Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.