#include <WiFi.h> //wifi library
#include <LiquidCrystal_I2C.h> //lcd library
#include <EEPROM.h>
#define PTC 34
WiFiServer server(80); //zapne web server na porte 80
LiquidCrystal_I2C lcd(0x27, 16, 2); //init lcd display
//premenne:
String header;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
int runtime = 0;
int lock;
int time_spent;
int final_time;
int last_time = 0;
int best_time;
void setup()
{
pinMode(PTC, INPUT);
Serial.begin(9600);
EEPROM.begin(512);
WiFi.begin("morenerob", "esp32projektos", 6); //wifi connection
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //ipcka espcka
server.begin();
//lcd init:
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lock = random(0,501);
EEPROM.put(1, 0);
}
void loop()
{
time_spent = millis();
for(int i = 0;i <= 10; i=i)
{
int raw = analogRead(PTC);
int guess = map(raw, 0,4095,0,500);
lcd.setCursor(0,0);
lcd.print(lock);
lcd.setCursor(0,1);
lcd.print(guess < 100 ? "0" : "");
lcd.print(guess);
lcd.setCursor(0,0);
if(guess == lock)
{
final_time = millis();
final_time = final_time + time_spent;
lock = random(0,501);
i = i +1;
Serial.println(i);
lcd.clear();
}
if(i == 10)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CAS: ");
last_time = final_time;
lcd.print(last_time);
serialShow();
if(EEPROM.get(1, best_time) > last_time)
{
EEPROM.put(1, last_time);
}
else if(EEPROM.get(1, best_time) == 0)
{
EEPROM.put(1, last_time);
}
EEPROM.put(2, last_time);
startWeb();
}
}
}
void startWeb()
{
WiFiClient client = server.available(); // hlada ci sa niekto pripojil na stranku
if (client) { // ak sa klient pripoji:
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // alert v seriali o tom, ze sa niekto pripojil
String currentLine = ""; // String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { //loop kym je klient pripojeny
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// html kod
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// simple css
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println("h1{font-size: 300px;}</style>");
// content
client.print("<body><h1>");
client.print("BEST TIME: ");
client.println(EEPROM.get(1, best_time));
client.println("</h1><br><h1>");
client.print("LAST TIME: ");
client.println(EEPROM.get(2, last_time));
client.println("</h1></body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
// vycistit link
header = "";
// vypnut server
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
void serialShow()
{
Serial.print("BEST: ");
Serial.println(EEPROM.get(1, best_time));
Serial.print("LAST: ");
Serial.println(last_time);
delay(1000);
}