Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things Projects with ESP32

You're reading from   Internet of Things Projects with ESP32 Build exciting and powerful IoT projects using the all-new Espressif ESP32

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher Packt
ISBN-13 9781789956870
Length 252 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Agus Kurniawan Agus Kurniawan
Author Profile Icon Agus Kurniawan
Agus Kurniawan
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with ESP32 FREE CHAPTER 2. Making Visual Data and Animation on an LCD 3. Building a Simple Game with an Embedded ESP32 Board 4. Building a Sensor Monitoring Logger 5. Controlling IoT Devices over the Internet 6. Building an IoT Weather Station 7. Making Your Own Wi-Fi Wardriving 8. Building Your Own Wi-Fi Cam 9. Making IoT Devices Interact with Mobile Applications 10. Building IoT Monitoring with Cloud Technology 11. Other Books You May Enjoy

Demo 2 - making an Arduino Sketch program with ESP32

In this section, we develop an Arduino program for ESP32 boards. We will use the previous demo, but we still use Arduino software. If you don't have experience with Arduino Sketch, I recommend learning Sketch programming on this site: https://www.arduino.cc/reference/en/. Since ESP32 has two cores (core 0 and core 1), our Arduino program runs one core. You don't need to worry which about core will be used by Arduino. You can verify which core is used with the xPortGetCoreID() function.

  1. We use the pinMode() function to set ESP32 GPIO pins as input or output. Then, we can write digital values using the digitalWrite() function. Using the previous demo, we can implement the demo using Sketch, as follows:
#define LED1 12
#define LED2 14
#define LED3 26
  1. Let's set the current_let to 1. That meant that later will start turning the LEDs with LED number 1.
int current_led = 1;
  1. Every program developed with the help of Arduino IDE will contain the setup() function. Code in setup function will run only on time at the beginning. For now let’s setup the pins that will drives our LEDS as output pins.
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}
  1. This is a helper function that will get a led number as parameter and will turn off all the LEDS and based on the value of the input parameter will turn on that LED by using the digitalWrite() function.
void turn_on_led(int led)
{
// turn off all leds
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);

switch(led)
{
case 1:
digitalWrite(LED1, HIGH);
break;
case 2:
digitalWrite(LED2, HIGH);
break;
case 3:
digitalWrite(LED3, HIGH);
break;
}
}
  1. The code in the loop() function will run continuously like in a while(1). For now the code will turn on one LED every second. When the code reach the last LED then will go back to the first one and the process will run forever.
void loop() {
turn_on_led(current_led);
delay(1000);
current_led++;
if(current_led>3)
current_led = 1;
}
  1. Save the program.

Now, you can set the ESP32 board target and its port, as shown in the following screenshot:

Configuring the ESP32 Wrover module

Now, you can compile and upload the Sketch program via Arduino software. If you succeed, you can see the program output as shown in the following screenshot:

Uploading the Sketch program to the ESP32 board

If you still get errors, please verify your ESP32 board type and its serial port.

You have been reading a chapter from
Internet of Things Projects with ESP32
Published in: Mar 2019
Publisher: Packt
ISBN-13: 9781789956870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image