<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BoozTheBoss</title>
    <description>The latest articles on DEV Community by BoozTheBoss (@bassparanoya).</description>
    <link>https://dev.to/bassparanoya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F795000%2Fc871ef54-83cb-402e-9af7-2c7b2757be51.png</url>
      <title>DEV Community: BoozTheBoss</title>
      <link>https://dev.to/bassparanoya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bassparanoya"/>
    <language>en</language>
    <item>
      <title>ESP32 MicroPython MQTT TLS</title>
      <dc:creator>BoozTheBoss</dc:creator>
      <pubDate>Sun, 16 Jan 2022 19:24:41 +0000</pubDate>
      <link>https://dev.to/bassparanoya/esp32-micropython-mqtt-tls-28fd</link>
      <guid>https://dev.to/bassparanoya/esp32-micropython-mqtt-tls-28fd</guid>
      <description>&lt;h1&gt;
  
  
  &lt;h1 id="3"&gt;Implementation of an encrypted MQTT connection with MicroPython on a ESP32&lt;/h1&gt;
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
Introduction: ESP32 MicroPython MQTT TLS
&lt;/li&gt;
&lt;li&gt;Installing MicroPython&lt;/li&gt;
&lt;li&gt;Using MicroPython&lt;/li&gt;
&lt;li&gt;Wifi and MQTT Connection&lt;/li&gt;
&lt;li&gt;MQTT and TLS&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Introduction: ESP32 MicroPython MQTT TLS &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;by: Bass Paranoya&lt;/p&gt;




&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this tutorial you will learn how to program the ESP32 using MicroPython. At first we will connect the device to the Internet via WIFI. Furthermore we will use this knowledge to implement a MQTT connection to send internal sensor data. In the last steps we will discuss data security and use it to authenticate and encrypt our MQTT connection over TLS. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites and required Equipment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Computer&lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;li&gt;ESP32 DevKitC&lt;/li&gt;
&lt;li&gt;USB-Cable which allows to transfer data &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The tutorial was made on a Windows system. Nevertheless, I will also present the commands for Ubuntu. If you use a Mac or another system you'll find the necessary commands most of the time in the presented external links (APIs) at the end of each lesson under "Hints and pitfalls", "Useful Resources for Own Searches" and "Sources"). I recommend to use Visual Studio Code.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Lesson 1: Installing MicroPython &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Objectives
&lt;/h2&gt;

&lt;p&gt;Our first lesson will be flashing the ESP32 with MicroPython.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites and required Equipment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ESP32 DevKitC&lt;/li&gt;
&lt;li&gt;USB-Cable which allows to transfer data &lt;/li&gt;
&lt;li&gt;Computer &lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solution Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Download the latest MicroPython firmware:&lt;br&gt;&lt;br&gt;
&lt;a href="https://micropython.org/download/esp32/" rel="noopener noreferrer"&gt;Click &lt;strong&gt;here&lt;/strong&gt; for downloading the latest .bin file&lt;/a&gt; (e.g.:  v1.17 (2021-09-02) .bin) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Connect the ESP32 with the USB cable to your computer. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the esptool extension using &lt;code&gt;pip&lt;/code&gt; in a terminal or the powershell in VS Code:  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://pip.pypa.io/en/stable/installation/" rel="noopener noreferrer"&gt;Click &lt;strong&gt;here&lt;/strong&gt; if you have not installed pip.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;pip install esptool
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;alternatively you can use &lt;a href="https://github.com/espressif/esptool/" rel="noopener noreferrer"&gt;this &lt;strong&gt;GitHub Link&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You need to look up the port used for the ESP32 in the Device Manager. Adjust the COMx for your command in the next step.&lt;br&gt;&lt;br&gt;
If the ESP32 does not connect to any port, the chance is high that your USB cable is not suitable for exchanging data (it is a power supply only). Use a suitable one instead. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Erase the flash under Windows with:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;python -m esptool --port COMx erase_flash 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Or on Linux
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;esptool.py --port /dev/ttyUSBx erase_flash
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If the command output says something similar to "no permission", then it is likely the ESP32 is connected somewhere else. Disconnect it.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Next we will deploy the new firmware with this example command on Windows which worked best on my system (use your port and path):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;python -m esptool --chip esp32 --port COMx write_flash -z 0x1000 C:\path_to_firmware\esp32-20210902-v1.17.bin 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If you use Linux:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;esptool.py --chip esp32 --port /dev/ttyUSBx write_flash -z 0x1000 esp32-20180511-v1.9.4.bin
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;If the above commands run without error then MicroPython should be installed on your board.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Hints and pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You may need to reduce the baudrate if you get errors while flashing (e.g. down to 115200 by adding &lt;code&gt;--baud 115200&lt;/code&gt; into the command) or (&lt;code&gt;-b 9600&lt;/code&gt; is a very slow value that you can use to verify it is not a baud rate problem)&lt;/li&gt;
&lt;li&gt;For some boards with a particular FlashROM configuration you may need to change the flash mode (e.g. by adding &lt;code&gt;-fm dio&lt;/code&gt; into the command)&lt;/li&gt;
&lt;li&gt;Check if you have permissions to access the serial port, and if other software (such as modem-manager on Linux) is trying to interact with it. A common pitfall is leaving a serial terminal accessing this port open in another window and forgetting about it.&lt;/li&gt;
&lt;li&gt;for more tips check out the following links:&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Useful Resources for Own Searches
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.micropython.org/en/latest/esp32/tutorial/intro.html" rel="noopener noreferrer"&gt;Click here for the &lt;strong&gt;Getting Started with MicroPython on the ESP32&lt;/strong&gt; from the MicroPython API&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://docs.espressif.com/projects/esptool/en/latest/esp32/installation.html" rel="noopener noreferrer"&gt;Click here for the &lt;strong&gt;Installation and Dependencies&lt;/strong&gt; tutorial from ESPRESSIF&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://docs.espressif.com/projects/esptool/en/latest/esp32/troubleshooting.html#troubleshooting" rel="noopener noreferrer"&gt;Check out the &lt;strong&gt;Troubleshooting&lt;/strong&gt; from ESPRESSIF if you still get Errors&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;Using MicroPython&lt;/p&gt;


