#include <WiFi.h>
#include <WebServer.h>
#include <TFT_eSPI.h>
const int ONE_SECOND = 1000;
const char* SSID = "Neurync";
const char* PASSWORD = "12345678";
TFT_eSPI tft = TFT_eSPI();
const int YES_BUTTON_PIN = 12, NO_BUTTON_PIN = 13;
bool isYesButtonPressedPreviously = false, isNoButtonPressedPreviously = false;
String ip = "";
const int WEB_PORT = 80;
String username = "", message = "";
bool isWaitingQuestion = false;
String answer = "";
WebServer server(WEB_PORT);
bool isButtonPressed(int buttonPin);
void handleQuestion();
void handleAnswer();
void setup() {
pinMode(YES_BUTTON_PIN, INPUT_PULLUP);
pinMode(NO_BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
WiFi.softAP(SSID, PASSWORD);
delay(ONE_SECOND * 2);
ip = WiFi.softAPIP().toString();
Serial.print("Server started. IP: ");
Serial.println(ip);
server.on("/question", HTTP_POST, handleQuestion);
server.on("/answer", HTTP_GET, handleAnswer);
tft.init();
tft.setRotation(0);
tft.fillScreen(TFT_WHITE);
tft.setTextColor(TFT_BLACK, TFT_WHITE);
tft.setTextSize(3);
tft.setCursor(10, 10);
tft.print("Iniciando...");
tft.setCursor(10, 50);
tft.print(ip);
delay(ONE_SECOND * 3);
tft.fillScreen(TFT_WHITE);
server.begin();
}
void loop() {
server.handleClient();
bool currentYesButtonValue = isButtonPressed(YES_BUTTON_PIN);
if (currentYesButtonValue && !isYesButtonPressedPreviously) {
if (isWaitingQuestion && answer == "") {
answer = "yes";
tft.fillScreen(TFT_GREEN);
delay(ONE_SECOND / 2);
tft.fillScreen(TFT_WHITE);
Serial.println("Professor answered YES!");
}
}
isYesButtonPressedPreviously = currentYesButtonValue;
bool currentNoButtonValue = isButtonPressed(NO_BUTTON_PIN);
if (currentNoButtonValue && !isNoButtonPressedPreviously) {
if (isWaitingQuestion && answer == "") {
answer = "no";
tft.fillScreen(TFT_RED);
delay(ONE_SECOND / 2);
tft.fillScreen(TFT_WHITE);
Serial.println("Professor answered NO!");
}
}
isNoButtonPressedPreviously = currentNoButtonValue;
}
bool isButtonPressed(int buttonPin) {
return !digitalRead(buttonPin);
}
void handleQuestion() {
if (server.hasArg("username") && server.hasArg("message")) {
username = server.arg("username");
message = server.arg("message");
isWaitingQuestion = true;
answer = "";
Serial.println("Novo alerta recebido:");
Serial.println("De: " + username);
Serial.println("Mensagem: " + message);
tft.fillScreen(TFT_WHITE);
tft.setCursor(10, 10);
tft.print("Alerta de ");
tft.print(username);
tft.setCursor(10, 100);
tft.print(message);
server.send(200, "text/plain", "Alerta recebido");
} else {
server.send(400, "text/plain", "Faltando 'username' ou 'message'");
}
}
void handleAnswer() {
if (!isWaitingQuestion) {
server.send(200, "text/plain", "Sem nada para responder");
return;
}
if (answer == "") {
server.send(200, "text/plain", "pending");
} else {
server.send(200, "text/plain", answer);
isWaitingQuestion = false;
}
}