Let’s Switch! - A complete Guide for beginners to switch from Arduino IDE to ESP-IDF and ESP-Rainmaker.
Internet of Things is a major technology that plays a very important role in the Industry 4.0 revolution, everything that surrounds us has some information to share and IoT is playing a major role in collecting that data from the surroundings and transferring it on our mobile phones. Let’s have a look at the architecture of IoT, we have 3 basic layers — Perception Layer, Network Layer and Application Layer.
The Perception layer contains all the devices and hardware part that consists of sensor and micro-controller and combining these together with normal devices makes up the “Things” in Internet of Things.
The Network Layer is responsible for uploading the data collected by those “Things” to the cloud network and to transfer the commands from Cloud to those devices to perform a specific task. This layer contains the communication protocols and Gateways which are required for the transmission of data from device to cloud and vice-versa.
The Application Layer is the end layer from where we generate IoT applications out of the data which is collected by the IoT devices and create a solution for the end user to interact with those devices.
When we talk about the IoT devices, majority of them are running on WiFi and when it comes to WiFi modules, we all have worked with ESP modules by Espressif. The ESP family of micro-controllers provide an optimal solution by combining the WiFi capabilities within a micro-controller. Most of us use Arduino IDE to program these boards as it is a very easy solution indeed, but things were changed when ESP-32 came in picture, now we have a complete IoT development Framework for the ESP-32 boards and its successors. The ESP-IDF provides a better control over the the firmware and helps to develop a more stable solution for our IoT devices. In this guide we’ll learn how to get started with the ESP-IDF and the ESP-Rainmaker(We’ll talk about the Rainmaker later in this article).
ESP-IDF is available for windows, Linux and Mac users and here in this guide we’ll talk about the Windows Users only. If you’re a Linux or a Mac user I’ll recommend you to read the documentation for that.
Let’s start with the things we’ll be needing to get our hands on ESP-IDF.
- The ESP-IDF Tool Chain (this will take care of all the prerequisites)
- The ESP-IDF
- ESP-32 Board
Let’s Switch!
Now first thing first, We need to download the ESP-IDF Tool Chain, this executable file will install all the prerequisites for ESP-IDF to run and we don’t need to worry about anything.
This tool will automatically detect the python, Git and ESP-IDF on your computer(if available already) or you’ve to select the path for each tool. If not already available you can select the option to download these tools and the toolchain will download all the necessary files for you.
Now we need to select the ESP-IDF Version from the list, here we will be working on the version 4.2 and then we need to select the path for the ESP-IDF, you can choose any path but here, I’ve installed the ESP-IDF at
C:/esp/esp-idf and I recommend you to do the same.
We’ve successfully installed the ESP-IDF on our system.
Since, we’re new to this platform let’s start using the ESP-IDF step by step before programming or flashing a new firmware on our device, we need to run a few commands.
Open the “ESP-IDF Terminal” from the Start menu and go to the directory where we’ve installed the ESP-IDF. If you’ve chosen the same path as demonstrated here, you need to type —
$ cd c:/esp/esp-idf
then type,
$ install.bat
This is for setting up the tools
after the process is completed type,
$ export.bat
This command is for setting the environment variables.
We are now ready to program our ESP32 board from ESP-IDF.
you can upload any example file present in the esp-idf directory.
just go to any example directory and use these commands to upload the firmware to the device.
$ idf.py set-target esp32
To set the target device.
$ idf.py build
To build the project, and finally
$ idf.py -p (COM PORT) flash
To flash the Firmware
- Note — you might need to press the Boot button on the ESP32 board to flash the Firmware.
To open the serial monitor type,
$ idf.py -p (COM PORT) monitor
and it will start the Serial Monitor.
Let’s Move to the ESP - Rainmaker
We’ve already discussed about the Network Layer and the Application Layer previously in this article, in simplest terms the ESP- Rainmaker will eliminate all the hassles of designing the complete cloud networking structure and the front-end user application.
You only have to focus on designing the firmware for your device and the ESP-Rainmaker will take care of cloud, protocols and the frontend application.
- Please note that the ESP-Rainmaker is available for ESP32-S2 boards, if you want to program your ESP-32 board then you have do the Assisted Claiming in your ESP32 board.
First, we need to clone the ESP-Rainmaker Project. Go to the same directory i.e. c:/esp Using,
$ cd c:/esp
Execute the following command for cloning the complete project.
$ git clone https://github.com/espressif/esp-rainmaker.git
We need to set up the CLI, make sure you have python 3 installed on your PC. Now we will need to install the dependencies, type the following commands in CMD.
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ cd c:/esp/esp-rainmaker/cli/
$ pip install -r requirements.txt
This will complete all the requirements for the Rainmaker.
At this point, we need to to sign-up for the ESP-Rainmaker, download the mobile app and create an account. After signing-up do the following,
$ python rainmaker.py -login
This will open the webpage on the browser and you’ve to login to your rainmaker account. Now we need to complete the assisted claiming for our esp32 board. Open ESP-IDF Command Prompt and type,
$ cd c:/esp/esp-rainmaker/examples/switch
$ idf.py menuconfig
This will open a config menu and now, we can do the assisted claiming. Select,
ESP RainMaker Config → Use assisted Claiming
We are all ready to work with the ESP-Rainmaker.
Making a Motion Sensor Device and controlling it from ESP- Rainmaker App.
We will be making motion sensor device to get started with the ESP-Rainmaker and ESP-IDF, In this project we will be attaching a PIR sensor and a LED to the ESP-32 board.
Working : When the motion is detected by the PIR Sensor it will update the data on the ESP-Rainmaker application and the Built-in LED will turn ON automatically along with the External LED attached to the Board.
The Built-In LED will turn OFF automatically as soon as there is no motion, but the External LED will still be ON, we can also control the external LED with the ESP- Rainmaker Mobile app and all the Data from the Motion sensor and the current state of the LED will be updated on the Mobile App.
The Complete code and the Code explanation is present here, this is the git-hub repository for this project and you’ll find main program here.
Uploading the Firmware
Clone the above github repo in “esp-rainmaker/examples”, and using ESP-IDF Command Prompt, open the same directory.
$cd C:/esp/esp-rainmaker/examples/ESP-Rainmaker
select the target as esp-32
$ idf.py set-target esp32
build the firmware,
$ idf.py build
and finally flash the firmware
$ idf.py -p COM3 flash
Now open the Serial Monitor and you’ll a QR code on the Screen. open the Rainmaker app and click on add device to scan the QR Code.
After scanning, you’ll be directed to provide the WiFi Credentials to your ESP32 board. Select the Wifi network and enter the password and the device will be connected to your wifi network and you can see the data on the serial monitor.
And the mobile app dashboard will look like this.
You can now easily control the LED with the Mobile app.
Please note that a LED is used here just for the demonstration, you can use the single relay module and connect any other appliance to control it according to the motion and from the Application itself.
The GPIO Pins that are used here are,
- GPIO — 34 for PIR sensor.
- Built_in LED on pin 2
- External LED on GPIO 26