Skip to content

14. Networking and communications

Objectives

Individual

  • design, build, and connect wired or wireless node(s) with network or bus addresses

Group (To redirect to group assigment page Click here)

  • send a message between two projects

ESP32 Pager

Pager

A pager is a wireless telecommunications device that receives and displays alphanumeric or voice messages. One-way pagers can only receive messages, while response pagers and two-way pagers can also acknowledge, reply to and originate messages using an internal transmitter(Wiki).

ESP-NOW protocol

ESP-NOW is a protocol developed by Espressif, which enables multiple devices to communicate with one another without using Wi-Fi. The protocol is similar to the low-power 2.4GHz wireless connectivity that is often deployed in wireless mouses(espressif.com). for more information you can watch this youtube video

Designing

Master Board

I was planning to make a pager which have a master board which can send a spesific predifined msg to slave modules while pressing buttons throuch wifi using ESP-NOW protocol so I took fusion and strted creating the schematic
schem.jpg.jpg and went to PCB Documents and arranged the components and ruted out the PCB wich was ready to be milled out. schem.jpg.jpg the files were exported as monochrome images and milled out.
for downloading file Click here

Components used

Device Value Qty
VR_REGULATOR LM1117SOT223 1
2828XX-2282837-2 2828XX-2282837-2 1
OLED Display 1
CONN_00X06_HEADER 1
ESP32-WROOM 1
LED RED 1
MOSFET NSOT23 2
Resistor 0k 2
Capasitor 100nf 2
Capasitor 1uf 2
Resistor 5k 2
6MM_SWITCH 3
Resistor 10k 7

Slave Board

for slave bord i will use one switch wich can send a replay to the master board. it was easier as i just wanted to remove the extra switches provided in the master board schem.jpg.jpg and went to PCB Documents and arranged the components and ruted out the PCB wich was ready to be milled out. schem.jpg.jpg the files were exported as monochrome images and milled out.
for downloading file Click here

Components used

Device Value Qty
VR_REGULATOR LM1117SOT223 1
2828XX-2282837-2 2828XX-2282837-2 1
OLED Display 1
CONN_00X06_HEADER 1
ESP32-WROOM 1
LED RED 1
MOSFET NSOT23 2
Resistor 0k 2
Capasitor 100nf 2
Capasitor 1uf 2
Resistor 5k 2
6MM_SWITCH 1
Resistor 10k 4

Milling, Stuffing and Soldering

Both bords were milled out and components were placed and the soldering was done Click here to learn more about the milling and soldering processes.This was the end outcome of my work.

Master Board

schem.jpg.jpg

Slave Board

schem.jpg.jpg

Programming

Master Board



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

#define but_one 18
#define but_two 5
#define but_three 19
bool new_data = 0; // Flag to send data only once


// Universal MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//First Slave MAC Address
uint8_t broadcastAddress1[] = {0xYY, 0xYY, 0xYY, 0xYY, 0xYY, 0xYY};
//Second Slave MAC Address
uint8_t broadcastAddress2[] = {0xZZ, 0xZZ, 0xZZ, 0xZZ, 0xZZ, 0xZZ};

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

// Create a struct_message called myData
struct_message myData;

struct_message incomingReadings;
// Create peer interface
esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.println(incomingReadings.but_status);
  if (incomingReadings.but_status == 1)
  {
    display.clearDisplay();
    display.setTextSize(2); display.setCursor(20, 0);
    display.print("Message Recived");
    display.display();
  }

}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  pinMode(but_one, INPUT_PULLUP);

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

  // Send Callback Function
  esp_now_register_send_cb(OnDataSent);

  // Receive Callback Function
  esp_now_register_recv_cb(OnDataRecv);

  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

void loop()
{
  // Set values to send
  if (digitalRead(but_one) == LOW && new_data == 3)
  {
    myData.but_status = 1;
    new_data = 0;
  }
  if (digitalRead(but_two) == LOW && new_data == 3)
  {
    myData.but_status = 0;
    new_data = 1;
  }
  if (digitalRead(but_three) == LOW && new_data == 3)
  {
    myData.but_status = 2;
    new_data = 2;
  }

  esp_err_t result; // declaration

  // Send message via ESP-NOW
  if (new_data == 0)
  {

    result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
    new_data = 3;
  }

  if (new_data == 1)
  {

    result = esp_now_send(broadcastAddress1, (uint8_t *) &myData, sizeof(myData));
    new_data = 3;
  }

   if (new_data == 2)
  {

    result = esp_now_send(broadcastAddress2, (uint8_t *) &myData, sizeof(myData));
    new_data = 3;
  }

  if (result == ESP_OK)
  {
    Serial.println("Sent with success");
  }
  else
  {
    Serial.println("Error sending the data");
  }

  // delay(10000);
}


