Advertisement
rhandycan1

Esp32_usb_dongle_controller_wifi

Jul 19th, 2025
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2.  
  3. const char* ssid = "YOUR SSID";
  4. const char* password = "Your Password";
  5. WiFiServer server(1234);
  6.  
  7. struct ButtonAction {
  8.   const char* name;
  9.   int pin;
  10. };
  11.  
  12. ButtonAction buttonMap[] = {
  13.   { "BTN_SOUTH", 19 },   # replace pin no. accdg to your esp32. In this case I use, 19,18,23 and 22
  14.   { "BTN_EAST", 18 },
  15.   { "BTN_NORTH", 23 },
  16.   { "BTN_WEST", 22 },
  17. };
  18.  
  19. const int flashDuration = 200;
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.   Serial.println(WiFi.localIP());  // ← this prints the IP address to Serial Monitor
  24.  
  25.   for (auto& btn : buttonMap) {
  26.     pinMode(btn.pin, OUTPUT);
  27.     digitalWrite(btn.pin, LOW);
  28.   }
  29.  
  30.   WiFi.begin(ssid, password);
  31.   Serial.print("Connecting to Wi-Fi");
  32.   while (WiFi.status() != WL_CONNECTED) {
  33.     delay(500);
  34.     Serial.print(".");
  35.   }
  36.   Serial.println("\n✅ Wi-Fi connected");
  37.   Serial.println(WiFi.localIP());
  38.   server.begin();
  39. }
  40.  
  41. void flashLED(int pin) {
  42.   digitalWrite(pin, HIGH);
  43.   delay(flashDuration);
  44.   digitalWrite(pin, LOW);
  45. }
  46.  
  47. void loop() {
  48.   WiFiClient client = server.available();
  49.  
  50.   if (client) {
  51.     while (client.connected()) {
  52.       if (client.available()) {
  53.         String input = client.readStringUntil('\n');
  54.         input.trim();
  55.  
  56.         for (auto& btn : buttonMap) {
  57.           if (input == "BTN:" + String(btn.name)) {
  58.             flashLED(btn.pin);
  59.             Serial.println("Triggered: " + input);
  60.           }
  61.         }
  62.       }
  63.     }
  64.     client.stop();  // Close client cleanly after disconnect
  65.   }
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement