[Updated - 1st March, 2020] : Course updated, now you can control your connected light either using Google Assistant or Alexa. Kindly check out the Bonus section at the end of the course. Welcome to , This is the last part of the series and here we will achieve our ultimate goal to Power ON the Light bulb with just one-click from anywhere, literally from anywhere even from the Antarctic as they got internet there. IoT empowers everyone to make life smarter & easier. Though this is just a beginning, if readers show up enthusiasm, I will make more tutorials where the primary focus is to teach from [Do let me know on LinkedIn]. SIMPLE LEARNING AWS Cloud Home Automation Zero to Hero Series. In the fourth part, we will create an automated system to make the home light bulb connected to the web using AWS, an ESP32 board, a relay module and Arduino Code. scratch and make cost-effective solutions 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. 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 : Prerequisites It's the continuation of the following series. Make sure you have exercised rigorously. - Connect your device (ESP32) to AWS cloud Part I This tutorial mostly covers hands-on, prior knowledge of AWS IoT is an advantage. to check AWS Official IoT Documentation. Click here A 2 channel relay module (Easily available in / or Local Robotics Shop) and a power bank to power up the ESP32. Amazon.com Amazon.in /Aliexpress : Learning Objectives By using a web client, we will turn ON/OFF the light bulb by using AWS, an ESP32 board, a relay module and Arduino Code. We will leverage the power of Device Shadow which is very effective in case of power loss and connectivity issue. The device shadow stores the desired state and when the light bulb establishes the connection, the desired state gets synced. Bonus - Toggle the light by using Google Assistant or Alexa. 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 lightbulb; int relay_pin = ; 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( ); (lightbulb.publish(SHADOW_UPDATE,reportpayload) == ) { Serial.print( ); Serial.println(reportpayload); } { Serial.println( ); Serial.println(reportpayload); } } setup() { Serial.begin( ); pinMode(relay_pin, OUTPUT); delay( ); (status != WL_CONNECTED) { Serial.print( ); Serial.println(WIFI_SSID); status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); delay( ); } Serial.println( ); (lightbulb.connect(HOST_ADDRESS,CLIENT_ID)== ) { Serial.println( ); delay( ); ( ==lightbulb.subscribe(SHADOW_GET,mySubCallBackHandler)) { Serial.println( ); } { Serial.println( ); ( ); } } { Serial.println( ); ( ); } delay( ); (lightbulb.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(relay_pin, LOW); (power == ) digitalWrite(relay_pin, HIGH); updateShadow(power); } } /********* Gourav Das Complete project details at https://hackernoon.com/cloud-home-automation-series-part-4-connected-light-bulb-using-aws-esp32-and-arduino-94n377d The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ 13 // Relay 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: // wait 5 seconds for connection: 5000 "Connected to wifi" if 0 //Connect to AWS IoT COre "Connected to AWS" 1000 if 0 //Subscribe to 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 //Sent Empty string to fetch Shadow desired state 3000 if "{}" 0 "Empty String Published\n" else "Empty String Publish failed\n" void if 1 0 "Received Message:" 256 // Test if parsing succeeds. if "parseObject() failed" return "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 char WIFI_SSID[]= ; char WIFI_PASSWORD[]= ; char HOST_ADDRESS[]= ; char CLIENT_ID[]= ; char SHADOW_GET[]= ; char SENT_GET[]= ; char SHADOW_UPDATE[]= ; "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" 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 (Region: N. Virginia), 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. ESP32 Wiring Warning: In this exercise, we’re dealing with the mains voltage . Playing with mains voltage is like playing with fire. If you’re not familiar with mains voltage seek help. While programming the ESP or wiring your circuit make sure everything disconnected from the mains voltage. At first, we will connect the jumper wires (F/F) with 2 channel relay module, as shown below. We will use an extension cord to light up the bulb, as a you should avoid putting the plug to mains voltage while making all the following connections. Next, put the live wire coming out of the plug into relay's port and make a connection from relay's port to the bulb. Thus, by making this connection, ESP32 will able to control the Relay Module and instruct it when to switch ON/OFF the light bulb. First, understand the fundamentals and by take some basic electrical courses you would be able to light up your entire home with just a click of a button. precautionary measure COM1 NO1 Note: Never touch the relay module when powered from the mains voltage. ESP32 to Relay VIN VCC ESP32 to Relay GND GND ESP32 to Relay D13 IN1 Relay to Main Power Source COM1 Relay to Bulb NO1 Once we have built the above connection we are almost done. Now, power on the ESP32 by using an external power source (Power Bank), wait for some time for the connection to establish and also we need to Switch ON the main power of the bulb for to make the circuitry functioning. Now we are going to test the web endpoints we have created in part 3, hit your Node-RED primary URL followed by , AWS (e.g. ) and IBM Cloud (e.g. ). It will load a website, click on the light bulb to turn ON/OFF the bulb. If you have come so far, tadaa! You did it finally 💪 and be proud of your new achievement and show the world what are you capable of 👩💻 👨💻. /ON http://52.90.186.136:1880/ON https://aws-iot.eu-gb.cf.appdomain.cloud/ON Few things to remember: Be gentle while turning ON/OFF the bulb, it may get stuck if clicked very fast. Reset will surely help in that case, press EN in ESP32 to re-establish the connection with AWS. AWS Device shadow is a powerful tool. In case of power loss or connectivity issue, the device shadow stores the desired state which it receives from the web-client hosted in Node-RED. When the light bulb establishes the link, the desired state gets synced. You can try it out by taking the mains power supply of the bulb after that send the desired state (ON/OFF) request from the web-client. When you powered back the Bulb you will see the magic. 🧙♀️ 🧙♂️ . It is an immense pleasure to see you here and reading this, I thank you . Stay curious and if you want more tutorials do let me know on LinkedIn. It will help me to come back with more tutorial on various topic of public interest. Congratulations on completing the 4th part :) individually : After getting a few feedbacks, I thought to integrate the following tutorial with Voice Assistance. You can now continue with the tutorial & integrate it either with Google Assistant or Alexa. Bonus Click here for step-by-step instructions. 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.