{"id":1353,"date":"2025-06-17T19:44:21","date_gmt":"2025-06-17T14:14:21","guid":{"rendered":"https:\/\/vihaaniotgateway.in\/?p=1353"},"modified":"2025-11-12T20:27:33","modified_gmt":"2025-11-12T14:57:33","slug":"beginners-guide-to-aws-iot-core-connect-manage-secure-iot-devices","status":"publish","type":"post","link":"https:\/\/vihaaniotgateway.in\/index.php\/2025\/06\/17\/beginners-guide-to-aws-iot-core-connect-manage-secure-iot-devices\/","title":{"rendered":"Beginner\u2019s Guide to AWS IoT Core: Connect, Manage &amp; Secure IoT Devices"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In this guide, you\u2019ll learn how to connect an <strong>ESP32-WROOM-32 (38 Pin)<\/strong> development board to <strong>AWS IoT Core<\/strong> using the <strong>MQTT protocol<\/strong>. You\u2019ll also control a <strong>2-Channel Relay Module with optocoupler<\/strong> to switch devices remotely via the cloud. This is a perfect starting point for building smart home systems, industrial IoT projects, or remote device management solutions.<\/p>\n\n\n\n<p>By the end of this tutorial, you will:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set up AWS IoT Core for secure device communication.<\/li>\n\n\n\n<li>Program the ESP32 to communicate via MQTT.<\/li>\n\n\n\n<li>Control relays connected to the ESP32 from AWS.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before you get started with AWS IoT Core and ESP32, make sure you have the following:<\/p>\n\n\n\n<p><strong>Creating and Configuring a Thing in AWS IoT Core<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Basic Knowledge of MQTT Protocol<\/strong><br>Understanding of MQTT topics, publish\/subscribe model, and message handling.<\/li>\n\n\n\n<li><strong>ESP32-WROOM-32 (38 Pin) Development Board<\/strong><br>A compatible ESP32 board for connecting to AWS IoT Core.<\/li>\n\n\n\n<li><strong>2-Channel Relay Module with Optocoupler<\/strong><br>To control external devices or appliances via the ESP32.<\/li>\n\n\n\n<li><strong>Active AWS Account<\/strong><br>Necessary for creating IoT Things, certificates, and policies on AWS IoT Core.<\/li>\n\n\n\n<li><strong>Latest Arduino IDE Installed<\/strong><br>Required for writing, compiling, and uploading code to the ESP32 board.<\/li>\n<\/ul>\n\n\n\n<p>Start by logging into your AWS Management Console with the root user credentials. This guide assumes that your AWS account has already been created.<\/p>\n\n\n\n<p>\u2705 <strong>If you&#8217;re a new user and need help setting up an AWS account from scratch<\/strong>, we\u2019ll cover that in a detailed article soon.<br>\ud83c\udfa5 Meanwhile, follow this video to quickly create your AWS account step by step!<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"AWS Root Account Creation + MFA Setup | Secure Your Cloud Access (Beginner-Friendly)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/SUfDOKTYb7E?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"AWS IoT Core Explained: Beginner\u2019s Step-by-Step Guide for IoT Device Connectivity &amp; Security\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/3dAkXAZmNVc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Search for IoT Core:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"991\" height=\"340\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image.png\" alt=\"\" class=\"wp-image-1366\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image.png 991w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-300x103.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-768x263.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-600x206.png 600w\" sizes=\"(max-width: 991px) 100vw, 991px\" \/><\/figure>\n\n\n\n<p>Select AWS IoT Core and Click on Things:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1-1024x346.png\" alt=\"\" class=\"wp-image-1367\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1-1024x346.png 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1-300x101.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1-768x259.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1-600x203.png 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-1.png 1252w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Click on create things:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"396\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2-1024x396.png\" alt=\"\" class=\"wp-image-1368\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2-1024x396.png 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2-300x116.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2-768x297.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2-600x232.png 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-2.png 1399w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Click on create single thing:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"727\" height=\"391\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-3.png\" alt=\"\" class=\"wp-image-1369\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-3.png 727w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-3-300x161.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-3-600x323.png 600w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding AWS IoT Shadows: No Shadow, Named Shadow, and Unnamed (Classic) Shadow<\/strong><\/h2>\n\n\n\n<p>When you create a Thing in AWS IoT Core, you can choose how its state (shadow) will be managed. Device Shadows allow AWS IoT Core to store the device&#8217;s last reported and desired states, even when the device is offline. Here&#8217;s a quick overview of the available options:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>No Shadow<\/strong><\/h3>\n\n\n\n<p>If you select this option, AWS IoT Core will not maintain any shadow document for the device. The device must handle state management entirely through direct communication (such as MQTT messages). This is suitable for devices that do not require state persistence.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Unnamed (Classic) Shadow<\/strong><\/h3>\n\n\n\n<p>Also known as the default shadow, the unnamed or classic shadow provides a single JSON document that represents the entire device state. This is ideal for simpler devices where one shadow is enough to reflect the device\u2019s status, commands, and configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Named Shadow<\/strong><\/h3>\n\n\n\n<p>Named shadows allow you to create multiple shadow documents for the same Thing. This is useful for complex devices with multiple components \u2014 for example, a smart home device that manages both a thermostat and a light system can have separate named shadows for each component.<\/p>\n\n\n\n<p><strong>For This Tutorial: Selecting &#8216;No Shadow&#8217; for Simplicity<\/strong><\/p>\n\n\n\n<p>In this guide, we will choose the <strong>&#8216;No Shadow&#8217;<\/strong> option to keep the setup simple. This means AWS IoT Core will not maintain a Device Shadow for our Thing, and all device state management will rely on direct communication (such as MQTT messages). This approach is suitable for basic use cases or when shadow functionality is not required.<\/p>\n\n\n\n<p><strong>Assigning a Thing Name and Keeping Default Settings<\/strong><\/p>\n\n\n\n<p>For this example, set the <strong>Thing Name as &#8220;ESP32&#8221;<\/strong> to represent your IoT device. Leave all other settings at their <strong>default values<\/strong> to simplify the configuration process. This ensures a quick and hassle-free setup, especially suitable for beginners or testing purposes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"889\" height=\"733\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-4.png\" alt=\"\" class=\"wp-image-1370\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-4.png 889w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-4-300x247.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-4-768x633.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-4-600x495.png 600w\" sizes=\"(max-width: 889px) 100vw, 889px\" \/><\/figure>\n\n\n\n<p><strong>Generate Certificates with Default Settings<\/strong><\/p>\n\n\n\n<p>In the next step, choose the option to <strong>automatically generate a new certificate<\/strong> and keep all other settings at their <strong>default values<\/strong>. AWS IoT Core will create a certificate and private key pair for your Thing, which is essential for secure device authentication and communication.<\/p>\n\n\n\n<p>After the certificates are generated, be sure to <strong>download them<\/strong> for future use, as they won\u2019t be accessible later.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"754\" height=\"286\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-5.png\" alt=\"\" class=\"wp-image-1371\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-5.png 754w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-5-300x114.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-5-600x228.png 600w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/figure>\n\n\n\n<p><strong>Skip Policy Attachment for Now and Create the Thing<\/strong><\/p>\n\n\n\n<p>For this tutorial, we will <strong>not attach any policy at this stage<\/strong>. Policies define permissions for your device, but we will create and attach a policy separately in a later step.<\/p>\n\n\n\n<p>Simply click on <strong>&#8220;Create Thing&#8221;<\/strong> to complete the process. Your AWS IoT Thing (named <strong>ESP32<\/strong>) will now be created with default settings and a generated certificate.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"271\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-1024x271.png\" alt=\"\" class=\"wp-image-1373\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-1024x271.png 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-300x79.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-768x203.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-1536x406.png 1536w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7-600x159.png 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-7.png 1639w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Download All Certificates for Future Use<\/strong><\/p>\n\n\n\n<p>Once the Thing is created, make sure to <strong>download all the generated certificates and the private key file<\/strong>. These files are essential for establishing a secure connection between your <strong>ESP32 device and AWS IoT Core<\/strong>. Without these certificates, the ESP32 will not be able to authenticate and communicate with AWS IoT services.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"576\" height=\"684\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-8.png\" alt=\"\" class=\"wp-image-1374\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-8.png 576w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-8-253x300.png 253w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/figure>\n\n\n\n<p><strong>Creating an AWS IoT Policy for ESP32<\/strong><\/p>\n\n\n\n<p>When creating the policy, use the following JSON policy document. This policy grants the required permissions to your ESP32 device for connecting, publishing, subscribing, and receiving messages in AWS IoT Core:<\/p>\n\n\n\n<p>In left side panel go to Security-&gt;Policies-&gt;Create Policy and select JSON<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Allow\",\n      \"Action\": [\n        \"iot:Connect\",\n        \"iot:Publish\",\n        \"iot:Subscribe\",\n        \"iot:Receive\"\n      ],\n      \"Resource\": \"*\"\n    }\n  ]\n}\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"437\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-1024x437.png\" alt=\"\" class=\"wp-image-1375\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-1024x437.png 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-300x128.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-768x328.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-1536x655.png 1536w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9-600x256.png 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-9.png 1542w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u26a0\ufe0f <strong>Important:<\/strong><br>This policy provides <strong>full access<\/strong> to AWS IoT Core resources (<code>\"Resource\": \"*\"<\/code>). For production environments, it&#8217;s recommended to restrict this to specific IoT Things or topics to ensure better security.<\/p>\n\n\n\n<p><strong>Attach the Policy to the Thing&#8217;s Certificate<\/strong><\/p>\n\n\n\n<p>Now that the IoT Policy has been created, the next step is to <strong>attach it to the certificate<\/strong> associated with your ESP32 Thing. This allows your device to securely communicate with AWS IoT Core using the permissions defined in the policy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Steps to Attach the Policy:<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the <strong>AWS IoT Core Console<\/strong>.<\/li>\n\n\n\n<li>In the left sidebar, navigate to <strong>Security \u2192 Certificates<\/strong>.<\/li>\n\n\n\n<li>Find the certificate you created earlier (associated with the ESP32 Thing).<\/li>\n\n\n\n<li>Click on the <strong>certificate ID<\/strong> to open its details.<\/li>\n\n\n\n<li>Under <strong>&#8220;Actions&#8221;<\/strong>, click <strong>&#8220;Attach policy&#8221;<\/strong>.<\/li>\n\n\n\n<li>From the list, select the policy you just created (e.g., <code>ESP32_Policy<\/code>) and click <strong>&#8220;Attach&#8221;<\/strong>.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"565\" height=\"271\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-10.png\" alt=\"\" class=\"wp-image-1376\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-10.png 565w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-10-300x144.png 300w\" sizes=\"(max-width: 565px) 100vw, 565px\" \/><\/figure>\n\n\n\n<p>\u2705 <strong>Now your ESP32 certificate is authorized to connect and interact with AWS IoT Core.<\/strong><\/p>\n\n\n\n<p><strong>Note Down the Device Endpoint<\/strong><\/p>\n\n\n\n<p>Carefully <strong>note down the AWS IoT Device Endpoint URL<\/strong>, as this will be required later when configuring your <strong>ESP32 to securely connect to AWS IoT Core<\/strong>.<\/p>\n\n\n\n<p>You can find this endpoint in the AWS IoT Core Console under <strong>AWS Console \u2192 Connect -&gt; Domain Configurations<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"147\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11-1024x147.png\" alt=\"\" class=\"wp-image-1378\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11-1024x147.png 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11-300x43.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11-768x110.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11-600x86.png 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-11.png 1254w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\ud83d\udd0c Connecting 5V Relay Module to ESP32 (Using GPIO13)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>ESP32 Pin<\/strong><\/th><th><strong>Relay Module Pin<\/strong><\/th><th><strong>Function<\/strong><\/th><\/tr><\/thead><tbody><tr><td>V5 (5V)<\/td><td>VCC<\/td><td>Power supply for the relay module<\/td><\/tr><tr><td>GPIO13 (G13)<\/td><td>IN1<\/td><td>Relay control input<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td>Common ground connection<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-1024x768.jpg\" alt=\"\" class=\"wp-image-1388\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-1024x768.jpg 1024w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-300x225.jpg 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-768x576.jpg 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-1536x1152.jpg 1536w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections-600x450.jpg 600w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/Edited-Connections.jpg 1800w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Steps to Connect ESP32 to AWS IoT Core<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>const char WIFI_SSID[] = &#8220;Change This&#8221;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/li>\n\n\n\n<li>const char WIFI_PASSWORD[] = &#8220;Change This&#8221;; &nbsp; &nbsp;<\/li>\n\n\n\n<li>const char AWS_IOT_ENDPOINT[] = &#8220;Use this from previous step&#8221;; &nbsp;\/\/change this<\/li>\n\n\n\n<li>\/\/ Amazon Root CA 1<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char AWS_CERT_CA&#91;] PROGMEM = R\"EOF(\n\n-----BEGIN CERTIFICATE-----\n\nReplace with AWS CA1\n\n-----END CERTIFICATE-----\n\n)EOF\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\/\/ Device Certificate \/\/change this<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char AWS_CERT_CRT&#91;] PROGMEM = R\"KEY(\n\n-----BEGIN CERTIFICATE-----\n\nReplace with device certificate\n\n-----END CERTIFICATE-----\n\n)KEY\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\/\/ Device Private Key \/\/change this<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static const char AWS_CERT_PRIVATE&#91;] PROGMEM = R\"KEY(\n\n-----BEGIN RSA PRIVATE KEY-----\n\nReplace with device private key\n\n-----END RSA PRIVATE KEY-----\n\n)KEY\";<\/code><\/pre>\n\n\n\n<p><strong>Upload the ESP32 Sketch After Updating Required Details<\/strong><\/p>\n\n\n\n<p>Use the following <strong>ESP32 Arduino sketch<\/strong> to connect your device to AWS IoT Core. Before uploading, make sure to <strong>update the placeholders<\/strong> with your specific AWS IoT credentials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Wi-Fi SSID and Password<\/strong><\/li>\n\n\n\n<li><strong>AWS IoT Endpoint URL<\/strong><\/li>\n\n\n\n<li><strong>Root CA Certificate<\/strong><\/li>\n\n\n\n<li><strong>Device Certificate<\/strong><\/li>\n\n\n\n<li><strong>Private Key<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u26a0\ufe0f <strong>Important:<\/strong> Replace the placeholder values with the actual credentials and endpoint you obtained from AWS IoT Core. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;WiFi.h>\n#include &lt;WiFiClientSecure.h>\n#include &lt;PubSubClient.h>\n\/\/ -------- WiFi ----------\nconst char* WIFI_SSID = \"CHANGE_ME\";\nconst char* WIFI_PASSWORD = \"CHANGE_ME\";\n\n\/\/ -------- AWS IoT ----------\n \/*This is the unique DNS address given by AWS IoT Core, \n which acts as the MQTT broker for your device to communicate with cloud services.*\/\nconst char* AWS_IOT_ENDPOINT = \"change-me\";  \n\/*This is the port used for the MQTT protocol, typically 8883 for secure (TLS) communication.*\/\nconst int AWS_IOT_PORT = 8883;\n\n\/\/ Certificates (download from AWS IoT Core)\nconst char* AWS_CERT_CA = R\"EOF(\n-----BEGIN CERTIFICATE-----\nUPDATE ME\n-----END CERTIFICATE-----\n)EOF\";\n\nconst char* AWS_CERT_CRT = R\"EOF(\n-----BEGIN CERTIFICATE-----\nUPDATE ME\n-----END CERTIFICATE-----\n)EOF\";\n\nconst char* AWS_CERT_PRIVATE = R\"EOF(\n-----BEGIN RSA PRIVATE KEY-----\nUPDATE ME \n-----END RSA PRIVATE KEY-----\n)EOF\";\n#define LIGHT 13\n\/\/ -------- Global Variables ----------\nWiFiClientSecure net;\nPubSubClient client(net);\n\nvoid publishMessage(const char* topic, char* payload) {\n  client.publish(topic, payload);\n  Serial.print(\"Published &#91; \");\n  Serial.print(\" \");\n  Serial.print(topic);\n  Serial.print(\"] \");\n  Serial.println(payload);\n}\n\n\/\/ Connect to AWS IoT Core\nvoid connectAWS() {\n    \/*This loads the root CA certificate provided by AWS (Amazon Root CA), \n    which is used to verify that the AWS server the IoT device connects\n    to is legitimate.*\/\n    net.setCACert(AWS_CERT_CA);\n    \/*It is used by the AWS server to verify the identity of your specific \n    IoT device when it tries to connect.*\/\n    net.setCertificate(AWS_CERT_CRT);\n    \/*The private key ensures that only your device can prove ownership of its \n    certificate, supporting secure encrypted communication.*\/\n    net.setPrivateKey(AWS_CERT_PRIVATE);\n\n    client.setServer(AWS_IOT_ENDPOINT, AWS_IOT_PORT);\n    client.setCallback(messageHandler);\n    Serial.print(\"Connecting to AWS IoT Core...\");\n    while (!client.connected()) {\n        client.connect(\"ESP32\");\n        Serial.print(\".\");\n        delay(1000);\n    }\n    client.subscribe(\"$aws\/things\/ESP32\/command\/light\");\n    Serial.println(\" connected!\");\n}\n\nvoid handle_mqtt_msg(char *topic, byte *payload, unsigned int length) {\n  char resp&#91;60] = { 0 }; \n  if (strstr(topic, \"$aws\/things\/ESP32\/command\/light\")) {\n    if (strstr((const char *)payload, \"light-on\") != NULL) {\n      digitalWrite(LIGHT, LOW);\n      Serial.println(\"relay_update(LIGHT,LOW)\");\n    } else if (strstr((const char *)payload, \"light-off\") != NULL) {\n      digitalWrite(LIGHT, HIGH);\n      Serial.println(\"relay_update(LIGHT,HIGH)\");\n    } else {\n      Serial.println(\"relay_update(LIGHT,INVALID)\");\n    }\n  } else {\n    Serial.println(\"Invalid: command\");\n  }\n}\n\nvoid messageHandler(char *topic, byte *payload, unsigned int length) {\n  Serial.print(\"Message arrived &#91;\");\n  Serial.print(topic);\n  Serial.print(\"] \");\n  for (int i = 0; i &lt; length; i++) {\n    Serial.print((char)payload&#91;i]);\n  }\n  handle_mqtt_msg(topic, payload, length);\n  Serial.println();\n}\n\nvoid setup() {\n    pinMode(LIGHT, OUTPUT);\n    digitalWrite(LIGHT, HIGH);\n    Serial.begin(115200);\n       \/\/ Connect to WiFi\n    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n    Serial.print(\"Connecting to WiFi...\");\n    while (WiFi.status() != WL_CONNECTED) {\n        Serial.print(\".\");\n        delay(1000);\n    }\n    Serial.println(\" connected!\");\n    connectAWS();\n}\n\nvoid loop() {\n    static unsigned long lastTime = 0;\n    unsigned long currentTime = millis();\n\n    if (currentTime - lastTime > 5000) {  \/\/ every 5 seconds\n        lastTime = currentTime;\n        String message = \"Hello from ESP32\";\n        String jsonPayload = \"{\\\"message\\\": \\\"\" + message + \"\\\"}\";\n        publishMessage(\"$aws\/things\/esp32\/data\", (char *)jsonPayload.c_str());\n    }\n\n    if (!client.connected()) {\n        connectAWS();\n    }\n\n    client.loop();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>AWS IoT Topic and Message Structure for Relay Control<br><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>MQTT Topic<\/strong><\/th><th><strong>Message Payload<\/strong><\/th><th><strong>Action Performed<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>$aws\/things\/ESP32\/command\/light<\/code><\/td><td><code>light-on<\/code><\/td><td>Turns the relay (light) ON<\/td><\/tr><tr><td><code>$aws\/things\/ESP32\/command\/light<\/code><\/td><td><code>light-off<\/code><\/td><td>Turns the relay (light) OFF<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To turn on the relay, send below command and message<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1018\" height=\"445\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-12.png\" alt=\"\" class=\"wp-image-1389\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-12.png 1018w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-12-300x131.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-12-768x336.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-12-600x262.png 600w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<p>To turn off the relay send below command and message<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"841\" height=\"550\" src=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-13.png\" alt=\"\" class=\"wp-image-1390\" srcset=\"https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-13.png 841w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-13-300x196.png 300w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-13-768x502.png 768w, https:\/\/vihaaniotgateway.in\/wordpress\/wp-content\/uploads\/2025\/06\/image-13-600x392.png 600w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this guide, you\u2019ll learn how to connect an ESP32-WROOM-32 (38 Pin) development board to AWS IoT Core using the MQTT protocol. You\u2019ll also control a 2-Channel Relay Module with optocoupler to switch devices remotely via the cloud. This is a perfect starting point for building smart home systems, industrial IoT projects, or remote [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[66],"tags":[],"class_list":["post-1353","post","type-post","status-publish","format-standard","hentry","category-iot"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/posts\/1353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/comments?post=1353"}],"version-history":[{"count":13,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/posts\/1353\/revisions"}],"predecessor-version":[{"id":2418,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/posts\/1353\/revisions\/2418"}],"wp:attachment":[{"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/media?parent=1353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/categories?post=1353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vihaaniotgateway.in\/index.php\/wp-json\/wp\/v2\/tags?post=1353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}