Lampiran
Lampiran
Lampiran
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Servo.h>
#define BLYNK_PRINT Serial
#define TRIGGER 4
#define ECHO 5
#define pompa 16
#define DHTPIN 2
#define DHTTYPE DHT11
const int btnPin = 0;
const int moisture = A0;
int kelembapantanah = 0;
int pos = 0;
long duration, air, persediaan;
int suhu;
int kelembapanudara;
char auth[] = "00ebcef814bd4f77a0556afbdbc7ba05";
char ssid[] = "sekodong";
char pass[] = "87654321";
WidgetLED lampu(V0);
WidgetLCD lcd(V1);
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
BlynkTimer timer;
boolean btnState = false;
void buttonLedWidget()
{
boolean isPressed = (digitalRead(btnPin) == HIGH);
if (isPressed != btnState) {
if (isPressed) {
lampu.on();
} else {
lampu.off();
}
btnState = isPressed;
}
}
void sendSensor()
{
int suhu = dht.readTemperature();
Blynk.virtualWrite(V2, suhu); //temperature
int kelembapanudara = dht.readHumidity();
Blynk.virtualWrite(V3, kelembapanudara); //temperature
}
void sendSensor2()
{
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
float air = 12.5 - ((duration/2) / 29.1);
Serial.println(air);
float persediaan = (air*100) / 12.5;
Blynk.virtualWrite(V4, persediaan);//persediaan
Blynk.virtualWrite(V5, persediaan);//persediaan
if (persediaan <= 30){
Blynk.notify("Fill Your Water Tank!");
}
else {
}
}
void sendSensor3()
{
kelembapantanah = analogRead(moisture);
Blynk.virtualWrite(V6, kelembapantanah);//moisture
Blynk.virtualWrite(V7, kelembapantanah);//moisture
delay(10);
if (kelembapantanah <= 300){
digitalWrite(pompa, HIGH);
lcd.clear();
lcd.print(0, 0, "Tanah Kering");
lcd.print(0, 1, "Penyiraman Aktif");
for(pos = 0; pos < 90; pos += 1)
{
myservo.write(pos);
delay(10);
}
for(pos = 90; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
}
else {
digitalWrite(pompa, LOW);
lcd.clear();
lcd.print(0, 0, "Tanah Lembab");
lcd.print(0, 1, "Penyiraman Mati");
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
myservo.attach(14);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
dht.begin();
timer.setInterval(1000L, sendSensor);
int air();
timer.setInterval(5000L, sendSensor2);
int kelembapantanah();
timer.setInterval(3000L, sendSensor3);
pinMode(pompa, OUTPUT);
digitalWrite(pompa, HIGH);
pinMode(btnPin, INPUT_PULLUP);
timer.setInterval(8000L, buttonLedWidget);
}
void loop()
{
Blynk.run();
timer.run();
}