&lt;h2&gt;
  
  
  Source(s)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Micropython API for ESP32 Boards: &lt;a href="https://docs.micropython.org/en/latest/esp32/quickref.html" rel="noopener noreferrer"&gt;https://docs.micropython.org/en/latest/esp32/quickref.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ESPRESSIF documentation: &lt;a href="https://docs.espressif.com/projects/esptool/en/latest/esp32/index.html#" rel="noopener noreferrer"&gt;https://docs.espressif.com/projects/esptool/en/latest/esp32/index.html#&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  Lesson 2: Using MicroPython &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Objectives
&lt;/h2&gt;

&lt;p&gt;Next, we will get a REPL promt in which we can use MicroPython on the ESP32.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites and required Equipment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ESP32 DevKitC&lt;/li&gt;
&lt;li&gt;Computer&lt;/li&gt;
&lt;li&gt;USB-Cable which allows data transfer &lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Solution Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In order to use Pymakr extension on VS Code, you need node.js installed on your computer. &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;Use this link to get to the download page.&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open Visual Studio Code and install the Pymakr Extension (ID: pycom.pymakr)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have any trouble, &lt;a href="https://randomnerdtutorials.com/micropython-esp32-esp8266-vs-code-pymakr/#pymakr" rel="noopener noreferrer"&gt;here is a detailed manual&lt;/a&gt; (at the top of the page you can also follow the manual for installing VS Code)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit the pymakr.json file which opens automatically after installing Pymakr, in case it did not: Hit the "ALL commands" button at the very bottom of the VS Code window and click on "Global Setting".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pymakr has a list of USB devices it will connect to. You need to make sure that your device is on the list in the pymakr.json configuration file. You need to add the USB Manufacturer that the device uses to connect to the PC. In our case, the ESP32 uses the "Silicon Labs" USB drivers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To find out which drivers your board uses, open your computer’s Device Manager (with the board connected to your computer) and search for connected USB devices. Under "General" you find the "Manufacturer". Now, edit the file to add the Silicon Labs manufacturer (or other) to the &lt;code&gt;autoconnect_comport_manufacturers&lt;/code&gt;&lt;br&gt;&lt;br&gt;
section. Then, save the file. The section should look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"autoconnect_comport_manufacturers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"Pycom"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"Pycom Ltd."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"FTDI"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"Microchip Technology, Inc."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"Silicon Labs"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Connect your flashed ESP32 to your computer and hit the "Pymakr Console" button at the very bottom left of the VS Code window. Navigate to the Pymakr Console (similar to powershell in VS Code). You will get the MicroPython REPL indicated by &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2F1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2F1.png" width="" height=""&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can now test the REPL promt by entering:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hallo Studierende!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Your output should look like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hallo Studierende!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Hallo&lt;/span&gt; &lt;span class="n"&gt;Studierende&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;If it does, congratulations, you are using MicroPython on your ESP32 🎉🥳
&amp;lt;!--- einfach reinkopieren z.B. von &lt;a href="https://emojipedia.org/party-popper/" rel="noopener noreferrer"&gt;https://emojipedia.org/party-popper/&lt;/a&gt; ---&amp;gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  For a Warm-Up try:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;some data&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;some data&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;boot.py&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;main.py&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mkdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;dir&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remoce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dir&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;There are two files that are treated specially by the ESP32 (and ESP8266) when it starts up: boot.py and main.py. The boot.py script is executed first (if it exists) and then once it is completed the main.py script is executed. You can create these files yourself and populate them with the code that you want to run when the device starts up.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  REPL promt tips:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pressing ctrl-E will enter a special paste mode. This allows you to copy and paste a chunk of text into the REPL. If you press ctrl-E you will see the paste-mode prompt:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;paste&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;Ctrl&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Ctrl&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;finish&lt;/span&gt;
&lt;span class="o"&gt;===&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can then paste (or type) your text in. Note that none of the special keys or commands work in paste mode (e.g. Tab or backspace), they are just accepted as-is. Press ctrl-D to finish entering the text and execute it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There are four other control commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ctrl-A on a blank line will enter raw REPL mode. This is like a permanent paste mode, except that characters are not echoed back.&lt;/li&gt;
&lt;li&gt;Ctrl-B on a blank like goes to normal REPL mode.&lt;/li&gt;
&lt;li&gt;Ctrl-C cancels any input, or interrupts the currently running code.&lt;/li&gt;
&lt;li&gt;Ctrl-D on a blank line will do a soft reset.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Hints and pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Regarding pymakr.json: put your COMx in the address setting and set auto_correct to &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Regarding pymakr.json: For automatic connect, let the address setting empty, and set the auto_connect to &lt;code&gt;True&lt;/code&gt;. Play around with these settings if you encounter problems.&lt;/li&gt;
&lt;li&gt;If you can't get the Pymakr Console after opening VS Code make sure your computer is not running on a battery saving mode or similar. Set it to high performance mode and open VS Code again.&lt;/li&gt;
&lt;li&gt;If you cannot get the REPL promt indicated by &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;, perform a keyboard interrupt or click the "Upload" or "Run" button at the very bottom of the VS Code window. Play around, that was sometimes necessary on my system.&lt;/li&gt;
&lt;li&gt;If you cannot connect your ESP32, you can try it with TeraTerm or &lt;code&gt;picocom&lt;/code&gt; on Linux. &lt;a href="https://randomnerdtutorials.com/micropython-esp32-esp8266-vs-code-pymakr/#pymakr" rel="noopener noreferrer"&gt;Read here for a detailed tutorial&lt;/a&gt;. Don not forget to set the baudrate to &lt;code&gt;115200&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Useful Resources for Own Searches
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://randomnerdtutorials.com/micropython-esp32-esp8266-vs-code-pymakr/#pymakr" rel="noopener noreferrer"&gt;Click here for a &lt;strong&gt;detailed tutorial&lt;/strong&gt; on how to install VS Code and the Pymakr extension&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.micropython.org/en/latest/esp8266/tutorial/index.html" rel="noopener noreferrer"&gt;Click here for a &lt;strong&gt;MicroPython tutorial&lt;/strong&gt; for the ESP8266 (most of them also work on your ESP32)&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;Setting up you Wifi connection and connect to a public test MQTT broker.&lt;/p&gt;


