// WiFi Treasure Hunt - TARGET/BEACON // This ESP32 acts as the hidden target to find /* The ssid and password may be modified for custom usage */ #include //================================================= // may be modified : // MUST match targetSsid in the EMF scanner code const char* ssid = "emf_treasure_target"; const char* password = "treasure123"; //================================================= bool inGame = false; //================================================= // Pin definitions : // may be modified (only) here. #define STATUS_LED 5 // Built-in LED #define DONE_SWITCH 16 // switch #define GAMEMODE_LED 17 // led game statement //================================================= void setup() { // Initialisation : Serial.begin(115200); // Pin initialisation pinMode(STATUS_LED, OUTPUT); pinMode(DONE_SWITCH, INPUT_PULLUP); pinMode(GAMEMODE_LED, OUTPUT); // Wifi is disabled by default WiFi.mode(WIFI_OFF); WiFi.disconnect(true); Serial.println("WiFi eteint"); // Both LEDs blink to confirm initialisation is ready for (int i = 0; i < 5; i++) { digitalWrite(STATUS_LED, HIGH); digitalWrite(GAMEMODE_LED, HIGH); delay(250); digitalWrite(STATUS_LED, LOW); digitalWrite(GAMEMODE_LED, LOW); delay(250); } } //================================================= void loop() { // Variables : int switchState = digitalRead(DONE_SWITCH); digitalWrite(STATUS_LED, LOW); delay(250); digitalWrite(STATUS_LED, HIGH); // If the switch is turned on, the game starts here : if (switchState == 1) { // LED signal before the game starts Serial.print("enter in the game loop\n"); digitalWrite(STATUS_LED, HIGH); delay(3000); digitalWrite(STATUS_LED, LOW); // This LED is always turned on while the game is running digitalWrite(GAMEMODE_LED, HIGH); // WiFi is activated here as Access Point WiFi.mode(WIFI_AP); WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("Target AP active! IP: "); Serial.println(IP); Serial.print("Channel: "); Serial.println(WiFi.channel()); // Game loop while the switch is on while (inGame == false) { switchState = digitalRead(DONE_SWITCH); if (switchState == 0) inGame = true; // LED is turned on when being tracked (at least one client connected) int numClients = WiFi.softAPgetStationNum(); if (numClients > 0) { digitalWrite(STATUS_LED, HIGH); Serial.println("tracked"); } else { digitalWrite(STATUS_LED, LOW); } delay(500); // Small delay to avoid hammering softAPgetStationNum } // Switch off the game LED when the game is over digitalWrite(GAMEMODE_LED, LOW); // Disconnect the WiFi here WiFi.softAPdisconnect(true); WiFi.mode(WIFI_OFF); Serial.print("exit the game loop\n"); // Reset inGame so the target can be reused inGame = false; } }