This an example of how to configure an ESP32 dev kit connecting to Wi-Fi, and subscribing and publishing data from/to topics with Ubidots using the MQTT protocol.
- Arduino IDE (recommended for beginners) or VSCode with Arduino VSCode Extension (not covered in this README)
Before this, you need to create an account in Ubidots. Once created, follow the next steps:
-
Go to your dashboards page.
-
Click on
Devices > Devices
. -
Hover the Add icon and click on
Add new Device
. -
From all the device types listed, select
Blank Device
. -
Indicate as a name and label 'ESP32 01' and 'esp32-01', respectively.
You can use another name and label, but then you have to edit the code example accordingly.
-
Now, click in the recently created device, and click on
Add Variable > Raw
. Name it 'Led status'. -
Repeat step 6 with a variable named
ADC
.By default, Ubidots labels your variables automatically, but if you didn't indicate any name during the creation of the variable, it will use a generic non-readable label. Ensure that the variable 'Led status' and 'ADC' have as labels 'led-status' and 'adc', respectively.
Your new device should look like this.
We have created our device and its available variables. Let's create a dashboard to plot them.
- Go to the dashboards page clicking on
Data > Dashboards
. - Create a new dashboard clicking on the Add new Dashboard button.
- Indicate a name for the dashboard and create the dashboard.
- Click on 'Add new Widget' and select 'Switch'. Name it 'Led status'.
- In the data section, click 'Add Variables' and select
ESP32 01 > Led status
. - Once created, the widget will serve us to publish the led status to the cloud and the board will read the variable to control the on board led accordingly.
- Create another widget of type 'Line chart' named 'ADC values' and with the variable
ESP32 01 > ADC
. The widgets can be moved and resized.
Once finished, you should have a dashboard like this (no data are shown as we have not sent something yet).
First of all, we need to add our ESP32 board in Ardunio IDE in order to make it appear in the board manager. Follow these steps:
-
Open Arduino IDE (how to install is not covered here... it's just a
Next
,Next
,Next
, ..., procedure). -
Go to
File > Preferences
. -
In Additional Boards Manager URLs, add the URL
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
If you have other board URLs, add a comma at the end and then place the new URL.
-
Then, go to
Tools > Board > Boards Manager...
and, after an update, typeESP32
in the search field. The esp32 board by Espressif Systems will be listed. Click Install. -
Once finished, you have your available in Arduino IDE. To select it, go to
Tools > Board > ESP32 Arduino
and selectESP Dev Module
. -
Plug your board to the PC using a USB cable and then select the proper COM port in
Tools > Port
.
Now you can develop code for your board and upload it to the MCU.
In order to upload code to your MCU, you must hold the BOOT in your board when the terminal outputs Connecting...
. Once connected, you can release the BOOT button.
To add the required libraries:
- Download them from here and here.
- In Arduino IDE, go to
Sketch > Include Library > Add .ZIP Library...
and select the downloaded.ZIP
files (one at a time).
Easy peasy.
The code needs the Wi-Fi credentials and your Ubidots token to operate. These are sensitive data that must not be shared with anyone. For this reason, the file containing this information must not be committed.
The sensitive data must be added to a file called private_data.h
in the root of the project. This file is in the .gitignore
file and will not be committed.
A private_data.template.h
file is provided with the variables to be initialized. Rename this file private_data.h
and fill it with your personal data.
You can find the UBIDOTS_TOKEN
in the web page of your device on Ubidots.
Now just plug you board to the PC, verify and upload the code to the MCU (remeber the BOOT button). Check the output at the serial terminal and, once connected, control the on board led from the dashboard and view the data from the ADC.
You have implemented a node for an IoT application! Congrats!! π
Play with the code to subscribe to more topics, to publish more data or to change the publish frequency, or add more nodes/devices in the network.
This code is released under the MIT License.