Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Serial GPIOs
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-09-11 09:03:12
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* I have iPhone in home app. I have eight */
- /* accessories for my home automation ac, TV, music, */
- /* projector, charger, bulb lamp good night and fan */
- /* in GPIo pain is 5’18’19’25’26’27’23’21 End when */
- /* power cut accessories will be on is on not all */
- /* oll off */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /* No external libraries required for this simple ESP32 GPIO controller. */
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- static const int NUM_DEVICES = 8;
- static const int DEV_PINS[NUM_DEVICES] = {5, 18, 19, 25, 26, 27, 23, 21};
- static const char* DEV_NAMES[NUM_DEVICES] = {"AC","TV","Music","Projector","Charger","BulbLamp","GoodNight","Fan"};
- static bool devState[NUM_DEVICES] = {false, false, false, false, false, false, false, false};
- static const int POWER_FAULT_PIN = 4; // Active HIGH when power OK (pullup used)
- static bool powerOk = true;
- static bool lastPowerOk = true;
- void applyDeviceState(int idx, bool state) {
- if (idx < 0 || idx >= NUM_DEVICES) return;
- digitalWrite(DEV_PINS[idx], state ? HIGH : LOW);
- devState[idx] = state;
- }
- void printStatus(void) {
- Serial.print("Device States: ");
- for (int i = 0; i < NUM_DEVICES; i++) {
- Serial.print(DEV_NAMES[i]);
- Serial.print(":");
- Serial.print(devState[i] ? "ON" : "OFF");
- if (i < NUM_DEVICES - 1) Serial.print(" ");
- }
- Serial.println();
- }
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println("ESP32 Eight-Device Controller: Initializing...");
- for (int i = 0; i < NUM_DEVICES; i++) {
- pinMode(DEV_PINS[i], OUTPUT);
- digitalWrite(DEV_PINS[i], LOW); // Start OFF
- }
- pinMode(POWER_FAULT_PIN, INPUT_PULLUP);
- powerOk = digitalRead(POWER_FAULT_PIN) == HIGH;
- lastPowerOk = powerOk;
- Serial.print("Power status: ");
- Serial.println(powerOk ? "OK" : "FAULT");
- printStatus();
- Serial.println("Commands:");
- Serial.println("ON <idx> - Turn ON device <idx> (0-7)");
- Serial.println("OFF <idx> - Turn OFF device <idx> (0-7)");
- Serial.println("TOGGLE <idx> - Toggle device <idx> (0-7)");
- Serial.println("STATE - Print current device states");
- Serial.println("Example: ON 2");
- }
- void loop(void)
- {
- // Monitor power status
- lastPowerOk = powerOk;
- powerOk = digitalRead(POWER_FAULT_PIN) == HIGH;
- if (!powerOk && lastPowerOk) {
- // Power cut detected
- Serial.println("Power cut detected. Preserving current device states during outage.");
- }
- // Simple serial command interface
- if (Serial.available() > 0) {
- String line = Serial.readStringUntil('\n');
- line.trim();
- if (line.length() == 0) return;
- if (line.startsWith("ON")) {
- int idx = line.substring(2).trim().toInt();
- if (idx >= 0 && idx < NUM_DEVICES) {
- applyDeviceState(idx, true);
- Serial.println(String("Device ") + DEV_NAMES[idx] + " ON");
- } else {
- Serial.println("Invalid index. Use 0-7.");
- }
- } else if (line.startsWith("OFF")) {
- int idx = line.substring(3).trim().toInt();
- if (idx >= 0 && idx < NUM_DEVICES) {
- applyDeviceState(idx, false);
- Serial.println(String("Device ") + DEV_NAMES[idx] + " OFF");
- } else {
- Serial.println("Invalid index. Use 0-7.");
- }
- } else if (line.startsWith("TOGGLE")) {
- int idx = line.substring(6).trim().toInt();
- if (idx >= 0 && idx < NUM_DEVICES) {
- bool newState = !devState[idx];
- applyDeviceState(idx, newState);
- Serial.println(String("Device ") + DEV_NAMES[idx] + (newState ? " ON" : " OFF"));
- } else {
- Serial.println("Invalid index. Use 0-7.");
- }
- } else if (line.startsWith("STATE")) {
- printStatus();
- } else if (line.startsWith("HELP")) {
- Serial.println("Commands:");
- Serial.println("ON <idx> - Turn ON device <idx> (0-7)");
- Serial.println("OFF <idx> - Turn OFF device <idx> (0-7)");
- Serial.println("TOGGLE <idx> - Toggle device <idx> (0-7)");
- Serial.println("STATE - Print current device states");
- } else {
- Serial.println("Unknown command. Type HELP for commands.");
- }
- }
- // Optional: small delay to avoid busy loop
- delay(10);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment