Welcome to SIMPLE LEARNING , . Before starting, let's do a recap, in we have connected the ESP32 MCU with AWS and able to publish Messages to IoT Core. Everything covered from scratch you won't face any difficulty understanding In case of any clarification, . Feel free to explore them with ease, skip to the one which is relevant to you. AWS Cloud Home Automation Zero to Hero Series. In the second part, we will turn ON/OFF the inbuilt led of ESP32 using AWS Device Shadow Service Part 1 The following series split into four parts (refer below) with very simple and clear instructions to provision a home automation system to control house appliances through the web. . drop me a note on LinkedIn - Connect your device (ESP32) to AWS cloud. Part I - Use Device Shadow Service (AWS IoT) to control ESP32 inbuilt led using MQTT client. Part II - Create a secure web client hosted in Node-RED to control ESP32 inbuilt led. Part III - The Real Deal: Create an automated system to make the light bulb connected to the web using AWS, an ESP32 board and a relay module. Part IV A device's shadow is a JSON document that is used to store and retrieve current state information for a device. The Device Shadow service maintains a shadow for each device you connect to AWS IoT. You can use the shadow to get and set the state of a device over MQTT or HTTP, regardless of whether the device is connected to the Internet. Each device's shadow is uniquely identified by the name of the corresponding thing. [Ref: AWS Documentation] : Prerequisites Prior knowledge to connect ESP32 to AWS. If you are unaware, go to Part 1 of the series . This tutorial mostly covers hands-on, prior knowledge of AWS IoT is an advantage. to check AWS Official IoT Documentation. Click here : Learning Objectives Using Device Shadow to control the ESP32 built-in led, a stepping stone to achieve the final objective to control your home appliance anywhere from the world. Use AWS MQTT Client to publish messages to activate Device Shadow service and control ESP32 inbuilt led. So let's see something happen now . We will copy the below Arduino code, do some minor changes in Arduino IDE and make the code functional. Alternatively, you can download the Arduino Code as ino file . here #include <AWS_IOT.h> #include <WiFi.h> #include <ArduinoJson.h> AWS_IOT shadow; #define ledPin char WIFI_SSID[]= ; char WIFI_PASSWORD[]= ; char HOST_ADDRESS[]= ; char CLIENT_ID[]= ; char SHADOW_GET[]= ; char SENT_GET[]= ; char SHADOW_UPDATE[]= ; int status = WL_IDLE_STATUS; int msgReceived= ; char payload[ ]; char reportpayload[ ]; char rcvdPayload[ ]; mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad) { strncpy(rcvdPayload,payLoad,payloadLen); rcvdPayload[payloadLen] = ; msgReceived = ; } updateShadow (int power) { sprintf(reportpayload, ,power); delay( ); (shadow.publish(SHADOW_UPDATE,reportpayload) == ) { Serial.print( ); Serial.println(reportpayload); } { Serial.println( ); Serial.println(reportpayload); } } setup() { Serial.begin( ); pinMode(ledPin, OUTPUT); delay( ); (status != WL_CONNECTED) { Serial.print( ); Serial.println(WIFI_SSID); status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); delay( ); } Serial.println( ); (shadow.connect(HOST_ADDRESS,CLIENT_ID)== ) { Serial.println( ); delay( ); ( ==shadow.subscribe(SHADOW_GET,mySubCallBackHandler)) { Serial.println( ); } { Serial.println( ); ( ); } } { Serial.println( ); ( ); } delay( ); (shadow.publish(SENT_GET, ) == ) { Serial.print( ); } { Serial.println( ); } } loop() { (msgReceived == ) { msgReceived = ; Serial.print( ); Serial.println(rcvdPayload); StaticJsonDocument< > doc; deserializeJson(doc, rcvdPayload); (doc.isNull()) { Serial.println( ); ; } int power = doc[ ][ ][ ]; Serial.println(power); (power == ) digitalWrite(ledPin, HIGH); (power == ) digitalWrite(ledPin, LOW); updateShadow(power); } } /********* Gourav Das Complete project details at https://hackernoon.com/cloud-home-automation-series-part-2-use-aws-device-shadow-to-control-esp32-with-arduino-code-tq9a37aj The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ //Download latest ArduinoJson 6 Version library only by Benoît Blanchon from Manage Libraries 2 // LED Pin "Wifi-Name" "Wifi_Password" "************-***.iot.us-east-1.amazonaws.com" //AWS IoT Custom Endpoint Address "ESP32" "$aws/things/{Thingname}/shadow/get/accepted" "$aws/things/{ThingName}/shadow/get" "$aws/things/{ThingName}/shadow/update" 0 512 512 512 void 0 1 void "{\"state\": {\"reported\": {\"power\": \"%d\"}}}" 3000 if 0 "Publish Message:" else "Publish failed" void 115200 2000 while "Attempting to connect to SSID: " // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 5000 // wait 5 seconds for connection: "Connected to wifi" if 0 //Connect to AWS IoT COre "Connected to AWS" 1000 if 0 //Subscribe to Accepted GET Shadow Service "Subscribe Successfull" else "Subscribe Failed, Check the Thing Name and Certificates" while 1 else "AWS connection failed, Check the HOST Address" while 1 3000 /*Sent Empty string to fetch Shadow desired state*/ if "{}" 0 "Empty String Published\n" else "Empty String Publish failed\n" /*Sent Empty string to fetch Shadow desired state*/ void if 1 0 "Received Message:" 256 if /* Test if parsing succeeds. */ "parseObject() failed" return /* Test if parsing succeeds. */ "state" "desired" "power" if 1 else if 0 We need to add a library in Arduino IDE, go to . Search for ArduinoJson (by Benoit Blanchon), select the latest 6.x.x version only and click install. Also, remember that we have used library in the code to integrate ESP32 with AWS. If you are unaware, go to . Also, check the Certificates enrollment section under Hardware integration. Tools → Manage Libraries The code won't work on 5.x.x and below versions. AWS_IOT Part 1 of the series Code Modifications From the AWS region selector in the navigation bar (top right corner), choose the US East (N. Virginia) Region only throughout the series. Update the necessary details, skip (Keep it as it is). In put the AWS IoT Endpoint Address which you can retrieve from IoT Dashboard, settings(check sidebar). Next, we will replace in the code with the actual thing name (e.g. ESP32) we gave at the time of thing creation in Part 1 of the series. To check the , to go to AWS IoT Console, select under the option from the Sidebar and check for the name as highlighted below. CLIENT_ID[]= "ESP32" HOST_ADDRESS[] {ThingName} Thing Name Click Here Things Manage Now plug your ESP32 board with your Desktop/Laptop using a USB cable and upload the code. Next, we will cover the Code workflow and Test the functionality. Code Workflow Testing Plug your ESP32 board to your Desktop/Laptop using a USB cable. to go to AWS IoT Console. Choose Test from the Sidebar it will connect the MQTT client. Under Subscription topic, subscribes to the following Topics: and [Do replace with the Thing name you provided in AWS]. Here we are using 1 as ON flag and 0 as OFF flag. Click Here $aws/things/{ThingName}/shadow/update $aws/things/{ThingName}/shadow/get {ThingName} Now in publish the following msg in JSON format: $aws/things/{ThingName}/shadow/update { : { : { : }}} "state" "desired" "power" "1" and publish to topic . Now, repeat the same just replace 1 with 0 in JSON it will switch OFF the led. {} $aws/things/{ThingName}/shadow/get By doing so it will switch ON the led. . In the next part, we will build Web client to interact with device shadow and switch on/off the led by using Node-RED by IBM. Congratulations on completing the 2nd part :) Kindly Share & Comment only if you find it useful and help me on my mission to educate and familiarize people in the world of digitization 💪 #This is a Free tutorial and all my upcoming tutorials will be free and accessible from Public forums.# Appreciate if you drop me a note on LinkedIn & share your opinion. Don't worry, I don't bite 👻 so don't shy away 🏃🏻♀️ 🏃🏻. Your feedback will help me to come up with more awesome contents on the internet.