Saturday, 28 January 2017

Kontrol Lampu via Web (Arduino+EthernetShield)

"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

Selamat Datang di Arduino Ethernet Shield WEB Server

"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

-- Online-kan ARDUINO kamu -- "));

plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
Kontrol keluaran digital"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
Baca input digital analog disini"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
") );
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Versi 0.0 lab KomDat "));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(""));

return(plen);
}


void setup(){

es.ES_enc28j60SpiInit();

es.ES_enc28j60Init(mymac);

es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
}

void loop(){
uint16_t plen, dat_p;

while(1) {
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
if(dat_p==0){
// tidak ada http request
continue;
}
// tcp port 80
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
dat_p=http200ok();
dat_p=es.ES_fill_tcp_data_p(buf,dat_p,PSTR("

200 OK

"));
goto SENDTCP;
}
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
}
else{
dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n

401 Unauthorized

"));
goto SENDTCP;
}
SENDTCP:
es.ES_www_server_reply(buf,dat_p); // kirim data web page
// akhir tcp port 80

}
}
Langkah kelima adalah membuat gambar sketsa rumah dan denah elektriknya dan kita masukkan pada folder htdocs pada XAMPP. Nantinya gambar tersebut akan kita panggil dengan kode yang sudah kita gabungkan pada program arduino. Gambar yang kita buat nanti akan tampil pada halaman web, tapi untuk ukuran silahkan disesuaikan sendiri. Contohnya seperti ini, jelek gapapa. Hehehe...

Langkah keenam adalah copas kode berikut ini dan upload ke arduino!

#include

#define LEDAPIN 2
#define LEDBPIN 3
#define LEDCPIN 4
#define LEDDPIN 5
#define LEDEPIN 6


PROGMEM prog_char led_off[]= {
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x0E,
0x00, 0x00, 0x00, 0x0E, 0x08, 0x04, 0x00, 0x00, 0x00, 0xB5, 0x41, 0xE5, 0x5A, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x02, 0x62, 0x4B, 0x47, 0x44, 0x00, 0xFF, 0x87, 0x8F, 0xCC, 0xBF,
0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0B, 0x12, 0x00, 0x00, 0x0B, 0x12, 0x01, 0xD2, 0xDD, 0x7E,
0xFC, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4D, 0x45, 0x07, 0xDC, 0x01, 0x1D, 0x14, 0x21, 0x10, 0xBC, 0x3C, 0xDE, 0x2D,
0x00, 0x00, 0x00, 0xD2, 0x49, 0x44, 0x41, 0x54, 0x18, 0xD3, 0x75, 0xD0, 0xB1, 0x4A, 0x03, 0x51, 0x10, 0x05, 0xD0, 0xB3,
0x21, 0x1F, 0x10, 0x04, 0x3B, 0x53, 0x89, 0x5A, 0x09, 0x9B, 0x58, 0xD9, 0x25, 0x5F, 0x60, 0x63, 0x9F, 0x87, 0x5F, 0xE0,
0x27, 0x6D, 0x7A, 0x5B, 0xC1, 0x6E, 0xF3, 0x01, 0x92, 0x2C, 0x36, 0x56, 0x62, 0x61, 0x23, 0xA4, 0xB1, 0x12, 0xF2, 0x64,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0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x0E,
0x00, 0x00, 0x00, 0x0E, 0x04, 0x03, 0x00, 0x00, 0x00, 0xED, 0x66, 0x30, 0xE2, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x27, 0x50, 0x4C, 0x54, 0x45, 0x00, 0x00, 0x00, 0xFF, 0xCC, 0x00,
0xCC, 0x66, 0x00, 0xFF, 0xFF, 0x33, 0xFF, 0x99, 0x00, 0xFF, 0xCC, 0x33, 0xCC, 0x99, 0x66, 0xFF, 0xFF, 0x99, 0xCC, 0x99,
0x33, 0xCC, 0x99, 0x00, 0xFF, 0xCC, 0x66, 0x99, 0x66, 0x00, 0xFF, 0xCC, 0x99, 0x4A, 0x4E, 0xC9, 0x79, 0x00, 0x00, 0x00,
0x01, 0x74, 0x52, 0x4E, 0x53, 0x00, 0x40, 0xE6, 0xD8, 0x66, 0x00, 0x00, 0x00, 0x01, 0x62, 0x4B, 0x47, 0x44, 0x00, 0x88,
0x05, 0x1D, 0x48, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0B, 0x12, 0x00, 0x00, 0x0B, 0x12, 0x01,
0xD2, 0xDD, 0x7E, 0xFC, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4D, 0x45, 0x07, 0xDC, 0x01, 0x1D, 0x14, 0x23, 0x1C, 0x87,
0xBC, 0xF0, 0x84, 0x00, 0x00, 0x00, 0x48, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0x60, 0x80, 0x03, 0x26, 0x25, 0x05,
0x30, 0x95, 0x55, 0x3E, 0x0D, 0xC4, 0xD0, 0x31, 0x16, 0x14, 0x6C, 0x02, 0xD2, 0xC9, 0x86, 0x82, 0x82, 0xD2, 0x40, 0xE1,
0xC5, 0x40, 0x3A, 0x44, 0x81, 0x81, 0xA9, 0x18, 0x46, 0x0B, 0x0A, 0x8A, 0xBA, 0x00, 0xE9, 0x60, 0x08, 0xCD, 0x90, 0x0C,
0xA4, 0x9D, 0x80, 0xEA, 0x35, 0x80, 0xD2, 0x20, 0x9A, 0xA9, 0x53, 0xD0, 0x49, 0x01, 0xC9, 0x5C, 0x08, 0x00, 0x00, 0x9A,
0x56, 0x0B, 0x58, 0x9D, 0x6A, 0x76, 0x99, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
};

static byte mymac[] = {0x54,0x55,0x58,0x10,0x00,0x25};
static byte myip[] = {192,168,1,11}; //Alamat IP EthernetShield
byte Ethernet::buffer[700];

boolean led0Status;
boolean led1Status;
boolean led2Status;
boolean led3Status;
boolean led4Status;

void setup () {

Serial.begin(9600);
Serial.println("Kendali Lampu Rumah via Web");

if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
Serial.println( "Failed");
else
Serial.println("Initialyzing");

if (!ether.staticSetup(myip))
Serial.println("Setting IP address GAGAL");

Serial.println();

pinMode(LEDAPIN, OUTPUT);
pinMode(LEDBPIN, OUTPUT);
pinMode(LEDCPIN, OUTPUT);
pinMode(LEDDPIN, OUTPUT);
pinMode(LEDEPIN, OUTPUT);
off ();
led0Status = true;
led1Status = true;
led2Status = true;
led3Status = true;
led4Status = true;
}

void loop() {

word len = ether.packetReceive();
word pos = ether.packetLoop(len);

if(pos) {
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED0") != 0) {
led0Status = !led0Status;
off();
Teras();

}
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED1") != 0) {
led1Status = !led1Status;
off();
R_Klg();
}
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2") != 0) {
led2Status = !led2Status;
off();
K_Tidur();
}
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED3") != 0) {
led3Status = !led3Status;
off();
Dapur();
}
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED4") != 0) {
led4Status = !led4Status;
off();
Toilet();
}
BufferFiller bfill = ether.tcpOffset();
bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
""
"Web Lamp"
"STATUS LAMPU:

"));
if(led0Status) bfill.emit_p(PSTR("Lampu Teras:"));


Video yang berkaitan dengan Kontrol Lampu via Web (Arduino+EthernetShield)


0 comments:

Post a Comment