Wireless Control: ESP32 8-Buttons Sender to ESP8266 8-Relays via ESP-NOW

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

 In this video, we build a wireless control system using an ESP32 with 8 buttons as the sender, and an ESP8266 with a relay module integrated as the receiver. We'll show you how to set up the communication and trigger the relay with the press of a button. Perfect for home automation projects!.

components you'll need: 
1. ESP32 development boards. 
2. ESP8266 with a relay module integrated. 
3. 8 push buttons. 
4. 1 LEDs. 
5. Resistors (typically 220-330 ohms for the LEDs). 
6. Breadboards. 
7. Jumper wires. 

Sender side: 
1. Take your first ESP32. 
2. Connect one pin of your first button to GPIO pin 13, for example, and the other side to a Ground pin. 
3. Repeat this for the next button on pin 12 and so on, each going to a different pin and Ground. 
Receiver side: 
1. For the receiver board, since you have both the ESP8266 and the relay module integrated. 

Diagram : 

Transmitter 


Receiver


Code :

Transmitter

// - Lien vidéo: https://youtu.be/6nia2yvJs5s

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

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t RxMACaddress[] = {0xD4, 0x5E, 0xAC, 0x64, 0x29, 0xDE}; //Esp8266
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/6nia2yvJs5s

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


#define numtouch 8
const uint8_t Led_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 Led_state[numtouch];

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

// 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");
    // Retry InitESPNow, add a counte and then restart?
    // InitESPNow();
    // or Simply Restart
    // ESP.restart();
  }
}
void SetCommand(int i) {
  if (myData.Datacom != Datacomd[i] || myData.Endcom != Endcomd[i])
    return;
  digitalWrite(Led_pin[i], myData.statled);
  Serial.print("Led ");
  Serial.print(i+1);
  (myData.statled) ? Serial.println(" On") : Serial.println(" Off");
}

// 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(data));
  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(Led_pin[j], OUTPUT);
    digitalWrite(Led_pin[j], LOW);
    Led_state[j] = false;
  }
  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
}

  More information: 

How to easily and quickly program the ESP8266 module via an arduino Uno

How to control 8 relays with remote control using ESP8266 :

https://youtu.be/F7BNXpQAFiQ

How to easily and quickly program the ESP 01 module via an arduino Uno : 

https://youtu.be/8j--sRhjUfw 

How to easily and quickly program the ESP 01 module via an arduino nano : 

https://youtu.be/Tw7eJnhDBQ8 

How to program the ESP-01 ESP8266 module via an ESP8266 : 

https://youtu.be/F7BNXpQAFiQ 

How to configure Arduino IDE and program ESP-01 with USB To ESP8266 01 Serial Adapter : 

https://youtu.be/QRnPRKbtEGU

Lien vidéo:

       * https://youtu.be/6nia2yvJs5s

How to control 8 relays with remote control using esp 8266

Controlling the relay via remote control and ESP8266

How to control 8 relays with remote control using esp 8266

 This video demonstrates how to control a 8-channel relay via remote control using an ESP8266 board. The presenter explains the pin configuration of the ESP8266 and the relay, and then walks through the process of connecting the components and writing a simple code to control the relay. The video is a good introduction for beginners who want to learn how to use relays with ESP8266 boards.
ESP8266 with 8ch relay board - Home Automation

relay 8-ch via remote control and ESP8266

Code :

Esp8266 IR Code


#define IR_RECEIVE_PIN      2     // D4
#define DECODE_NEC

#include <Arduino.h>
#include <IRremote.hpp>

void OnIRDataRecv() {
  if (IrReceiver.decode()) {
    uint16_t received = IrReceiver.decodedIRData.command;
    if (received) {
      Serial.print("Command: 0x");
      Serial.println(received, HEX);
      delay(250);
    }
    IrReceiver.resume();
  }
}
 
