|
|
| Line 1: |
Line 1: |
| Halaman ini menyajikan panduan teknis mengenai perancangan dan implementasi sistem pemantauan kondisi lingkungan (suhu dan kelembapan) menggunakan mikrokontroler ESP8266 dan sensor DHT22. Sistem ini dirancang untuk memberikan data secara ''real-time'' yang ditampilkan melalui layar LCD.
| |
|
| |
|
| == Daftar Komponen / Spesifikasi Hardware ==
| |
| Komponen-komponen berikut dipilih berdasarkan kriteria fungsionalitas dan kompatibilitas sistem:
| |
|
| |
| * '''[[Daftar Komponen#ESP32|ESP8266]]:''' Mikrokontroler dengan modul Wi-Fi.
| |
| * '''Sensor [[Daftar Komponen#DHT22|DHT22]]:''' Sensor digital untuk mengukur suhu dan kelembapan udara.
| |
| * '''LCD 16x2 dengan Modul I2C:''' Media untuk menampilkan hasil output sensor.
| |
| * '''Box X4:''' Enclosure atau wadah pelindung fisik untuk mengintegrasikan seluruh komponen.
| |
| * '''Kabel Jumper:''' Penghubung antar komponen (tipe Female-to-Female).
| |
| * '''Kabel Micro USB:''' Power
| |
|
| |
| == Skema Perakitan (Wiring) ==
| |
| Berikut adalah konfigurasi pinout untuk menghubungkan komponen ke ESP8266:
| |
| {| class="wikitable"
| |
| |+Tabel pinout komponen
| |
| !Komponen
| |
| !Pin Komponen
| |
| !Pin ESP8266
| |
| |-
| |
| |DHT22
| |
| |VCC / +
| |
| |3V
| |
| |-
| |
| |
| |
| |Data / out
| |
| |D4 (GPIO 2)
| |
| |-
| |
| |
| |
| |GND / -
| |
| |GND / G
| |
| |-
| |
| |LCD l2C
| |
| |VCC
| |
| |VIN (5V) / 3V3
| |
| |-
| |
| |
| |
| |GND / G
| |
| |GND / G
| |
| |-
| |
| |
| |
| |SDA
| |
| |D2 (GPIO 4)
| |
| |-
| |
| |
| |
| |SCL
| |
| |D1 (GPIO 5)
| |
| |}
| |
|
| |
| [[File:DIY_1.1.png|alt=Gambar skema perakitan (wiring)|none|thumb|500x500px|Diagram pemasangan kabel jumper dari dht22 dan lcd ke esp 8266]]
| |
|
| |
| == Instalasi Arduino IDE & Setup ESP8266 ==
| |
| Tahapan ini menjelaskan langkah instalasi Arduino IDE dan setup ESP8266.
| |
|
| |
| === Download dan Instalasi Software ===
| |
|
| |
| # Buka situs resmi [http://arduino.cc arduino.cc].
| |
| # Pilih versi terbaru sesuai sistem operasi Anda (Windows, macOS, atau Linux)[[File:Gambar file installer arduino.png|none|thumb|Gambar file installer arduino]]
| |
| #Jalankan file installer yang telah diunduh.
| |
| #Pastikan semua opsi checklist terpilih (terutama Install USB Driver dan Associate .ino files).
| |
| #Klik Next dan Install hingga selesai.
| |
|
| |
| === Konfigurasi Board ESP8266 ===
| |
| Secara default, Arduino IDE hanya mendukung board Arduino. Agar bisa memprogram ESP8266, maka harus dilakukan konfigurasi dan instalasi secara manual, berikut langkah-langkahnya:
| |
|
| |
| # Buka Arduino IDE, klik menu '''File > Preferences'''.[[File:Gambar menu preferences arduino.png|none|thumb|Gambar menu preferences arduino]]
| |
| # Pada kolom '''Additional Boards Manager URLs''', masukkan url berikut: http://arduino.esp8266.com/stable/package_esp8266com_index.json
| |
| # Klik '''OK'''.[[File:Gambar input additional boards manager URLs.png|none|thumb|Gambar input additional boards manager URLs]]
| |
| # Buka menu '''Tools > Board > Boards Manager…'''[[File:Gambar klik menu boards manager arduino.png|none|thumb|Gambar klik menu boards manager arduino]]
| |
| # Ketik "esp8266" di kolom pencarian, lalu klik '''Install''' pada paket dari '''ESP8266 Community'''.[[File:Gambar install paket dari ESP8266 Community.png|none|thumb|Gambar install paket dari ESP8266 Community]]
| |
|
| |
| === Install Driver CH340 dan CP2102 ===
| |
|
| |
| # Install driver CH340 untuk Windows pada link : https://sparks.gogo.co.nz/assets/_site_/downloads/CH34x_Install_Windows_v3_4.zip
| |
| # Install driver CP2102 untuk Windows pada link: https://www.pololu.com/file/0J14/pololu-cp2102-windows-220616.zip
| |
|
| |
| === Instalasi Library (DHT & LCD) ===
| |
|
| |
| # Pilih '''Sketch > Include Library > Manage Libraries...'''
| |
| # Cari '''"DHT sensor library"''' (by Adafruit) dan klik '''Install'''. Jika muncul pop-up untuk menginstal dependensi (seperti ''Adafruit Unified Sensor''), pilih '''Install All'''.
| |
| # Cari '''"LiquidCrystal I2C"''' (by Frank de Brabander) dan klik '''Install'''.[[File:Gambar instalasi library DHT dan LCD.png|none|thumb|Gambar instalasi library DHT dan LCD]]
| |
|
| |
| === Koneksi ke Perangkat ===
| |
|
| |
| # Hubungkan ESP8266 ke laptop menggunakan kabel data Micro USB.
| |
| # Pilih board di '''Tools > Board > ESP8266 Boards >''' (Contoh: '''NodeMCU 1.0 (ESP-12E Module)''').[[File:Gambar pilih board NodeMCU 1.0 (ESP-12E Module).png|none|thumb|Gambar pilih board NodeMCU 1.0 (ESP-12E Module)]]
| |
| # Pilih Port yang sesuai di '''Tools > Port''' (Biasanya akan muncul COM3, COM4, dsb. di Windows).[[File:Gambar pilih port COM6.png|none|thumb|Gambar pilih port COM6]]
| |
|
| |
| ==== Tips Troubleshooting ====
| |
|
| |
| * ''Driver USB: Jika Port tidak muncul, langkah yang perlu dilakukan adalah menginstal driver USB-to-Serial secara manual. Biasanya chip yang digunakan adalah CH340 atau CP2102 (tergantung varian NodeMCU yang digunakan).''
| |
| * ''Kecepatan Upload: Untuk ESP8266, gunakan Upload Speed 115200 agar proses pengiriman kode lebih stabil.''
| |
|
| |
| == Pemrograman (Source Code) ==
| |
| Gunakan kode berikut untuk membaca data sensor dan menampilkannya ke LCD.
| |
| <syntaxhighlight lang="cpp" style="max-height:300px; overflow:auto;">
| |
| #include <Wire.h>
| |
| #include <LiquidCrystal_I2C.h>
| |
| #include "DHT.h"
| |
| #include <Adafruit_Sensor.h>
| |
| #include <ESP8266WiFi.h>
| |
| #include <ESP8266HTTPClient.h>
| |
|
| |
| #define DHTPIN D4 // Digital pin connected to the DHT sensor
| |
| #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
| |
|
| |
| // Replace with network credentials
| |
| #define WIFISSID "Nama-Kelompok"
| |
| #define WIFIPASS "Pass-SSID"
| |
|
| |
| const char* deviceid = "ID=Nama_Kelompok";
| |
| const char* groupid = "GROUP=RURAL_SIK_LOMBA";
| |
| //server to receive the data
| |
| const char* host = "pond.weather.id";
| |
| const int port = 80;
| |
|
| |
| // do not touch below
| |
| unsigned long startTime = millis();
| |
|
| |
| const int watchdog = 10000; // delay before pushing data
| |
| unsigned long previousMillis = millis();
| |
|
| |
| void connectWifi() {
| |
| Serial.println("Connecting to access point");
| |
| WiFi.disconnect();
| |
| if (WiFi.getMode() != WIFI_STA) { //WIFI_STA= station mode (connect ke akses point)
| |
| WiFi.mode(WIFI_STA);
| |
| }
| |
| WiFi.begin(WIFISSID, WIFIPASS);
| |
| //...Give ESP 10 seconds to connect to station.
| |
| startTime = millis();
| |
| while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
| |
| delay(1000);
| |
| Serial.print(".");
| |
| }
| |
| Serial.println("");
| |
| // Check Connection
| |
| if (WiFi.status() == WL_CONNECTED) {
| |
| Serial.print("Wifi connected; IP address: ");
| |
| Serial.println(WiFi.localIP());
| |
| } else {
| |
| Serial.print("WiFi connect failed to ssid: ");
| |
| Serial.println(WIFISSID);
| |
| }
| |
| }
| |
|
| |
| // Set the LCD address to 0x27 for a 16 chars and 2 line display
| |
| LiquidCrystal_I2C lcd(0x27, 16, 2);
| |
| DHT dht(DHTPIN, DHTTYPE);
| |
|
| |
| void setup() {
| |
| Serial.begin(9600);
| |
| Serial.println(F("DHT22 test!"));
| |
| dht.begin();
| |
| lcd.init();
| |
| lcd.backlight();
| |
| connectWifi();
| |
|
| |
| //intro gak penting
| |
| lcd.setCursor(0, 0);
| |
| lcd.print(" Temperature");
| |
| lcd.setCursor(0, 1);
| |
| lcd.print(" Monitor");
| |
| delay(2000);
| |
| lcd.clear();
| |
| }
| |
|
| |
| void loop() {
| |
| unsigned long currentMillis = millis();
| |
| if (currentMillis - previousMillis > watchdog) {
| |
| previousMillis = currentMillis;
| |
|
| |
| // Wait a few seconds between measurements.
| |
| delay(1000);
| |
| // Reading temperature or humidity takes about 250 milliseconds!
| |
| // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
| |
| float h = dht.readHumidity();
| |
| // Read temperature as Celsius (the default)
| |
| float t = dht.readTemperature();
| |
|
| |
| // Check if any reads failed and exit early (to try again).
| |
| if (isnan(h) || isnan(t)) {
| |
| Serial.println(F("Failed to read from DHT sensor!"));
| |
| lcd.setCursor(0, 0);
| |
| lcd.print("No Sensor!");
| |
| return;
| |
| }
| |
|
| |
| Serial.print(F("Humidity: "));
| |
| Serial.print(h);
| |
| Serial.print(F("% Temperature: "));
| |
| Serial.print(t);
| |
| Serial.print(F("°C "));
| |
|
| |
| //Display data ke LCD =>tampung di data string
| |
| String print_temp = "Temp: ";
| |
| print_temp += String(t);
| |
| print_temp += " C";
| |
| String print_humd = "Humd: ";
| |
| print_humd += String(h);
| |
| print_humd += " %";
| |
| lcd.setCursor(0, 0);
| |
| lcd.print(print_temp);
| |
| lcd.setCursor(0, 1);
| |
| lcd.print(print_humd);
| |
|
| |
| WiFiClient client; //deklarasi method client
| |
| HTTPClient http; //deklarasi method http
| |
|
| |
| if (!client.connect(host, port)) {
| |
| Serial.print("cannot connect to web server ");
| |
| Serial.println(host);
| |
| return;
| |
| }
| |
|
| |
| //String url="/weather/poller.php?";
| |
| String url = "http://";
| |
| url += host;
| |
| url += ":";
| |
| url += port;
| |
| url += "/?";
| |
| //url += "ID=YOURID";
| |
| url += deviceid;
| |
| url += "&";
| |
| url += groupid;
| |
| url += "&temperature=";
| |
| url += String(t);
| |
| url += "&humidity=";
| |
| url += String(h);
| |
| url += "&softwaretype=RuralICT2024";
| |
|
| |
| // send it
| |
|
| |
| client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
| |
|
| |
| // wait for respond
| |
| unsigned long timeout = millis();
| |
| while (client.available() == 0) {
| |
| if (millis() - timeout > 5000) {
| |
| Serial.print("timeout");
| |
| client.stop();
| |
| return;
| |
| }
| |
| }
| |
|
| |
| // ok, we got the reply
| |
| while (client.available()) {
| |
| String line = client.readStringUntil('\r');
| |
| Serial.print(line);
| |
| }
| |
| }
| |
| }
| |
|
| |
| </syntaxhighlight>
| |
|
| |
| == Langkah Implementasi ==
| |
|
| |
| # '''Persiapan Box:''' Lubangi Box X4 di sisi depan untuk layar LCD dan lubang kecil di sisi samping untuk ventilasi sensor DHT22 serta kabel power.
| |
| # '''Wiring:''' Hubungkan semua komponen sesuai tabel di atas menggunakan kabel jumper.
| |
| # '''Upload:''' Hubungkan ESP8266 ke komputer dan upload kode melalui Arduino IDE.
| |
| # '''Testing:''' Pastikan LCD menyala dan menampilkan angka. Jika layar kosong, putar potensiometer (trimpot) di belakang modul I2C untuk mengatur kontras.
| |
|
| |
| [[Table of Contents|< Kembali ke halaman awal]] | [[Sensor Laut|Ke halaman berikutnya >]]
| |