#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN_A 15
#define DHTPIN_B 4
#define DHTPIN_C 5
#define DHTTYPE DHT22
DHT dhtA(DHTPIN_A, DHTTYPE);
DHT dhtB(DHTPIN_B, DHTTYPE);
DHT dhtC(DHTPIN_C, DHTTYPE);
// Sensor A
#define HIJAU_A 14
#define KUNING_A 27
#define MERAH_A 26
// Sensor B
#define HIJAU_B 25
#define KUNING_B 33
#define MERAH_B 32
// Sensor C
#define HIJAU_C 23
#define KUNING_C 22
#define MERAH_C 21
const char* ssid = "NamaWiFi";
const char* password = "PasswordWiFi";
const char* serverName = "http://192.168.1.100/codeigniter3/iot/terima_data";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
dhtA.begin(); dhtB.begin(); dhtC.begin();
int pins[] = {HIJAU_A, KUNING_A, MERAH_A, HIJAU_B, KUNING_B, MERAH_B, HIJAU_C, KUNING_C, MERAH_C};
for (int i = 0; i < 9; i++) pinMode(pins[i], OUTPUT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Menghubungkan ke WiFi...");
}
Serial.println("Terkoneksi WiFi!");
}
void aturLampu(float suhu, int pinH, int pinK, int pinM) {
if (suhu < 50) {
digitalWrite(pinH, HIGH);
digitalWrite(pinK, LOW);
digitalWrite(pinM, LOW);
} else if (suhu >= 50 && suhu < 120) {
digitalWrite(pinH, LOW);
digitalWrite(pinK, HIGH);
digitalWrite(pinM, LOW);
} else if (suhu >= 125) {
digitalWrite(pinH, LOW);
digitalWrite(pinK, LOW);
digitalWrite(pinM, HIGH);
} else {
// Jika antara 120–124, matikan semua sebagai kondisi tidak pasti
digitalWrite(pinH, LOW);
digitalWrite(pinK, LOW);
digitalWrite(pinM, LOW);
}
}
void loop() {
float suhuA = dhtA.readTemperature();
float suhuB = dhtB.readTemperature();
float suhuC = dhtC.readTemperature();
if (!isnan(suhuA) && !isnan(suhuB) && !isnan(suhuC)) {
aturLampu(suhuA, HIJAU_A, KUNING_A, MERAH_A);
aturLampu(suhuB, HIJAU_B, KUNING_B, MERAH_B);
aturLampu(suhuC, HIJAU_C, KUNING_C, MERAH_C);
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String postData = "suhuA=" + String(suhuA) + "&suhuB=" + String(suhuB) + "&suhuC=" + String(suhuC);
int httpResponseCode = http.POST(postData);
Serial.println("Data dikirim: " + postData);
Serial.println("Respon server: " + String(httpResponseCode));
http.end();
} else {
Serial.println("Gagal membaca salah satu sensor.");
}
delay(5000);
}