Membuat alat kendali Traffic Light / LED Menggunakan ARDUINO Ethernet WEB SERVER dan RTC
Pada pagi hari ini saya akan menjelaskan mengenai bagaimana cara membuat alat yang digunakan untuk kendali traffic light LED menggunakan WIFI Ethernet Web Server. Disini saya akan membuat sebuah sistem lampu lalu lintas yang mana delay lampu diset melalui server dan juga waktu nyala dan padam lampu juga diset melalui server, sehingga pada server akan tampil nilai jam dan delay lampu yang diset dan juga pada server terdapat tombol pemilihan untuk setting ulang nilainya. Untuk lebih jelas berikut adalah sema dan programnya.
a. Arduino Mega + LCD 16x2
b. Router
c. Ethernet Shield
d. RTC dan LED
Arduino Mega
SDA - Pin 20
SCL - Pin 21
e. Program Arduino IDE
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(16, 17, 22, 24, 19, 18);
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
int led1 = A7;
int led2 = A8;
int led3 = A9;
int led4 = A10;
int led5 = A11;
int led6 = A12;
int led7 = A13;
int led8 = A14;
int led9 = A15;
int dmerah = 0;
int dkuning = 0;
int dhijau = 0;
int ton = 0;
int toff = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80);; //server port
String readString;
void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
Wire.begin();
// DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(0,59,11,2,16,5,16);
pinMode(13, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("servertest1"); // so I can keep track of what is loaded
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// send it to the serial monitor
lcd.setCursor(0, 0);
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.setCursor(0, 1);
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(year, DEC);
if (hour >= ton) {
if (hour >= toff) {
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, LOW);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
}
else {
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, LOW);
digitalWrite(led8, LOW);
digitalWrite(led7, HIGH);
delay(dhijau);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, LOW);
digitalWrite(led8, HIGH);
digitalWrite(led7, LOW);
delay(dkuning);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dmerah);
//==============================
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dhijau);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dkuning);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dmerah);
//=====================================
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dhijau);
digitalWrite(led3, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dkuning);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led9, HIGH);
digitalWrite(led8, LOW);
digitalWrite(led7, LOW);
delay(dmerah);
}
}
}
void loop(){
lcd.setCursor(0, 0);
lcd.print("SET DELAY LAMPU");
lcd.setCursor(0, 1);
lcd.print("SET ON/OFF LAMPU");
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led8, HIGH);
digitalWrite(led9, HIGH);
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString);
//now output HTML data header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H3>kontrol ON / OFF dan delay lampu</H3>");
client.println("<FORM ACTION=\"http://192.168.1.102:80\" method=get >");
client.println("Delay Lampu Merah : <INPUT TYPE=TEXT NAME=\"Merah\" VALUE=\"A\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Delay Lampu Kuning: <INPUT TYPE=TEXT NAME=\"Kuning\" VALUE=\"B\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Delay Lampu Hijau : <INPUT TYPE=TEXT NAME=\"Hijau\" VALUE=\"C\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Jam ON : <INPUT TYPE=TEXT NAME=\"LEDON\" VALUE=\"D\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Jam OFF : <INPUT TYPE=TEXT NAME=\"LEDOFF\" VALUE=\"E\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\" SUBMIT\">");
client.println("  <a href = \"/?mulai\"\"><input type = 'button' value = 'MULAI' ></a>");
client.println("  <a href = \"/?set\"\"><input type = 'button' value = 'SETTING' ></a>");
client.println("</FORM>");
client.println("<BR>");
client.println("<h5>");
client.println("SET DELAY LAMPU MERAH = ");
client.println(dmerah);
client.println("<BR>");
client.println("SET DELAY LAMPU KUNING = ");
client.println(dkuning);
client.println("<BR>");
client.println("SET DELAY LAMPU HIJAU = ");
client.println(dhijau);
client.println("<BR>");
client.println("SET ON LAMPU = ");
client.println(ton);
client.println("<BR>");
client.println("SET OFF LAMPU = ");
client.println(toff);
client.println("<BR>");
client.println("</h5>");
client.println("<BR>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
/////////////////////
if(readString.indexOf("A10") >0)//checks for on
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY MERAH");
lcd.setCursor(0, 1);
lcd.print("10 Detik");
dmerah = 1000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("A20") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY MERAH");
lcd.setCursor(0, 1);
lcd.print("20 Detik");
dmerah = 2000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("A30") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY MERAH");
lcd.setCursor(0, 1);
lcd.print("30 Detik");
dmerah = 3000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("B10") >0)//checks for on
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY KUNING");
lcd.setCursor(0, 1);
lcd.print("10 Detik");
dkuning = 1000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("B20") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY KUNING");
lcd.setCursor(0, 1);
lcd.print("20 Detik");
dkuning = 2000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("B30") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY KUNING");
lcd.setCursor(0, 1);
lcd.print("30 Detik");
dkuning = 3000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("C10") >0)//checks for on
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY HIJAU");
lcd.setCursor(0, 1);
lcd.print("10 Detik");
dhijau = 1000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("C20") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY HIJAU");
lcd.setCursor(0, 1);
lcd.print("20 Detik");
dhijau = 2000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("C30") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET DELAY HIJAU");
lcd.setCursor(0, 1);
lcd.print("30 Detik");
dhijau = 3000;
delay(3000);
lcd.clear();
}
if(readString.indexOf("D2") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET ON LAMPU");
lcd.setCursor(0, 1);
lcd.print("JAM 07.00 AM");
ton = 2;
delay(3000);
lcd.clear();
}
if(readString.indexOf("D18") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET ON LAMPU");
lcd.setCursor(0, 1);
lcd.print("JAM 08.00 AM");
ton = 18;
delay(3000);
lcd.clear();
}
if(readString.indexOf("E2") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET OFF LAMPU");
lcd.setCursor(0, 1);
lcd.print("JAM 22.00 PM");
toff = 2;
delay(3000);
lcd.clear();
}
if(readString.indexOf("E18") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET OFF LAMPU");
lcd.setCursor(0, 1);
lcd.print("JAM 21.00 PM");
toff = 18;
delay(3000);
lcd.clear();
}
if(readString.indexOf("?mulai") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("START PROGRAM");
lcd.setCursor(0, 1);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
}
if(readString.indexOf("?set") >0)//checks for off
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MODE SETTING");
lcd.setCursor(0, 1);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
}
while (readString.indexOf("?mulai") >0) {
mulai();
}
//clearing string for next read
readString="";
}
}
}
}
}
void mulai(){
displayTime();
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString);
//now output HTML data header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H3>kontrol ON / OFF dan delay lampu</H3>");
client.println("<FORM ACTION=\"http://192.168.1.102:80\" method=get >");
client.println("Delay Lampu Merah : <INPUT TYPE=TEXT NAME=\"Merah\" VALUE=\"A\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Delay Lampu Kuning: <INPUT TYPE=TEXT NAME=\"Kuning\" VALUE=\"B\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Delay Lampu Hijau : <INPUT TYPE=TEXT NAME=\"Hijau\" VALUE=\"C\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Jam ON : <INPUT TYPE=TEXT NAME=\"LEDON\" VALUE=\"D\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("Jam OFF : <INPUT TYPE=TEXT NAME=\"LEDOFF\" VALUE=\"E\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR><BR>");
client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\" SUBMIT\">");
client.println("  <a href = \"/?mulai\"\"><input type = 'button' value = 'MULAI' ></a>");
client.println("  <a href = \"/?set\"\"><input type = 'button' value = 'SETTING' ></a>");
client.println("</FORM>");
client.println("<BR>");
client.println("<h5>");
client.println("SET DELAY LAMPU MERAH = ");
client.println(dmerah);
client.println("<BR>");
client.println("SET DELAY LAMPU KUNING = ");
client.println(dkuning);
client.println("<BR>");
client.println("SET DELAY LAMPU HIJAU = ");
client.println(dhijau);
client.println("<BR>");
client.println("SET ON LAMPU = ");
client.println(ton);
client.println("<BR>");
client.println("SET OFF LAMPU = ");
client.println(toff);
client.println("<BR>");
client.println("</h5>");
client.println("<BR>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
//clearing string for next read
readString="";
}
}
}
}
}
f. Tampilan Interface WEB
g. Cara Penggunaan Alat
Untuk menggunakan alat ini sangat lah mudah, yaitu masukkan nilai misal pada Delay lampu merah A10, lalu klik SUBMIT, setelah itu KLIK SETTING, kemudian masukkan nilai delay lampu kuning B20 untuk delay 20 detik, begitu seterusnya, jika berhasil maka nilai akan tersimpan pada bagian bawah form, jika sudah terisi semua nilainya KLIK MULAI, maka akan berjalan sesuai delay yang disetting, untuk kembali ke mode setting bisa dengan menekan tombol SETTING.
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :