Have you ever played the game Hotter/Colder? Well, this is a high-tech version of it. Scroll down if you want to learn how to make it!
Parts List
- 9 green LEDs
- 2 ESP8266 development board (link to one I found on Amazon)
- Button
- Wires
- 100-ohm resistor
- 2 breadboards
First, go to one of my other ESP8266 tutorials if you have not worked with an ESP8266 in the Arduino IDE yet to install the IDE and the driver and run the "Blink" program. Don't worry, we'll wait for you.
Now, let's start.
Now, let's start.
Here is the circuit for the transmitter:
And here is the circuit for the receiver:
Done building the circuit? Okay, here is the code for the transmitter:
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> ESP8266WebServer server(80); bool pressed = 0; void setup() { // put your setup code here, to run once: WiFi.softAP("hotColdTransmit", "f2e3r7t40u5wf"); server.on("/", handleResults); server.begin(); } void loop() { // put your main code here, to run repeatedly: if (digitalRead(5) == 1) { pressed = 1; } server.handleClient(); delay(5); } void handleResults() { if (pressed == 0) { server.send(200, "text/plain", "Keep looking!"); } if (pressed == 1) { server.send(200, "text/plain", "Found it!"); delay(5000); pressed = 0; } }
And here is the code for the receiver:
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> uint8_t i =0; const int ledCount = 9; // the number of LEDs in the bar graph long prevRSSI = 0; long currentRSSI = 0; long counter = 1; int ledLevel = 0; long start; #define MAX_SRV_CLIENTS 5 WiFiServer server(23); WiFiClient serverClients[MAX_SRV_CLIENTS]; // RSSI: -25 to -95 int ledPins[] = { 16, 5, 4, 0, 2, 14, 12, 13, 15 }; void setup() { // put your setup code here, to run once: Serial.begin(115200); for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } WiFi.begin("hotColdTransmit", "password"); while (WiFi.status() != WL_CONNECTED) { digitalWrite(16, HIGH); delay(250); digitalWrite(16, LOW); delay(250); Serial.print("."); } Serial.print(" IP Address: "); Serial.println(WiFi.localIP()); for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } server.begin(); server.setNoDelay(true); // 16, 5 pinMode(16, OUTPUT); // Red pinMode(5, OUTPUT); // Blue start = millis(); } void loop() { // put your main code here, to run repeatedly: if (server.hasClient()){ for(int i = 0; i < MAX_SRV_CLIENTS; i++){ //find free/disconnected spot if (!serverClients[i] || !serverClients[i].connected()){ if(serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); Serial.print("New client: "); Serial.print(i); continue; } } for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if(serverClients[i].available()){ //get data from the telnet client and push it to the UART while(serverClients[i].available()) Serial.write(serverClients[i].read()); } } } //check UART for data if(Serial.available()){ size_t len = Serial.available(); uint8_t sbuf[len]; Serial.readBytes(sbuf, len); //push UART data to all connected telnet clients for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len); delay(1); } } } //no free/disconnected spot so reject WiFiClient serverClient = server.available(); serverClient.stop(); } String received = receiveServer(); if (received == "Keep looking!") { Serial.println("Keep going!"); telnetPrint("Keep going!\n"); } if (received == "Found it!") { Serial.println("Good job!"); telnetPrint("Good job!\n"); long found = millis(); long duration = found-start; String foundstr = "It took you"+String(duration)+" milliseconds to find it."; Serial.print(foundstr); telnetPrint(foundstr); while(1) { if (server.hasClient()){ for(i = 0; i < MAX_SRV_CLIENTS; i++){ //find free/disconnected spot if (!serverClients[i] || !serverClients[i].connected()){ if(serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); Serial.print("New client: "); Serial.print(i); continue; } } //no free/disconnected spot so reject WiFiClient serverClient = server.available(); serverClient.stop(); } //check clients for data for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if(serverClients[i].available()){ //get data from the telnet client and push it to the UART while(serverClients[i].available()) Serial.write(serverClients[i].read()); } } } victoryDance(); if (server.hasClient()){ for(i = 0; i < MAX_SRV_CLIENTS; i++){ //find free/disconnected spot if (!serverClients[i] || !serverClients[i].connected()){ if(serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); Serial.print("New client: "); Serial.print(i); continue; } } //no free/disconnected spot so reject WiFiClient serverClient = server.available(); serverClient.stop(); } //check clients for data for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if(serverClients[i].available()){ //get data from the telnet client and push it to the UART Serial.println("************* MESSAGE RECEIVED *************"); while(serverClients[i].available()) Serial.write(serverClients[i].read()); Serial.println("************* END MESSAGE *************"); } } } if(Serial.available()){ size_t len = Serial.available(); uint8_t sbuf[len]; Serial.readBytes(sbuf, len); //push UART data to all connected telnet clients for(int i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].println("************* MESSAGE RECEIVED *************"); serverClients[i].write(sbuf, len); serverClients[i].println("************* END MESSAGE *************"); delay(1); } } } } } if (!isEven(counter)) { prevRSSI = findTransmitter(); Serial.println(prevRSSI); telnetPrint(String(prevRSSI)+"\n"); ledLevel = map(prevRSSI, -95, -25, 0, ledCount); } if (isEven(counter)) { currentRSSI = findTransmitter(); Serial.println(currentRSSI); telnetPrint(String(currentRSSI)+"\n"); ledLevel = map(currentRSSI, -95, -25, 0, ledCount); } if (isEven(counter)) { if (isWarmer(prevRSSI, currentRSSI) == 1) { Serial.println("getting warmer"); telnetPrint("getting warmer\n"); } else if (isWarmer(prevRSSI, currentRSSI) == -1) { Serial.println("Same status"); telnetPrint("Same status\n"); } else { Serial.println("getting colder"); telnetPrint("getting colder\n"); } } for (int thisLed = 0; thisLed < ledCount; thisLed++) { // if the array element's index is less than ledLevel, // turn the pin for this element on: if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); } // turn off all pins higher than the ledLevel: else { digitalWrite(ledPins[thisLed], LOW); } } for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if(serverClients[i].available()){ //get data from the telnet client and push it to the UART Serial.println("************* MESSAGE RECEIVED *************"); while(serverClients[i].available()) Serial.write(serverClients[i].read()); Serial.println("************* END MESSAGE *************"); } } } //check UART for data if(Serial.available()){ size_t len = Serial.available(); uint8_t sbuf[len]; Serial.readBytes(sbuf, len); //push UART data to all connected telnet clients for(int i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].println("************* MESSAGE RECEIVED *************"); serverClients[i].write(sbuf, len); serverClients[i].println("************* END MESSAGE *************"); delay(1); } } } delay(1000); counter++; } long findTransmitter() { digitalWrite(15, HIGH); delay(100); digitalWrite(15, LOW); delay(100); currentRSSI = WiFi.RSSI(); return currentRSSI; digitalWrite(15, HIGH); delay(100); digitalWrite(15, LOW); delay(100); } boolean isEven(long num){ return ((num % 2) == 0); } int isWarmer(long prevrssi, long currentrssi) { if (prevrssi > currentrssi) { return 0; } else if (prevrssi == currentrssi) { return -1; } else { return 1; } } String receiveServer() { HTTPClient http; Serial.println("[HTTP] begin..."); telnetPrint("[HTTP] begin...\n"); http.begin("http://192.168.4.1/"); //HTTP Serial.print("[HTTP] GET...\n"); telnetPrint("[HTTP] GET...\n"); int httpCode = http.GET(); if(httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); telnetPrint("[HTTP] GET.. code: "+String(httpCode)+"\n"); // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); telnetPrint(payload+"\n"); return payload; } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); telnetPrint("[HTTP] GET.. failed, error: "+http.errorToString(httpCode)+"\n"); } http.end(); } void victoryDance() { for (int i = 0; i <= 9; i++) { digitalWrite(ledPins[i], 1); delay(100); digitalWrite(ledPins[i], 0); delay(100); } for (int i = 9; i >= 0; i--) { digitalWrite(ledPins[i], 1); delay(100); digitalWrite(ledPins[i], 0); delay(100); } } void telnetPrint(String message) { size_t len = message.length()+1; uint8_t sbuf[len]; message.getBytes(sbuf, len); for(int i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len); delay(1); } } }
How to play:
Plug the transmitter and the receiver into USB power supplies. Wait until the receiver's top LED stops blinking. Then, one person hides the transmitter while the other person closes their eyes. The person looking for the transmitter uses the receiver (the more LEDs that light up on the receiver, the closer you are to the transmitter) to find the transmitter. When the person looking finds the transmitter, they push the big button on it. The LEDs will start a victory dance.
Plug the transmitter and the receiver into USB power supplies. Wait until the receiver's top LED stops blinking. Then, one person hides the transmitter while the other person closes their eyes. The person looking for the transmitter uses the receiver (the more LEDs that light up on the receiver, the closer you are to the transmitter) to find the transmitter. When the person looking finds the transmitter, they push the big button on it. The LEDs will start a victory dance.
Here is a video of the project working:
How it works:
The transmitter makes a WiFi network that the receiver connects to. Then, the receiver checks the signal strength of the network. The higher signal strength, the more LEDs light up.
The transmitter makes a WiFi network that the receiver connects to. Then, the receiver checks the signal strength of the network. The higher signal strength, the more LEDs light up.