IoT Part 2: Sensors & Actuators

This topic will contain a table of contents

IOT Workshop - Day 2/4

SmartPlant - ESP32

Introduction

In this part of the workshop you are going to connect the rest of the sensors. At the end of this workshop you will have the following peripherals connected to your ESP:

  • Soil Moisture sensor
  • DHT Sensor
  • LDR
  • RF Transmitter
  • Relay

You will also have some basic programming skills, enough to read out and control all the peripherals.

Basic Script Layout

The basic layout of an arduino style sketch has three parts:

  • Part one is where you include the libraries, declare variables and define functions
  • Part two is where you put your code that runs once. Examples of this are configuring pins, starting the serial output and general setup of your script.
  • Part three is where you put the code that is repeated continously. In our case this will be reading out sensors and sending data, but this could be anything.

The layout looks like this:

//Part one is here

void setup() {
  // Part two is here
  // put your setup code here, to run once:

}

void loop() {
  // Part three is here
  // put your main code here, to run repeatedly:

}

One of the most imported tools for a programmer is comments. Comments are completely ignored by the esp32 so you can write whatever you want. In fact the comments wont even be part of the program that is uploaded to the ESP32.
You can use comments to make your code more readable or to easely disable part of your script when you are troubleshooting

The are two ways of commenting:

// This is a single line comment

/* 
This is a multiline comment
This can be as long as you want
*/

Monitoring soil moisture


The first thing we want to do is use the moisture sensor to monitor the water content of the soil. This enables us to decide when to turn on the pump to water the plant.

Connect the sensor to the ESP32 as follows:

Soil Moisture Sensor ESP32
S GPIO36
+ 3.3V
- GND

Now we need to update the code.

  • Create a new script file
  • Put these lines at the top of your script:
//Set pin number
int moistSensPin = 36;
int moistValue = 0;
  • Add this line to the setup part of your script:

Serial.begin(9600); //start a serial connection with a baud rate of 9600
  • Put these lines in the Loop function:
//Save the moisture value to a variable
moistValue = analogRead(moistSensPin);

//Print the value to the serial output
Serial.println(moistValue);

//Wait for one second
delay(1000);

Upload the code and verify that the sensor works by touching the long metal contacts of the sensor with your finger.

The println function prints the input and start a new line. If you use print instead you can print multiple times on the same line.

In order to make the output a bit more readable you can replace change the print part of the code to this:

//Print the value to the serial output
Serial.print("Moisture Value = ");
Serial.println(moistValue);

Check to see that the serial monitor now displays a nicely readable output.

Controlling the relay

We not only want to know the humidity of the ground we also want to be able to control it. In the kit you also got a small water pump. The pump allows us to automate the plant watering. This pump will be controlled by a motor driver (as shown in the image below)

  • For this step you will need the pump and a relay.
  • Connect the VCC to the 3.3V output of the ESP, the GND to GND and the IN to GPIO33.
    We will connect the outputs of the relais in the next workshop.

  • Add the following code to first part of your script:
#include <Arduino.h>
int pumpPin = 33;
  • Add the following to the setup part of your script:
//set the pin to output mode
pinMode(pumpPin, OUTPUT);
  • Add the following to the looping part of your script
 Serial.println("Pump is now on");
 digitalWrite(pumpPin, HIGH);
delay(500);

Serial.println("Pump is now off");
digitalWrite(pumpPin, LOW);
delay(500);
  • Upload the script and verify that the relais is turning on and off

  • Save your script file and create a new one

Integrating the DHT (digital temperature / humidity) sensor

The DHT Sensor works by performing a measurement and afterwards sending the results to the ESP32. The following things have to happen to measure this sensor:

  • The ESP32 must send a pulse to the DHT sensor
  • The DHT Sensor will send the response back in the form of short pulses representing bits (40 bits in total)

A more complete explanation can be found here DHT Explained

image

Because we don’t really want to write this code again to decode the response from the DHT we will use a library. A library is a piece of code that you can include in your own project without having to know how it works exactly. You just have to know what the input is, and what the output will be.

Wire up the sensor

There are three pins on the DHT11 Sensor, connect them as described below:

DHT11 Sensor ESP32
out GPIO32
+ 3.3V
- GND

Connect the + to the 3.3V from your ESP32, the - to the GND and the out to pin 32.

Install the library

You can install the library using the library manager from PlatformIO. In the PIO Home tab you can click the Libraries tab, and search for DHT Sensor. Click on the DHT sensor library, you will also have to install the Adafruit Unified Sensor library

In the next screen you can add this library to your project. Click the Add to project button.
image

Select your project and press Add.
image

Now do the same for the Adafruit Unified Sensor library.

There is also a working example that you can use to test if everything is working. It is available at the installed library, or (slightly changed) in the code block below:

#include <Arduino.h>

#include "DHT.h"

#define DHTPIN 32

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHT11 test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(1000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) ){
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.println(F("°C "));

}

Switching the klikaanklikuit

To control a light above the plant we can use the klikaanklikuit.

Normally you control this unit with a remote controller. We are going to use a 433mhz transmitter and replicate the signal that the remote would be sending.

Wire up the RF Transmitter

In order to do this we first have to wire the transmitter to the ESP32. The transmitter is shown below.
The pin order from left to right is:

Wire the pins according to the table below:

RF Transmitter ESP32
DATA GPIO25
VCC 3.3V
GND GND

Install the library

Download the following file and unzip it.
switchKaKu.zip (2.4 KB)

Copy the folder switchKaKu to the lib folder of your project in VS Code. The directory structure should now look like this:
image

Program your socket with an transmitterId of your choice

To be able to use your socket program it first using the appendix about Programming the klikaanklikuit

Now you should be able to toggle your KaKu by using the following code:

#include <Arduino.h>

#include "switchKaKu.h"
#define TRANSMITTERID1 123456
#define rfPin 25
void setup() {
  
}

void loop() {
  switchKaku(rfPin, TRANSMITTERID1, 1, 1, true, 3);
  delay(3000);
  switchKaku(rfPin, TRANSMITTERID1, 1, 1, false, 3);
  delay(3000);
}

Upload the code to your ESP32.

You are now finished with part 2, congratulations! If you have extra time you can try to do some integration as well.

Integration

Now that all your sensors are working independently you can try to integrate them in a single script. Try to combine your code and use inputs (LDR, Soil Moist sensor) to control your outputs (KaKu, relay).