Slave Board



//Slave
#include <esp_now.h>
#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define but_one 18
bool new_data = 0; // Flag to send data only once


//Master MAC Address
uint8_t broadcastAddress1[] = {0xYY, 0xYY, 0xYY, 0xYY, 0xYY, 0xYY};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message
{
  int but_status;
} struct_message;

// Create a struct_message called myData
struct_message myData;

struct_message incomingReadings;
// Create peer interface
esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.println(incomingReadings.but_status);
  if (incomingReadings.but_status == 1)
  {
    display.clearDisplay();
    display.setTextSize(2); display.setCursor(20, 0);
    display.print("Required Text Meaasge");
    display.display();
  }

}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  pinMode(but_one, INPUT_PULLUP);

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

  // Send Callback Function
  esp_now_register_send_cb(OnDataSent);

  // Receive Callback Function
  esp_now_register_recv_cb(OnDataRecv);

  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

void loop()
{
  // Set values to send
  if (digitalRead(but_one) == LOW && new_data == 1)
  {
    myData.but_status = 1;
    new_data = 0;
  }


  esp_err_t result; // declaration

  // Send message via ESP-NOW
  if (new_data == 0)
  {

    result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
    new_data = 1;
  }

  if (result == ESP_OK)
  {
    Serial.println("Sent with success");
  }
  else
  {
    Serial.println("Error sending the data");
  }

  // delay(10000);
}

For finding MAC address


#include <WiFi.h>

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

void loop() {

}

Result

Multiple devices can be added to the project and can be triggered by other switches.

Group Assigment

our idea was to connect my bord running with ESP NOW Protocol with Arun’s with bord to contorl an LED

Master Code for 1st project


#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define but_on 19
#define but_off 18
#define LED 14


bool new_data = 0; // Flag to send data only once


// Universal MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

bool incomingLED_status;

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

// Create a struct_message called myData
struct_message myData;

struct_message incomingReadings;
// Create peer interface
esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}


void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  pinMode(but_on, INPUT_PULLUP);
  pinMode(but_off, INPUT_PULLUP);
  pinMode(LED, 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;
  }

  // Send Callback Function
  esp_now_register_send_cb(OnDataSent);



  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  //display.setFont(&FreeSerif9pt7b);
  display.clearDisplay();


  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  Serial.write(incomingLED_status);
  // Display static text
  display.println("For LED ON - BTN_1");
  display.println("For LED OFF - BTN_2");
  display.display();
}

void loop()
{
  // Set values to send
  if (digitalRead(but_on) == LOW && new_data == 1)
  {
    myData.but_status = 1;
    new_data = 0;
  }
  if (digitalRead(but_off) == LOW && new_data == 1)
  {
    myData.but_status = 0;
    new_data = 0;
  }

  esp_err_t result; // declaration

  // Send message via ESP-NOW
  if (new_data == 0)
  {

    result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
    new_data = 1;
  }

  if (result == ESP_OK)
  {
    Serial.println("Sent with success");
  }
  else
  {
    Serial.println("Error sending the data");
  }

  // delay(10000);
}

Slave Code for 1st project


#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

bool new_data = 0; // Flag to send data only once


// Universal MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

bool incomingLED_status;

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

// Create a struct_message called myData
struct_message myData;

struct_message incomingReadings;
// Create peer interface
esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  incomingLED_status = incomingReadings.but_status;
  if (incomingLED_status == 1 )
  {
    display.clearDisplay();

    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 20);
    Serial.write("1");
    // Display static text
    display.println("Turing LED ON");
    display.display();
  }
  else if (incomingLED_status == 0 )
  {
    display.clearDisplay(); \

    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 20);
    Serial.write("0");
    // Display static text
    display.println("Turing LED OFF");
    display.display();
  }

}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    return;
  }

  // Send Callback Function
  esp_now_register_send_cb(OnDataSent);

  // Receive Callback Function
  esp_now_register_recv_cb(OnDataRecv);

  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    return;
  }
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    for (;;);
  }
  delay(2000);
  display.setFont(&FreeSerif9pt7b);
  display.clearDisplay();
  display.display();
}

void loop()
{

}

to know more about reciving ends part visit Arun’s page

Result