#include <Preferences.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WiFiAP.h>
#define OUT1 37
#define OUT2 39
#define OUT3 40
#define OUT4 41
#define OUT5 42
#define OUT6 47
#define INP1 4
#define INP2 5
#define INP3 6
#define INP4 7
#define INP5 15
#define INP6 16
Preferences fileConf;
AsyncWebServer server(80);
Adafruit_SSD1306 oledDisp(128, 32, &Wire, -1);
const char* ssid = "BELCC";
const char* password = "12345678";
const char* PARAM_INPUT_1 = "Buzzer_Interval";
const char* PARAM_INPUT_2 = "TimeOut_Jawab";
const char* PARAM_INPUT_3 = "input3";
unsigned long INTVLBUZZ = 0;
unsigned long INTVLANSW = 0;
static const unsigned char PROGMEM logo_bmp3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe6, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf7, 0xff, 0xcf, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff,
0xff, 0xff, 0xcf, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc3, 0xc7, 0x83, 0xf3, 0x03, 0xe0, 0xf8, 0x07, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x3f, 0x0c, 0x07, 0xc0, 0xe0, 0x0f, 0x80, 0xfe, 0x70, 0x03, 0xc0, 0x67, 0xc7,
0xff, 0xef, 0xfe, 0x3e, 0x18, 0x07, 0x00, 0x40, 0x0e, 0x00, 0xfe, 0x70, 0x03, 0x80, 0xe7, 0xe7,
0xff, 0xef, 0xfe, 0x3c, 0x71, 0xe7, 0x1f, 0xfc, 0x7e, 0x3c, 0xfe, 0x71, 0xff, 0x1f, 0xe7, 0xc7,
0xff, 0xff, 0xfe, 0x38, 0xe1, 0xe7, 0x1f, 0xfc, 0x7c, 0x3c, 0xfe, 0x70, 0x0f, 0x3f, 0xe0, 0x07,
0xff, 0xff, 0xff, 0x31, 0xf0, 0x07, 0x00, 0x7c, 0x7c, 0x00, 0xfe, 0x70, 0x07, 0x3f, 0xe0, 0x07,
0xff, 0xff, 0xfe, 0x03, 0xf0, 0x07, 0x00, 0x7c, 0x7c, 0x00, 0xfe, 0x70, 0xff, 0x3f, 0xe3, 0xc7,
0xff, 0xff, 0xfe, 0x07, 0xf1, 0xe7, 0xfe, 0x7c, 0x7c, 0x7c, 0xfe, 0x71, 0xff, 0x3f, 0xe7, 0xc7,
0xff, 0xcf, 0xfe, 0x1f, 0xe3, 0xe7, 0x00, 0x7c, 0x7c, 0x7c, 0xfe, 0x70, 0x03, 0x00, 0x67, 0xe7,
0xff, 0x87, 0xff, 0x3f, 0xf3, 0xe7, 0x00, 0x7c, 0x7e, 0x7c, 0xfe, 0x70, 0x07, 0x00, 0x67, 0xe7,
0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
<form action="/get">
Buzzer Interval: <input type="text" name="Buzzer_Interval" %BUZZVAL%>
<input type="submit" value="Submit">
</form><br>
<form action="/get">
Timeout Menjawab: <input type="text" name="TimeOut_Jawab" %TOVAL%>
<input type="submit" value="Submit">
</form><br>
</body></html>)rawliteral";
void notFound(AsyncWebServerRequest* request) {
request->send(404, "text/plain", "Not found");
}
String processor(const String& var) {
if (var == "BUZZVAL") {
String inclText = "";
inclText = "value=\"" + String(INTVLBUZZ / 1000) + "\"";
return inclText;
}
if (var == "TOVAL") {
String inclText = "";
inclText = "value=\"" + String(INTVLANSW / 1000) + "\"";
return inclText;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
if (!oledDisp.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial2.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
oledDisp.clearDisplay();
oledDisp.drawBitmap(0, 0, logo_bmp3, 128, 32, WHITE);
oledDisp.display();
delay(5000);
pinMode(OUT1, OUTPUT);
pinMode(OUT2, OUTPUT);
pinMode(OUT3, OUTPUT);
pinMode(OUT4, OUTPUT);
pinMode(OUT5, OUTPUT);
pinMode(OUT6, OUTPUT);
pinMode(INP1, INPUT_PULLUP);
pinMode(INP2, INPUT_PULLUP);
pinMode(INP3, INPUT_PULLUP);
pinMode(INP4, INPUT_PULLUP);
pinMode(INP5, INPUT_PULLUP);
pinMode(INP6, INPUT_PULLUP);
fileConf.begin("app-param", false);
INTVLBUZZ = fileConf.getULong("INTVLBUZZ", 3000);
INTVLANSW = fileConf.getULong("INTVLANSW", 25000);
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, LOW);
digitalWrite(OUT5, LOW);
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while (1)
;
}
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
INTVLBUZZ = fileConf.getULong("INTVLBUZZ", 3000);
INTVLANSW = fileConf.getULong("INTVLANSW", 25000);
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/get?input1=<inputMessage>
server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) {
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
inputParam = PARAM_INPUT_1;
Serial.print("PARAM 1 = ");
Serial.println(inputMessage);
unsigned long dataParam = inputMessage.toInt() * 1000;
fileConf.putULong("INTVLBUZZ", dataParam);
}
// GET input2 value on <ESP_IP>/get?input2=<inputMessage>
else if (request->hasParam(PARAM_INPUT_2)) {
inputMessage = request->getParam(PARAM_INPUT_2)->value();
inputParam = PARAM_INPUT_2;
Serial.print("PARAM 2 = ");
Serial.println(inputMessage);
unsigned long dataParam = inputMessage.toInt() * 1000;
fileConf.putULong("INTVLANSW", dataParam);
}
// GET input3 value on <ESP_IP>/get?input3=<inputMessage>
else if (request->hasParam(PARAM_INPUT_3)) {
inputMessage = request->getParam(PARAM_INPUT_3)->value();
inputParam = PARAM_INPUT_3;
} else {
inputMessage = "No message sent";
inputParam = "none";
}
//Serial.println(inputMessage);
request->send(200, "text/html", "Setting Parameter (" + inputParam + ") dengan Nilai: " + inputMessage + "<br><a href=\"/\">Return to Home Page</a>");
});
server.onNotFound(notFound);
server.begin();
Serial.println("Ready");
}
void loop() {
// put your main code here, to run repeatedly:
int STAT1 = digitalRead(INP1);
int STAT2 = digitalRead(INP2);
int STAT3 = digitalRead(INP3);
int STAT4 = digitalRead(INP4);
int STAT5 = digitalRead(INP5);
int STAT6 = digitalRead(INP6);
if (STAT1 == 0) {
Serial.println("Tombol 1");
waitReset(1);
} else if (STAT2 == 0) {
Serial.println("Tombol 2");
waitReset(2);
} else if (STAT3 == 0) {
Serial.println("Tombol 3");
waitReset(3);
} else if (STAT4 == 0) {
Serial.println("Tombol 4");
waitReset(4);
}
}
void waitReset(int grup) {
if (grup == 1) {
digitalWrite(OUT1, HIGH);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, LOW);
}
if (grup == 2) {
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, HIGH);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, LOW);
}
if (grup == 3) {
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, HIGH);
digitalWrite(OUT4, LOW);
}
if (grup == 4) {
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, HIGH);
} /*
if (grup == 5) {
digitalWrite(OUT5, HIGH);
}
if (grup == 6) {
digitalWrite(OUT6, HIGH);
}*/
digitalWrite(OUT5, HIGH);
unsigned long lastBuzzOn = millis();
bool waitJudge = true;
bool warnTimeOut = true;
Serial.println("Wait Judge Press");
while (waitJudge == true) {
if (millis() - lastBuzzOn > INTVLBUZZ) {
digitalWrite(OUT5, LOW);
}
if (millis() - lastBuzzOn > INTVLANSW) {
if (warnTimeOut == true) {
digitalWrite(OUT5, HIGH);
delay(500);
digitalWrite(OUT5, LOW);
delay(500);
digitalWrite(OUT5, HIGH);
delay(500);
digitalWrite(OUT5, LOW);
delay(500);
digitalWrite(OUT5, HIGH);
delay(500);
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, LOW);
digitalWrite(OUT5, LOW);
warnTimeOut = false;
waitJudge = false;
}
}
if (digitalRead(INP6) == 0) {
digitalWrite(OUT1, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT3, LOW);
digitalWrite(OUT4, LOW);
digitalWrite(OUT5, LOW);
waitJudge = false;
}
}
}