<?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: julz</title>
    <description>The latest articles on DEV Community by julz (@julzor42).</description>
    <link>https://dev.to/julzor42</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%2F18685%2F5ad40cec-0a72-4406-8655-95877267703b.jpeg</url>
      <title>DEV Community: julz</title>
      <link>https://dev.to/julzor42</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/julzor42"/>
    <language>en</language>
    <item>
      <title>Create an ESP32 based smart device with Arduino and MQTT</title>
      <dc:creator>julz</dc:creator>
      <pubDate>Thu, 06 Jan 2022 22:16:50 +0000</pubDate>
      <link>https://dev.to/julzor42/create-an-esp32-based-smart-device-with-arduino-and-mqtt-3lkh</link>
      <guid>https://dev.to/julzor42/create-an-esp32-based-smart-device-with-arduino-and-mqtt-3lkh</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;This article will show you how to quickly create an ESP32 based "smart device", using Arduino, by creating a simple application that basically connects your WiFi router, then a MQTT server, and publishes a message every five seconds.&lt;/p&gt;

&lt;p&gt;Of course, you can use any other WiFi enabled board.&lt;br&gt;
I chose the ESP32 because I recently bought a LiliGo TTGO ESP32 board, without any specific project in mind.&lt;br&gt;
This is the board I use: &lt;a href="http://www.lilygo.cn/prod_view.aspx?Id=1126"&gt;http://www.lilygo.cn/prod_view.aspx?Id=1126&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Configuration
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Board
&lt;/h2&gt;

&lt;p&gt;First, we need to add support to our ESP32 board.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the Arduino preferences, in the &lt;em&gt;Additional Boards Manager URLs&lt;/em&gt; field, add: &lt;code&gt;https://dl.espressif.com/dl/package_esp32_index.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the &lt;strong&gt;Tools&lt;/strong&gt; menu, open the &lt;strong&gt;Boards Manager&lt;/strong&gt; and look for &lt;em&gt;esp32&lt;/em&gt;, then install it.&lt;/li&gt;
&lt;li&gt;Still in the &lt;strong&gt;Tools&lt;/strong&gt; menu, choose your board (&lt;em&gt;TTGO LoRa32-OLED V1&lt;/em&gt; in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Libraries
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Sketch&lt;/strong&gt; menu, select &lt;strong&gt;Manage Libraries...&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Install library &lt;em&gt;PubSubClient&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Headers
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;WiFi.h&amp;gt;
#include &amp;lt;PubSubClient.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Definitions
&lt;/h3&gt;

&lt;p&gt;Let's define our WiFi SSID, password, and the MQTT server&lt;br&gt;
informations (hostname, port, username, password, client).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define ssid          "MyWiFi" 
#define password      "MyWifiPassword"
#define mqtt_host     "MyMQTTServer"
#define mqtt_port     1883
#define mqtt_client   "ArduinoCl"
#define mqtt_user     "julzor"
#define mqtt_password "SomePassword"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Global variables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WiFiClient espClient;
PubSubClient cli = PubSubClient(espClient);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Connecting to WiFi
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void setup_wifi()
{
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Connecting to MQTT
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void setup_mqtt()
{
  cli.setServer(mqtt_host, mqtt_port);

  if (cli.connect(mqtt_client, mqtt_user, mqtt_password))
  {
    // Now we're connected to the MQTT server
    // Let's publish a first message...
    cli.publish("test/hello", "hello world");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void setup()
{
    Serial.begin(115200);

    delay(10);

    setup_wifi();
    setup_mqtt();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Loop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;long last = 0;

void loop() {
  if (!cli.connected())
  {
    // We were disconnected, let's reconnect
    delay(1000);
    setup_mqtt();
  }
  else
  {
    cli.loop();

    long now = millis();
    if (now - last &amp;gt; 5000)
    {
      last = now;
      cli.publish("test/ping", "Ping");
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;You should now have a working application that does nothing but send a ping message on your MQTT server. That's a start!&lt;/p&gt;

&lt;p&gt;In another article, I will show you how I use my useless smart device with Node-RED, a Raspberry Pi, and Alexa.&lt;/p&gt;

</description>
      <category>esp32</category>
      <category>mqtt</category>
      <category>arduino</category>
      <category>smartdevice</category>
    </item>
  </channel>
</rss>
