ALAT TEST BUTA WARNA ESP32 WEBSERVER
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat digunakan untuk melakukan test buta warna, jadi nanti ada 5 buah kertas test yang mana nanti akan diinputkan kedalam alat sehingga bisa muncul nilainya. untuk lebih jelasnya berikut adalah koding dan komponenya.
a. Komponen
b. Program ESP32
#include <Wire.h>
#include <Keypad.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <SPI.h>
static const int servoPin = 18;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo1;
int benar;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13,12,14,27};
byte colPins[COLS] = {26,25,33,32};
int x = 0;
int nilaisetmax = 0;
int nilai,angka;
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char* ssid = "hotspothpku";
const char* password = "123456789";
WiFiServer server(80);
//AsyncWebServer server(80);
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
servo1.attach(servoPin);
servo1.write(100);
}
void loop()
{
test1();
gerakservo();
test2();
gerakservo();
test3();
gerakservo();
test4();
gerakservo();
test5();
gerakservo();
hasil();
}
void gerakservo(){
servo1.write(150);
delay(1000);
servo1.write(100);
}
void hasil(){
WiFiClient client = server.available();
int result = benar * 20;
lcd.setCursor(0,0);
lcd.print("NILAI: ");
lcd.print(result);
if(result < 90){
lcd.setCursor(0,1);
lcd.print("BUTA WARNA ");
}
if(result > 90){
lcd.setCursor(0,1);
lcd.print("NORMAL ");
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Refresh: 1");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<center>");
client.println("<h1>");
client.println("Monitoring WARNA");
client.println("</h1>");
client.println("<h3>");
if(angka == 0){
client.println("BUDI ");
if(result > 90){
client.println(" NORMAL ");
}
if(result < 90){
client.println(" BUTA WARNA ");
}
}
client.println("<br>");
if(angka == 1){
client.println("DEWI ");
if(result > 90){
client.println(" NORMAL ");
}
if(result < 90){
client.println(" BUTA WARNA ");
}
}
if(angka == 2){
client.println("TONI ");
if(result > 90){
client.println(" NORMAL ");
}
if(result < 90){
client.println(" BUTA WARNA ");
}
}
client.println("</h3>");
client.println("</center>");
client.println("</html>");
delay(1);
customKey = customKeypad.getKey();
if(customKey == 'B'){
lcd.clear();
angka++;
delay(1000);
return;
}
hasil();
}
void test1(){
lcd.setCursor(0,0);
lcd.print("JAWAB 1 ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
nilai = nilai * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(nilai);
}
if(customKey == 'A'){
if(nilai == 2){
benar++;
}
lcd.clear();
delay(1000);
nilai = 0;
return;
}
delay(200);
test1();
}
void test2(){
lcd.setCursor(0,0);
lcd.print("JAWAB 2 ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
nilai = nilai * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(nilai);
}
if(customKey == 'A'){
if(nilai == 5){
benar++;
}
lcd.clear();
delay(1000);
nilai = 0;
return;
}
delay(200);
test2();
}
void test3(){
lcd.setCursor(0,0);
lcd.print("JAWAB 3 ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
nilai = nilai * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(nilai);
}
if(customKey == 'A'){
if(nilai == 8){
benar++;
}
lcd.clear();
delay(1000);
nilai = 0;
return;
}
delay(200);
test3();
}
void test4(){
lcd.setCursor(0,0);
lcd.print("JAWAB 4 ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
nilai = nilai * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(nilai);
}
if(customKey == 'A'){
if(nilai == 8){
benar++;
}
lcd.clear();
delay(1000);
nilai = 0;
return;
}
delay(200);
test4();
}
void test5(){
lcd.setCursor(0,0);
lcd.print("JAWAB 5 ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
nilai = nilai * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(nilai);
}
if(customKey == 'A'){
if(nilai == 0){
benar++;
}
lcd.clear();
delay(1000);
nilai = 0;
return;
}
delay(200);
test5();
}
c. VIDEO HASILNYA
No comments:
Post a Comment