Last active
December 9, 2018 08:42
-
-
Save artistio/00d3ec1c9742d5b37950ddc05ba348a0 to your computer and use it in GitHub Desktop.
Contoh program untuk membaca data sensor DHT22 dengan NodeMCU.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Program untuk membaca suhu dan kelembaban dari sensor DHT22 | |
* menggunakan NodeMCU | |
* | |
* Hak cipta (c) 2016 x.benny.id. All rights reserved. | |
* https://x.benny.id | |
*/ | |
// Konfigurasi DHT22 | |
#include <DHT.h> | |
#define DHTPIN 0 // PIN D3 (GPIO 0) dari NodeMCU terhubung dengan PIN Data dari DHT22 | |
#define DHTTYPE DHT22 // Kita menggunakan tipe DHT22 | |
DHT dht(DHTPIN, DHTTYPE); // Inisialisasi DHT22 | |
// Mempersiapkan NodeMCU melalui fungsi setup() | |
// Catatan: Nama fungsi dibawah ini tidak boleh dirubah | |
void setup() { | |
// Inisialisasi Serial port untuk debugging | |
Serial.begin(115600); | |
Serial.setTimeout(2000); | |
while(!Serial) { } | |
Serial.println("NodeMCU Siap Dipakai"); | |
Serial.println("Membaca sensor DHT22"); | |
} | |
int timeSinceLastRead = 0; | |
void loop() { | |
// Report every 2 seconds. | |
if(timeSinceLastRead > (2000)) { | |
// Membaca kelembaban | |
float h = dht.readHumidity(); | |
// Membaca suhu dalam derajat celcius | |
float t = dht.readTemperature(); | |
// Membaca suhu dalam derajat farenheit | |
float f = dht.readTemperature(true); | |
// Memastikan kalau semua pembacaan sukses | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
timeSinceLastRead = 0; | |
return; | |
} | |
// Menghitung index suhu dalam farenheit | |
float hif = dht.computeHeatIndex(f, h); | |
// Menghitung index suhu dalam farenheit | |
float hic = dht.computeHeatIndex(t, h, false); | |
// Menulis hasil pembacaan melalui Serial | |
Serial.print("\Kelembaban: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Suhu: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print(f); | |
Serial.print(" *F\t"); | |
Serial.print("Persepsi Suhu: "); | |
Serial.print(hic); | |
Serial.print(" *C "); | |
Serial.print(hif); | |
Serial.println(" *F"); | |
timeSinceLastRead = 0; | |
} | |
delay(100); // Menunggu pembacaan selanjutnya | |
timeSinceLastRead += 100; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment