ESP32 is one of the microcontrollers for IoT applications par excellence. Created and manufactured by Espressif, it is currently the microcontroller of this compañy most used by the community, it is widely used for both educational and industrial development due to its great versatility, reliability and very low cost.
This Repository contains the source code and the steps to follow to be able to make ESP32 Microcontroller read sensor data and send it, in an organized way, to the Tangle (DLT) of the IOTA Network through the Streams layer.
This is the list of Sensors/Modules that you can connect and it will be recognized immediately.
- BME280 (Bosch) - Temperature, Humidity and Pressure sensor. -> Connected by I2C Bus via: GPIO33/SDA and GPIO32/SCL --- Be careful with the supply of this sensor, the BM280 is powered with 3.3V, if your module does not have a voltage regulator (some modules do not have it) the sensor can be damaged if you supply 5V.
- MPU6050 (InvenSense-TDK) - Acelerometer and Gyroscope 6-axis. -> Connected by I2C Bus via: GPIO33/SDA and GPIO32/SCL.
- BH1750 (ROHM) - Ambient Light Sensor. -> Connected by I2C Bus via: GPIO33/SDA and GPIO32/SCL.
- Generic Adjustable Sound Sensor with digital output (like KY038 Module) - -> Digital Signal on GPIO21, GPIO22 to GND (to enable sound data collection).
- Also, you can connect a Green LED in GPIO2 that blink when the data is sent to the Tangle, and a Red LED in GPIO15 that will Blink in a certain way when it detects certain errors (totally optional)
The following diagram explains how each sensor of our stack must be connected to the ESP32 pins. The "NodeMCU-32s" development board has been used in this diagram, which contains the ESP32 microcontroller. However, any development board that contains the ESP32 microcontroller can be used.
It is not necessary to have all the sensors listed here, the code is able to detect which sensors were connected. In the case of not connecting any sensor, the only real data that will be displayed on the Tangle will be the Internal Temperature of ESP32.
IMPORTANT NOTE: Espressif has deprecated the ESP32's internal temperature sensor a few years ago, as it has proven not to be a very accurate measurement. However we will use it as a minimum unit of information to be able to send data to the Tangle without having sensors connected. Please keep this in mind, so this value should not be taken into account for critical applications.
This repository uses the Iot2Tangle C Core devices adapted for ESP32-FreeRTOS offered in the official Espressif Toolchain ESP-IDF SDK. Once the SDK is installed you will have all the tools to compile and download the program on your ESP32.
The easiest way to install ESP-IDF and their prerequisites is to download the ESP-IDF Tools installer from this URL: https://dl.espressif.com/dl/esp-idf-tools-setup-2.3.exe
Just follow the steps and you will have the official Espressif SDK Toolchain installed on your computer.
To check other methods, the following page is suggested: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html
Prerequisites of ESP-IDF SDK:
sudo apt update
sudo apt install git wget flex bison gperf python python-pip python-setuptools cmake ninja-build ccache libffi-dev libssl-dev
It is recommended to install the stable version: ESP-IDF v4.1, you can download it from here:
git clone -b v4.1 --recursive https://github.com/espressif/esp-idf.git
Now install the SDK, this may take a while:
cd ~/esp-idf
./install.sh
. ./export.sh
After doing this last step do not close the shell, as we will compile and flash from here. If you close the shell you will have to do the previous step again.
You can download the repository directly from Github, or from shell or Command Prompt with the following command:
git clone https://github.com/iot2tangle/ESP32.git
cd ESP32/mqtt-sender
The config.h file must be opened and modified, this file is in the directory 'ESP32/mqtt-sender/main' of the repository.
This step is very important if you want to make a connection to the gateway. Your WiFi Credentials, the address and port that will have the I2T Streams MQTT Gateway (It is also possible to configure username and password in case the Broker has it), the Device Id, and others configurations. The Id Name Device you define here must be between the devices you set in on the Gateway configuration file.
/* Device */
const char* id_name = "ESP32-MQTT";
/* Network Configuration */
const char* ssid_WiFi = "mySSID";
const char* pass_WiFi = "myPASS";
/* Broker Configuration */
const char* address = "mqtt.iot2tangle.link"; /* Broker address (MQTT), must NOT include 'http://xxx' or 'tcp://xxx' */
int port = 8883;
const char* topic = "iot2tangle"; /* MQTT topic */
const char* user = "mqtti2t"; /* MQTT user */
const char* password = "integrateeverything"; /* MQTT password */
/* Enable Sensors */
bool isEnable_TemperatureIntern = true;
bool isEnable_TemperatureExtern = true; /* true: Enable -- false: Disable */
bool isEnable_Humidity = true;
...
/* Interval of time */
long interval = 30; /* Time in seconds between */
In case of not using Username and Password in the Broker, leave these fields empty:
const char* user = ""; /* MQTT user */
const char* password = ""; /* MQTT password */
Remembering to have the ESP-IDF Toolchain open, and you make sure you are at the root of the http-sender folder run the following command:
idf.py build
If the compilation was correct it should read: Project build complete.
Now make sure you have ESP32 connected to your computer, and know what COM port it is connected to. (You can see this in 'Device Manager').
Then run the following command that will start flashing the firmware. (You will probably have to press the reset button on your ESP32 development board, even several times for it to recognize the board.)
idf.py -p COM1 flash # COM1 is an Windows port example, you must put your port. In Linux /dev/ttyUSB0 is an example, and in macOS: '/dev/cu'
Upon completion, the firmware is downloaded to your ESP32. If the I2T Streams HTTP Gateway is configured correctly (we will explain this next), you will be sending data to Tangle via Streams.
If configured correctly, ESP32 should be sending data to the gateway automatically. However, you may want to verify that it is running on ESP32.
The code continuously sends information out the serial port, so it can read the serial port to see what is happening and detect errors.
You can use the 'Arduino Serial Monitor' for this, but we recommend using the following software:
Open Command Prompt.
Configure the Baud Rate of the port to 115200 bps:
mode COM1: baud=115200
Read the serial port:
copy COM1: con:
Install cu Monitor. It is an excellent shell monitor and very useful.
sudo apt install cu
Run cu Monitor:
cu -l /dev/ttyUSB0 -s 115200
The following screenshot is a reading of the Serial Port, you should see something like this:
Install Rust if you don't have it already. More info about Rust here https://www.rust-lang.org/tools/install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Make sure you also have the build dependencies installed, if not run:
sudo apt update
sudo apt install build-essential pkg-config libssl-dev cmake
Get the Streams MQTT Gateway repository https://github.com/iot2tangle/Streams-mqtt-gateway
git clone https://github.com/iot2tangle/Streams-mqtt-gateway
Navigate to the root of Streams-MQTT-gateway directory and edit the config.json file to define yours device names, broker address, ports, you can also change the IOTA Full Node used, among others.
Run the Streams Gateway:
cargo run --release
This will compile and start the Streams Gateway. Note that the compilation process may take from 3 to 25 minutes (Pi3 took us around 15/25 mins, Pi4 8 mins and VPS or desktop machines will generally compile under the 5 mins) depending on the device you are using as host. You will only go through the compilation process once and any restart done later will take a few seconds to have the Gateway working.
Once started, the Channel Id will be displayed, and the gateway will be open waiting for data to send to the Tangle.
The Channel Id that will allow subscribers to access the channel data.
You can read the received messages directly from the I2T Explorer: https://explorer.iot2tangle.io/ using the Channel Id printed by the Gateway in shell.
For inquiries, improvements or errors detected, please start an issue in this repository.