ESP32

ESP8266 and ESP32 as Transmitter and receiver, Home Automation Project

ESP8266 and ESP32:

ESP8266 and ESP32 as Transmitter and receiver, Home Automation Project- I have been using the Nodemcu ESP8266 and ESP32 WiFi + Bluetooth Modules for years in different IoT based projects for monitoring different types of sensors and for controlling AC and DC loads.

Today’s project is completely different from my previous projects; because this time round, we are not using any IoT platform.

ESP8266 and ESP32

These two boards will connect with each other through Wi-Fi, and one more thing I’d like to mention is that we don’t need a WiFi router or a hotspot for this project.




Anyway, we are going to use ESP8266 as the Transmitter and ESP32 WiFi + Bluetooth module as the receiver.

On the transmitter side, I have connected some switches with the ESP8266 WiFi module.

ESP8266 and ESP32

On the receiver side, I have connected 110/220Vac bulbs with the ESP32 WiFi + Bluetooth module. You can also connect DC loads; it depends on what you want to control.

ESP8266 and ESP32

Anyway, let’s proceed to power up the transmitter and receiver sides. When the 110/220Vac supply is connected, never touch the relay contacts because such high voltage can be fatal. So, as far as possible wear protective gloves and perform such high voltage experiments in the presence of a friend or any companion.

On the transmitter side, you can small toggle switches; this will reduce the size, and also the cost.

Instead of using the ESP32 WiFi module on the receiver side, you can also use the ESP8266. However, one advantage of using the ESP32 module on the receiver is that you can modify it in the future. As you may know, the ESP32 also includes a Bluetooth module. So you can design your own Android application for controlling the loads. This way, you’ll be able to control the loads both from the transmitter and your Smartphone.

ESP8266 and ESP32

I successfully controlled all the 4 Bulbs wirelessly, the communication was insanely fast, and there was no false triggering. In comparison to regular Bluetooth modules, its range is also greater. Now, let’s go ahead and take a look at the connections.



Amazon Links:

ESP32 WiFi + Bluetooth Module

ESP8266 WiFi Module

4 channels relay module

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

ESP8266 Transmitter Circuit:

ESP8266 and ESP32

The 4 switches are connected to the Nodemcu ESP8266 WiFi module digital pins D1, D3, D5 and D7. This is the 5V regulated power supply based on the 7805 linear voltage regulator. This power supply is optional. While performing your initial experiments you can use your Laptop or PC to power up the Nodemcu ESP8266 Wifi module. You can also use a power bank, lithium ion battery, or a Lipo battery to power up the Nodemcu module.



ESP32 Receiver Circuit:

ESP8266 and ESP32

It doesn’t matter if you don’t have a development board like this, you can do the same exact connections on a breadboard.

ESP8266 and ESP32

4 SPDT type relays are connected to the ESP32 GPIO pins 12, 13, 14, and 27. You must connect AC or DC loads between the common and normally open contacts of the relays. These are the freewheeling diodes connected across the relays coil pins. You can use 1N4007 diodes. I am using a pair of 10K ohm and 2N2222 NPN transistor to control each relay. You can follow this circuit diagram.



If you want to use the Arduino IDE for programming the ESP32 and ESP8266, then first you will need to install these boards in the Arduino IDE. Because, by default no ESP32 and ESP8266 boards are installed in the Arduino IDE and you can confirm this by going to the Tools Menu… then to Board and you can see there are no ESP32 and ESP8266 boards in the list.

ESP8266 and ESP32

So, first we will need to add it in the boards list, for this copy this Board Manager URL link;

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Then go back to the Arduino IDE, go to the File Menu, then preferences, and paste this link in the Additional Boards Manager URLs and click the Ok button.

ESP8266 and ESP32

Next, go to the Tools Menu, then Board and click on the Boards Manager. Search for the ESP32. You can see we have Arduino ESP32 Boards and ESP32 by Espressif Systems.

ESP8266 and ESP32

So, make sure you install this one and don’t forget to select the latest version. Finally, the board installation has been completed and now we can confirm this by going to the boards list.

ESP8266 and ESP32

You can see all the different variants of the ESP32 Boards have been added.




ESP8266 Board Installation:

Now, let’s install the ESP8266 Board, just follow the same exact steps but this time we will use a different board manager URL link given below.

http://arduino.esp8266.com/stable/package_esp8266com_index.json

So, copy the above link, go back to the Arduino IDE. Go to the File Menu, and then preferences. Paste the link in the Additional Boards Manager URLs. Don’t forget to put a comma if there is already a link.

ESP8266 and ESP32

