ESP-NOW Remote controller

remote
This content provide you Experience

Today we learn about how to make Remote controller using NodeMCU . We can control almost every appliances using ESP8266 without using any other communication modules.

Components and software required

  • Two ESP8266 microcontroller
  • Push button switch
  • jumper wires

Getting started with ESP32 follow this link to setting up environment for ESP32 — https://elconics.com/getting-started-withesp32/

Get a MAC Address of receiver

Upload this code to receiver and open serial monitor to get MAC address.

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

Note down This MAC address some ware, I use notepad .

Setting up Transmitter

Write down this code in reciver end

#include <ESP8266WiFi.h>
#include <espnow.h>

int newbuttonstate1;
int  oldbuttonstate1= 0;
int ledState1;
#define b1 D1
#define led1 D4


int newbuttonstate2;
int  oldbuttonstate2= 0;
int ledState2;



#define b2 D2

#define led1 D5
#define led2 D7

int DTime=50;

uint8_t broadcastAddress1[] = {0x48,0x55,0x19,0xC8,0xD9,0xEE}; // REPLACE WITH RECEIVER MAC Address
//uint8_t broadcastAddress2[] = {0x48,0x55,0x19,0xC8,0xAF,0x02}; // 48:55:19:C8:AF:02



typedef struct struct_message {
int a;
int b;

} struct_message;

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;  
unsigned long timerDelay = 500;  // send readings timer

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}
 
void setup() {
   pinMode(led1,OUTPUT);
   pinMode(b1,INPUT_PULLUP);
   pinMode(b1,INPUT_PULLUP);
  // Init Serial Monitor
  Serial.begin(115200);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_add_peer(broadcastAddress1, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
 // esp_now_add_peer(broadcastAddress2, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
 
void loop() {

    // Button 1
  newbuttonstate1=digitalRead(b1);
  if (oldbuttonstate1==1 && newbuttonstate1==0)

  if (ledState1==0)
  {
    digitalWrite(led1,HIGH);
    ledState1=1;
    Serial.print("ledState1 ");
    Serial.println(ledState1);
    
    }
    else 
    {
      
      digitalWrite(led1,LOW);
         ledState1=0;
     Serial.print("ledState1 ");
    Serial.println(ledState1);
      }
      oldbuttonstate1=newbuttonstate1;
      delay(DTime);

// Button 2
newbuttonstate2=digitalRead(b2);
       if (oldbuttonstate2==1 && newbuttonstate2==0)

  if (ledState2==0)
  {
    digitalWrite(led2,HIGH);
    ledState2=1;
    Serial.print("ledState2 ");
    Serial.println(ledState2);
    
    }
    else 
    {
      
      digitalWrite(led2,LOW);
    ledState2=0;
    Serial.print("ledState2 ");
    Serial.println(ledState2);
      }
      oldbuttonstate2=newbuttonstate2;
      delay(DTime);



  if ((millis() - lastTime) > timerDelay) {
    // Set values to send
    myData.a=ledState1;
    myData.b = ledState2;
  
Serial.println(ledState1);
Serial.println(ledState2);

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress1, (uint8_t *) &myData, sizeof(myData));
   // esp_now_send(broadcastAddress2, (uint8_t *) &myData, sizeof(myData));

    lastTime = millis();
  }
}

Setting Up Receiver

Upload this code and attach Relay module as per your requirement

#include <ESP8266WiFi.h>
#include <espnow.h>
int led1 = D1;
int led2 = D2;


// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
    int a;
    int b;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("A: ");
  Serial.println(myData.a);
  Serial.print("B: ");
  Serial.println(myData.b);

}
 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {

if (myData.a==1)
{
    digitalWrite(led1,HIGH);
    Serial.println("Light 1 has Turn ON");
    }
   else if (myData.a==0)
   {
    digitalWrite(led1,LOW);
    Serial.println("Light 1 has Turn OFF");
    }
 
  if (myData.b==1)
  {
    digitalWrite(led2,HIGH);
    Serial.println("Light 2 has Turn ON");
    }
   else if (myData.b==0)
   {
    digitalWrite(led2,LOW);
    Serial.println("Light 2 has Turn OFF");
    }        
 
delay(50);




 
}

You can follow this video for more details-

Leave a Comment

Your email address will not be published. Required fields are marked *

Related post