Perancangan dan Pembuatan Alat Penyiram Tanaman Otomatis Dengan Smartphone Android Berbasis Arduino

Teks penuh

(1)

LISTING PROGRAM

1. Program Arduino

#include<SoftwareSerial.h> #include<Servo.h>

#define button 8 #define button1 2 #define relay 9

#include <DS1307RTC.h> #include <Wire.h> #include <TimeLib.h>

SoftwareSerial wifi(2,11); Servo servoMain;

int sensorPin = A0; int powerPin = 6; int kelembaban;

int data;

void setup() {

// put your setup code here, to run once: wifi.begin(115200);

Serial.begin(4800); servoMain.attach(10);

pinMode(button, INPUT);digitalWrite(button,HIGH); pinMode(relay, OUTPUT);digitalWrite(relay,HIGH); pinMode(wifi, INPUT);digitalWrite(wifi,LOW);

pinMode(powerPin, OUTPUT);

digitalWrite(powerPin, HIGH); }

void loop() {

// put your main code here, to run repeatedly:

tmElements_t tm;

if (RTC.read(tm)) {

Serial.print("Ok, Time = "); print2digits(tm.Hour);

Serial.write(':');

print2digits(tm.Minute); Serial.write(':');

print2digits(tm.Second);

Serial.print(", Date (D/M/Y) = "); Serial.print(tm.Day);

(2)

Serial.print(tmYearToCalendar(tm.Year)); Serial.println();}

if(wifi.available()>0){ data=wifi.read(); }

kelembaban = bacaSensor(); Serial.print("Kelembaban =" ); Serial.println(kelembaban);

if(digitalRead(button)==LOW){

digitalWrite(relay,LOW);

int i,x;

for(x=0;x<=2;x++) {

for(i=1;i<=180;i++) {

servoMain.write(i); delay(50);

}

for(i=180;i>=0;i--) {

servoMain.write(i);

digitalWrite(relay,LOW); delay(50);

} } }

else if(data == 'H'){ digitalWrite(relay,LOW); int i,x;

for(x=0;x<=2;x++) {

for(i=1;i<=180;i++) {

servoMain.write(i); delay(50);

}

for(i=180;i>=0;i--) {

servoMain.write(i); delay(50);

} } }

else if(tm.Hour == 20 && tm.Minute == 42 && tm.Second == 00) {

(3)

for(i=20;i<=160;i++) {

servoMain.write(i); delay(50);}

for(i=160;i>=20;i--) {

servoMain.write(i); delay(50);

} } }

else if(kelembaban >= 500) {

digitalWrite(relay,LOW); int i,a;

for(a=1;a<=3;a++) {

for(i=20;i<=160;i++) {

servoMain.write(i); delay(50);

}

for(i=160;i>=20;i--) {

servoMain.write(i); delay(50);

} } }

servoMain.write(90);

digitalWrite(relay,HIGH); }

int bacaSensor() { // hidupkan power

digitalWrite(powerPin, HIGH); //delay(500);

// baca nilai analog dari sensorzz

int nilaiSensor = analogRead(sensorPin); digitalWrite(powerPin, LOW);

//

return 1023 - nilaiSensor; }

void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0');

}

(4)

2. Program Wifi

#include <ESP8266WiFi.h> #include<SoftwareSerial.h>

SoftwareSerial wifi(D7,D8); //RX,TX

const char* ssid = "Nisa_Icha"; const char* password = "nisa1234";

WiFiServer server(80);

void setup() {

Serial.begin(115200); wifi.begin(9600); delay(10);

// Connect to WiFi network Serial.println();

Serial.println();

Serial.print("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500);

Serial.print("."); }

Serial.println("");

Serial.println("WiFi connected");

// Start the server server.begin();

Serial.println("Server started");

// Print the IP address

Serial.print("Use this URL to connect: "); Serial.print("http://");

Serial.print(WiFi.localIP()); Serial.println("/");

}

void loop() {

// Check if a client has connected WiFiClient client = server.available(); if (!client) {

return; }

// Wait until the client sends some data Serial.println("new client");

(5)

// Read the first line of the request

String request = client.readStringUntil('\r'); Serial.println(request);

client.flush();

// Match the request

if (request.indexOf("/LED=ON") != -1) { wifi.write("H");

}

// Set ledPin according to the request //digitalWrite(ledPin, value);

// Return the response

client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println("<!DOCTYPE HTML>");

client.println("<html>");

client.print("PENYIRAM TANAMAN OTOMATIS");

client.println("<br><br>"); client.println("<a

href=\"/LED=ON\"\"><button>Hidupkan</button></a>"); client.println("</html>");

delay(1);

Figur

Memperbarui...

Referensi

Memperbarui...