{"id":78497,"date":"2018-12-07T11:53:39","date_gmt":"2018-12-07T11:53:39","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=78497"},"modified":"2024-05-14T10:06:37","modified_gmt":"2024-05-14T10:06:37","slug":"micropython-mqtt-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/","title":{"rendered":"MicroPython &#8211; Getting Started with MQTT on ESP32\/ESP8266"},"content":{"rendered":"\n<p>In this tutorial, we&#8217;ll show you how to use MQTT to exchange data between two ESP32\/ESP8266 boards using MicroPython firmware. As an example, we\u2019ll exchange simple text messages between two ESP boards. The idea is to use the concepts learned here to exchange sensor readings, or commands.<\/p>\n\n\n\n<!--more-->\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython Getting Started with MQTT on ESP32\/ESP8266\" class=\"wp-image-78511\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Note<\/strong>: <em>this tutorial <span style=\"text-decoration: underline;\">is compatible with both the ESP32 and ESP8266<\/span> development boards.&nbsp;<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this tutorial, make sure you complete the following prerequisites:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MicroPython firmware<\/h3>\n\n\n\n<p>To program the ESP32 and ESP8266 with MicroPython, we use uPyCraft IDE as a programming environment. Follow the next tutorials to install uPyCraft IDE and flash MicroPython firmware on your board:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install uPyCraft IDE:\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\" target=\"_blank\" rel=\"noopener noreferrer\">Windows PC<\/a>,\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\" target=\"_blank\" rel=\"noopener noreferrer\">MacOS X<\/a>, or\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux Ubuntu<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT Broker<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"200\" height=\"197\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?resize=200%2C197&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-73566\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?w=200&amp;quality=100&amp;strip=all&amp;ssl=1 200w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mosquitto-broker.png?resize=80%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 80w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n<p>To use MQTT, you need a broker. We&#8217;ll be using <a href=\"https:\/\/mosquitto.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Mosquitto broker<\/a> installed on a Raspberry Pi. Read&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install Mosquitto Broker on Raspberry Pi<\/a>.<\/p>\n\n\n\n<p>If you&#8217;re not familiar with MQTT make sure you read our introductory tutorial:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\" target=\"_blank\" rel=\"noopener noreferrer\">What is MQTT and How It Works<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this tutorial you need two ESP32 or two ESP8266 boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>2x <a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP32 DEVKIT DOIT board<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP32 Development Boards Review and Comparison<\/a><\/li>\n\n\n\n<li>(alternative) 2x <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP8266-12E NodeMCU Kit<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best ESP8266 Wi-Fi Development Board<\/a><\/li>\n<\/ul>\n\n\n\n<p>You also need a Raspberry Pi and the following accessories:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi board<\/a>\u00a0\u2013 read <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-3-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/microsd-card-raspberry-pi-16gb-class-10\/\" target=\"_blank\" rel=\"noopener noreferrer\">MicroSD Card \u2013 32GB Class10<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-power-supply\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi Power Supply (5V 2.5A)<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>Here\u2019s a high-level overview of the project we\u2019ll build:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"329\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/MQTT-Diagram.png?resize=750%2C329&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-78498\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/MQTT-Diagram.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/MQTT-Diagram.png?resize=300%2C132&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>ESP#1 publishes messages on the <em><strong>hello<\/strong> <\/em>topic. It publishes a \u201cHello\u201d message followed by a counter (Hello 1, Hello 2, Hello 3, \u2026). It publishes a new message every 5 seconds.<\/li>\n\n\n\n<li>ESP#1 is subscribed to the <em><strong>notification<\/strong> <\/em>topic to receive notifications from the ESP#2 board.<\/li>\n\n\n\n<li>ESP#2 is subscribed to the <em><strong>hello<\/strong><\/em> topic. ESP #1 is publishing in this topic. Therefore, ESP#2 receives ESP#1 messages.<\/li>\n\n\n\n<li>When ESP#2 receives the messages, it sends a message saying \u2018received\u2019. This message is published on the <em><strong>notification<\/strong>\u00a0<\/em>topic.\u00a0ESP#1 is subscribed to that topic, so it receives the message.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing ESP#1<\/h2>\n\n\n\n<p>Let\u2019s start by preparing ESP#1:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is subscribed to the <em><strong>notification<\/strong> <\/em>topic<\/li>\n\n\n\n<li>It publishes on the <em><strong>hello<\/strong> <\/em>topic<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Importing umqttsimple library<\/h3>\n\n\n\n<p>To use MQTT with the ESP32\/ESP8266 and MicroPython, you need to install the <em>umqttsimple<\/em> library.<\/p>\n\n\n\n<p><strong>1.<\/strong> Create a new file by pressing the <strong>New File<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"135\" height=\"41\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/uPycraft-ide-tools-menu-new-file.png?resize=135%2C41&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-75063\"\/><\/figure><\/div>\n\n\n<p><strong>2.<\/strong> Copy the <em>umqttsimple<\/em> library code into it. You can access the <em>umqttsimple<\/em> library code in the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/MQTT\/umqttsimple.py\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/MQTT\/umqttsimple.py<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>3.<\/strong> Save the file by pressing the <strong>Save<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"142\" height=\"47\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/uPycraft-ide-tools-menu-save-file.png?resize=142%2C47&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-75064\"\/><\/figure><\/div>\n\n\n<p><strong>4.<\/strong> Call this new file \u201c<strong>umqttsimple.py<\/strong>\u201d and press <strong>ok<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"305\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/import-umqttsimple-library.png?resize=750%2C305&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-78499\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/import-umqttsimple-library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/import-umqttsimple-library.png?resize=300%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p><strong>5.<\/strong> Click the <strong>Download and Run<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"238\" height=\"45\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/uPycraft-ide-tools-menu-download-and-run.png?resize=238%2C45&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-75060\"\/><\/figure><\/div>\n\n\n<p><strong>6.<\/strong> The file should be saved on the <em>device<\/em> folder with the name \u201c<strong>umqttsimple.py<\/strong>\u201d as highlighted in the figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"468\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/umqttsimple-library-installed.png?resize=750%2C468&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-78502\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/umqttsimple-library-installed.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/umqttsimple-library-installed.png?resize=300%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Now, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><em>boot.py<\/em><\/h3>\n\n\n\n<p>Open the <em>boot.py<\/em> file and copy the following code to ESP#1.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nimport time\nfrom umqttsimple import MQTTClient\nimport ubinascii\nimport machine\nimport micropython\nimport network\nimport esp\nesp.osdebug(None)\nimport gc\ngc.collect()\n\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\nmqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP'\nmqtt_user = 'REPLACE_WITH_YOUR_MQTT_USERNAME'\nmqtt_pass = 'REPLACE_WITH_YOUR_MQTT_PASSWORD'\n\n#EXAMPLE IP ADDRESS\n#mqtt_server = '192.168.1.144'\nclient_id = ubinascii.hexlify(machine.unique_id())\ntopic_sub = b'notification'\ntopic_pub = b'hello'\n\nlast_message = 0\nmessage_interval = 5\ncounter = 0\n\nstation = network.WLAN(network.STA_IF)\n\nstation.active(True)\nstation.connect(ssid, password)\n\nwhile station.isconnected() == False:\n  pass\n\nprint('Connection successful')\nprint(station.ifconfig())\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/MQTT\/MQTT_Hello_World\/ESP_1\/boot.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>You need to import all the following libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\nfrom umqttsimple import MQTTClient\nimport ubinascii\nimport machine\nimport micropython\nimport network\nimport esp<\/code><\/pre>\n\n\n\n<p>Set the debug to None and activate the garbage collector.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esp.osdebug(None)\nimport gc\ngc.collect()<\/code><\/pre>\n\n\n\n<p>In the following variables, you need to enter your network credentials, your broker IP address, the broker username and corresponding password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\nmqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP'\nmqtt_user = 'REPLACE_WITH_YOUR_MQTT_USERNAME'\nmqtt_pass = 'REPLACE_WITH_YOUR_MQTT_PASSWORD'<\/code><\/pre>\n\n\n\n<p>For example, our broker IP address is: 192.168.1.144.<\/p>\n\n\n\n<p><strong>Note:<\/strong> read&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\" target=\"_blank\" rel=\"noopener noreferrer\">this tutorial<\/a> to see how to get your broker IP address.<\/p>\n\n\n\n<p>To create an MQTT client, we need to get the ESP unique ID. That\u2019s what we do in the following line (it is saved on the <span class=\"rnthl rntliteral\">client_id<\/span> variable).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client_id = ubinascii.hexlify(machine.unique_id())<\/code><\/pre>\n\n\n\n<p>Next, write the topic the ESP#1 is subscribed to, and the topic it will be publishing messages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>topic_sub = b'<strong>notification<\/strong>'\ntopic_pub = b'<strong>hello<\/strong>'<\/code><\/pre>\n\n\n\n<p>Then, create the following variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>last_message = 0\nmessage_interval = 5\ncounter = 0<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">last_message<\/span> variable will hold the last time a message was sent. The <span class=\"rnthl rntliteral\">message_interval<\/span> is the time between each message sent. Here, we\u2019re setting it to 5 seconds (this means a new message will be sent every 5 seconds). The <span class=\"rnthl rntliteral\">counter<\/span> variable is simply a counter to be added to the message.<\/p>\n\n\n\n<p>After that, we make the procedures to connect to the network.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>station = network.WLAN(network.STA_IF)\n\nstation.active(True)\nstation.connect(ssid, password)\n\nwhile station.isconnected() == False:\n  pass\n\nprint('Connection successful')\nprint(station.ifconfig())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><em>main.py<\/em><\/h3>\n\n\n\n<p>In the <em>main.py<\/em> file is where we\u2019ll write the code to publish and receive the messages. Copy the following code to your <em>main.py<\/em> file.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\ndef sub_cb(topic, msg):\n  print((topic, msg))\n  if topic == b'notification' and msg == b'received':\n    print('ESP received hello message')\n\ndef connect_and_subscribe():\n  global client_id, mqtt_server, topic_sub\n  client = MQTTClient(client_id, mqtt_server, user=mqtt_user, password=mqtt_pass)\n  client.set_callback(sub_cb)\n  client.connect()\n  client.subscribe(topic_sub)\n  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))\n  return client\n\ndef restart_and_reconnect():\n  print('Failed to connect to MQTT broker. Reconnecting...')\n  time.sleep(10)\n  machine.reset()\n\ntry:\n  client = connect_and_subscribe()\nexcept OSError as e:\n  restart_and_reconnect()\n\nwhile True:\n  try:\n    client.check_msg()\n    if (time.time() - last_message) &gt; message_interval:\n      msg = b'Hello #%d' % counter\n      client.publish(topic_pub, msg)\n      last_message = time.time()\n      counter += 1\n  except OSError as e:\n    restart_and_reconnect()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/MQTT\/MQTT_Hello_World\/ESP_1\/main.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>The first thing you should do is creating a callback function that will run whenever a message is published on a topic the ESP is subscribed to.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Callback function<\/h4>\n\n\n\n<p>The callback function should accept as parameters the topic and the message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def <strong>sub_cb(topic, msg)<\/strong>:\n  print((topic, msg))\n  if topic == b'notification' and msg == b'received':\n    print('ESP received hello message')<\/code><\/pre>\n\n\n\n<p>In our callback function, we start by printing the topic and the message. Then, we check if the message was published on the <em><strong>notification<\/strong> <\/em>topic, and if the content of the message is \u2018received\u2019. If this if statement is True, it means that ESP#2 received the \u2018hello\u2019 message sent by ESP#1.<\/p>\n\n\n\n<p>Basically, this callback function handles what happens when a certain message is received on a certain topic.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Connect and subscribe<\/h4>\n\n\n\n<p>Then, we have the <span class=\"rnthl rntliteral\">connect_and_subscribe()<\/span> function. This function is responsible for connecting to the broker as well as to subscribe to a topic.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def <strong>connect_and_subscribe()<\/strong>:<\/code><\/pre>\n\n\n\n<p>Start by declaring the <span class=\"rnthl rntliteral\">client_id<\/span>, <span class=\"rnthl rntliteral\">mqtt_server<\/span> and <span class=\"rnthl rntliteral\">topic_sub<\/span> variables as global variables. This way, we can access these variables throughout the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global client_id, mqtt_server, topic_sub<\/code><\/pre>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">MQTTClient<\/span> object called <span class=\"rnthl rntliteral\">client<\/span>. We need to pass as parameters the <span class=\"rnthl rntliteral\">cliend_id<\/span>, the IP address of the MQTT broker (<span class=\"rnthl rntliteral\">mqtt_server<\/span>), and the broker username and password. These variables were set on the <em>boot.py<\/em> file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client = MQTTClient(client_id, mqtt_server, user=mqtt_user, password=mqtt_pass)<\/code><\/pre>\n\n\n\n<p>After that, set the callback function to the client (<span class=\"rnthl rntliteral\">sub_cb<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client.set_callback(sub_cb)<\/code><\/pre>\n\n\n\n<p>Next, connect the client to the broker using the <span class=\"rnthl rntliteral\">connect()<\/span> method on the <span class=\"rnthl rntliteral\">MQTTClient<\/span> object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client.connect()<\/code><\/pre>\n\n\n\n<p>After connecting, we subscribe to the <span class=\"rnthl rntliteral\">topic_sub<\/span> topic. Set the <span class=\"rnthl rntliteral\">topic_sub<\/span> on the <em>boot.py<\/em> file (<span class=\"rnthl rntliteral\">notification<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client.subscribe(topic_sub)<\/code><\/pre>\n\n\n\n<p>Finally, print a message and return the client:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))\nreturn client<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Restart and reconnect<\/h4>\n\n\n\n<p>We create a function called <span class=\"rnthl rntliteral\">restart_and_reconnect()<\/span>. This function will be called in case the ESP32 or ESP8266 fails to connect to the broker.<\/p>\n\n\n\n<p>This function prints a message to inform that the connection was not successful. We wait 10 seconds. Then, we reset the ESP using the <span class=\"rnthl rntliteral\">reset()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def <strong>restart_and_reconnect()<\/strong>:\n  print('Failed to connect to MQTT broker. Reconnecting...')\n  time.sleep(10)\n  machine.reset()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Receive and publish messages<\/h4>\n\n\n\n<p>Until now, we\u2019ve created functions to handle tasks related with the MQTT communication. From now on, the code will call those functions to make things happen.<\/p>\n\n\n\n<p>The first thing we need to do is to connect to the MQTT broker and subscribe to a topic. So, we create a client by calling the <span class=\"rnthl rntliteral\">connect_and_subscribe()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n  client = connect_and_subscribe()<\/code><\/pre>\n\n\n\n<p>In case we\u2019re not able to connect to the MQTTT broker, we\u2019ll restart the ESP by calling the <span class=\"rnthl rntliteral\">restart_and_reconnect()<\/span> function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>except OSError as e:\n  restart_and_reconnect()<\/code><\/pre>\n\n\n\n<p>In the while loop is where we\u2019ll be receiving and publishing the messages. We use <span class=\"rnthl rntliteral\">try<\/span> and <span class=\"rnthl rntliteral\">except<\/span> statements to prevent the ESP from crashing in case something goes wrong.<\/p>\n\n\n\n<p>Inside the <span class=\"rnthl rntliteral\">try<\/span> block, we start by applying the <span class=\"rnthl rntliteral\">check_msg()<\/span> method on the <span class=\"rnthl rntliteral\">client<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n  client.check_msg()<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">check_msg()<\/span> 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\u2019ve defined earlier (the <span class=\"rnthl rntliteral\">sub_cb()<\/span> function). If there isn\u2019t a pending message, it returns with <span class=\"rnthl rntliteral\">None<\/span>.<\/p>\n\n\n\n<p>Then, we add an <span class=\"rnthl rntliteral\">if<\/span> statement to checker whether 5 seconds (<span class=\"rnthl rntliteral\">message_interval<\/span>) have passed since the last message was sent.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (time.time() - last_message) &gt; message_interval:<\/code><\/pre>\n\n\n\n<p>If it is time to send a new message, we create a <span class=\"rnthl rntliteral\">msg<\/span> variable with the \u201cHello\u201d text followed by a counter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>msg = b'Hello #%d' % counter<\/code><\/pre>\n\n\n\n<p>To publish a message on a certain topic, you just need to apply the <span class=\"rnthl rntliteral\">publish()<\/span> method on the <span class=\"rnthl rntliteral\">client<\/span> and pass as arguments, the topic and the message. The <span class=\"rnthl rntliteral\">topic_pub<\/span> variable was set to <span class=\"rnthl rntliteral\">hello<\/span> in the <em>boot.py<\/em> file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client.<strong>publish<\/strong>(<strong>topic_pub<\/strong>, <strong>msg<\/strong>)<\/code><\/pre>\n\n\n\n<p>After sending the message, we update the last time a message was received by setting the <span class=\"rnthl rntliteral\">last_message<\/span> variable to the current time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>last_message = time.time()<\/code><\/pre>\n\n\n\n<p>Finally, we increase the <span class=\"rnthl rntliteral\">counter<\/span> variable in every loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter += 1<\/code><\/pre>\n\n\n\n<p>If something unexpected happens, we call the <span class=\"rnthl rntliteral\">restart_and_reconnect()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>except OSError as e:\n  restart_and_reconnect()<\/code><\/pre>\n\n\n\n<p>That\u2019s it for ESP#1. Remember that you need to upload all the next files to make the project work (you should upload the files in order):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><em>umqttsimple.py<\/em>;<\/li>\n\n\n\n<li><em>boot.py<\/em>;<\/li>\n\n\n\n<li><em>main.py<\/em>.<\/li>\n<\/ol>\n\n\n\n<p>After uploading all files, you should get success messages on: establishing a network connection; connecting to the broker; and subscribing to the topic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP #2<\/h2>\n\n\n\n<p>Let\u2019s now prepare ESP#2:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is subscribed to the <em><strong>hello<\/strong> <\/em>topic<\/li>\n\n\n\n<li>It publishes on the <em><strong>notification<\/strong> <\/em>topic<\/li>\n<\/ul>\n\n\n\n<p>Like the ESP#1, you also need to upload the <em>umqttsimple.py<\/em>, <em>boot.py<\/em>, and <em>main.py<\/em> files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing umqttsimple<\/h3>\n\n\n\n<p>To use MQTT with the ESP32\/ESP8266 and MicroPython, you need to install the <em>umqttsimple<\/em> library. Follow the steps described earlier to install the <em>umqttsimple<\/em> library in ESP#2.<\/p>\n\n\n\n<p>You can access the umqttsimple library code in the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/MQTT\/umqttsimple.py\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/MQTT\/umqttsimple.py<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><em>boot.py<\/em><\/h3>\n\n\n\n<p>Copy the following code to the ESP#2 <em>boot.py<\/em> file.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nimport time\nfrom umqttsimple import MQTTClient\nimport ubinascii\nimport machine\nimport micropython\nimport network\nimport esp\nesp.osdebug(None)\nimport gc\ngc.collect()\n\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\nmqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP'\nmqtt_user = 'REPLACE_WITH_YOUR_MQTT_USERNAME'\nmqtt_pass = 'REPLACE_WITH_YOUR_MQTT_PASSWORD'\n\n#EXAMPLE IP ADDRESS\n#mqtt_server = '192.168.1.144'\nclient_id = ubinascii.hexlify(machine.unique_id())\ntopic_sub = b'hello'\ntopic_pub = b'notification'\n\nstation = network.WLAN(network.STA_IF)\n\nstation.active(True)\nstation.connect(ssid, password)\n\nwhile station.isconnected() == False:\n  pass\n\nprint('Connection successful')\nprint(station.ifconfig())\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/MQTT\/MQTT_Hello_World\/ESP_2\/boot.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This code is very similar to the previous <em>boot.py<\/em> file. You need to replace the following variables with your network credentials, the broker IP address, the broker username and password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\nmqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP'\nmqtt_user = 'REPLACE_WITH_YOUR_MQTT_USERNAME'\nmqtt_pass = 'REPLACE_WITH_YOUR_MQTT_PASSWORD'<\/code><\/pre>\n\n\n\n<p>The only difference here is that we subscribe to the <em><strong>hello<\/strong> <\/em>topic and publish on the <em><strong>notification<\/strong> <\/em>topic.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>topic_sub = b'<strong>hello<\/strong>'\ntopic_pub = b'<strong>notification<\/strong>'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">main.py<\/h3>\n\n\n\n<p>Copy the following code to the ESP#2 <em>main.py<\/em> file.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\ndef sub_cb(topic, msg):\n  print((topic, msg))\n\ndef connect_and_subscribe():\n  global client_id, mqtt_server, topic_sub\n  client = MQTTClient(client_id, mqtt_server, user=mqtt_user, password=mqtt_pass)\n  client.set_callback(sub_cb)\n  client.connect()\n  client.subscribe(topic_sub)\n  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))\n  return client\n\ndef restart_and_reconnect():\n  print('Failed to connect to MQTT broker. Reconnecting...')\n  time.sleep(10)\n  machine.reset()\n\ntry:\n  client = connect_and_subscribe()\nexcept OSError as e:\n  restart_and_reconnect()\n\nwhile True:\n  try:\n    new_message = client.check_msg()\n    if new_message != 'None':\n      client.publish(topic_pub, b'received')\n    time.sleep(1)\n  except OSError as e:\n    restart_and_reconnect()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/MQTT\/MQTT_Hello_World\/ESP_2\/main.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This code is very similar to the <em>main.py<\/em> from ESP#1. We create the <span class=\"rnthl rntliteral\">sub_cb()<\/span>, the <span class=\"rnthl rntliteral\">connect_and_subscribe()<\/span> and the <span class=\"rnthl rntliteral\">restart_and_reconnect()<\/span> functions. This time, the <span class=\"rnthl rntliteral\">sub_cb()<\/span> function just prints information about the topic and received message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def sub_cb(topic, msg):\n  print((topic, msg))<\/code><\/pre>\n\n\n\n<p>In the while loop, we check if we got a new message and save it in the <span class=\"rnthl rntliteral\">new_message<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new_message = client.check_msg()<\/code><\/pre>\n\n\n\n<p>If we receive a new message, we publish a message saying \u2018received\u2019 on the <span class=\"rnthl rntliteral\">topic_sub<\/span> topic (in this case we set it to <span class=\"rnthl rntliteral\">notification<\/span> in the <em>boot.py<\/em> file).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if new_message != 'None':\n  client.publish(topic_pub, b'received')<\/code><\/pre>\n\n\n\n<p>That\u2019s it for ESP#2. Remember that you need to upload all the next files to make the project work (you should upload the files in order):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><em>umqttsimple.py<\/em>;<\/li>\n\n\n\n<li><em>boot.py<\/em>;<\/li>\n\n\n\n<li><em>main.py<\/em>.<\/li>\n<\/ol>\n\n\n\n<p>The ESP32\/ESP8266 should establish a network connection and connect to the broker successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading all the necessary scripts to both ESP boards and having both boards and the Raspberry Pi with the Mosquitto broker running, you are ready to test the setup.<\/p>\n\n\n\n<p>The ESP#2 should be receiving the \u201cHello\u201d messages from ESP#1, as shown in the figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"535\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/receive-mqtt-messages-f.png?resize=750%2C535&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-78505\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/receive-mqtt-messages-f.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/receive-mqtt-messages-f.png?resize=300%2C214&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>On the other side, ESP#1 board should receive the \u201creceived\u201d message. The \u201creceived\u201d message is published by ESP#2 on the <em><strong>notification<\/strong> <\/em>topic. ESP#1 is subscribed to that topic, so it receives the message.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"550\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-messages-2.png?resize=750%2C550&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-78507\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-messages-2.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-messages-2.png?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this simple example, you\u2019ve learned how to exchange text between two ESP32\/ESP8266 boards using MQTT communication protocol. The idea is to use the concepts learned here to exchange useful data like sensor readings or commands to control outputs.<\/p>\n\n\n\n<p>If you like MicroPython with the ESP32\/ESP8266, you may also like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" rel=\"noopener noreferrer\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">Getting Started with MicroPython on ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">MicroPython with ESP32 and ESP8266: Interacting with GPIOs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll show you how to use MQTT to exchange data between two ESP32\/ESP8266 boards using MicroPython firmware. As an example, we\u2019ll exchange simple text messages between two &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython &#8211; Getting Started with MQTT on ESP32\/ESP8266\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/#more-78497\" aria-label=\"Read more about MicroPython &#8211; Getting Started with MQTT on ESP32\/ESP8266\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":78511,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,214,265,310,309,282,264],"tags":[],"class_list":["post-78497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp8266","category-esp8266-project","category-micropython","category-0-esp32-micropython","category-mqtt","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/mqtt-micropython-thumbnail.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/78497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=78497"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/78497\/revisions"}],"predecessor-version":[{"id":155348,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/78497\/revisions\/155348"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/78511"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=78497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=78497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=78497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}