You are on page 1of 46

ESP32 & Blynk App

Design 2B - Medical Instrumentation Design


Table of content

I. ESP32 Introduction & Setup

II. How to Upload a Sketch to a ESP

III. Coding for blinking LED

IV. Coding for using OLED

V. OLED & Sensor Combination

VI. Introduction to Blynk App


I. ESP32 Introduction & Setup
1. ESP32 Introduction

● A feature-rich MCU with integrated Wi-Fi and Bluetooth connectivity for a wide range of applications such as

controlling sensors, motors, etc.

● The ESP32 is actually cheaper than Arduino Uno, which means that you get a more powerful board for a lower

price.

● ESP32 is as a supercharged Arduino: faster, better in many respects.

● In particular, ESP32 has built-in dual-mode WiFi and Bluetooth that allows us to transfer data to the network or

mobile phone, laptop, PC, etc.


I. ESP32 Setup
In your Arduino IDE, go to File > Preferences
I. ESP32 Setup
Enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field as
shown in the figure below. Then, click the “OK” button:
I. ESP32 Setup
Open the Boards Manager. Go to Tools > Board > Boards Manager…
I. ESP32 Setup
Search for ESP32 and press install button for the “ESP32 by Espressif Systems“:
I. ESP32 Setup
That’s it. It should be installed after a few seconds
II. How to upload a Sketch to ESP32
1. Connect your ESP32 using the USB cable.
● The square end of the USB cable connects to your ESP32 and the flat end
connects to a USB port on your laptop.
2. Choose Tools→Board→ESP32 Arduino

● The one we usually use is ESP32 Dev


Module
3. Choose the correct serial port for your board.
● You find a list of all the available serial
ports by choosing Tools→Serial Port→
comX
4. Click the Upload button
● Press and hold BOOT button on ESP32
when “Connecting …..____........” occurs
on Arduino IDE
III. Coding for Blinking LED
Schematic
Source Code
const int ledPin = 2; //GPIO2
int ledState = LOW; // ledState = LOW mean that at the beginning LED is off

unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:


if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
IV. Coding for using OLED
Wiring Diagram
Installing SSD1306 OLED Library
● Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should
open.
● Type “SSD1306” in the search box and install the SSD1306 library from Adafruit.
Installing SSD1306 OLED Library
After installing the SSD1306 library from Adafruit, type “GFX” in the search box and install the library.
Source Code
#include <Wire.h> // Thư viện cho giao tiếp I2C. void loop() {

#include <Adafruit_SSD1306.h> while(Serial.available()){

#define OLED_RESET 4
String x = Serial.readString();// Biến x lưu chuỗi dữ liệu được nhập từ
Adafruit_SSD1306 display(OLED_RESET); Serial

void setup(){ Serial.println(x);// In ra Serial Monitor dữ liệu vừa nhận được.

Serial.begin(9600); display.clearDisplay();

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Khởi tạo OLED với địa chỉ 0x3C. display.setCursor(0,0);

display.clearDisplay();// Xóa dữ liệu trên màn hình OLED. display.println(x);

display.setTextSize(1);// Thiết lập kích thước kí tự bằng 1. display.display();// Hiển thị dữ liệu nhận được lên màn hình OLED.
display.setTextColor(WHITE);// Thiết lập màu chữ là màu trắng.
}
display.setCursor(28,12);// Thiết lập con trỏ tại vị trí cột 28 hàng 12.
}
display.println("Hello World!");// Thiết lập chuỗi cần in là "Hello World!".

display.display();// Hiển thị dữ liệu lên màn hình.

}
V. OLED & Sensor Combination
Wiring Diagram
void setup(){

Serial.begin(9600);

Source Code mlx.begin(); //Initialize MLX90614

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
#include <Adafruit_MLX90614.h>
Serial.println("Cam bien nhiet do MLX90614");
#include <Adafruit_GFX.h>
pinMode(bt, INPUT_PULLUP);
#include <Adafruit_SSD1306.h>
display.clearDisplay();
#define SCREEN_WIDTH 128
display.setCursor(25, 15);

#define SCREEN_HEIGHT 64
display.setTextSize(1);

#define OLED_RESET -1 display.setTextColor(WHITE);

#define bt 3 display.println(" mlx90614");

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); display.setCursor(25, 35);

Adafruit_MLX90614 mlx = Adafruit_MLX90614(); display.setTextSize(1);

double temp_amb; display.print("Dang khoi tao");

double temp_obj; display.display();