Next, go to the Tools Menu, then Board and click on the Boards Manager. Search for the ESP8266.

ESP8266 and ESP32

You can see the ESP8266 boards have also been added in the list.

ESP8266 and ESP32

Now, we can use the Arduino IDE for programming the ESP32 and ESP8266 WiFi modules.



MAC address Finder:

#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(){

}

You will need this program to find the MAC address of the receiver. In this project, I am using ESP32 module as the receiver. So, I will simply upload this program into the ESP32 board. Then I can open the Serial monitor and copy the MAC Address.

ESP8266 and ESP32

You can see the mac address is 98 : F4 : AB : 68 : F4 : A4

On your side, this address may be different. So, make sure, you replace this mac address with yours. Don’t forget to remove the colons and add 0x with the numbers. As you can see in the transmitter side programming. 

0x98, 0xF4, 0xAB, 0x68,0xF4,0xA4



ESP8266 Transmitter Programming:

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

int button1=D1;
int button2=D3;
int button3=D5;
int button4=D7;

// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = { 0x98, 0xF4, 0xAB, 0x68,0xF4,0xA4};

typedef struct struct_message {
  bool sw1;
  bool sw2;
  bool sw3;
  bool sw4;
} struct_message;

// Create a struct_message called outgoingReadings to hold outgoing data
struct_message outgoingReadings;

// Create a struct_message called incomingReadings to hold incoming data
struct_message incomingReadings;

// Variable to store if sending data was successful
String success;

// 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");
  }
}

//// Callback when data is received
//void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
//  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
//  Serial.print("Bytes received: ");
//  Serial.println(len);
////  Serial.print("Data: "); Serial.println(incomingReadings.msg);
//}


void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
pinMode(button1,INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP);
pinMode(button4,INPUT_PULLUP);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

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

  // Set ESP-NOW Role
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

  // Register for a callback function that will be called when data is received
 // esp_now_register_recv_cb(OnDataRecv);
}

void loop() {

  int buttonstate1=digitalRead(button1);
delay(10);
    int buttonstate2=digitalRead(button2);
 delay(10);
  int buttonstate3=digitalRead(button3);
delay(10);
 int buttonstate4=digitalRead(button4);
delay(10);
Serial.println(buttonstate1);
Serial.println(buttonstate2);
Serial.println(buttonstate3);
Serial.println(buttonstate4);

  outgoingReadings.sw1= buttonstate1;
    outgoingReadings.sw2= buttonstate2;
  outgoingReadings.sw3= buttonstate3;
  outgoingReadings.sw4= buttonstate4;
//Serial.println(outgoingReadings);
   esp_now_send(broadcastAddress, (uint8_t *) &outgoingReadings, sizeof(outgoingReadings));

  //delay(2000);
}



ESP32 Receiver Programming:

#include <esp_now.h>
#include <WiFi.h>
#define Relay1            12
#define Relay2            13
#define Relay3            14
#define Relay4            27

typedef struct struct_message {
    bool sw1;
    bool sw2;
    bool sw3;
    bool sw4;
} struct_message;



struct_message incomingReadings;

// Variable to store if sending data was successful
String success;


// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Switch 1: ");Serial.println(incomingReadings.sw1);
  Serial.print("Switch 2: ");Serial.println(incomingReadings.sw2);
  Serial.print("Switch 3: ");Serial.println(incomingReadings.sw3);
  Serial.print("Switch 4: ");Serial.println(incomingReadings.sw4);

  if ( incomingReadings.sw1 == 0)
  {
  digitalWrite(Relay1,HIGH);
  }
   if ( incomingReadings.sw1 == 1)
  {
  digitalWrite(Relay1,LOW );
  }
  if ( incomingReadings.sw2 == 0)
  {
  digitalWrite(Relay2,HIGH);
  }
   if ( incomingReadings.sw2 == 1)
  {
  digitalWrite(Relay2,LOW );
  }

if ( incomingReadings.sw3 == 0)
  {
  digitalWrite(Relay3,HIGH);
  }
   if ( incomingReadings.sw3 == 1)
  {
  digitalWrite(Relay3,LOW );
  }
  
  if ( incomingReadings.sw4 == 0)
  {
  digitalWrite(Relay4,HIGH);
  }
   if ( incomingReadings.sw4 == 1)
  {
  digitalWrite(Relay4,LOW );
  }

}

void setup()
{
  // Init Serial Monitor
  Serial.begin(115200);
  Serial.println("Code start");
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

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

  
  
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);

}

void loop()
{  
}




Watch Video Tutorial:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button