{"id":1553,"date":"2020-04-28T21:30:18","date_gmt":"2020-04-28T13:30:18","guid":{"rendered":"https:\/\/www.strongd.net\/?p=1553"},"modified":"2020-04-28T21:30:18","modified_gmt":"2020-04-28T13:30:18","slug":"how-to-use-mqtt-with-the-raspberry-pi-and-esp8266","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1553","title":{"rendered":"How to Use MQTT With the Raspberry Pi and ESP8266"},"content":{"rendered":"<p><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FQH\/51NN\/IZ6COHFI\/FQH51NNIZ6COHFI.LARGE.jpg\" rel=\"photoset188\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FQH\/51NN\/IZ6COHFI\/FQH51NNIZ6COHFI.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FQH\/51NN\/IZ6COHFI\/FQH51NNIZ6COHFI.LARGE.jpg?auto=webp&amp;frame=1&amp;width=804&amp;height=1024&amp;fit=bounds\" alt=\"How to Use MQTT With the Raspberry Pi and ESP8266\" data-src=\"https:\/\/cdn.instructables.com\/FQH\/51NN\/IZ6COHFI\/FQH51NNIZ6COHFI.LARGE.jpg?auto=webp&amp;frame=1&amp;width=804&amp;height=1024&amp;fit=bounds\" \/><\/a><\/p>\n<p>In this Instructable, I will explain what the MQTT protocol is and how it is used to communicate between devices.Then, as a practical demonstration, I shall show you how to setup a simple two client system, where an ESP8266 module will send a message to a Python program when a button is pushed. Specifically, I am using an Adafruit HUZZAH module for this project, a Raspberry Pi and a desktop computer. The Raspberry Pi will be acting as the MQTT broker, and the Python client will be run from a separate desktop computer (optional, as this could be run on the Raspberry Pi).<\/p>\n<p>To follow along with this Instructable, you will need to have some basic knowledge of electronics, and how to use the Arduino software. You should also be familiar with using a command line interface (for the Raspberry Pi). Hopefully, once you&#8217;ve gained the knowledge of what MQTT is, and how to use it in a basic scenario, you will be able to create your own IoT projects!<\/p>\n<p><strong><u>Required Parts<\/u><\/strong><\/p>\n<ul>\n<li>1 x Raspberry Pi, connected to a local network (running Jessie)<\/li>\n<li>1 x ESP8266 Module (Adafruit HUZZAH)<\/li>\n<li>1 x Breadboard<\/li>\n<li>3 x Jumper Wires (Male-to-Male)<\/li>\n<li>1 x Pushbutton<\/li>\n<li>1 x 10k Ohm Resistor (Brown-Black-Orange colour code)<\/li>\n<\/ul>\n<p>I&#8217;ve created this Instructable, as MQTT has always interested me as a protocol and there are many different ways it could be used. However, I couldn&#8217;t seem to get my head around how to code devices to use it. This was because I didn&#8217;t know\/understand what was actually going on to take my &#8220;Hello, World!&#8221; from device A and send it to device B. Hence, I decided to write this Instructable to (hopefully) teach you how it works, and to also reinforce my own understanding of it!<\/p>\n<p>&nbsp;<\/p>\n<h2 class=\"step-title\">Step 1: What Is MQTT?<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FQS\/CY7K\/IZ6C97H3\/FQSCY7KIZ6C97H3.LARGE.jpg\" rel=\"photoset191\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FQS\/CY7K\/IZ6C97H3\/FQSCY7KIZ6C97H3.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FQS\/CY7K\/IZ6C97H3\/FQSCY7KIZ6C97H3.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1024&amp;height=1024&amp;fit=bounds\" alt=\"What Is MQTT?\" data-src=\"https:\/\/cdn.instructables.com\/FQS\/CY7K\/IZ6C97H3\/FQSCY7KIZ6C97H3.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1024&amp;height=1024&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>MQTT, or MQ Telemetry Transport, is a messaging protocol which allows multiple devices to talk to each other. Currently, it is a popular protocol for the Internet of Things, although it has been used for other purposes &#8211; for example, Facebook Messenger. Interestingly MQTT was invented in 1999 &#8211; meaning it&#8217;s as old as me!<\/p>\n<p>MQTT is based around the idea that devices can\u00a0<strong>publish\u00a0<\/strong>or\u00a0<strong>subscribe<\/strong>\u00a0to\u00a0<strong>topics<\/strong>. So, for example. If Device #1 has recorded the temperature from one of its sensors, it can\u00a0<strong>publish<\/strong>\u00a0a message which contains the temperature value it recorded, to a topic (e.g.\u00a0<em>&#8220;Temperature&#8221;<\/em>). This message is sent to an MQTT Broker, which you can think of as a switch\/router on a local area network. Once the MQTT Broker has received the message, it will send it to any devices (in this case, Device #2) which are\u00a0<strong>subscribed\u00a0<\/strong>to the same topic.<\/p>\n<p>In this project, we will be publishing to a topic using an ESP8266, and creating a Python script that will subscribe to this same topic, via a Raspberry Pi which will act as the MQTT Broker. The great thing about MQTT is that it is lightweight, so it perfect for running on small microcontrollers such as an ESP8266, but it is also widely available &#8211; so we can run it on a Python script as well.<\/p>\n<p>Hopefully, at the end of this project, you will have an understanding of what MQTT is and how to use it for your own projects in the future.<\/p>\n<h2 class=\"step-title\">Step 2: Installing the MQTT Broker on the Raspberry Pi<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-3 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/F2C\/U67A\/IZ6C9AQ5\/F2CU67AIZ6C9AQ5.LARGE.jpg\" rel=\"photoset194\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/F2C\/U67A\/IZ6C9AQ5\/F2CU67AIZ6C9AQ5.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/F2C\/U67A\/IZ6C9AQ5\/F2CU67AIZ6C9AQ5.LARGE.jpg?auto=webp&amp;frame=1&amp;crop=3:2&amp;width=825&amp;height=1024&amp;fit=bounds\" alt=\"Installing the MQTT Broker on the Raspberry Pi\" data-src=\"https:\/\/cdn.instructables.com\/F2C\/U67A\/IZ6C9AQ5\/F2CU67AIZ6C9AQ5.LARGE.jpg?auto=webp&amp;frame=1&amp;crop=3:2&amp;width=825&amp;height=1024&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<div class=\"photoset-col\">\n<div class=\"photoset-cell\" data-imagenotes=\"[{&quot;id&quot;:&quot;N67TKE5IZ6CAHJJ&quot;,&quot;author&quot;:&quot;Tango172&quot;,&quot;text&quot;:&quot;Delete this line&quot;,&quot;top&quot;:0.3546218487394958,&quot;left&quot;:0,&quot;height&quot;:0.08067226890756303,&quot;width&quot;:0.3563501849568434}]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FF8\/8M0A\/IZ6CAHCS\/FF88M0AIZ6CAHCS.LARGE.jpg\" rel=\"photoset194\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FF8\/8M0A\/IZ6CAHCS\/FF88M0AIZ6CAHCS.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FF8\/8M0A\/IZ6CAHCS\/FF88M0AIZ6CAHCS.LARGE.jpg?auto=webp&amp;frame=1&amp;width=375&amp;fit=bounds\" alt=\"Installing the MQTT Broker on the Raspberry Pi\" data-src=\"https:\/\/cdn.instructables.com\/FF8\/8M0A\/IZ6CAHCS\/FF88M0AIZ6CAHCS.LARGE.jpg?auto=webp&amp;frame=1&amp;width=375&amp;fit=bounds\" \/><i class=\"notes-icon\"><\/i><\/a><\/div>\n<\/div>\n<div class=\"photoset-cell\" data-imagenotes=\"[{&quot;id&quot;:&quot;NWCTKDUIZ6CAHL9&quot;,&quot;author&quot;:&quot;Tango172&quot;,&quot;text&quot;:&quot;Add these lines to the file&quot;,&quot;top&quot;:0.35858585858585856,&quot;left&quot;:0,&quot;height&quot;:0.15151515151515152,&quot;width&quot;:0.37623762376237624}]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/F35\/LXFD\/IZ6CAHCR\/F35LXFDIZ6CAHCR.LARGE.jpg\" rel=\"photoset194\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/F35\/LXFD\/IZ6CAHCR\/F35LXFDIZ6CAHCR.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/F35\/LXFD\/IZ6CAHCR\/F35LXFDIZ6CAHCR.LARGE.jpg?auto=webp&amp;frame=1&amp;width=375&amp;fit=bounds\" alt=\"Installing the MQTT Broker on the Raspberry Pi\" data-src=\"https:\/\/cdn.instructables.com\/F35\/LXFD\/IZ6CAHCR\/F35LXFDIZ6CAHCR.LARGE.jpg?auto=webp&amp;frame=1&amp;width=375&amp;fit=bounds\" \/><i class=\"notes-icon\"><\/i><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>To setup our MQTT system, we need a broker, as explained in the previous step. For the Raspberry Pi, we will be using the &#8220;<em>Mosquitto<\/em>&#8221; MQTT broker. Before we install this, it is always best to update our Raspberry Pi.<\/p>\n<pre>sudo apt-get update\r\nsudo apt-get upgrade<\/pre>\n<p>Once you&#8217;ve done this, install\u00a0<strong>mosquitto\u00a0<\/strong>and then the\u00a0<strong>mosquitto-clients<\/strong>\u00a0packages.<\/p>\n<pre>sudo apt-get install mosquitto -y\r\nsudo apt-get install mosquitto-clients -y<\/pre>\n<p>When you&#8217;ve finished installing these two packages, we are going to need to configure the broker. The mosquitto broker&#8217;s configuration file is located at\u00a0<strong>\/etc\/mosquitto\/mosquitto.conf<\/strong>, so open this with your favourite text editor. If you don&#8217;t have a favourite text editor or don&#8217;t know how to use any of the command line editors, I&#8217;ll be using\u00a0<strong>nano\u00a0<\/strong>so you can follow along:<\/p>\n<pre>sudo nano \/etc\/mosquitto\/mosquitto.conf<\/pre>\n<p>At the bottom of this file, you should see the line:<\/p>\n<pre>include_dir \/etc\/mosquitto\/conf.d<\/pre>\n<p>Delete this line. Add the following lines to the bottom of the file.<\/p>\n<pre>allow_anonymous false\r\npassword_file \/etc\/mosquitto\/pwfile\r\nlistener 1883<\/pre>\n<p>By typing those lines, we&#8217;ve told mosquitto that we don&#8217;t want anyone connecting to our broker who doesn&#8217;t supply a valid username and password (we&#8217;ll get on to set these in a second) and that we want mosquitto to listen for messages on port number 1883.<\/p>\n<p><em>If you don&#8217;t want the broker to require a username and password, don&#8217;t include the first two lines that we added (i.e. allow_anonymous&#8230; and password_file&#8230;). If you have done this, then skip to rebooting the Raspberry Pi.<\/em><\/p>\n<p>Now close (and save) that file. If you are following along with the nano example, press\u00a0<strong>CTRL+X<\/strong>, and type\u00a0<strong>Y<\/strong>\u00a0when prompted.<\/p>\n<p>Because we&#8217;ve just told mosquitto that users trying to use the MQTT broker need to be authenticated, we now need to tell mosquitto what the username and password are! So, type the following command &#8211; replacing\u00a0<strong>username<\/strong>\u00a0with the username that you would like &#8211; then enter the password you would like when prompted (Note: if, when editing the configuration file, you specified a different\u00a0<strong>password_file<\/strong>\u00a0path, replace the path below with the one you used).<\/p>\n<pre>sudo mosquitto_passwd -c \/etc\/mosquitto\/pwfile username<\/pre>\n<p>As we&#8217;ve just changed the mosquitto configuration file, we should reboot the Raspberry Pi.<\/p>\n<pre>sudo reboot<\/pre>\n<p>Once the Raspberry Pi has finished rebooting, you should have a fully functioning MQTT broker! Next, we are going to try to interact with it, using a number of different devices\/methods!<\/p>\n<h2 class=\"step-title\">Step 3: Testing the Broker<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FWY\/XQC0\/IZ6CBRXG\/FWYXQC0IZ6CBRXG.LARGE.jpg\" rel=\"photoset199\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FWY\/XQC0\/IZ6CBRXG\/FWYXQC0IZ6CBRXG.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FWY\/XQC0\/IZ6CBRXG\/FWYXQC0IZ6CBRXG.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1024&amp;fit=bounds\" alt=\"Testing the Broker\" data-src=\"https:\/\/cdn.instructables.com\/FWY\/XQC0\/IZ6CBRXG\/FWYXQC0IZ6CBRXG.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1024&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>Once you&#8217;ve installed mosquitto on the Raspberry Pi, you can give it a quick test &#8211; just to make sure everything is working correctly. For this purpose, there are two commands that we can use on the command line.\u00a0<strong>mosquitto_pub<\/strong>\u00a0and\u00a0<strong>mosquitto_sub<\/strong>. In this step, I will guide you through using each of these to test our broker.<\/p>\n<p>In order to test the broker, you will need to open two command line windows. If you are using Putty or another SSH client, this is as simple as opening another SSH window and logging in as usual. If you are accessing your Pi from a UNIX terminal, this is exactly the same. If you are using the Raspberry Pi directly, you will need to open two terminal windows in the GUI mode (the command\u00a0<strong>startx<\/strong>can be used to start the GUI).<\/p>\n<p>Now that you have opened two windows, we can get started on the testing. In one of the two terminals, type the following command, replacing\u00a0<strong>username<\/strong>\u00a0and\u00a0<strong>password\u00a0<\/strong>with the ones you setup in the previous step.<\/p>\n<pre>mosquitto_sub -d -u username -P password -t test<\/pre>\n<p><em>If you decided not to set a username and password in the previous step, then from now on, ignore the -u and -P flags in the commands. So, as an example, the mosquitto_sub command would now be:<\/em><\/p>\n<pre>mosquitto_sub -d -t test<\/pre>\n<p>The mosquitto_sub command will subscribe to a topic, and display any messages that are sent to the specified topic in the terminal window. Here,\u00a0<strong>-d<\/strong>\u00a0means\u00a0<strong>debug mode<\/strong>, so all messages and activity will be output on the screen.\u00a0<strong>-u<\/strong>\u00a0and<strong>\u00a0-P\u00a0<\/strong>should be self-explanatory. Finally,\u00a0<strong>-t<\/strong>\u00a0is the name of the\u00a0<strong>topic\u00a0<\/strong>we want to subscribe to &#8211; in this case, &#8220;test&#8221;.<\/p>\n<p>Next, in the other terminal window, we are going to try and publish a message to the &#8220;test&#8221; topic. Type the following, remembering again to change\u00a0<strong>username\u00a0<\/strong>and\u00a0<strong>password<\/strong>:<\/p>\n<pre>mosquitto_pub -d -u username -P password -t test -m \"Hello, World!\"<\/pre>\n<p>When you press enter, you should see your message\u00a0<strong>&#8220;Hello, World!&#8221;<\/strong>\u00a0appear in the first terminal window we used (to subscribe). If this is the case, you&#8217;re all set to start working on the ESP8266!<\/p>\n<h2 class=\"step-title\">Step 4: Setting Up the ESP8266 (Adafruit HUZZAH)<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-4 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FQF\/1PMH\/IZ6C995G\/FQF1PMHIZ6C995G.LARGE.jpg\" rel=\"photoset202\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FQF\/1PMH\/IZ6C995G\/FQF1PMHIZ6C995G.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FQF\/1PMH\/IZ6C995G\/FQF1PMHIZ6C995G.LARGE.jpg?auto=webp&amp;frame=1&amp;width=939&amp;fit=bounds\" alt=\"Setting Up the ESP8266 (Adafruit HUZZAH)\" data-src=\"https:\/\/cdn.instructables.com\/FQF\/1PMH\/IZ6C995G\/FQF1PMHIZ6C995G.LARGE.jpg?auto=webp&amp;frame=1&amp;width=939&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<div class=\"photoset-col\">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FCO\/X380\/IZ6C9B2S\/FCOX380IZ6C9B2S.LARGE.jpg\" rel=\"photoset202\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FCO\/X380\/IZ6C9B2S\/FCOX380IZ6C9B2S.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FCO\/X380\/IZ6C9B2S\/FCOX380IZ6C9B2S.LARGE.jpg?auto=webp&amp;frame=1&amp;width=261&amp;fit=bounds\" alt=\"Setting Up the ESP8266 (Adafruit HUZZAH)\" data-src=\"https:\/\/cdn.instructables.com\/FCO\/X380\/IZ6C9B2S\/FCOX380IZ6C9B2S.LARGE.jpg?auto=webp&amp;frame=1&amp;width=261&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FSG\/ABPA\/IZ6C9B2T\/FSGABPAIZ6C9B2T.LARGE.jpg\" rel=\"photoset202\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FSG\/ABPA\/IZ6C9B2T\/FSGABPAIZ6C9B2T.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FSG\/ABPA\/IZ6C9B2T\/FSGABPAIZ6C9B2T.LARGE.jpg?auto=webp&amp;frame=1&amp;crop=3:2&amp;width=261&amp;fit=bounds\" alt=\"Setting Up the ESP8266 (Adafruit HUZZAH)\" data-src=\"https:\/\/cdn.instructables.com\/FSG\/ABPA\/IZ6C9B2T\/FSGABPAIZ6C9B2T.LARGE.jpg?auto=webp&amp;frame=1&amp;crop=3:2&amp;width=261&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FJW\/DCSF\/IZ6C9B2R\/FJWDCSFIZ6C9B2R.LARGE.jpg\" rel=\"photoset202\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FJW\/DCSF\/IZ6C9B2R\/FJWDCSFIZ6C9B2R.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FJW\/DCSF\/IZ6C9B2R\/FJWDCSFIZ6C9B2R.LARGE.jpg?auto=webp&amp;frame=1&amp;width=261&amp;fit=bounds\" alt=\"Setting Up the ESP8266 (Adafruit HUZZAH)\" data-src=\"https:\/\/cdn.instructables.com\/FJW\/DCSF\/IZ6C9B2R\/FJWDCSFIZ6C9B2R.LARGE.jpg?auto=webp&amp;frame=1&amp;width=261&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>This step if specific to the Adafruit HUZZAH (as that is what I am using to complete this project). If you are using a different Arduino \/ ESP8266 device, you may wish to skip this step. However, I would advise you skim read it, just in case there is any information here that may be relevant to you.<\/p>\n<p>For this project, I am going to be programming the HUZZAH with the Arduino software. So, if you haven&#8217;t already, make sure to install the Arduino software (<strong>newer<\/strong>\u00a0than\u00a0<strong>1.6.4<\/strong>). You can download it\u00a0<a href=\"https:\/\/www.arduino.cc\/en\/Main\/Software\" rel=\"nofollow\">here<\/a>.<\/p>\n<p>Once you have installed the Arduino software, open it and navigate to\u00a0<em>File-&gt;Preferences<\/em>. Here you should see (near the bottom of the window) a text box with the label:\u00a0<strong>&#8220;Additional Boards Manager URLs&#8221;<\/strong>. In this text box, copy and paste the following link:<\/p>\n<pre>http:\/\/arduino.esp8266.com\/stable\/package_esp8266com_index.json<\/pre>\n<p>Click OK to save your changes. Now open the Board Manager (<em>Tools-&gt;Board-&gt;Board Manager<\/em>) and search for ESP8266. Install the\u00a0<strong>esp8266 by ESP8266 Community<\/strong>\u00a0package. Restart the Arduino software.<\/p>\n<p>Now, before we can program the board, we need to select a few different options. In the Tools menu option, select\u00a0<strong>Adafruit HUZZAH ESP8266<\/strong>\u00a0for Board,\u00a0<strong>80 MHz<\/strong>\u00a0for the CPU Frequency (you can use 160 MHz if you wish to overclock it, but for now I&#8217;m going to use 80 MHz),\u00a0<strong>4M (3M SPIFFS)<\/strong>\u00a0for the Flash Size, and\u00a0<strong>115200<\/strong>\u00a0for the Upload Speed. Also, make sure to select the COM port that you are using (this will depend on your setup).<\/p>\n<p>Before you can upload any code, you need to make sure that the HUZZAH is in bootloader mode. To enable this, hold down the button on the board marked\u00a0<strong>GPIO0<\/strong>, and whilst this is held, hold down the\u00a0<strong>Reset<\/strong>\u00a0button as well. Then, release the\u00a0<strong>Reset<\/strong>\u00a0button, and then\u00a0<strong>GPIO0<\/strong>. If you have done this correctly, the red LED that came on when you pressed GPIO0 should now be dimly lit.<\/p>\n<p>To upload code to the microcontroller, first make sure the HUZZAH is in bootloader mode, then simply click the upload button in the Arduino IDE.<\/p>\n<p>If you are having any trouble setting up the HUZZAH, further information can be found at Adafruit&#8217;s own\u00a0<a href=\"https:\/\/learn.adafruit.com\/adafruit-huzzah-esp8266-breakout\/using-arduino-ide\" rel=\"nofollow\">tutorial<\/a>.<\/p>\n<\/div>\n<h2 class=\"step-title\">Step 5: Programming the ESP8266<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FLH\/MNCL\/IZ6CAAI3\/FLHMNCLIZ6CAAI3.LARGE.jpg\" rel=\"photoset208\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FLH\/MNCL\/IZ6CAAI3\/FLHMNCLIZ6CAAI3.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FLH\/MNCL\/IZ6CAAI3\/FLHMNCLIZ6CAAI3.LARGE.jpg?auto=webp&amp;frame=1&amp;width=914&amp;fit=bounds\" alt=\"Programming the ESP8266\" data-src=\"https:\/\/cdn.instructables.com\/FLH\/MNCL\/IZ6CAAI3\/FLHMNCLIZ6CAAI3.LARGE.jpg?auto=webp&amp;frame=1&amp;width=914&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>Now we will begin to program the ESP8266, but before we can start, you will need to install the following libraries in the Arduino Library manager (<em>Sketch-&gt;Include Libraries-&gt;Manage Libraries<\/em>)<\/p>\n<ul>\n<li><strong>Bounce2<\/strong><\/li>\n<li><strong>PubSubClient<\/strong><\/li>\n<\/ul>\n<p>Once you&#8217;ve installed those libraries, you will be able to run the code I&#8217;ve included in this Instructable (MQTT_Publish.zip). I&#8217;ve made sure to comment it so that you can understand what each section is doing, and this should hopefully enable you to adapt it to your needs.<\/p>\n<p><em>Remember to change the constants at the top of the code so that your ESP8266 can connect to your WiFi network and your MQTT Broker (the Raspberry Pi).<\/em><\/p>\n<p><em>If you decided not to set a username and password for the MQTT Broker, then download the MQTT_PublishNoPassword.zip file instead.<\/em><\/p>\n<\/div>\n<div class=\"downloads\">\n<h3 class=\"sr-only\">Attachments<\/h3>\n<ul id=\"attachments\" class=\"ible-files unstyled list-unstyled\">\n<li class=\"file-info clearfix \"><a class=\"thumb-wrapper\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FBC\/K3YU\/IZ6CAAAO\/FBCK3YUIZ6CAAAO.zip\" download=\"MQTT_Publish.zip\"><span class=\"file-thumb\"><img decoding=\"async\" class=\"tiny-img ls-is-cached lazyloaded\" src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" alt=\"download {{ file.name }}\" data-src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" \/><\/span><span class=\"title\">MQTT_Publish.zip<\/span><\/a>\n<div class=\"file-actions\"><a class=\"btn pull-right\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FBC\/K3YU\/IZ6CAAAO\/FBCK3YUIZ6CAAAO.zip\" download=\"MQTT_Publish.zip\">Download<\/a><\/div>\n<\/li>\n<li class=\"file-info clearfix \"><a class=\"thumb-wrapper\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FYO\/3U8D\/IZ6DEKX2\/FYO3U8DIZ6DEKX2.zip\" download=\"MQTT_PublishNoPassword.zip\"><span class=\"file-thumb\"><img decoding=\"async\" class=\"tiny-img lazyloaded\" src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" alt=\"download {{ file.name }}\" data-src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" \/><\/span><span class=\"title\">MQTT_PublishNoPassword.zip<\/span><\/a>\n<div class=\"file-actions\"><a class=\"btn pull-right\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FYO\/3U8D\/IZ6DEKX2\/FYO3U8DIZ6DEKX2.zip\" download=\"MQTT_PublishNoPassword.zip\">Download<\/a><\/div>\n<\/li>\n<\/ul>\n<h2 class=\"step-title\">Step 6: Installing Python Client (paho-mqtt)<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FDC\/UQE3\/IZ6CAIRO\/FDCUQE3IZ6CAIRO.LARGE.jpg\" rel=\"photoset211\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FDC\/UQE3\/IZ6CAIRO\/FDCUQE3IZ6CAIRO.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FDC\/UQE3\/IZ6CAIRO\/FDCUQE3IZ6CAIRO.LARGE.jpg?auto=webp&amp;frame=1&amp;fit=bounds\" alt=\"Installing Python Client (paho-mqtt)\" data-src=\"https:\/\/cdn.instructables.com\/FDC\/UQE3\/IZ6CAIRO\/FDCUQE3IZ6CAIRO.LARGE.jpg?auto=webp&amp;frame=1&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>Thankfully, this step is very simple! To install the mosquitto python client, you just need to type the following into the command line (Linux\/Mac) or even command prompt (Windows).<\/p>\n<pre>pip install paho-mqtt<\/pre>\n<p><em>Note: Windows command prompt may have an issue running the\u00a0<strong>pip\u00a0<\/strong>command if you didn&#8217;t specify that you wanted pip installed and python added to your PATH variable when you installed Python. There are a number of ways of fixing this, but I think just reinstalling Python is the easiest way. If in doubt &#8211; give it a google!<\/em><\/p>\n<h2 class=\"step-title\">Step 7: Python Client &#8211; Subscribing<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FTS\/V843\/IZ6CAIK4\/FTSV843IZ6CAIK4.LARGE.jpg\" rel=\"photoset214\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FTS\/V843\/IZ6CAIK4\/FTSV843IZ6CAIK4.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FTS\/V843\/IZ6CAIK4\/FTSV843IZ6CAIK4.LARGE.jpg?auto=webp&amp;frame=1&amp;fit=bounds\" alt=\"Python Client - Subscribing\" data-src=\"https:\/\/cdn.instructables.com\/FTS\/V843\/IZ6CAIK4\/FTSV843IZ6CAIK4.LARGE.jpg?auto=webp&amp;frame=1&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>In this step, we are going to setup the Python script (either on the Raspberry Pi itself or on another computer connected to the network) to handle all of the messages that are sent (published) by the ESP8266 to the MQTT topic.<\/p>\n<p>I have included the python code below (PythonMQTT_Subscribe.py), which has been commented to help you understand what is going on, but I will explain some of the main features here as well.<\/p>\n<p><em>If you didn&#8217;t set a username and password for the MQTT connection earlier, download the PythonMQTT_SubscribeNoPassword.py file instead.<\/em><\/p>\n<\/div>\n<div class=\"downloads\">\n<h3 class=\"sr-only\">Attachments<\/h3>\n<ul id=\"attachments\" class=\"ible-files unstyled list-unstyled\">\n<li class=\"file-info clearfix \"><a class=\"thumb-wrapper\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FO1\/6NPZ\/JX0NVCDA\/FO16NPZJX0NVCDA.py\" download=\"PythonMQTT_Subscribe.py\"><span class=\"file-thumb\"><img decoding=\"async\" class=\"tiny-img ls-is-cached lazyloaded\" src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file.TINY.gif\" alt=\"download {{ file.name }}\" data-src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file.TINY.gif\" \/><\/span><span class=\"title\">PythonMQTT_Subscribe.py<\/span><\/a>\n<div class=\"file-actions\"><a class=\"btn pull-right\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FO1\/6NPZ\/JX0NVCDA\/FO16NPZJX0NVCDA.py\" download=\"PythonMQTT_Subscribe.py\">Download<\/a><\/div>\n<\/li>\n<li class=\"file-info clearfix \"><a class=\"thumb-wrapper\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FVF\/LWSC\/JX0NVCG0\/FVFLWSCJX0NVCG0.py\" download=\"PythonMQTT_SubscribeNoPassword.py\"><span class=\"file-thumb\"><img decoding=\"async\" class=\"tiny-img lazyloaded\" src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file.TINY.gif\" alt=\"download {{ file.name }}\" data-src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file.TINY.gif\" \/><\/span><span class=\"title\">PythonMQTT_SubscribeNoPassword.py<\/span><\/a>\n<div class=\"file-actions\"><a class=\"btn pull-right\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FVF\/LWSC\/JX0NVCG0\/FVFLWSCJX0NVCG0.py\" download=\"PythonMQTT_SubscribeNoPassword.py\">Download<\/a><\/div>\n<\/li>\n<\/ul>\n<h2 class=\"step-title\">Step 8: Communicating Between ESP8266 Devices<\/h2>\n<div class=\"mediaset\">\n<div class=\"photoset-wrapper\">\n<div class=\"photoset\">\n<div class=\"photoset-row items-1 \">\n<div class=\"photoset-cell\" data-imagenotes=\"[]\">\n<div class=\"photoset-image\"><a class=\"gallery-link\" href=\"https:\/\/cdn.instructables.com\/FZ5\/A7YE\/J0COSQM9\/FZ5A7YEJ0COSQM9.LARGE.jpg\" rel=\"photoset217\" data-download=\"https:\/\/cdn.instructables.com\/ORIG\/FZ5\/A7YE\/J0COSQM9\/FZ5A7YEJ0COSQM9.png\"><img decoding=\"async\" class=\" lazyloaded\" src=\"https:\/\/cdn.instructables.com\/FZ5\/A7YE\/J0COSQM9\/FZ5A7YEJ0COSQM9.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1003&amp;height=1024&amp;fit=bounds\" alt=\"Communicating Between ESP8266 Devices\" data-src=\"https:\/\/cdn.instructables.com\/FZ5\/A7YE\/J0COSQM9\/FZ5A7YEJ0COSQM9.LARGE.jpg?auto=webp&amp;frame=1&amp;width=1003&amp;height=1024&amp;fit=bounds\" \/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"step-body\">\n<p>If you want to set up an IoT network, for example, you may wish to communicate between ESP8266 devices. Thankfully, this isn&#8217;t much more complex than the code we&#8217;ve written before, however, there are a couple of notable changes.<\/p>\n<p>For one ESP to send data to another, the first ESP will need to\u00a0<strong>publish\u00a0<\/strong>to the topic, and the second ESP will need to\u00a0<strong>subscribe\u00a0<\/strong>to that topic. This setup will allow for a one-way conversation &#8211; ESP(1) to ESP(2). If we want ESP(2) to talk back to ESP(1), we can create a new topic, to which ESP(2) will publish, and ESP(1) will subscribe. Thankfully, we can have multiple subscribers on the same topic, so if you want to send data to a number of systems, you will only need one topic (to which they all subscribe, except the device which is sending the data, as that will be publishing).<\/p>\n<p>If you need help figuring out what each device needs to do, think about the system as a room of people. If ESP(1) is publishing, you can imagine this device as a &#8220;speaker&#8221;, and any devices that are subscribing to the topic are &#8220;listeners&#8221; in this example.<\/p>\n<p>I have included some example code below, which demonstrates how an ESP8266 can subscribe to a topic, and listen for certain messages &#8211; 1 and 0. If 1 is received, the on-board LED (for the HUZZAH &#8211; GPIO 0) is switched on. If 0 is received, this LED is switched off.<\/p>\n<p>If you want to process more complex data, this should be done in the\u00a0<strong>ReceivedMessage<\/strong>\u00a0function (see code).<\/p>\n<p>For your own projects, if you need to both send and receive data, you can incorporate the publish function from the previous example into the code included in this step. This should be handled in the main Arduino\u00a0<strong>loop()<\/strong>\u00a0function.<\/p>\n<p><em>Remember to change the variables at the top of the code to suit your network!<\/em><\/p>\n<\/div>\n<div class=\"downloads\">\n<h3 class=\"sr-only\">Attachments<\/h3>\n<ul id=\"attachments\" class=\"ible-files unstyled list-unstyled\">\n<li class=\"file-info clearfix \"><a class=\"thumb-wrapper\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FY6\/S2W6\/J0COSIOJ\/FY6S2W6J0COSIOJ.zip\" download=\"MQTT_Subscribe.zip\"><span class=\"file-thumb\"><img decoding=\"async\" class=\"tiny-img ls-is-cached lazyloaded\" src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" alt=\"download {{ file.name }}\" data-src=\"https:\/\/www.instructables.com\/static\/defaultIMG\/file\/ZIP.gif\" \/><\/span><span class=\"title\">MQTT_Subscribe.zip<\/span><\/a>\n<div class=\"file-actions\"><a class=\"btn pull-right\" href=\"https:\/\/cdn.instructables.com\/ORIG\/FY6\/S2W6\/J0COSIOJ\/FY6S2W6J0COSIOJ.zip\" download=\"MQTT_Subscribe.zip\">Download<\/a><\/div>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this Instructable, I will explain what the MQTT protocol is and how it is used to communicate between devices.Then, as a practical demonstration, I shall show you how to setup a simple two client system, where an ESP8266 module will send a message to a Python program when a button is pushed. Specifically, I &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1553\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to Use MQTT With the Raspberry Pi and ESP8266<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[236,20],"tags":[237,240,199],"class_list":["post-1553","post","type-post","status-publish","format-standard","hentry","category-iot","category-20","tag-esp8266","tag-mqtt","tag-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1553"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1553\/revisions"}],"predecessor-version":[{"id":1554,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1553\/revisions\/1554"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}