ESP8266 อ่านค่าอุณหภูมิ ความชื้น จาก DHT22 แสดงในแอป Blynk
การเชื่อมต่อ
ESP8266 -> Sensor DHT22 วัดอุณหภูมิและความชื้น
3V -> ขา+
GND -> ขา-
D4(GPIO 2) -> ขาOUT
วิธีทำ
1.Quick Start สร้าง Template เชื่อมต่อแอป Blynk กับ ESP8266
2.เขียนโคด้ เชื่อมต่อ บอร์ด ESP8266 and DHT22
3.สร้าง Datastream in Blynk
4.สร้าง Web Dashboard in Blynk
5.เขียนโค้ด ให้ทำงานร่วมกับ และส่งค่าด้วยคำสั่ง
Blynk.virtualWrite(Virtualpin0, ตัวแปร1);//h,t,f,hic,hif
Blynk.virtualWrite(Virtualpin1, ตัวแปร2);
6.ลบโค้ดส่วนที่ไม่ใช้งานออกไป
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
/*************************************************************
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6wMZTx-cb"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "mnhX72ZXaX1WN5gPgft3wUdsqJOOxlrc"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ใส่ชื่อ wifi 2.4 ghz";
char pass[] = "ใส่รหัสผ่าน wifi";
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
// Update state
Blynk.virtualWrite(V1, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
delay(2000);
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, f);
Blynk.virtualWrite(V3, h);
}
Relate Link :
Contact:
https://www.facebook.com/chaiyutpong
ความคิดเห็น
แสดงความคิดเห็น