IoT Part 1: Getting started

This topic will contain a table of contents

IOT Workshop - Day 1

SmartPlant ESP32

Introduction

In this workshop we are going to make a smart-plant. Watering plants is boring and we often forget it. To solve this problem you are going to make a system that monitors the climate a plant is in and waters the plant when necessary.

You are going to connect a few sensors and actuators to the internet using a wifi Enabled microcontroller (ESP32). Using an app called Blynk you can monitor the status (ground moisture-, temperature-, light-levels) of your plant from anywhere on the world.

This will be a step by step tutorial, starting with the basics and slowly working up to the end result.

Learning The Hardware

Before we get started it is important to know which hardware we are going to use and how to use it.

Components

During this workshop we are going to be using a few different basic components, like LEDs, resistors, LDRs. It is important to have a basic understanding of how these components work.

Breadboard

In order to connect different components we are going to use a breadboard in the prototyping stage. A so called ‘breadboard’ is used to make temporary electrical connections between components. The internal connections of a breadboard are as shown in the image to the right.

LED

A light emitting diode (LED) is a diode that emits light, logically. Since a led is in fact a diode current will only flow from positive to negative (High to low potential). Since a led is apparently a polarised component you need to keep track of the polarity. The polarity can be found by looking at the image to the right. Keep in mind that you always need a resistor in series with a led in order to limit the current and voltage going through the led.

Resistor/LDR


A resistor will limit the current that flows. Resistors often get used in series with leds to limit the current that flow through a led. Since a led has a very low internal resistance to much current would flow if you don’t use a resistor.

If you need to measure the light intensity you could use a LDR. A LDR is a Light Dependent Resistor. The resistance of a LDR changes when the light level changes. By creating a voltage divider with an LDR and a normal resistor you can measure the light level, more on this later.

ESP32

The ESP32 is a microcontroller which can be programmed in c++. You can use this pinout reference to wire up your sensors.

Not all pins can always be used, to check if a pin can really be used for your purpose check this link:
Pin reference

If you try to program your ESP32 and your program doesn’t upload try pressing the BOOT button:
image

Setting up the programming environment

There are a few simple tools that need to be installed in order to start programming.
When everything is setup you can test if everything is working as expected.

Installing the USB to UART drivers

To get a stable connection with the ESP32 it is important to have the latest drivers for the USB to UART connection.

  • Download USB to UART drivers
  • Extract the .zip file
  • Run the installer and follow the instructions

If you don’t install this your ESP32 might not work!

In some cases you might need this driver instead: CH341SER.ZIP - Nanjing Qinheng Microelectronics Co., Ltd.

Visual Studio Code

Download and install Visual Studio Code from https://code.visualstudio.com/

After installing VS Code you can use the plugin manager to install PlatformIO. image

After a reload / restart you can see this screen:

Press “New Project” to start a new project.
image

Make sure to:

  • Give your project a name
  • Choose DOIT ESP32 DEVKIT V1 as Board
  • Leave framework to Arduino
  • Press Finish

Now you wil have a project folder which you can start using.

Upload your first (empty) program

We should now be able to upload an empty program to make sure we can program the ESP32.

Attach your ESP32 to your laptop / PC with the USB cable.

Make sure to look at the Devices tab in PIO Home. If there are no devices you should fix this first.

Open the main.cpp file as shown in the image below:
image

Press the ant icon and search for the project task called:

  • Upload

If everything is setup correctly you should get output like this at the bottom:

Starting programming

You can now upload your program to the ESP32 Board. To get ourselves familiar with the PlatformIO IDE and programming we are going to follow a few tutorials.

Blinking LED

The first thing to get started is a blinking LED. The ESP32 has one built in LED on GPIO 2. Upload the following code to the ESP32 and see what happens:

#include <Arduino.h>

// Create integer with constant value
const int ledPin = 2;

