Two Nodemcu and LED

Hi guys, I am having two nodemcu and I want to connect with each other on their own wifi, I am using a switch with one of them and led with the other.
By using switch I want to connect the led which is connected to other nodemcu wirelessly. How I can do so ??

Have you looked at ESP-NOW?

Thanks, I have connected them and now when I am turning on the switch I am getting 1 on the serial monitor of the other esp8266 and when I am turning off the switch I am getting 0 (That's how really I wanted it to work). Now I want that when the serial monitor of other esp 8266 gets 0 or 1 it should read it and turn off or on the led that I have connected to it.

The code for Sender is

//SENDER

#include <ESP8266WiFi.h>
#include <espnow.h>
#define ledPin 13 // choose the pin for the LED 
#define switchPin 12 // choose the input pin (for a pushbutton)
 
int val = 0; // variable for reading the pin status

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xC8, 0xC9, 0xA3, 0x69, 0xE8, 0x40};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  int b;

} struct_message;

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;  
unsigned long timerDelay = 20;  // 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() {
  // Init Serial Monitor
  Serial.begin(115200);
    pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(switchPin, INPUT); // declare pushbutton as input
 
  // 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(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
 
void loop() {
  if ((millis() - lastTime) > timerDelay) {
  val = digitalRead(switchPin); // read input value
  if (val == HIGH)
  { 
    digitalWrite(ledPin, LOW); // turn LED OFF
    Serial.println("0");
    myData.b = 0;
} else {
    digitalWrite(ledPin, HIGH); // turn LED ON } }
    Serial.println("1");
     myData.b = 1;
}

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

    lastTime = millis();
   
  }
 }
} 
   

The code for the receiver (I am doing serial read but it is working when I am typing 1 or 0 in serial monitor maybe I am using the wrong command)

//RECIEVER

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

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
   
  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("Incoming: ");
  Serial.println(myData.b);
}
 
void setup() {
  // Initialize Serial Monitor
  pinMode(13,OUTPUT);
  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 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(Serial.available())
{
myData.b = Serial.read();
}
if (myData.b == '1')
{
  digitalWrite(13,1);
}
else if (myData.b == '0')
{
  digitalWrite(13,0);
  }
}

So when you operate the switch and transmit its state using ESP-NOW, the receiver is receiving the state and displaying that state in the receiver's serial monitor? Do I have that right?

Please post the code for the transmitter and receiver so that I can know the data type that is being received. Please read the forum guidelines to see how to properly post code and some information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You would use an if - else structure to decide whether to to turn the LED on or off depending on the received state. The syntax of the if structure would depend on the data type of the received data.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.