Đọc giá trị nhiệt độ, độ ẩm hiển thị lên màn hình oled 0.96” và blynk sử dụng

Một phần của tài liệu EBOOK Tài liệu IOT ESP8266 (Trang 65 - 72)

II. Bài học mẫu ứng dụng lập trình esp8266:

14. Đọc giá trị nhiệt độ, độ ẩm hiển thị lên màn hình oled 0.96” và blynk sử dụng

blynk sử dụng board NodeMCU.

Mục tiêu:

 Đọc và hiển thị giá trị nhiệt độ và độ ẩm hiển thị màn hình oled và blynk.

Phần mền cần chuẩn bị

 Cài đặt phần mềm arduino IDE.  Cài đặt app Blynk cho điện thoại.

Phần cứng cần chuẩn bị:

 1 board NodeMCU.  Dây cắm test board.  1 breadboard.

 1 Module dht11.  1 màn hình oled 0.96”.

Code chương trình:

https://drive.google.com/file/d/1oHGKSNTXN-FSvqPlGY8R3EW07s5VwO_H/view?usp=drive_open

#include <Wire.h> #include "SSD1306.h"

#define BLYNK_PRINT Serial #include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Your WiFi credentials.

// Set password to "" for open networks. char ssid[] = "Trinh Quang Nam."; char pass[] = "bubu123789"; #include <DHT.h>

#define DHTPIN 4 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11

// Initialize the OLED display i2C // D3 -> SDA

// D5 -> SCL

// Initialize the OLED display using Wire library SSD1306 display(0x3C, D3, D5);

DHT dht(DHTPIN, DHTTYPE); BlynkTimer timer;

void setup(){

Serial.begin(115200);

// Initialising the UI will init the display too. display.init(); display.flipScreenVertically(); display.setFont(ArialMT_Plain_16); display.setTextAlignment(TEXT_ALIGN_LEFT); dht.begin(); // initialize dht

Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080); timer.setInterval(1000L, sendSensor);

void displayWeather(){

float h = dht.readHumidity(); // Read temperature as Celsius float t = dht.readTemperature(); // Read temperature as Fahrenheit float f = dht.readTemperature(true); if (isnan(h) || isnan(t) || isnan(f)){ display.clear(); // clearing the display display.drawString(5,0, "DHT Failed!"); return;

}

display.clear();

display.drawString(30, 0, "Weather");

display.drawString(0, 20, "Humidity: " + String(h) + "%\t"); display.drawString(0, 40, "Temp: " + String(t) + "°C"); //Uncomment to get temperature in farenheit

//display.drawString(0, 40, "Temp: " + String(f) + "°F"); }

void loop(){

Blynk.run(); // Running the blynk code

displayWeather(); //Calling back the displayWeather function display.display();

timer.run(); }

void sendSensor() {

float h = dht.readHumidity(); //Read the humidity

float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit if (isnan(h) || isnan(t)) {

Serial.println("Failed to read from DHT sensor!"); return;

}

// You can send any value at any time.

// Please don't send more that 10 values per second. Blynk.virtualWrite(V5, h);

Blynk.virtualWrite(V6, t); }

Cài đặt app Blynk:

Bước 1: Tải app blynk trên CH play hoặc App Store

Bước 2: Mở app lên đăng kí tài khoản miễn phí bằng email sau đó chọn NEW PROJECT.

Bước 3: Lập app như hình dưới.

Bước 4: Chọn vào nút OK mã token sẽ được gửi vào email sau đó dùng tay trượt theo dấu mũi tên.

Bước 5: Chọn đến mục như hình ở dưới ( chọn 2 lần ).

Bước 6: Chọn vào hình chữ nhật như hình dưới và thay đổi thông tin giồng hình.

Bước 7: Chọn vào hình chữ nhật như hình dưới và thay đổi thông tin giồng hình.

Hình ảnh thực tế:

Một phần của tài liệu EBOOK Tài liệu IOT ESP8266 (Trang 65 - 72)