&lt;h2&gt;
  
  
  Source(s)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Micropython API: &lt;a href="https://docs.micropython.org/en/latest/index.html" rel="noopener noreferrer"&gt;https://docs.micropython.org/en/latest/index.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Random nerd tutorials: &lt;a href="https://randomnerdtutorials.com/micropython-esp32-esp8266-vs-code-pymakr/#pymakr" rel="noopener noreferrer"&gt;https://randomnerdtutorials.com/micropython-esp32-esp8266-vs-code-pymakr/#pymakr&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  Lesson 3: Wifi and MQTT Connection &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Objectives
&lt;/h2&gt;

&lt;p&gt;In this lesson you will set up a connection to your Wifi. Followed by connecting your ESP32 as a client to a public testing broker. Moreover we will subscribe to a topic and publish some data to get familiar with MQTT.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites and required Equipment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ESP32 DevKitC&lt;/li&gt;
&lt;li&gt;Computer&lt;/li&gt;
&lt;li&gt;USB-Cable which allows data transfer&lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Solution Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;First, we will implement an automatic Wifi connection. This is done by writing the code into the &lt;code&gt;boot.py&lt;/code&gt; file. Create a new file in the explorer (from VS Code on the left side of the window and name it &lt;code&gt;boot.py&lt;/code&gt;). The code in this file will be executed automatically after every start of the module. (For testing purposes you can also first enter the code one by one into the REPL promt and see if your connection works.) To do so write the following lines (in the &lt;code&gt;boot.py&lt;/code&gt;):
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;network&lt;/span&gt;

