Malam ini saya akan menjelaskan mengenai bagaimana cara membuat alat dengan menggunakan Arduino Ethernet dan Android, jika biasanya kendali device seperti lampu, motor dc, kipas dll menggunakan bluetooth, sekarang diganti menggunakan wifi local bukan online, jadi sistemnya masih sama dengan bluetooth yaitu wireless namun jarak jangkaunya yang berbeda, kalau bluetooth biasanya hanya 1 meter, tapi kalau wifi bisa sampai 10 meter lebih tergantung tipe dan jenis router yang digunakan, prinsip kerjanya alat yang dibuat ini yaitu mengendalikan 4 buah led menggunakan HP Android dan media yang dipakai adalah wifi / jaringan ethernet, untuk lebih jelasnya berikut skema dan programnya.
a. Arduino + Ethernet Shield
b. Program Android
c. Program Arduino IDE
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,88);
EthernetServer server(80);
const int ledpin7 = 7;
const int ledpin8 = 8;
const int ledpin9 = 9;
const int ledpin2 = 2;
String readString;
void setup() {
pinMode(ledpin7, OUTPUT);
pinMode(ledpin8, OUTPUT);
pinMode(ledpin9, OUTPUT);
pinMode(ledpin2, OUTPUT);
Serial.begin(9600);
while (!Serial) {
}
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
int sumbux = analogRead(0);
int sumbuy = analogRead(1);
int sumbuz = analogRead(2);
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 1");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println(" ");
client.println("<title> Monitor Kemiringan </title> ");
client.println("<body bgcolor = #000000>");
client.println("<center>");
client.println("<font color = yellow><center><h1>KENDALI LAMPU VIA ETHERNET ANDROID<br>BY YANUAR MUKHAMMAD</h1></center><br></font>");
client.println("<a href = \"/?lighton1\"\"><input type = 'button' value = 'ON 1' ></a>");
client.println("<a href = \"/?lightoff1\"\"><input type = 'button' value = 'OFF 1' ></a> ");
client.println("<a href = \"/?lighton2\"\"><input type = 'button' value = 'ON 2' ></a>");
client.println("<a href = \"/?lightoff2\"\"><input type = 'button' value = 'OFF 2' ></a> ");
client.println("<a href = \"/?lighton3\"\"><input type = 'button' value = 'ON 3' ></a>");
client.println("<a href = \"/?lightoff3\"\"><input type = 'button' value = 'OFF 3' ></a> ");
client.println("<a href = \"/?lighton4\"\"><input type = 'button' value = 'ON 4' ></a>");
client.println("<a href = \"/?lightoff4\"\"><input type = 'button' value = 'OFF 4' ></a> ");
client.println("</center>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disonnected");
}
if(readString.indexOf("?lighton1") >0)
{
digitalWrite(ledpin7, HIGH);
}
else{
if(readString.indexOf("?lightoff1") >0)
{
digitalWrite(ledpin7, LOW);
}
}
if(readString.indexOf("?lighton2") >0)
{
digitalWrite(ledpin8, HIGH);
}
else{
if(readString.indexOf("?lightoff2") >0)
{
digitalWrite(ledpin8, LOW);
}
}
if(readString.indexOf("?lighton3") >0)
{
digitalWrite(ledpin9, HIGH);
}
else{
if(readString.indexOf("?lightoff3") >0)
{
digitalWrite(ledpin9, LOW);
}
}
if(readString.indexOf("?lighton4") >0)
{
digitalWrite(ledpin2, HIGH);
}
else{
if(readString.indexOf("?lightoff4") >0)
{
digitalWrite(ledpin2, LOW);
}
}
readString="";
}
d. VIDEO HASILNYA
No comments:
Post a Comment