void setup() {
  Serial.begin(74880);
  delay(2500);
  IrReceiver.begin(IR_RECEIVE_PIN)
  IrReceiver.registerReceiveCompleteCallback(OnIRDataRecv);
}

void loop() {
  if (IrReceiver.decode()) {
    uint16_t received = IrReceiver.decodedIRData.command;
    if (received) {
      Serial.print("Command: 0x");
      Serial.println(received, HEX);
      delay(250);
    }
    IrReceiver.resume();
  }
  delay(10);
}

Esp8266 Relay


#define IR_RECEIVE_PIN      2     // D4
#define DECODE_NEC

#include <Arduino.h>
#include <IRremote.hpp>

#define relay_1 0x8E
#define relay_2 0x86
#define relay_3 0x8F
#define relay_4 0x92
#define relay_5 0x87
#define relay_6 0x93
#define relay_7 0x96
#define relay_8 0x82

int relay_pin[8] = {5, 4, 0, 15, 13, 12, 14, 16};
bool relay_state[8];

void SetCommand(uint8_t Comd) {
  switch(Comd) {
    case relay_1:
      relay_state[0] = !relay_state[0];
      digitalWrite(relay_pin[0], relay_state[0]);
      (relay_state[0]) ? Serial.println("Relay 1 On") : Serial.println("Relay 1 Off");
      break;
    case relay_2:
      relay_state[1] = !relay_state[1];
      digitalWrite(relay_pin[1], relay_state[1]);
      (relay_state[1]) ? Serial.println("Relay 2 On") : Serial.println("Relay 2 Off");
      break;
    case relay_3:
      relay_state[2] = !relay_state[2];
      digitalWrite(relay_pin[2], relay_state[2]);
      (relay_state[2]) ? Serial.println("Relay 3 On") : Serial.println("Relay 3 Off");
      break;
    case relay_4:
      relay_state[3] = !relay_state[3];
      digitalWrite(relay_pin[3], relay_state[3]);
      (relay_state[3]) ? Serial.println("Relay 4 On") : Serial.println("Relay 4 Off");
      break;
    case relay_5:
      relay_state[4] = !relay_state[4];
      digitalWrite(relay_pin[4], relay_state[4]);
      (relay_state[4]) ? Serial.println("Relay 5 On") : Serial.println("Relay 5 Off");
      break;
    case relay_6:
      relay_state[5] = !relay_state[5];
      digitalWrite(relay_pin[5], relay_state[5]);
      (relay_state[5]) ? Serial.println("Relay 6 On") : Serial.println("Relay 6 Off");
      break;
    case relay_7:
      relay_state[6] = !relay_state[6];
      digitalWrite(relay_pin[6], relay_state[6]);
      (relay_state[6]) ? Serial.println("Relay 7 On") : Serial.println("Relay 7 Off");
      break;
    case relay_8:
      relay_state[7] = !relay_state[7];
      digitalWrite(relay_pin[7], relay_state[7]);
      (relay_state[7]) ? Serial.println("Relay 8 On") : Serial.println("Relay 8 Off");
      break;
    default:
      break;
  }
}

void OnIRDataRecv() {
  if (IrReceiver.decode()) {
    uint16_t received = IrReceiver.decodedIRData.command;
    if (received) {
      Serial.print("Command: 0x");
      Serial.println(received, HEX);
      SetCommand(received);
      delay(250);
    }
    IrReceiver.resume();
  }
}
void setup() {
  Serial.begin(74880);
  for ( int j=0; j<8;j++) {
    pinMode(relay_pin[j], OUTPUT);
    relay_state[j] = false;
    digitalWrite(relay_pin[j], LOW);
  }
  IrReceiver.begin(IR_RECEIVE_PIN);
  IrReceiver.registerReceiveCompleteCallback(OnIRDataRecv);
}

void loop() {
  // Chill
}

Arduino libraries:

       https://github.com/Arduino-IRremote/Arduino-IRremote