&lt;span class="n"&gt;ssid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your SSID&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Your password&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; 

&lt;span class="n"&gt;station&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;network&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WLAN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;network&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STA_IF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# creates station interface
&lt;/span&gt;
&lt;span class="n"&gt;station&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# activates the interface
&lt;/span&gt;&lt;span class="n"&gt;station&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ssid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# connects to wifi
&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;station&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isconnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;pass&lt;/span&gt; &lt;span class="c1"&gt;# waits till module is connected
&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Connection successful&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;station&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ifconfig&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
&lt;span class="c1"&gt;# prints the interface's IP/netmask/gw/DNS addresses
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Save the boot.py file and click the "Upload" button at the very bottom of the VS Code window. The whole project will be uploaded to your ESP32 and executed afterwards. If you just want to upload the boot.py file you can do that by clicking -&amp;gt; "All commands" -&amp;gt; "Upload current file only" (at the bottom of VS Code).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When executed successfully, your terminal should print something like:   &lt;code&gt;Connection successful&lt;br&gt;
('194.xxx.xxx.xxx', '254.xxx.xxx.xxx', '194.xxx.xxx.xxx', '194.xxx.xxx.xxx')&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;If that is the case, congratulations 🎉 you connected the ESP32 with your Wifi.   &lt;/p&gt;

