Week 11 · Networking & Communications

A.k.a. two boards, no wires between them, and a button that turns on someone else's light 📡💡


🪛 Overview

The assignment this week: design, build and connect wired or wireless node(s) with network or bus addresses, each with a local input and/or output device.

I built a two-node wireless network using two ESP8266 NodeMCU boards talking over ESP-NOW. Each node has a push button (input) and an LED (output). Pressing the button on one node toggles the LED on the other node, and it works both ways.

RequirementHow I met it
Wireless nodes2 × ESP8266 NodeMCU, no wires between them
Network addressesEach node is addressed by its unique Wi-Fi MAC address
Local inputPush button on D2 (GPIO4)
Local outputLED on D4 (GPIO2)

👥 Group Assignment

The group assignment was to send a message between two projects.

🔗 View Group Assignment Documentation

📡 Why ESP-NOW?

ESP-NOW is a protocol from Espressif that lets ESP boards send small packets directly to each other using the Wi-Fi radio, but without a router or internet connection. Each board is identified by its MAC address, which works like a house number: you send the message to a specific address, and only that board picks it up.

ESP-NOWRegular Wi-Fi (HTTP / WebSocket)Wired bus (I²C / UART)
Needs a router?❌ No✅ Yes (or one board acts as access point)❌ No
Needs wires between boards?❌ No❌ No✅ Yes
AddressingMAC addressIP addressBus address / direct link
Setup effortLowMediumLow, but boards must be close
Good forSmall, fast messages like button pressesWeb apps, bigger dataBoards on the same PCB or very near

Since I only needed to send a tiny on/off message, ESP-NOW was the simplest option. It's also the same family of radio I use for my final project, which runs over Wi-Fi from an ESP32 (see Week 14).

🧩 The Board: ESP8266 NodeMCU

Both nodes use the NodeMCU 1.0 (ESP-12E module). It has Wi-Fi built in, a USB port for programming, and labelled pins that fit straight into a breadboard.

ESP8266 NodeMCU pinout diagram with pin functions and features
NodeMCU pinout (source: Last Minute Engineers)

One thing to remember: the board runs at 3.3 V logic, so everything on the breadboard is powered from the 3V3 pin, not 5 V. The pin labels on the board (D2, D4) are not the same as the GPIO numbers the chip uses (GPIO4, GPIO2), so I kept the pinout open while wiring.

🔌 Wiring Each Node

Both nodes are wired the same way, so the same code runs on both. Only the address of the other board changes.

Breadboard wiring: push button on GPIO4 (D2) and LED with resistor on GPIO2 (D4) of the NodeMCU
Wiring for one node: button on D2, LED on D4
ComponentNodeMCU pinGPIORole
Push buttonD2GPIO4Input
LED (+ resistor)D4GPIO2Output
Power rail3V3-3.3 V supply
Ground railGND-Common ground

D4 (GPIO2) is also connected to the NodeMCU's small built-in blue LED, so the board itself lights up along with the breadboard LED. That's the blue glow you can see on the boards in the video below.

🏷️ Finding the Addresses

Before two boards can talk, each one needs to know the other's address. I uploaded a tiny sketch that just prints the board's MAC address to the Serial Monitor:

#include <ESP8266WiFi.h>
void setup(){
   Serial.begin(115200);
   delay(500);
   Serial.println();
   Serial.print("MAC: ");
   Serial.println(WiFi.macAddress());
}
void loop(){}

We ran it on both boards and swapped the results over chat. The scrambled characters at the top are normal: the ESP8266 prints boot messages at a different baud rate when it starts up, and the Serial Monitor shows them as junk.

Chat messages showing two MAC addresses, E0:98:06:0B:1E:EF and 50:02:91:51:26:40
Swapping MAC addresses between the two boards
NodeMAC addressSends to
Node A (my board)50:02:91:51:26:40Node B
Node BE0:98:06:0B:1E:EFNode A

Each board's code gets the other board's MAC address as its peer. In my sketch that's Node B:

uint8_t broadcastAddress[] = {0xE0, 0x98, 0x06, 0x0B, 0x1E, 0xEF};

💻 The Code

Both nodes run the same sketch, Bidirectional_Pushbutton_LED.ino, with the peer address swapped. Each node is set to the COMBO role, which means it can both send and receive.

