DIY Smart Home: 8 Buttons to 8 Relays via ESP-NOW and ESP32

Build a Remote Switch: ESP32 Buttons & ESP32 Relay via ESP-NOW.
 

In this video, learn how to build a powerful 8-channel wireless relay control system using two ESP32 microcontrollers and the ESP-NOW protocol. We'll show you how to connect 8 buttons to one ESP32 (the transmitter) to wirelessly switch 8 relays connected to a second ESP32 (the receiver). This is great for home automation or remote switching projects!  

components you'll need: 

 * 2x ESP32 Development Boards 
* 1x 8-Channel Relay Module (or 8 individual relays) 
* 8x Push Buttons 
* Jumper Wires 
* Power Supply (e.g., 5V for relays/ESP32s) 
* Breadboard (optional, for prototyping) 

Wiring Description:
Transmitter ESP32:
Connect each of the 8 push buttons between a digital GPIO pin and Ground (GND). (You'll configure these pins with internal pull-up resistors in the code.) 
Receiver ESP32:
Connect the input pins of the 8-channel relay module to 8 digital GPIO pins on the ESP32. Ensure both the ESP32 and the relay module share a common Ground (GND) and are properly powered.

Diagram : 

Transmitter 


Receiver


Code :

Transmitter

// - Lien vidéo: https://youtu.be/MqeR2rXToOM

#include <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t RxMACaddress[] = {0x87, 0xD9, 0xB0, 0xF4, 0x18, 0x32}; //ESP32
esp_now_peer_info_t peerInfo; // Variables to manage state changes

// Define button pins
const int buttonPins[] = {T4, T5, T6, T7, T8, T9, T2, T3};

volatile int pressedButtonID = 0;
volatile int idtouch = 0;
const int numButtons = 8;
const int ledPin = 23;

const uint8_t comand[numButtons] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8};
const uint8_t Datacomd[numButtons] = {0x9F, 0x9E, 0xBC, 0xAD, 0xFD, 0xA4, 0x3F, 0x94};
const uint8_t Endcomd[numButtons] = {0xF9, 0xE9, 0xCB, 0xDA, 0xDF, 0x4A, 0xF3, 0x49};
volatile bool TouchPressed[numButtons];
volatile bool TouchReleased[numButtons];
volatile bool TouchLower[numButtons];

// Structure to send data (must match receiver)
typedef struct struct_message {
  uint8_t Datacom;
  uint8_t idcomd;
  uint8_t Endcom;
} struct_message;
struct_message myData;

// Interrupt Service Routine (ISR)
// Declared IRAM_ATTR to run from RAM for speed
void IRAM_ATTR handleButtonPressUp(void* arg) {
  int id = (int)arg;
  if (TouchPressed[id] != TouchLower[id]) {
    TouchPressed[id] = true;
    pressedButtonID++;
  }
}
void IRAM_ATTR handleButtonPressDn(void* arg) {
  int id = (int)arg;
  if (TouchReleased[id] == TouchLower[id]) {
    TouchReleased[id] = true;
    pressedButtonID++;
  }
}

// Init ESP Now with fallback
void InitESPNow() {
  WiFi.disconnect();
  if (esp_now_init() == ESP_OK) {
    Serial.println("ESPNow Init Success");
  } else {
    Serial.println("ESPNow Init Failed");
  }
}

//Register peer
void Registerpeer() {
  memcpy(peerInfo.peer_addr, RxMACaddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  if (esp_now_add_peer(&peerInfo) == ESP_OK) {
    Serial.println("Add peer Success");
  } else {
    Serial.println("Failed to add peer");
  }
}

// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\t Status: ");
  if (status == ESP_NOW_SEND_SUCCESS) {
    // char macStr[18];
    // snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
    Serial.println("Delivery Success");
    // Serial.print("\t Last Packet Sent to: ");
    // Serial.println(macStr);
  } else {
    Serial.println("Delivery Fail");
  }
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  InitESPNow(); // Init ESP-NOW
  esp_now_register_send_cb(OnDataSent);
  Registerpeer(); // Register peer
  // Set up button pins and attach interrupts
  for (int i = 0; i < numButtons; i++) {
    TouchPressed[i] = false;
    TouchReleased[i] = false;
    TouchLower[i] = true;
    pinMode(buttonPins[i], INPUT_PULLUP);
    attachInterruptArg(digitalPinToInterrupt(buttonPins[i]), handleButtonPressUp, (void*)i, FALLING);
  }
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
}