&lt;p&gt;Lets move on with MQTT:&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a new file named &lt;code&gt;umqttsimple.py&lt;/code&gt; and copy the umqttsimple library code into it. You can access the umqttsimple library code via the following link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://raw.githubusercontent.com/RuiSantosdotme/ESP-MicroPython/master/code/MQTT/umqttsimple.py" rel="noopener noreferrer"&gt;https://raw.githubusercontent.com/RuiSantosdotme/ESP-MicroPython/master/code/MQTT/umqttsimple.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to save the file.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moving on you will have to &lt;strong&gt;extend&lt;/strong&gt; the &lt;code&gt;boot.py&lt;/code&gt; by &lt;strong&gt;adding&lt;/strong&gt; the following lines:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;umqttsimple&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MQTTClient&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ubinascii&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;machine&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;micropython&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;esp&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;esp32&lt;/span&gt;          &lt;span class="c1"&gt;# import all needed libraries
&lt;/span&gt;&lt;span class="n"&gt;esp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;osdebug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# debug set to none
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gc&lt;/span&gt;             &lt;span class="c1"&gt;# garbage collector
&lt;/span&gt;&lt;span class="n"&gt;gc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;mqtt_server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;YOUR MQTT BROKER&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;#mqtt_server = "broker.emqx.io"     # works best for me
#mqtt_server = "broker.hivemq.com"
#mqtt_server = '3.65.154.195:1883'  #broker.hivemq.com
#mqtt_server = '5.196.95.208'       #test.mosquitto.org
&lt;/span&gt;
&lt;span class="n"&gt;client_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ubinascii&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hexlify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;machine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unique_id&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;# gets ID
&lt;/span&gt;&lt;span class="n"&gt;topic_sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;OnBoard_Temp&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# toppic name
&lt;/span&gt;
&lt;span class="n"&gt;last_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
&lt;span class="n"&gt;message_interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;             &lt;span class="c1"&gt;# variables needed for improved timing
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Don't forget to save.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;last_message&lt;/code&gt; variable will hold the last time a message was sent. The &lt;code&gt;message_interval&lt;/code&gt; is the time between each message sent. Here we are setting it to 5 seconds (this means a new message will be sent every 5 seconds). The &lt;code&gt;counter&lt;/code&gt; variable is simply a counter to be added to the message.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;We will create the &lt;code&gt;main.py&lt;/code&gt; now. To do so, copy the following code into it:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Callback func handles what happens when message is received on a certain topic
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sub_cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# responsible for connecting to broker as well as to subscribe to a topic
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connect_and_subscribe&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic_sub&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MQTTClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1883&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;emqx&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0815&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sub_cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Connected to %s MQTT broker, subscribed to %s topic&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;restart_and_reconnect&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Failed to connect to MQTT broker. Reconnecting...&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;machine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;connect_and_subscribe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;OSError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nf"&gt;restart_and_reconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;check_msg&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;last_message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;message_interval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;tf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;esp32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raw_temperature&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;32.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Message #%d&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;: Temperature: %s degree Celsius&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;last_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;OSError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;restart_and_reconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Don't forget to save.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will receive and publish messages in the while loop. We use &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt; statements to prevent the ESP from crashing in case something goes wrong.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the &lt;code&gt;try&lt;/code&gt; block we start by applying the &lt;code&gt;check_msg()&lt;/code&gt; method on the client.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;check_msg()&lt;/code&gt; method checks whether a pending message from the server is available. It waits for a single incoming MQTT message and process it. The subscribed messages are delivered to the callback function we have defined earlier (the &lt;code&gt;sub_cb()&lt;/code&gt; function). If there is not a pending message, it returns with &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we add an if statement to check whether 5 seconds (&lt;code&gt;message_interval&lt;/code&gt;) have passed since the last message was sent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We prepare the data we want to send. In our case I have chosen the internal temperature (there is also an internal hall sensor). We format the data as we need it and put it in the &lt;code&gt;msg&lt;/code&gt; variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To publish a message on a certain topic, you just need to apply the &lt;code&gt;publish()&lt;/code&gt; method on the client and pass as arguments the topic and the message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After sending the message, we update the last time a message was received by setting the &lt;code&gt;last_message&lt;/code&gt; variable to the current time.&lt;br&gt;
Finally, we increase the &lt;code&gt;counter&lt;/code&gt; variable in every loop.&lt;br&gt;
If something unexpected happens, we call the &lt;code&gt;restart_and_reconnect()&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Finally, we can upload the files to the ESP32 ("Upload" button at the bottom). Your device will execute the code automatically when the upload succeeds.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2F2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2F2.png" width="" height=""&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If your output looks like in the picture above, you are done with this lesson 🥳💃🏽🕺🏽&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Question for you: What temperature is measured?&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Hints and pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You may need to open the port 1883 in your firewall.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As you may have noticed, I am giving you a few options to play with regarding the &lt;code&gt;mqtt_server&lt;/code&gt; variable. On my system the "broker.emqx.io" works best. Additionally I encountered that the &lt;strong&gt;IP address written in letters&lt;/strong&gt; works "better" than written in numbers. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Connecting to a MQTT broker worked on my system after providing a &lt;code&gt;user&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; within the &lt;code&gt;MQTTClient()&lt;/code&gt;. What you write in those variables is not crucial for now.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When your device will not connect, try to connect to test.mosquitto.org via the terminal. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When your device will not connect but connecting to test.mosquitto.org in the terminal(s) worked, you can try to make it work with Arduino first...&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Useful Resources for Own Searches
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://boneskull.com/micropython-on-esp32-part-2/" rel="noopener noreferrer"&gt;Click here to &lt;strong&gt;get on the Good Foot with MicroPython on the ESP32&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gloveboxes/ESP32-MicroPython-BME280-MQTT-Sample/blob/master/Micropython.md" rel="noopener noreferrer"&gt;Click here for &lt;strong&gt;another approach for streaming data from ESP32 using MicroPython and MQTT&lt;/strong&gt; (umqtt.robust)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/26716279/how-to-test-the-mosquitto-server" rel="noopener noreferrer"&gt;Click here for &lt;strong&gt;testing mosquitto in the terminal(s)&lt;/strong&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.emqx.com/en/blog/esp32-connects-to-the-free-public-mqtt-broker" rel="noopener noreferrer"&gt;Click here for a &lt;strong&gt;Arduino tutorial&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;Encrypted connection to a MQTT broker.&lt;/p&gt;


&lt;h2&gt;
  
  
  Further Inputs
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Play around with the hall sensor data or maybe you have another external sensor you can connect to your ESP32.&lt;/p&gt;

&lt;p&gt;For that check out the possibilities the ESP32 has to offer:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2FESP32_Function_Block_Diagram.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/.%2Ffiles%2FESP32_Function_Block_Diagram.svg" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9dsatada9ybdr2xutiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9dsatada9ybdr2xutiw.png" alt="DevKitC" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Source(s)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;MicroPython Hackathon: &lt;a href="https://micropython-iot-hackathon.readthedocs.io/en/latest/mqtt.html" rel="noopener noreferrer"&gt;https://micropython-iot-hackathon.readthedocs.io/en/latest/mqtt.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Random nerd tutorials 2: &lt;a href="https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/" rel="noopener noreferrer"&gt;https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Quick reference for the ESP32 - Micropython API: &lt;a href="https://docs.micropython.org/en/latest/esp32/quickref.html#networking" rel="noopener noreferrer"&gt;https://docs.micropython.org/en/latest/esp32/quickref.html#networking&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  Lesson 4: MQTT and TLS &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Objectives
&lt;/h2&gt;

