Skip to content

Instantly share code, notes, and snippets.

@muzafakar
Created December 26, 2017 19:32
Show Gist options
  • Save muzafakar/c2dbe79e1dcfdddc22873efd4cdc2b46 to your computer and use it in GitHub Desktop.
Save muzafakar/c2dbe79e1dcfdddc22873efd4cdc2b46 to your computer and use it in GitHub Desktop.
Final Project
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#define trigAtas 6
#define echoAtas 7
#define enA 8
#define in1 9
#define in2 10
#define trigBawah 11
#define echoBawah 12
//ngukur jarak
long durAtas, durBawah;
float disAtas, disBawah;
int x;
char auth[] = "9e0d746962114f8aa9ea7f571ea740b1";
char ssid[] = "esp";
char pass[] = "135792468";
//konekin ke esp nya
SoftwareSerial EspSerial(2, 3); // RX, TX
Servo svAtas, svBawah;
WidgetLED led(V2);
ESP8266 wifi(&EspSerial);
void setup() {
//set pin di microcontrollernya
pinMode(trigAtas, OUTPUT);
pinMode(echoAtas, INPUT);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(trigBawah, OUTPUT);
pinMode(echoBawah, INPUT);
digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
svAtasClose();
svAtasClose();
EspSerial.begin(9600);
Serial.begin(115200);
Blynk.begin(auth, wifi, ssid, pass);
Blynk.setProperty(V2, "color", "#00ff00");
Blynk.setProperty(V0, "value", 0);
readAtas();
led.on();
notif();
}
//BLYNK method
//ini bawaan dari servernya
BLYNK_WRITE(V0) {
//isinya alur alatnya pas lagi ngasi makan
notif();
readBawah();
Blynk.setProperty(V2, "color", "#0000ff");
if (disBawah > 16) {
refill();
} else {}
digitalWrite(enA, HIGH);
for(x=0; x<20;x++){
svBawahOpen();
svBawahClose();
}
if (!Blynk.connected()) {
Blynk.connect();
}
delay(1500);
digitalWrite(enA, LOW);
Blynk.setProperty(V2, "color", "#00ff00");
Blynk.setProperty(V0, "value", 0);
notif();
refill();
//sampai sini
}
//ini buat baca sensor, tapi gajadi kepake
BLYNK_READ(V1) {
Blynk.virtualWrite(V1, disAtas);
}
//buat baca isi galon
void readAtas() {
digitalWrite(trigAtas, HIGH);
delayMicroseconds(10);
digitalWrite(trigAtas, LOW);
durAtas = pulseIn(echoAtas, HIGH);
disAtas = (durAtas / 2) * 0.0343;
}
//buat baca isi botol
void readBawah() {
digitalWrite(trigBawah, HIGH);
delayMicroseconds(10);
digitalWrite(trigBawah, LOW);
durBawah = pulseIn(echoBawah, HIGH);
disBawah = (durBawah / 2) * 0.0343;
}
//kalo ada Open nya berarti buka pintunya, kalo Close berarti tutup.
//gampang kan hehehe
void svAtasOpen() {
svAtas.attach(4);
svAtas.write(0);
delay(1000);
svAtas.detach();
}
void svAtasClose() {
svAtas.attach(4);
svAtas.write(100);
delay(1000);
svAtas.detach();
}
void svBawahOpen() {
svAtas.attach(5);
svAtas.write(0);
delay(1500);
svAtas.detach();
}
void svBawahClose() {
svAtas.attach(5);
svAtas.write(100);
delay(1500);
svAtas.detach();
}
//void makan(){
// notif();
// readAtas();
// readBawah();
//
// Blynk.setProperty(V2, "color", "#0000ff");
// if(disBawah > 16){
// refill();
// }else{}
//
// digitalWrite(enA, HIGH);
// while(disBawah < 16){
// readBawah();
// svBawahOpen();
// readBawah();
// svBawahClose();
// readBawah();
//// }
//
//delay(3000);
//digitalWrite(enA, LOW);
//Blynk.setProperty(V2, "color", "#00ff00");
//Blynk.setProperty(V0, "value", 0);
//notif();
//refill();
//}
void refill() {
svAtasOpen();
delay(8500);
svAtasClose();
}
void notif() {
readAtas();
if (disAtas <= 100) {
Blynk.notify("Pakan habis, silahkan datang ke kolam dan isi ulang!");
delay(1000);
} else {}
}
void loop() {
if (!Blynk.connected()) {
Blynk.connect();
}
Blynk.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment