#include <DHT.h>
#include <Servo.h>
#define DHTPIN_TEMP 4 // Pin untuk sensor suhu DHT11
#define DHTPIN_HUM 5 // Pin untuk sensor kelembapan DHT11
#define LDRPIN 32 // Pin untuk sensor LDR
#define POTPIN 34 // Pin untuk potensiometer
#define LEDPIN 13 // Pin untuk LED
#define BUZZERPIN 14 // Pin untuk buzzer
#define MOTORPIN 15 // Pin untuk motor DC
#define RELAYPIN 16 // Pin untuk relay
#define SERVOPIN 17 // Pin untuk servo
#define DHTTYPE DHT11 // Tipe sensor DHT
DHT dht_temp(DHTPIN_TEMP, DHTTYPE);
DHT dht_hum(DHTPIN_HUM, DHTTYPE);
Servo servo;
int ldrValue;
int potValue;
void setup()
{
Serial.begin(115200);
dht_temp.begin();
dht_hum.begin();
servo.attach(SERVOPIN);
pinMode(LEDPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
pinMode(MOTORPIN, OUTPUT); // Motor DC
pinMode(RELAYPIN, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
String command = Serial.readStringUntil('\n');
if (command.startsWith("LED_ON"))
{
digitalWrite(LEDPIN, HIGH);
}
else if (command.startsWith("LED_OFF"))
{
digitalWrite(LEDPIN, LOW);
}
else if (command.startsWith("BUZZER_ON"))
{
digitalWrite(BUZZERPIN, HIGH);
}
else if (command.startsWith("BUZZER_OFF"))
{
digitalWrite(BUZZERPIN, LOW);
}
else if (command.startsWith("SERVO_ANGLE"))
{
int angle = command.substring(11).toInt();
servo.write(angle);
}
else if (command.startsWith("MOTOR_ON"))
{
digitalWrite(MOTORPIN, HIGH);
}
else if (command.startsWith("MOTOR_OFF"))
{
digitalWrite(MOTORPIN, LOW);
}
// Add more control commands for other actuators if needed
}
// Read sensor values
float tempValue = dht_temp.readTemperature();
float humValue = dht_hum.readHumidity();
ldrValue = analogRead(LDRPIN);
potValue = analogRead(POTPIN);
// Send sensor data to Serial
Serial.print("Temperature:");
Serial.print(tempValue);
Serial.print(";Humidity:");
Serial.print(humValue);
Serial.print(";LDR:");
Serial.print(ldrValue);
Serial.print(";Pot:");
Serial.println(potValue);
// Control actuators based on sensor values
if (ldrValue > 500)
{
digitalWrite(BUZZERPIN, HIGH);
}
else
{
digitalWrite(BUZZERPIN, LOW);
}
if (potValue > 512)
{
digitalWrite(RELAYPIN, HIGH);
}
else
{
digitalWrite(RELAYPIN, LOW);
}
delay(5000); // Delay 5 seconds
}