delay(5000);}
void loop() void display_temp_obj()
{ {
temp_amb = mlx.readAmbientTempC(); display.clearDisplay();
temp_obj = mlx.readObjectTempC(); display.setCursor(25, 10);
display.setTextSize(1);
//Serial Monitor display.setTextColor(WHITE);
display.println(" Nhiet do Obj");
if (digitalRead(bt) == LOW) display.setCursor(25, 30);
{ display.setTextSize(2);
display_temp_obj(); display.print(temp_obj);
delay(1000); display.print((char)247);
} display.print("C");
else { display.display();
Serial.print("Nhiet do phong = "); }
Serial.println(temp_amb);
Serial.print("Nhiet do doi tuong = ");
Serial.println(temp_obj);
display.clearDisplay();
display.setCursor(25, 10);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Nhiet do phong");
display.setCursor(25, 30);
display.setTextSize(2);
display.print(temp_amb);
display.print((char)247);
display.print("C");
display.display();
}
delay(1000);
}
VI. Introduction to Blynk App
Create an account in Blynk IoT Cloud Platform
● https://blynk.cloud/dashboard/register ● Enter your email ID and click on Sign Up. After
that, you will receive a verification email from
Blynk. In that email click on Create Password to
set the password for Blynk cloud account.
Create Template in Blynk IoT Cloud
● After login to Blynk IoT account, first you have create a template. In the dashboard click on the Templates from the
left side menu
● Then click on New Template. ● Give the template a name, choose ESP32 on
Hardware and Wifi on Connection Type.

● Then click on Done


Create Datastreams in the Blynk IoT template
● After creating the template, go to Datastreams ● Now, you have to enter a name for this
tab. Datastream.
● Then click on “New Datastream” and select ● Select the Virtual Pin and Datatype from the
Virtual Pin. dropdown menu as per the requirement.

● Then click on Create


Create Web Dashboard in the Blynk IoT template
● Now, go to Web Dashboard tab to add widgets. ● Now, if you hover over the widget, you will find a
● In the dashboard, you can easily drag and drop button with a gear icon. Click on the button to go
any widgets from the left side. to setting.

● On Datastream, choose Button 1 (V1), then click


on SAVE.
Install Blynk IoT app to set up Mobile Dashboard

Steps to install the Blynk IoT App

1. Install the Blynk IoT NEW app from Google Play Store or App Store. Then log in to the Blynk IoT Platform.

2. Tap on the Developer Mode.

3. Tap on the template which you have already made in Blynk cloud.

4. Now tap on the plus icon (on the right) to add widgets.
How to install new Blynk Library for Arduino IDE?
● In Arduino IDE go to Sketch — Include Library — Manage Libraries to open the Library manager. Then search
for Blynk in the search box. Then click on Install to install the Blynk Library (Current version 1.0.0). After that, you
can access all the Blynk Edgent examples code for ESP32 from File — Examples — Blynk — Blynk.Edgent path.
How to install new Blynk Library for Arduino IDE?
● Access https://github.com/blynkkk/blynk-library and Download Zip
● Install the library from Sketch — Include Library — Add .ZIP Library path in Arduino IDE.

● Browse to the folder where you download the library Zip file and click Open.
Wiring Diagram
How to get the BLYNK TEMPLATE ID and BLYNK DEVICE NAME

● Go to your Blynk IoT Cloud account and select the template. In the template go to the Info tab. On the right side,
you will find the BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME for that template. Click on “Click to copy
code”, then go to Arduino IDE and paste it into the code.
Source Code
#define BLYNK_TEMPLATE_ID "TMPL3PsNlMWb" BLYNK_WRITE(V1)
#define BLYNK_DEVICE_NAME "LED on off" {
// The 2 red lines are copied from the website like the int pinValue = param.asInt(); //Assigning incoming
previous slide value from pin V1 to a variable
#define BLYNK_FIRMWARE_VERSION "0.1.0" digitalWrite(2,pinValue);
}
#define BLYNK_PRINT Serial void setup()
//#define BLYNK_DEBUG {
pinMode(2,OUTPUT);
#define APP_DEBUG Serial.begin(115200);
delay(100);
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7 BlynkEdgent.begin();
}
#include "BlynkEdgent.h"
void loop() {
BlynkEdgent.run();
}
Connect Mobile Blynk App to ESP32
Finally, we can control the LED by tap on the
Button on our smartphone
Video Reference
● https://youtu.be/IKbbvEzZ7wg

You might also like