#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apSsid = "NodeMCU-Repeater";
const char* apPassword = "TheCaptsWifi";
WebServer server(80);
bool isRepeaterMode = false;
int signalStrengthThreshold = -80; // Adjust this threshold based on your requirements
void handleRoot() {
server.send(200, "text/plain", "Hello from the NodeMCU Wi-Fi repeater!");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Set initial mode as Station mode
// Try connecting to the existing Wi-Fi network
WiFi.begin(ssid, password);
// Set up the repeater access point
WiFi.softAP(apSsid, apPassword);
// Print the NodeMCU's IP addresses
Serial.print("Station IP address: ");
Serial.println(WiFi.localIP());
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
int currentSignalStrength = WiFi.RSSI();
if (currentSignalStrength < signalStrengthThreshold) {
if (!isRepeaterMode) {
WiFi.mode(WIFI_AP_STA); // Set mode to both AP and Station
isRepeaterMode = true;
Serial.println("Switched to Repeater mode");
}
} else {
if (isRepeaterMode) {
WiFi.mode(WIFI_STA); // Set mode back to Station only
isRepeaterMode = false;
Serial.println("Switched to Station mode");
}
}
delay(1000);
}