Arduino IDE showing the start of Bidirectional_Pushbutton_LED.ino with pins and peer address
Pins, peer address and the message struct (Arduino IDE, board: NodeMCU 1.0)

Arduino IDE showing the OnDataSent and OnDataRecv callbacks and setup
The send and receive callbacks

🔹 The message

The packet is tiny: a struct with a single integer, led, which is either 1 (on) or 0 (off). Both boards must use the exact same struct, or the receiver reads the bytes wrong.

typedef struct struct_message {
    int led;
} struct_message_send;

🔹 Setup

  • • Set the Wi-Fi mode to station (WIFI_STA) and disconnect from any network
  • • Start ESP-NOW with esp_now_init()
  • • Register two callbacks: OnDataSent (did it arrive?) and OnDataRecv (something came in)
  • • Set the role to ESP_NOW_ROLE_COMBO and add the other board as a peer

🔹 Sending (button pressed)

if(BTN_State == 0) {
  LED_State_Send = !LED_State_Send;   // toggle
  send_Data.led = LED_State_Send;
  esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data));
  while(BTN_State == 0) {             // wait for release
    BTN_State = digitalRead(BTN_Pin);
    delay(10);
  }
}

The button uses INPUT_PULLUP, so it reads 0 when pressed. Each press flips the state and sends it. The while loop waits until the button is released, so holding it down sends only one message.

🔹 Receiving

void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {
  memcpy(&receive_Data, incomingData, sizeof(receive_Data));
  LED_State_Receive = receive_Data.led;
  digitalWrite(LED_Pin, LED_State_Receive);
}

When a packet arrives, the board copies the bytes into its struct and writes the value straight to the LED.

🔁 How a Message Travels

 NODE A  50:02:91:51:26:40                    NODE B  E0:98:06:0B:1E:EF
 ┌──────────────────────┐                     ┌──────────────────────┐
 │ button (D2) pressed  │                     │                      │
 │ led = !led  → 1      │                     │                      │
 │ esp_now_send(B, {1}) │ ─── ESP-NOW ──────▶ │ OnDataRecv()         │
 │                      │    {led: 1}         │ digitalWrite(D4, 1)  │
 │ OnDataSent():        │ ◀── ack ─────────── │ 💡 LED ON            │
 │ "Delivery success"   │                     │                      │
 └──────────────────────┘                     └──────────────────────┘

        ...and the same thing in reverse when Node B's button is pressed
Serial Monitor messageMeaning
Peer added successfully !!The other board's MAC was registered
>>>>> Send dataThe button was pressed and a packet went out
Delivery successThe other board confirmed it received the packet
Delivery failNo confirmation: wrong MAC, out of range, or the other board is off
Receive Data: 1A packet arrived and the LED was set to that value

🎬 It Works!

Here are the two nodes on separate breadboards, each powered on its own, with nothing connecting them. Pressing the button on one board switches the LED on the other.

Two nodes toggling each other's LEDs over ESP-NOW 📡
Two NodeMCU boards on separate breadboards on a table, one being pressed
Both nodes: separate breadboards, separate power, no wires between them

Pressing the button on the far node while the near node waits
Pressing the button on the far node

Close-up of one node on its breadboard with button, LED and jumper wires
Close-up of one node

⚠️ Things to Watch Out For

IssueWhy it happensWhat to do
Junk characters in the Serial Monitor on startupThe ESP8266 boot messages use a different baud rateIgnore them; the real output starts after boot
Messages not arrivingThe peer address has to be the other board's MAC, not your ownPrint both MACs first, then swap them into each sketch
Mixing up pin namesD-labels on the board ≠ GPIO numbersUse D2 / D4 in code, with the NodeMCU board selected
One press sending many messagesThe loop reads the button many times while it's heldWait for the button to be released before sending again

✨ What I Learned

  • • A network address can be as simple as a MAC address: every ESP board already has a unique one
  • • ESP-NOW is great for small messages between boards without any router
  • • Sender and receiver must agree on the exact message format (the struct)
  • • Delivery callbacks are really useful for debugging: you know straight away if the other board heard you
  • • Writing one sketch for both nodes, with only the address changing, keeps things simple

📁 Files

Bidirectional_Pushbutton_LED.ino
Get_MacAddress.ino