void loop() {
  if (pressedButtonID > 0) {
    for (int i = 0; i < numButtons; i++) {
      if (TouchPressed[i] != TouchReleased[i]) {
        myData.Datacom = Datacomd[i];
        myData.idcomd = comand[i];
        myData.Endcom = Endcomd[i];
        Serial.print("Touch ");
        Serial.print(i + 1);
        if (TouchLower[i]) {
          Serial.print(" was Pressed");
          digitalWrite(ledPin,HIGH);
          esp_now_send(RxMACaddress, (uint8_t *) &myData, sizeof(myData));
          TouchPressed[i] = false;
          TouchLower[i] = false;
          delay(100);
          attachInterruptArg(digitalPinToInterrupt(buttonPins[i]), handleButtonPressDn, (void*)i, RISING);
        } else {
          Serial.println(" was Released");
          pressedButtonID -= 2;
          if (pressedButtonID <= 0) digitalWrite(ledPin,LOW);
          TouchReleased[i] = false;
          TouchLower[i] = true;
          delay(100);
          attachInterruptArg(digitalPinToInterrupt(buttonPins[i]), handleButtonPressUp, (void*)i, FALLING);
        }
      }
    }
  }
}

Receiver

// - Lien vidéo: https://youtu.be/MqeR2rXToOM

#include <esp_now.h>
#include <WiFi.h>


#define numtouch 8
const uint8_t relay_pin[numtouch] = {13, 12, 14, 27, 26, 25, 33, 32};
const uint8_t comand[numtouch] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8};
const uint8_t Datacomd[numtouch] = {0x9F, 0x9E, 0xBC, 0xAD, 0xFD, 0xA4, 0x3F, 0x94};
const uint8_t Endcomd[numtouch] = {0xF9, 0xE9, 0xCB, 0xDA, 0xDF, 0x4A, 0xF3, 0x49};
bool relay_state[numtouch];

// Structure to send data (must match receiver)
typedef struct struct_message {
  uint8_t Datacom;
  uint8_t idcomd;
  uint8_t Endcom;
} struct_message;
struct_message myData;

// Init ESP Now with fall
// Init ESP Now with fallback
void InitESPNow() {
  WiFi.disconnect();
  if (esp_now_init() == ESP_OK) {
    Serial.println("ESPNow Init Success");
  }
  else {
    Serial.println("ESPNow Init Failed");
  }
}

void SetCommand(int i) {
  if (myData.Datacom != Datacomd[i] || myData.Endcom != Endcomd[i])
    return;
  relay_state[i] = !relay_state[i];
  digitalWrite(relay_pin[i], relay_state[i]);
  Serial.print("Relay ");
  Serial.print(i+1);
  (relay_state[i]) ? Serial.println(" Off") : Serial.println(" On");
}

// callback when data is recv from Master
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  memcpy((uint8_t *)&myData, data, sizeof(myData));
  int comd = (int)(myData.idcomd - 0xB1);
  if (myData.idcomd == comand[comd])
    SetCommand(comd);
}

void setup() {
  Serial.begin(115200);
  delay(2500); // give me time to bring up serial monitor
  for ( int j=0; j<numtouch;j++) {
    pinMode(relay_pin[j], OUTPUT);
    digitalWrite(relay_pin[j], HIGH);
    relay_state[j] = true;
  }
  WiFi.mode(WIFI_STA);
  InitESPNow();
  esp_now_register_recv_cb(OnDataRecv);
  Serial.println();
  Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
  Serial.print("STA CHANNEL "); Serial.println(WiFi.channel());
}

void loop() {
  // greatprojects
}


 

Lien vidéo:

       https://youtu.be/MqeR2rXToOM

No comments:

Post a Comment