rhandycan1

Esp32_usb_dongle_controller_wifi

Jul 19th, 2025 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | Source Code | 0 0
  1. //PROGRAM BY: RANDY N. CAÑADA
  2. /*SUBSCRIBE: FB PAGES:
  3. Playlist.ph:
  4. https://www.facebook.com/profile.php?id=61573177858722
  5.  
  6. Arduino:
  7. https://www.facebook.com/rhacan2?__tn__=R
  8.  
  9. YOUTUBE:
  10. PlaylistPH
  11. https://www.youtube.com/@PlaylistPh-h2v
  12.  
  13. ElectroCrafters:
  14. https://www.youtube.com/channel/UCu1aqzUdTqSnTN5BHNlbVgA */
  15.  
  16.  
  17.  
  18. #include <WiFi.h>
  19.  
  20. const char* ssid = "YOUR SSID";
  21. const char* password = "Your Password";
  22. WiFiServer server(1234);
  23.  
  24. struct ButtonAction {
  25.   const char* name;
  26.   int pin;
  27. };
  28.  
  29. ButtonAction buttonMap[] = {
  30.   { "BTN_SOUTH", 19 },   # replace pin no. accdg to your esp32. In this case I use, 19,18,23 and 22
  31.   { "BTN_EAST", 18 },
  32.   { "BTN_NORTH", 23 },
  33.   { "BTN_WEST", 22 },
  34. };
  35.  
  36. const int flashDuration = 200;
  37.  
  38. void setup() {
  39.   Serial.begin(115200);
  40.   Serial.println(WiFi.localIP());  // ← this prints the IP address to Serial Monitor
  41.  
  42.   for (auto& btn : buttonMap) {
  43.     pinMode(btn.pin, OUTPUT);
  44.     digitalWrite(btn.pin, LOW);
  45.   }
  46.  
  47.   WiFi.begin(ssid, password);
  48.   Serial.print("Connecting to Wi-Fi");
  49.   while (WiFi.status() != WL_CONNECTED) {
  50.     delay(500);
  51.     Serial.print(".");
  52.   }
  53.   Serial.println("\n✅ Wi-Fi connected");
  54.   Serial.println(WiFi.localIP());
  55.   server.begin();
  56. }
  57.  
  58. void flashLED(int pin) {
  59.   digitalWrite(pin, HIGH);
  60.   delay(flashDuration);
  61.   digitalWrite(pin, LOW);
  62. }
  63.  
  64. void loop() {
  65.   WiFiClient client = server.available();
  66.  
  67.   if (client) {
  68.     while (client.connected()) {
  69.       if (client.available()) {
  70.         String input = client.readStringUntil('\n');
  71.         input.trim();
  72.  
  73.         for (auto& btn : buttonMap) {
  74.           if (input == "BTN:" + String(btn.name)) {
  75.             flashLED(btn.pin);
  76.             Serial.println("Triggered: " + input);
  77.           }
  78.         }
  79.       }
  80.     }
  81.     client.stop();  // Close client cleanly after disconnect
  82.   }
  83. }
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment