Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara mengambil data ADC dari Arduino ke HP Android menggunakan media WIFI atau Ethertnet, Jadi Arduino akan mengirimkan data ke server yaitu di address 198.168.1.88 kemudian HP Android akan mengambil data tersebut untuk ditampilkan di dalam aplikasinya, pada contoh kali ini syarat data yang dikirimkan di server adalah 3 digit dengan diawali dua karakter "##", jika ingin data yang panjang hanya perlu mengubah parameter yang ada di program Androidnya. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino + Ethernet
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>MONITOR KEMIRINGAN DATARAN<br>BY YANUAR MUKHAMMAD</h1></center><br></font>");
client.println("<font color = #FFFFFF>");
client.println("<h2>");
client.println("Sumbu X = ##");
client.print(sumbux);
client.println("<br />");
client.println("</h2>");
client.println("</font>");
client.println("<a href = \"/?lighton\"\"><input type = 'button' value = 'ON' ></a>");
client.println("<a href = \"/?lightoff\"\"><input type = 'button' value = 'OFF' ></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("?lighton") >0)
{
digitalWrite(ledpin7, HIGH);
}
else{
if(readString.indexOf("?lightoff") >0)
{
digitalWrite(ledpin7, LOW);
}
}
readString="";
if (sumbux > 400){
digitalWrite(ledpin8, HIGH);
digitalWrite(ledpin9, LOW);
digitalWrite(ledpin2, LOW);
} else if (sumbux < 300){
digitalWrite(ledpin8, LOW);
digitalWrite(ledpin9, HIGH);
digitalWrite(ledpin2, LOW);
}else {
digitalWrite(ledpin8, LOW);
digitalWrite(ledpin9, LOW);
digitalWrite(ledpin2, HIGH);
}
}
#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>MONITOR KEMIRINGAN DATARAN<br>BY YANUAR MUKHAMMAD</h1></center><br></font>");
client.println("<font color = #FFFFFF>");
client.println("<h2>");
client.println("Sumbu X = ##");
client.print(sumbux);
client.println("<br />");
client.println("</h2>");
client.println("</font>");
client.println("<a href = \"/?lighton\"\"><input type = 'button' value = 'ON' ></a>");
client.println("<a href = \"/?lightoff\"\"><input type = 'button' value = 'OFF' ></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("?lighton") >0)
{
digitalWrite(ledpin7, HIGH);
}
else{
if(readString.indexOf("?lightoff") >0)
{
digitalWrite(ledpin7, LOW);
}
}
readString="";
if (sumbux > 400){
digitalWrite(ledpin8, HIGH);
digitalWrite(ledpin9, LOW);
digitalWrite(ledpin2, LOW);
} else if (sumbux < 300){
digitalWrite(ledpin8, LOW);
digitalWrite(ledpin9, HIGH);
digitalWrite(ledpin2, LOW);
}else {
digitalWrite(ledpin8, LOW);
digitalWrite(ledpin9, LOW);
digitalWrite(ledpin2, HIGH);
}
}
d. VIDEO HASILNYA
keren kakak.....:3, omoshiroi...
ReplyDelete