How to connect ESP32 to Arduino IoT Cloud
Arduino IoT Cloud is Arduino’s attempt to tap the growing IoT market. It offers integration with several Arduino IoT boards, and also some third-party boards like ESP8266 and ESP32. In this tutorial, we will see:
- How to create a ‘thing’ using ESP32 as the device
- How to generate the sketch for the thing, edit it, and program the board
- Visualize the data coming in from the board on a simple dashboard on Arduino IoT Cloud
The steps mentioned in this tutorial can be replicated for any other board as well.
You should have an ESP32 board (or ESP8266 or one of the Arduino IoT boards). You should be familiar with basic Arduino programming.
Additionally, you should have created a free Arduino IoT Cloud Account. That’s pretty much it.
Part 1 – Create a Thing
What is a thing? It is an IoT node – an independent unit that will sense some physical quantity (temperature, humidity, etc.), and send the measurements to the Arduino IoT Cloud. Let’s create one.
Once you have created your free Arduino IoT Cloud Account, log in. You should be able to see a screen like the one below.
Click on the ‘CREATE THING’ button. You should see a screen similar to the one below:
Set up the Device
Click on ‘Select Device’ button in the Device Section. A popup will open up, similar to the one shown below.
Click on ‘Set up a 3rd Party device’ and select ESP32, and select ESP32, and choose your board from the dropdown. I have the TTGO LoRa32-OLED V1 board. You can select the board available with you.
Click on ‘CONTINUE’, give your device a name and click on ‘NEXT’. The next screen that opens up is quite important. You are given the Device ID and SecretKey. The SecretKey is available only once. So please note it down. You can also use the ‘download PDF’ option that is shown in the popup. Once you have noted the SecretKey/ downloaded the PDF, tick the check box stating so, and click ‘CONTINUE’
With this, your device is set.
What are variables? They correspond to the quantities being sensed. Every IoT device measures some physical quantity (temperature, humidity, location, heart rate, etc.), and sends the data to the server. On the Arduino IoT Cloud, these quantities of interest can be defined as variables. Defining variables is important as Arduino will do most of the work for us once the variables are generated, in the Auto-generated code. We’ll see that shortly.
Click on the ‘ADD VARIABLE’ button, and configure any variable that your ESP32 will measure. For now, I’ll set a variable named ‘temperature’ of type ‘Temperature Sensor (Celsius)’, mimicking the case when a temperature sensor is connected to the ESP32. It’ll be kept Read-only, i.e., the Cloud can only read the value of this variable from the device, and not send a new value to the device. I’ll configure the variable update policy such that the variable’s value will be sent to the Cloud whenever it changes by at least 1 degrees Celsius.
Once done, click on ‘ADD VARIABLE’. You can similarly add more variables. I’ll keep only one variable for now.
Click on the ‘Configure’ button in the ‘Network’ tile. A popup will show up, where you will be asked to enter the SSID and password of the network that your device will connect to. Enter these details. You will also be asked for the SecretKey, which you noted down in the ‘Set up the device’ step. Once these details are entered, click ‘SAVE’
That’s it. You have successfully configured your thing. Now let’s move on to the next part.
Part 2 – Program the board
Move on to the ‘Sketch’ tab. You will see the auto-generated code in the text editor, along with the familiar ‘Verify’ and ‘Upload’ buttons.
Editing the Code
While you can edit the code on this screen itself, I personally prefer to use the Arduino Online Editor. Click on the ‘Open Full Editor’ button. That should open up a new tab, showing the code in Arduino Online Editor (you may be asked to re-login to the editor). You should be able to see a screen similar to the one below.
You will be able to see 3 files (apart from the ReadMe):
- The .ino file: This is the file wherein we can make edits. The comment //Your code here, in the loop indicates where we can start making edits.
- A thingsProperties.h file: This is an important file, that declares the variables that we configured earlier, the SSID, password, and SecretKey that we configured earlier, and also contains functions for initializing the Properties of the board (initProperties).
- Secret: This tab should be used only if you wish to make changes to the SSID, Password or the SecretKey configured earlier.
Now, we will make a small edit. In the loop, we will set the temperature to increase by 0.5 and reset to 0 if its value crosses 200. We will also introduce a 1-second delay in the loop. Ideally, the temperature should come in from a sensor, and you should make edits to the code to read values from the sensor.
You can now hit the ‘Verify’ button to make sure that your sketch is getting compiled without any bugs.
Program the board
In order to program the board using the web browser, you will need to install the Arduino Create Agent. Click here and follow the steps to download and install the agent on your machine. Once you are done, you should be able to see the Arduino Create Agent Tray Icon. Also, the COM Port will be shown along with the board in the Arduino Online Editor. You may want to refresh once if the COM Port doesn’t show immediately, and you might have to click the dropdown and select the COM Port manually in case you have multiple peripherals connected.
Now, you can click on the Upload button (->) and the sketch should start getting uploaded. You can check the status in the terminal at the bottom.
Next, you can click on the Serial Monitor from the left pane, to see the serial print statements.
Part 3 – Visualize the incoming data on a dashboard
We will create a very simplistic dashboard here. Go to the Arduino IoT Cloud, and click on ‘Dashboards’ from the top menu, and click on ‘BUILD DASHBOARD’
Click on the Edit icon, then click on the ‘Add’ dropdown, go to the ‘Things’ pane, and select your thing.
Click on ‘CREATE WIDGET’ in the popup that opens up, and you should start seeing the data getting updated realtime (provided your thing is powered ON).
That’s it. Congratulations on successfully linking your ESP32 to the Arduino IoT Cloud, and displaying the data on a dashboard.
Since you are here, you may find this course on Udemy, which explains how to link ESP32 with AWS IoT, to be quite interesting. An introductory article on linking ESP32 to Arduino IoT Core can be found here.
Smart Home IoT Project using Arduino Cloud & ESP32 Alexa
In this smart home IoT project, I have explained how to make an Arduino Cloud ESP32 Alexa home automation system to control appliances with the internet from anywhere in the world. With this internet of things project, you can control 4 relays from Arduino IoT Cloud dashboard, Alexa, IR remote, and manual switches. You can also monitor the real-time room temperature in the Arduino cloud dashboard and Amazon Alexa app. If there is no internet available still, you can control the appliances from the IR remote and switches.
I have used all the FREE tools and you don’t need any Alexa devices or Amazon Echo Dot for this voice control smart home IoT project.
So if you follow all the steps, you can easily make this Home Automation System with Arduino IoT Cloud and ESP32 for your home.
Table of Contents
Required Components for the ESP32 IoT projects
Circuit of the Arduino Cloud ESP32 projects
The circuit is very simple, I have used D23, D22, D21 & D19 GPIO to control the 4-channel relay module.
And the GPIO D13, D12, D14 & D27 connected with pushbuttons to control the relay module manually.
I have used the INPUT_PULLUP function in Arduino IDE instead of using the pull-up resistors with each push button.
As per the source code, when the control pins of the relay module receive a LOW signal the relay will turn on and the relay will turn off for the HIGH signal in the control pin.
IR remote receiver (TSOP1838) connected with D35. And the DHT11 sensor connected with RX2 (GPIO16).
If you want to use the latched switches instead of pushbuttons, then just connect the switches instead of the pushbuttons across GPIO pins and GND.
I have used a 5V 5Amp mobile charger to supply the circuit.
Please take proper safety precautions while connecting the AC appliances.
Tutorial video on Arduino Cloud ESP32 IoT project
In the Arduino Cloud tutorial video, I have covered the following steps in detail.
- Create new things in Arduino IoT Cloud.
- How to set up Arduino IoT Cloud Dashboard.
- How to set up Arduino IoT Cloud for ESP32.
- Programming the ESP32 with Arduino IDE
- Connect Arduino IoT Cloud with Amazon Alexa App.
- Control relays with IR remote
Arduino IoT Cloud FREE account setup
For this IoT project, I have used the Arduino Cloud Free plan.
First, you have to add ESP32 devices in Arduino IoT Cloud.
Then you have to add 4 Alexa Compatible Switch variables and 1 Temperature sensor variable to the Arduino Cloud Thing.
Please click on the following link for more details on Arduino IoT Cloud setup.
Important points for the ESP32 internet of things project:
- You can control maximum 5 relays or sensors with the Arduino IoT cloud FREE plan.
- The IR receiver sensor must have a metallic casing. Otherwise, you may face issues while trying to get the Hex code.
- You don’t need an Amazon Echo Dot or any other Alexa devices for this project. But if you have you can use it.
- After compiling the code, you have to press and hold the BOOT button of ESP32 until the code starts uploading.
- You can also use Arduino Web Editor to program the ESP32.
- You will get real-time feedback if the ESP32 connected to the internet.
- Use a stable 5V 5A DC power supply.
Program ESP32 with Arduino IDE
In the Tutorial video, I have explained all the steps to program the NodeMCU using Arduino IDE.
- Update the Preferences –> Aditional boards Manager URLs: https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Then install the ESP32 board from the Board manager or Click Here to download the ESP32 board.
- Download the required libraries from the following links:
- ArduinoIoTCloud version 1.0.2 by Arduino and all the dependencies.
- IRremote Library version 3.3.0 by Shirriff.
Go to Sketch -> Include Libraries -> Manage Libraries in Arduino IDE.
When you try to install the ArduinoIoTCloud library, it will ask you to install all the dependencies. Then Click on Install All.
Code for Arduino IoT Cloud ESP32 home automation
After uploading the code to ESP32, please refer to the following articles for connecting the Arduino IoT Cloud Account with Amazon Alexa App.
After doing all these steps, now you control the appliances with Alexa.
ESP32 Alexa control Relay using Arduino Cloud
If the ESP32 is connected with WiFi, then you can ask Alexa, to turn on the light [“Alexa, Turn ON Room Light”]. Thus, you can control the appliances like lights, fans, etc with voice commands using Amazon Alexa App from anywhere in the world.
You can also monitor the real-time feedbacks and room temperature in the Alexa app.
Control Relays with Arduino IoT Cloud Remote App
You can also control the relays and monitor the room temperature from Arduino IoT Cloud Remote App.
Just download and install the Arduino IoT Cloud Remote app from Google Play Store or App Store, then log in to your Arduino Cloud account and select the dashboard.
Sometimes you have to refresh the dashboard multiple times if you face any issue.
Control Relays manually with Push Buttons
- No Wi-Fi Manual Control
- Control relays with Push Buttons
You can always control the appliances manually with push buttons. and if the ESP32 is not connected with the Wi-Fi, still you can control the appliances with push buttons.
IR Remote control relays with ESP32
You can also control the appliances with any IR remote.
First, you have to get the HEX codes of the remote buttons, then update the HEX code in the code. That’s it. Now you can control the appliances with the IR remote.
PCB for the ESP32 IoT Projects
You can also use this PCB for this ESP32 IoT project. Just download the Gerber file and order the PCB from any PCB manufacturer.
- Control relays from Alexa App
- Control relays with IR remote
I hope you like this Smart house IoT projects idea with the Arduino Cloud ESP32.
Please do share your feedback on this IoT project. Thank you for your time.