&lt;p&gt;In this final lesson you will establish a secured connection with a public test broker. MQTT provides security, but it is not enabled by default. MQTT has the option for Transport Layer Security (TLS) encryption, just as used with HTTPS. It is recommended using that for any system you put into production.&lt;br&gt;
MQTT also provides username/password authentication. Note that the password is transmitted in clear text. Thus, be sure to use TLS encryption if you are using authentication. In our case we already provided the authentication (unencrypted). Nevertheless, it was just to improve the system flow. Another popular way of authenticating is via certificates and can be used in addition to using user name and password authentication. &lt;a href="http://www.steves-internet-guide.com/ssl-certificates-explained/" rel="noopener noreferrer"&gt;Refer to &lt;strong&gt;this&lt;/strong&gt; explanation in order to learn more about TLS/SSL.&lt;/a&gt;  &lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites and required Equipment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ESP32 DevKitC&lt;/li&gt;
&lt;li&gt;Computer&lt;/li&gt;
&lt;li&gt;USB-Cable which allows data transfer&lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Solution Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We first need to install OpenSSL in order to create our certificates and keys. Click &lt;a href="https://github.com/openssl/openssl" rel="noopener noreferrer"&gt;here for GitHub&lt;/a&gt; or &lt;a href="https://slproweb.com/products/Win32OpenSSL.html" rel="noopener noreferrer"&gt;here for the exe&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create CA key pair: Navigate to the Windows start and search OpenSSL. Hit enter on "OpenSSL Command Promt". Make sure you run the following commands as administrator.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl genrsa -des3 -out ca.key 2048
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;genrsa&lt;/strong&gt;: generates a RSA private key&lt;br&gt;&lt;br&gt;
&lt;strong&gt;des3&lt;/strong&gt;: Using DES3 cipher for the key generation&lt;br&gt;&lt;br&gt;
&lt;strong&gt;out&lt;/strong&gt;: specifies the output file name (.key)&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2048&lt;/strong&gt;: number of bits for the private key    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Your output should look like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;openssl genrsa &lt;span class="nt"&gt;-des3&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; ca.key 2048
&lt;span class="go"&gt;Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

&lt;/span&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Enter any password. But remember it, we will need it in a moment again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The pass phrase is used to protect the private key. The generated private file ca.key has both the private and public key.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Create CA certificate: Next we are creating a certificate for the CA, using the key pair created in the step before:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl req -new -x509 -days 1826 -key ca.key -out ca.crt
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;req&lt;/strong&gt;: certificate request and certification utility&lt;br&gt;&lt;br&gt;
&lt;strong&gt;new&lt;/strong&gt;: generate new certificate, it will prompt user for several input fields&lt;br&gt;&lt;br&gt;
&lt;strong&gt;x509&lt;/strong&gt;: create a self signed certificate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;days&lt;/strong&gt;: specify the number of days the certificate is valid&lt;br&gt;&lt;br&gt;
&lt;strong&gt;key&lt;/strong&gt;: key file with private key to be used for signing&lt;br&gt;&lt;br&gt;
&lt;strong&gt;out&lt;/strong&gt;: specifies the file name for the certificate (.crt)  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;You should get something like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-x509&lt;/span&gt; &lt;span class="nt"&gt;-days&lt;/span&gt; 3650 &lt;span class="nt"&gt;-key&lt;/span&gt; ca.key &lt;span class="nt"&gt;-out&lt;/span&gt; ca.crt
&lt;span class="go"&gt;Enter pass phrase for ca.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:Bavaria
Locality Name (eg, city) []:Munich
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Uni
Organizational Unit Name (eg, section) []:Master
Common Name (e.g. server FQDN or YOUR name) []:schue
Email Address []:.

