diff --git a/milyoner.ino b/milyoner.ino new file mode 100644 index 0000000..90689f8 --- /dev/null +++ b/milyoner.ino @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define OLED_RESET 0 +Adafruit_SSD1306 display(OLED_RESET); + +ESP8266WebServer server(80); +WiFiClientSecure client; + +const char* ssid = ""; +const char* password = ""; +const char* Gemini_Token = ""; +const char* Gemini_Max_Tokens = "100"; +const char* fingerprint = ""; // SHA1 fingerprint + +int currentQuestion = 0; +int score = 0; +String currentCorrectAnswer = ""; +String questionHTML = ""; + +void setup() { + Serial.begin(115200); + Wire.begin(D2, D1); + + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { + Serial.println(F("OLED bulunamadı")); + while (true); + } + + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0, 0); + display.println("WiFi baglantisi yapiliyor..."); + display.display(); + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + + display.clearDisplay(); + display.setCursor(0, 0); + display.println("Baglandi!"); + display.display(); + + server.on("/", HTTP_GET, handleRoot); + server.on("/submit", HTTP_POST, handleSubmit); + server.on("/next", HTTP_POST, handleNext); + server.on("/quit", HTTP_POST, handleQuit); + server.on("/restart", HTTP_POST, handleRestart); + server.begin(); + + Serial.println("HTTP sunucu baslatildi"); + Serial.println(WiFi.localIP()); + getNewQuestion(); +} + +void loop() { + server.handleClient(); +} + +void handleRoot() { + server.send(200, "text/html", questionHTML); +} + +void handleSubmit() { + if (!server.hasArg("choice")) return; + String userAnswer = server.arg("choice"); + String result = ""; + + if (userAnswer == currentCorrectAnswer) { + result = "
Dogru bildiniz!
"; + score += 1000; + questionHTML = generateControlHTML(result); + } else { + result = "
Kaybettiniz!
"; + questionHTML = result + generateResultHTML(); + } + + server.send(200, "text/html", questionHTML); +} + +void handleNext() { + currentQuestion++; + getNewQuestion(); + server.sendHeader("Location", "/"); + server.send(303); +} + +void handleQuit() { + questionHTML = generateQuitHTML(); + server.send(200, "text/html", questionHTML); +} + +void handleRestart() { + currentQuestion = 0; + score = 0; + getNewQuestion(); + server.sendHeader("Location", "/"); + server.send(303); +} + +void getNewQuestion() { + String prompt = "Dünyada az bilinen bir bilgi içeren çoktan seçmeli (4 şıklı) bir genel kültür sorusu oluştur. Format: Soru: ...\\nA) ...\\nB) ...\\nC) ...\\nD) ...\\nCevap: A/B/C/D"; + String response = askGemini(prompt); + + int soruStart = response.indexOf("Soru:") + 6; + int aStart = response.indexOf("A)"); + int bStart = response.indexOf("B)"); + int cStart = response.indexOf("C)"); + int dStart = response.indexOf("D)"); + int answerStart = response.indexOf("Cevap:"); + + String soru = response.substring(soruStart, aStart); + soru.trim(); + + String A = response.substring(aStart + 2, bStart); + A.trim(); + + String B = response.substring(bStart + 2, cStart); + B.trim(); + + String C = response.substring(cStart + 2, dStart); + C.trim(); + + String D = response.substring(dStart + 2, answerStart); + D.trim(); + + currentCorrectAnswer = response.substring(answerStart + 7, answerStart + 8); + + display.clearDisplay(); + display.setCursor(0, 0); + display.println(("Soru " + String(currentQuestion + 1)).c_str()); + display.display(); + + questionHTML = "Quiz
"; + questionHTML += "

" + String(currentQuestion + 1) + ". Soru

" + soru + "

"; + questionHTML += "
"; + questionHTML += "
"; + questionHTML += "
"; + questionHTML += "
"; + questionHTML += "
"; + questionHTML += "
"; + questionHTML += "
"; +} + +String generateControlHTML(String message) { + String html = ""; + html += message; + html += "

Devam etmek ister misiniz?

"; + html += "
"; + html += "
"; + return html; +} + +String generateResultHTML() { + String html = ""; + html += "

Yarisma Bitti!

"; + html += "

Toplam Puan: " + String(score) + "

"; + html += "
"; + html += ""; + return html; +} + +String generateQuitHTML() { + String html = ""; + html += "

Yarismadan Cekildiniz

"; + html += "

Yanıtlanan Soru: " + String(currentQuestion + 1) + "

"; + html += "

Kazanc: " + String(score) + " TL

"; + html += "
"; + html += ""; + return html; +} + +String askGemini(String question) { + client.setInsecure(); + HTTPClient https; + String url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=" + String(Gemini_Token); + + if (!https.begin(client, url)) return "Baglanti basarisiz"; + + https.addHeader("Content-Type", "application/json"); + String payload = "{\"contents\":[{\"parts\":[{\"text\":\"" + question + "\"}]}],\"generationConfig\":{\"maxOutputTokens\":" + String(Gemini_Max_Tokens) + "}}"; + int httpCode = https.POST(payload); + + if (httpCode == HTTP_CODE_OK) { + String response = https.getString(); + DynamicJsonDocument doc(2048); + deserializeJson(doc, response); + String answer = doc["candidates"][0]["content"]["parts"][0]["text"].as(); + return answer; + } else { + return "Hata: " + https.errorToString(httpCode); + } + + https.end(); + return "Bilinmeyen hata"; +}