Setting up the ESP32 and controlling a relay - Crelde/iotapoweredplants GitHub Wiki

This part of the project enables you to switch on/off a water pump, by connecting it to a esp32 board with a relay! Here you can see what you need, how you set it up and finally how to flash the ESP32 to run the code.

Parts

Setting up the hardware

ESP32 Schematic

For this setup it would have been more correct to use a transistor instead of a relay, but in a larger scale setup where the pump is powered from an external data source a relay would make more sense.

My setup

Here you can see my actual wiring, sorry for the terrible colors on the wires.

In the schematic you can see the ESP32 board, the grey wire is connected to GPIO27 which is setup to be Relay 1 in the ESP32 code.

The Out part of the relay gets power from the ESP32 through the power channel on the breadboard and is then connected to the power wire of the pump. The ground wire on the pump is connected to the GND channel.

The pump initially had a USB interface, which I cut off in order to put the wires directly onto the breadboard!

Here is a link to the pump that I used. If you cut off the usb head you get a red (power) and a black (gnd) wire that you can connect to the breadboard.

Flashing your ESP32 to run the project

If you cloned the project you should already have a folder called ESP32_http-receiver which is set up for this usecase.

For this part I will copy the instructions from the iot2tangle repository that I slightly tweaked to fit this use case

4 GPIO ESP32 pins have been defined for the outputs. The outputs are Boolean: 1 or 0 (3.3V or 0V in voltage). Any device that takes advantage of these outputs can be connected, but without a doubt the ones that bring out the most potential to the application are the relays.

The default ESP32 Outputs GPIOs are: Output/Relay 1 -> GPIO26 -- Output/Relay 2 -> GPIO27 -- Output/Relay 3 -> GPIO14 -- Output/Relay 4 -> GPIO12

ESP32 receiver Hardware connections

It is quite possible that an external 5V source is needed to power the relays, as development boards generally do not have enough power to power a relay

IMPORTANT: The moment you start using Relays is when, for your safety, you should pay close attention to their electrical connections. Whenever you are going to make changes to the connection make sure you have disconnected the AC connector, otherwise there may be a risk of electrocution. The following link from Panasonic company has safety tips when handling Relays. Relays Cautions for Use - Panasonic

Download Firmware on ESP32

This repository uses the Iot2Tangle C Core receiver 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.

Remember that for ESP32 to be taking data you must have a Keepy instance running on some endpoint that ESP32 can reach.

1) Install ESP-IDF SDK:

Windows:

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

Linux and macOS:

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.

2) Download the Iot2Tangle ESP32 Repository and go to the 'http-receiver' folder:

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/http-receiver

3) Edit the file config.h

The config.h file must be opened and modified, this file is in the directory 'ESP32/http/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 Keepy Host running, 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-Receiver";

/* Network Configuration */
const char* ssid_WiFi = "mySSID";
const char* pass_WiFi = "myPASS";

/* HTTP Endpoint Configuration */
const char* address = "YOUR_KEEPY_HOST/messages/last";  /* Endpoint address (HTTP), must NOT include 'http://xxx' or 'tcp://xxx', and must include '/messages/last' for using in I2T Keepy*/
int port = 3002;

/* Enable Relays */
bool isEnable_Relay_1 = true;
bool isEnable_Relay_2 = true;	/*			true: Enable  --  false: Disable			*/
bool isEnable_Relay_3 = true;
bool isEnable_Relay_4 = true;

/* Interval of time */
long interval = 15;    /* Time in seconds between */

4) Edit the 'action()' function of the file 'main/core/lib.c' to define the actions you want to perform.

For example:

float temperature = atof(j->sensor[1].value[0]);  // atof() function converts string to float
printf("Temperature Sensor Example       -> ");
if (temperature > 35.0)   // If temperature is greater than 35 Celsius degrees, Turn on Refrigeration Equipment
{
  set_relay_GPIO(0, 1);  // Put in HIGH RELAY 1
  printf("High Temperature detected: Turning ON the Refrigeration Equipment\n\n");
}

5) Compile and Download the Firmware:

Remembering to have the ESP-IDF Toolchain open, and you make sure you are at the root of the http-receiver 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 Keepy is configured correctly you will reading data from Tangle via Streams and executing actions from there.

Debugging

Open Serial Monitor

If configured correctly, ESP32 should be getting data from Keepy 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:

Windows:

Open Command Prompt.

Configure the Baud Rate of the port to 115200 bps:

mode COM1: baud=115200

Read the serial port:

copy COM1: con:

Next steps

Learn how to set up Keepy with live visualization of the data!

⚠️ **GitHub.com Fallback** ⚠️