&lt;/span&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;As Common Name use your user name like "schue" in my case. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt; Create broker key pair: Next, we are creating a private key for the server with:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl genrsa -out server.pem 2048
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;genrsa&lt;/strong&gt;: generate a RSA private key&lt;br&gt;&lt;br&gt;
&lt;strong&gt;out&lt;/strong&gt;: specifies the output file name (.pem)&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2048&lt;/strong&gt;: number of bits for the private key  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Create certificate request from CA: That key needs to be certified, so we create a certificate request for it, and the certificate needs to be signed by the CA:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl req -new -out server.csr -key server.pem
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;req&lt;/strong&gt;: certificate request and certification utility&lt;br&gt;&lt;br&gt;
&lt;strong&gt;new&lt;/strong&gt;: create new request file file&lt;br&gt;&lt;br&gt;
&lt;strong&gt;out&lt;/strong&gt;: file name for the certificate signing request (.csr)&lt;br&gt;&lt;br&gt;
&lt;strong&gt;key&lt;/strong&gt;: file name of the key to be certified  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Your output should look like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; server.csr &lt;span class="nt"&gt;-key&lt;/span&gt; server.pem
&lt;span class="go"&gt;You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:Bavaria
Locality Name (eg, city) []:Munich
Organization Name (eg, company) [Internet Widgits Pty Ltd]:UniMuni
Organizational Unit Name (eg, section) []:EL
Common Name (e.g. server FQDN or YOUR name) []:schue
Email Address []:.

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:0815
An optional company name []:IT

&lt;/span&gt;&lt;span class="gp"&gt;C:\Users\schue&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Verify and sign the certificate request: The last step with OpenSSL is to sign the server request through the CA to get the broker certificate:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out cert.der -days 360
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;x509&lt;/strong&gt;: certificate display and signing utility&lt;br&gt;&lt;br&gt;
&lt;strong&gt;req&lt;/strong&gt;: a certificate request is expected as input&lt;br&gt;&lt;br&gt;
&lt;strong&gt;in&lt;/strong&gt;: input file for the certificate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;CA&lt;/strong&gt;: specifies the file to be signed&lt;br&gt;&lt;br&gt;
&lt;strong&gt;CAkey&lt;/strong&gt;: CA private key to sign the certificate with&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Cacreateserial&lt;/strong&gt;: the serial number file gets created if it does not exist&lt;br&gt;&lt;br&gt;
&lt;strong&gt;out&lt;/strong&gt;: output file name&lt;br&gt;&lt;br&gt;
&lt;strong&gt;days&lt;/strong&gt;: how long the certificate shall be valid  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Convert your .pem file to .der file:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;openssl rsa -inform pem -in server.pem -outform der -out key.der
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Finally move the created files to the ESP32: To do so get mpfshell:&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/hardye/657385210c5d613e69cb5ba95e8c57a7" rel="noopener noreferrer"&gt;Click here if the command below does not work.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;pip3 install mpfshell
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Make sure your device is not connected via a Pymakr console or anywhere else. Then enter (on Windows) (and insert YOUR port number):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;mpfshell -c "open COMx"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;and on Linux:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;mpfshell -c "open ttyUSBx"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you can list the files on the device with:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;mpfs&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To upload e.g. the local file &lt;code&gt;boot.py&lt;/code&gt; to the device use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;mpfs&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;put boot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You need to navigate to the directory  (&lt;code&gt;cd&lt;/code&gt;) which contains the &lt;code&gt;boot.py&lt;/code&gt; first.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/wendlers/mpfshell" rel="noopener noreferrer"&gt;Get more mpf commands &lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Lastly, we can adjust our code to use the "secured" connection (we are still connectiong to a public test broker! But you get the main idea, in case you would like to implement a TLS connection to your own broker).
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add&lt;/strong&gt; the following lines to your &lt;code&gt;boot.py&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;key.der&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;key_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cert.der&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;cert_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Change&lt;/strong&gt; the &lt;code&gt;connect_and_subscribe()&lt;/code&gt; in your &lt;code&gt;main.py&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connect_and_subscribe&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cert_data&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MQTTClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8883&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;emqx&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0815&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                      &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ssl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ssl_params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cert&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cert_data&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sub_cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Connected to %s MQTT broker, subscribed to %s topic&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mqtt_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic_sub&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;My output:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Uploading project (main folder)...
Not safe booting, disabled in settings

