pleasedontcode

ESP32 Relays rev_03

Sep 6th, 2025
302
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: ESP32 Relays
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-06 07:46:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* clontrol 8 SSD relay with IR remote and MPR121 */
  21.     /* touch pad */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  31. #include <Wire.h>
  32. #include <Adafruit_MPR121.h>
  33. #include <IRrecv.h>
  34. #include <IRutils.h>
  35.  
  36. /****** CONSTANTS AND PIN DEFINITIONS *****/
  37. #define NUM_RELAYS 8
  38.  
  39. // Relay pin mapping (ESP32 GPIOs). Ensure these pins are suitable for your board and relay driver wiring.
  40. const uint8_t RELAY_PINS[NUM_RELAYS] = {
  41.   2, 4, 5, 12, 13, 14, 15, 16
  42. };
  43.  
  44. // I2C pins for MPR121
  45. #define MPR_SDA_PIN 21
  46. #define MPR_SCL_PIN 22
  47.  
  48. // IR receiver
  49. #define IR_PIN 27
  50.  
  51. /****** FUNCTION PROTOTYPES *****/
  52. void setup(void);
  53. void loop(void);
  54.  
  55. // MPR121 touch support
  56. Adafruit_MPR121 cap = Adafruit_MPR121();
  57.  
  58. // IR receiver object
  59. IRrecv irrecv(IR_PIN);
  60. decode_results results;
  61.  
  62. // Relay modules (explicit pin mapping for clarity)
  63. Relay relays[NUM_RELAYS] = {
  64.   Relay(2, true),
  65.   Relay(4, true),
  66.   Relay(5, true),
  67.   Relay(12, true),
  68.   Relay(13, true),
  69.   Relay(14, true),
  70.   Relay(15, true),
  71.   Relay(16, true)
  72. };
  73.  
  74. // Previous touch states for MPR121 electrodes (we use first 8 electrodes)
  75. bool prevTouch[NUM_RELAYS];
  76.  
  77. // Helper to toggle a relay state
  78. void toggleRelay(uint8_t idx) {
  79.   if (idx >= NUM_RELAYS) return;
  80.   if (relays[idx].getState()) {
  81.     relays[idx].turnOff();
  82.   } else {
  83.     relays[idx].turnOn();
  84.   }
  85. }
  86.  
  87. // Process IR remote codes (example mappings for common NEC-like remotes)
  88. void processIR(uint32_t code) {
  89.   switch (code) {
  90.     case 0xFF30CF: toggleRelay(0); break; // Key 1
  91.     case 0xFF18E7: toggleRelay(1); break; // Key 2
  92.     case 0xFF7A85: toggleRelay(2); break; // Key 3
  93.     case 0xFF10EF: toggleRelay(3); break; // Key 4
  94.     case 0xFF38C7: toggleRelay(4); break; // Key 5
  95.     case 0xFF5AA5: toggleRelay(5); break; // Key 6
  96.     case 0xFF42BD: toggleRelay(6); break; // Key 7
  97.     case 0xFF4AB5: toggleRelay(7); break; // Key 8
  98.     default: break;
  99.   }
  100. }
  101.  
  102. void setup(void)
  103. {
  104.   // Initialize serial for debugging
  105.   Serial.begin(115200);
  106.   while (!Serial) { ; }
  107.  
  108.   // Initialize Relay modules and reset touch states
  109.   for (uint8_t i = 0; i < NUM_RELAYS; i++) {
  110.     relays[i].begin();
  111.     prevTouch[i] = false;
  112.   }
  113.  
  114.   // Initialize I2C for MPR121 on defined pins
  115.   Wire.begin(MPR_SDA_PIN, MPR_SCL_PIN);
  116.   if (!cap.begin(0x5A)) {
  117.     Serial.println("MPR121 not found");
  118.     while (1);
  119.   }
  120.  
  121.   // Initialize IR receiver
  122.   irrecv.enableIRIn();
  123.  
  124.   Serial.println("System ready");
  125. }
  126.  
  127. void loop(void)
  128. {
  129.   // IR remote handling
  130.   if (irrecv.decode(&results)) {
  131.     processIR(results.value);
  132.     irrecv.resume();
  133.   }
  134.  
  135.   // MPR121 touch handling (toggle relays on touch edge)
  136.   uint16_t touches = cap.touched();
  137.   for (uint8_t i = 0; i < NUM_RELAYS; i++) {
  138.     bool isTouched = (touches & (1 << i)) != 0;
  139.     if (isTouched && !prevTouch[i]) {
  140.       toggleRelay(i);
  141.     }
  142.     prevTouch[i] = isTouched;
  143.   }
  144.  
  145.   delay(20);
  146. }
  147.  
  148. /* END CODE */
  149.  
Advertisement
Add Comment
Please, Sign In to add comment