pleasedontcode

Serial GPIOs rev_01

Sep 11th, 2025
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Serial GPIOs
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-11 09:03:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I have iPhone in home app. I have eight */
  21.     /* accessories for my home automation ac, TV, music, */
  22.     /* projector, charger, bulb lamp good night and fan */
  23.     /* in GPIo pain is 5’18’19’25’26’27’23’21   End when */
  24.     /* power cut  accessories will be on is on not all */
  25.     /* oll off */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32.  
  33. /* No external libraries required for this simple ESP32 GPIO controller. */
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. static const int NUM_DEVICES = 8;
  40. static const int DEV_PINS[NUM_DEVICES] = {5, 18, 19, 25, 26, 27, 23, 21};
  41. static const char* DEV_NAMES[NUM_DEVICES] = {"AC","TV","Music","Projector","Charger","BulbLamp","GoodNight","Fan"};
  42.  
  43. static bool devState[NUM_DEVICES] = {false, false, false, false, false, false, false, false};
  44.  
  45. static const int POWER_FAULT_PIN = 4; // Active HIGH when power OK (pullup used)
  46. static bool powerOk = true;
  47. static bool lastPowerOk = true;
  48.  
  49. void applyDeviceState(int idx, bool state) {
  50.   if (idx < 0 || idx >= NUM_DEVICES) return;
  51.   digitalWrite(DEV_PINS[idx], state ? HIGH : LOW);
  52.   devState[idx] = state;
  53. }
  54.  
  55. void printStatus(void) {
  56.   Serial.print("Device States: ");
  57.   for (int i = 0; i < NUM_DEVICES; i++) {
  58.     Serial.print(DEV_NAMES[i]);
  59.     Serial.print(":");
  60.     Serial.print(devState[i] ? "ON" : "OFF");
  61.     if (i < NUM_DEVICES - 1) Serial.print(" ");
  62.   }
  63.   Serial.println();
  64. }
  65.  
  66. void setup(void)
  67. {
  68.   // put your setup code here, to run once:
  69.   Serial.begin(115200);
  70.   Serial.println("ESP32 Eight-Device Controller: Initializing...");
  71.   for (int i = 0; i < NUM_DEVICES; i++) {
  72.     pinMode(DEV_PINS[i], OUTPUT);
  73.     digitalWrite(DEV_PINS[i], LOW); // Start OFF
  74.   }
  75.   pinMode(POWER_FAULT_PIN, INPUT_PULLUP);
  76.   powerOk = digitalRead(POWER_FAULT_PIN) == HIGH;
  77.   lastPowerOk = powerOk;
  78.   Serial.print("Power status: ");
  79.   Serial.println(powerOk ? "OK" : "FAULT");
  80.   printStatus();
  81.   Serial.println("Commands:");
  82.   Serial.println("ON <idx>      - Turn ON device <idx> (0-7)");
  83.   Serial.println("OFF <idx>     - Turn OFF device <idx> (0-7)");
  84.   Serial.println("TOGGLE <idx>  - Toggle device <idx> (0-7)");
  85.   Serial.println("STATE         - Print current device states");
  86.   Serial.println("Example: ON 2");
  87. }
  88.  
  89. void loop(void)
  90. {
  91.   // Monitor power status
  92.   lastPowerOk = powerOk;
  93.   powerOk = digitalRead(POWER_FAULT_PIN) == HIGH;
  94.   if (!powerOk && lastPowerOk) {
  95.     // Power cut detected
  96.     Serial.println("Power cut detected. Preserving current device states during outage.");
  97.   }
  98.  
  99.   // Simple serial command interface
  100.   if (Serial.available() > 0) {
  101.     String line = Serial.readStringUntil('\n');
  102.     line.trim();
  103.     if (line.length() == 0) return;
  104.  
  105.     if (line.startsWith("ON")) {
  106.       int idx = line.substring(2).trim().toInt();
  107.       if (idx >= 0 && idx < NUM_DEVICES) {
  108.         applyDeviceState(idx, true);
  109.         Serial.println(String("Device ") + DEV_NAMES[idx] + " ON");
  110.       } else {
  111.         Serial.println("Invalid index. Use 0-7.");
  112.       }
  113.     } else if (line.startsWith("OFF")) {
  114.       int idx = line.substring(3).trim().toInt();
  115.       if (idx >= 0 && idx < NUM_DEVICES) {
  116.         applyDeviceState(idx, false);
  117.         Serial.println(String("Device ") + DEV_NAMES[idx] + " OFF");
  118.       } else {
  119.         Serial.println("Invalid index. Use 0-7.");
  120.       }
  121.     } else if (line.startsWith("TOGGLE")) {
  122.       int idx = line.substring(6).trim().toInt();
  123.       if (idx >= 0 && idx < NUM_DEVICES) {
  124.         bool newState = !devState[idx];
  125.         applyDeviceState(idx, newState);
  126.         Serial.println(String("Device ") + DEV_NAMES[idx] + (newState ? " ON" : " OFF"));
  127.       } else {
  128.         Serial.println("Invalid index. Use 0-7.");
  129.       }
  130.     } else if (line.startsWith("STATE")) {
  131.       printStatus();
  132.     } else if (line.startsWith("HELP")) {
  133.       Serial.println("Commands:");
  134.       Serial.println("ON <idx>      - Turn ON device <idx> (0-7)");
  135.       Serial.println("OFF <idx>     - Turn OFF device <idx> (0-7)");
  136.       Serial.println("TOGGLE <idx>  - Toggle device <idx> (0-7)");
  137.       Serial.println("STATE         - Print current device states");
  138.     } else {
  139.       Serial.println("Unknown command. Type HELP for commands.");
  140.     }
  141.   }
  142.  
  143.   // Optional: small delay to avoid busy loop
  144.   delay(10);
  145. }
  146.  
  147. /* END CODE */
  148.  
Advertisement
Add Comment
Please, Sign In to add comment