#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
// Setting MAC address and IP address for Ethernet module
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
const int numVariables = 12;
int variables[numVariables];
// Pin definitions for 74HC595
const int latchPin = 5;
const int clockPin = 6;
const int dataPin = 7;
void setup() {
// Initialize Ethernet
Ethernet.begin(mac, ip);
server.begin();
// Initialize pins for 74HC595
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Read variables from EEPROM
for (int i = 0; i < numVariables; i++) {
variables[i] = EEPROM.read(i);
}
Serial.begin(9600);
}
void loop() {
// Check for incoming client connections
EthernetClient client = server.available();
if (client) {
handleClient(client);
}
// Display variables on 7-segment display
displayVariables();
}
void handleClient(EthernetClient client) {
boolean currentLineIsBlank = true;
String request = "";
boolean isPost = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n' && currentLineIsBlank) {
// If POST request, process the data
if (isPost) {
processPostData(request);
}
// Send the HTML page
sendHtmlPage(client);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
if (request.indexOf("POST /submit") >= 0) {
isPost = true;
}
}
}
delay(1);
client.stop();
}
void processPostData(String request) {
int indexStart = request.indexOf("\r\n\r\n") + 4;
String data = request.substring(indexStart);
int varIndex = 0;
while (data.length() > 0 && varIndex < numVariables) {
int delimiterIndex = data.indexOf('&');
String param = (delimiterIndex > 0) ? data.substring(0, delimiterIndex) : data;
int equalsIndex = param.indexOf('=');
if (equalsIndex > 0) {
int value = param.substring(equalsIndex + 1).toInt();
variables[varIndex] = value;
EEPROM.write(varIndex, value);
}
data = (delimiterIndex > 0) ? data.substring(delimiterIndex + 1) : "";
varIndex++;
}
}
void sendHtmlPage(EthernetClient client) {
// Update the variables array with the latest values from EEPROM
for (int i = 0; i < numVariables; i++) {
variables[i] = EEPROM.read(i);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html lang=\"id\">");
client.println("<head>");
client.println("<meta charset=\"UTF-8\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("<title>Input Memori Arduino</title>");
client.println("<style>");
client.println("body {font-family: Arial, sans-serif; background-color: #f2f2f2; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0;}");
client.println(".container {background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); max-width: 600px; width: 100%;}");
client.println("h1 {text-align: center; color: #333;}");
client.println("form {display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;}");
client.println("label {margin-bottom: 5px; color: #555;}");
client.println(".input-group {display: flex; flex-direction: column;}");
client.println("input[type=\"number\"] {padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; width: 100%;}");
client.println("input[type=\"submit\"] {grid-column: span 3; padding: 10px; background-color: #4CAF50; border: none; border-radius: 4px; color: white; font-size: 16px; cursor: pointer;}");
client.println("input[type=\"submit\"]:hover {background-color: #45a049;}");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<div class=\"container\">");
client.println("<h1>Input Data Angka</h1>");
client.println("<form action=\"/submit\" method=\"post\">");
client.print("<div class=\"input-group\">");
client.print("<label for=\"value1\">Tanggal:</label>");
client.print("<input type=\"number\" id=\"value1\" name=\"value1\" value=\"");
client.print(formatNumber(variables[0]));
client.print("\" min=\"0\" max=\"99\" required>");
client.println("</div>");
client.print("<div class=\"input-group\">");
client.print("<label for=\"value2\">Bulan:</label>");
client.print("<input type=\"number\" id=\"value2\" name=\"value2\" value=\"");
client.print(formatNumber(variables[1]));
client.print("\" min=\"0\" max=\"99\" required>");
client.println("</div>");
client.print("<div class=\"input-group\">");
client.print("<label for=\"value3\">Tahun:</label>");
client.print("<input type=\"number\" id=\"value3\" name=\"value3\" value=\"");
client.print(formatNumber(variables[2]));
client.print("\" min=\"0\" max=\"99\" required>");
client.println("</div>");
for (int i = 3; i < numVariables; i++) {
client.print("<div class=\"input-group\">");
client.print("<label for=\"value");
client.print(i + 1);
client.print("\">Angka ");
client.print(i + 1);
client.print(":</label>");
client.print("<input type=\"number\" id=\"value");
client.print(i + 1);
client.print("\" name=\"value");
client.print(i + 1);
client.print("\" value=\"");
client.print(formatNumber(variables[i]));
client.println("\" min=\"0\" max=\"99\" required>");
client.println("</div>");
}
client.println("<input type=\"submit\" value=\"Kirim\">");
client.println("</form>");
client.println("</div>");
client.println("</body>");
client.println("</html>");
}
String formatNumber(int num) {
if (num < 10) {
return "0" + String(num);
} else {
return String(num);
}
}
void displayVariables() {
for (int i = 0; i < numVariables; i++) {
displayNumber(variables[i]);
delay(500);
}
}
void displayNumber(int number) {
int tens = number / 10;
int ones = number % 10;
byte segmentsTens = getSegments(tens);
byte segmentsOnes = getSegments(ones);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, segmentsOnes);
shiftOut(dataPin, clockPin, MSBFIRST, segmentsTens);
digitalWrite(latchPin, HIGH);
}
byte getSegments(int number) {
switch (number) {
case 0: return B00111111;
case 1: return B00000110;
case 2: return B01011011;
case 3: return B01001111;
case 4: return B01100110;
case 5: return B01101101;
case 6: return B01111101;
case 7: return B00000111;
case 8: return B01111111;
case 9: return B01101111;
default: return B00000000; // Blank for non-decimal digits
}
}