Uploading to /...
Reading file status
No files to upload
Upload done, resetting board...
OKets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:5656
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
Connection successful
('194.xxx.xxx.xxx', '254.xxx.xxx.xxx', '194.xxx.xxx.xxx', '194.xxx.xxx.xxx')
Connected to broker.emqx.io MQTT broker, subscribed to b'OnBoard_Temp' topic
&lt;/span&gt;&lt;span class="gp"&gt;(b'OnBoard_Temp', b'Message #&lt;/span&gt;0: Temperature: 40.0 degree Celsius&lt;span class="s1"&gt;')
&lt;/span&gt;&lt;span class="gp"&gt;(b'OnBoard_Temp', b'Message #&lt;/span&gt;&lt;span class="s1"&gt;1: Temperature: 40.0 degree Celsius'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;(b'OnBoard_Temp', b'Message #&lt;/span&gt;2: Temperature: 40.0 degree Celsius&lt;span class="s1"&gt;')
&lt;/span&gt;&lt;span class="gp"&gt;(b'OnBoard_Temp', b'Message #&lt;/span&gt;&lt;span class="s1"&gt;3: Temperature: 40.0 degree Celsius'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Yeay, we are publishing our data over TLS.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Hints and pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You may need to open the port 8883 in your firewall.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can move the cert files to your VS Code workspace and hit the upload button. But it will not upload the certificates to your device!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can also use ampy to move files to the ESP32. This is the most common way online. Nevertheless, ampy did not work on my system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since my colleagues encountered problems with mpfshell and ampy on their systems, I am giving you a last option: You can use the uPyCraft IDE to transfer files to your ESP32. Installation instructions for: &lt;a href="https://randomnerdtutorials.com/install-upycraft-ide-windows-pc-instructions/" rel="noopener noreferrer"&gt;Windows&lt;/a&gt;, &lt;a href="https://randomnerdtutorials.com/install-upycraft-ide-mac-os-x-instructions/" rel="noopener noreferrer"&gt;MacOS X&lt;/a&gt; and &lt;a href="https://randomnerdtutorials.com/install-upycraft-ide-linux-ubuntu-instructions/" rel="noopener noreferrer"&gt;Linux&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Useful Resources for Own Searches
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mcuoneclipse.com/2017/04/14/introduction-to-security-and-tls-transport-security-layer/" rel="noopener noreferrer"&gt;Click here for &lt;strong&gt;another introduction to TLS&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gloveboxes/ESP32-MicroPython-BME280-MQTT-Sample/blob/master/Micropython.md" rel="noopener noreferrer"&gt;Click here for &lt;strong&gt;another approach for streaming data from ESP32 using MicroPython and MQTT&lt;/strong&gt; (umqtt.robust)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/micropython/micropython-esp32/issues/90" rel="noopener noreferrer"&gt;ENOENT ERROR&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Further Inputs
&lt;/h2&gt;

&lt;p&gt;You can also encrypt your message before sending it. In our case it will not work. Can you find out why? It would work with a separate broker (with own code). Here are two useful links to begin with:&lt;br&gt;
&lt;a href="https://forum.micropython.org/viewtopic.php?t=6726" rel="noopener noreferrer"&gt;https://forum.micropython.org/viewtopic.php?t=6726&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.micropython.org/en/latest/library/cryptolib.html" rel="noopener noreferrer"&gt;https://docs.micropython.org/en/latest/library/cryptolib.html&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Steve: "Encrypting the MQTT payload rather than the link has the advantage that the data is encrypted end to end and not just between the broker and the client.&lt;br&gt;&lt;br&gt;
It also means that the intermediate brokers don’t need to support SSL and that you don’t need to obtain and install certificates."&lt;br&gt;&lt;br&gt;
Unknown: "It is always a good idea to use further security mechanisms such as payload encryption, payload signature verifications, and authorization mechanisms. In general, more security never hurts."&lt;br&gt;&lt;br&gt;
Do you agree?&lt;br&gt;
What do you think about these quotations?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Congratulations! You are at the end of my tutorial. Maybe you would like to set up your own broker on a Rpy next?&lt;/p&gt;




&lt;h2&gt;
  
  
  Source(s)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Enable Secure Communication with TLS and the Mosquitto Broker: &lt;a href="https://mcuoneclipse.com/2017/04/14/enable-secure-communication-with-tls-and-the-mosquitto-broker/" rel="noopener noreferrer"&gt;https://mcuoneclipse.com/2017/04/14/enable-secure-communication-with-tls-and-the-mosquitto-broker/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mosquitto SSL Configuration: &lt;a href="http://www.steves-internet-guide.com/mosquitto-tls/" rel="noopener noreferrer"&gt;http://www.steves-internet-guide.com/mosquitto-tls/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenSSL Command Line Utilities: &lt;a href="https://wiki.openssl.org/index.php/Command_Line_Utilities" rel="noopener noreferrer"&gt;https://wiki.openssl.org/index.php/Command_Line_Utilities&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>micropython</category>
      <category>mqtt</category>
      <category>esp32</category>
    </item>
  </channel>
</rss>