void setup() {
  // initialize digital pin ledPin  as an output.
  pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Fading a LED

Now you can blink a LED the next step is to make it fade on and off.

  • The next thing is to make the LED dimmable. Upload the following code and see what happens. You can refer to the original Tutorial
#include <Arduino.h>

const int ledPin = 2;

int freq = 5000;
int ledChannel = 0;
int resolution = 8;
 
void setup() {
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(ledPin, ledChannel);
}
 
void loop() {
 
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    ledcWrite(ledChannel, dutyCycle);
    delay(7);
  }
 
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    ledcWrite(ledChannel, dutyCycle);
    delay(7);
  }
 
}

If you lower the both delays from 7ms to 2ms you will see the led fading way quicker.
Save and rename main.cpp to fading_led.cpp and move the file to

Wire up and read out an LDR

LDRPINOUT

Red → 3.3v
Black → GND
Blue → SVN (GPIO39 / ADC3) (look at the pinout again)

You can use a wide range of resistors. You received one with a value of 10K ohm.

We want to make a program that will turn the LED on when the ambient light falls below a certain threshold. The first thing to to is measure the ambient light level.

Clear your main.cpp file or make a copy in the Test folder.

Now add the following code to main.cpp

#include <Arduino.h>

const int ldrPin = 39;

//setup code only runs once at startup
void setup() {
  Serial.begin(9600); //start a serial connection with a baud rate of 9600
  pinMode(ldrPin, INPUT); //define ldrPin as INPUT since we want to read it
}

    //loop runs all the time over and over again
void loop() {
  Serial.println(analogRead(ldrPin)); //print the read value
  delay(200); //wait 200ms
}
  • Upload and monitor the code to the ESP32

  • Hold your hand over the LDR to see if the value changes.

We are now able to read the ambient light levels. The next thing to do is to write code that will read out the light level at the start of the program and then when the light readings after that reading fall below a certain threshold the LED will turn on.

#include <Arduino.h>

const int ldrPin = 39;
const int ledPin = 2;

int lightInitial;
int lightValue;

void setup() {
  Serial.begin(9600);
  pinMode(ldrPin, INPUT);
  pinMode(ledPin, OUTPUT);
  lightInitial = analogRead(ldrPin);
}

void loop() {
  lightValue = analogRead(ldrPin);
  Serial.println(lightValue);
  if(lightInitial - lightValue >= 200) {
  digitalWrite (ledPin, HIGH); // turn on light
}

  else {
    digitalWrite (ledPin, LOW); // turn off light
  }

}
  • Upload and monitor
  • Now when you hold you hand over the ESP32 the LED should light up.

Did the LED turn on? Congratulations! You finished the Module!

1 Like

If GPIO2 does not work, it can be GPIO1. See also the ebook of the manufacturer: ESP32 Development Board_Englisch.pdf (579.5 KB)

If you have an ESP32 with the LED on GPIO1 you can’t use serial communication if you use the LED!

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/
Mooi framework voor ESP32 en varianten. Alle hardware onder 1 SDK
gebruik bijvoorbeeld visual studio + visualGDB als je de toolchain niet wil installeren
IDF = standaard hardware framework
ADF = Audio framework
MDF = Mesh netwerk framework

1 Like

Ik krijg de volgende melding wanneer ik het programma upload wat ervoor zou moeten zorgen dat de led gaat knipperen. Ik lees op internet verschillende dingen. Een daarvan zou zijn dat mijn ESP32 defect is.
Zou dit kunnen of kan dit nog opgelost worden?
@Mathijs

Dat ligt aan de error. Zie je je device wel staan?

Je kan bij PlatformIO Home een knopje vinden met Devices en dan zou je een lijst moeten zien.

Eventueel kun je een ESP32 van iemand anders verbinden en testen of dat wel werkt?

En zou je voor de zekerheid de inhoud van platformio.ini van je project kunnen delen?

Het is inmiddels gefixt. Ik runde het programa door op “run and debug” te klikken, maar dat werkt niet. Wanneer ik “upload and monitor” aanklik doet ie het wel gewoon!