Multiple Nodemcu(esp8266) Communication

Introduction: Multiple Nodemcu(esp8266) Communication

In this project we collect two transmitter data in mediator trans-receiver and send that data to receiver

//Data transmitter_1

#include

#if defined(ESP8266)

#include

#elifdefined(ESP32)

#include

#endif

char v[255];

char w[255];

static uint8_t PEER[] {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // put mac address of your trans-receiver

float a=0;

voidprintReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {

Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

}

Serial.println();

}

void setup() {

Serial.begin(115200);

Serial.println();

WiFi.persistent(false);

WiFi.mode(WIFI_AP);

WiFi.softAP("ESPNOW", nullptr, 3);

WiFi.softAPdisconnect(false);

Serial.print("MAC address of this node is ");

Serial.println(WiFi.softAPmacAddress());

bool ok = WifiEspNow.begin();

if (!ok) {

Serial.println("WifiEspNow.begin() failed");

ESP.restart();

}

WifiEspNow.onReceive(printReceivedMessage, nullptr);

ok = WifiEspNow.addPeer(PEER);

if (!ok) {

Serial.println("WifiEspNow.addPeer() failed");

ESP.restart();

}

}

void loop()

{

charmsg[60];

a=a+0.1; //data to be transmitted

Intlen= snprintf(msg, sizeof(msg), "%f", a);

WifiEspNow.send(PEER, reinterpret_cast(msg), len);

delay(1000);

}

// DATA transmitter_2

#include

#if defined(ESP8266)

#include

#elifdefined(ESP32)

#include

#endif

//const uint8_t* buf;

char v[255];

char w[255];

static uint8_t PEER[] {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // put mac address of your trans-receiver

float a=0;

voidprintReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {

Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

}

Serial.println();

}

void setup() {

Serial.begin(115200);

Serial.println();

WiFi.persistent(false);

WiFi.mode(WIFI_AP);

WiFi.softAP("ESPNOW", nullptr, 3);

WiFi.softAPdisconnect(false);

Serial.print("MAC address of this node is ");

Serial.println(WiFi.softAPmacAddress());

bool ok = WifiEspNow.begin();

if (!ok) {

Serial.println("WifiEspNow.begin() failed");

ESP.restart();

}

WifiEspNow.onReceive(printReceivedMessage, nullptr);

ok = WifiEspNow.addPeer(PEER);

if (!ok) {

Serial.println("WifiEspNow.addPeer() failed");

ESP.restart();

}

}

void loop()

{

charmsg[60];

a=a+3.1;

Int len= snprintf(msg, sizeof(msg), "%f", a);

WifiEspNow.send(PEER, reinterpret_cast(msg), len);

delay(1010);

}

// DATA Trans_receiver

#include

#if defined(ESP8266)

#include

#elifdefined(ESP32)

#include

#endif

char v[255];

char w[255];

static uint8_t PEER[] {0xWW, 0xWW, 0xWW, 0xWW, 0xWW, 0xWW}; //put your receiver mac address

//3E:71:BF:A6:AA:AE

//EE:FA:BC:BA:3D:AA

float a=0;

voidprintReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {

Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

if(mac[0]== 0x3E) // 3E:71:BF:A6:AA:AE it is mac address of my transmitter nodemcu_1

{

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

v[i]=buf[i];

}

}

if(mac[0]== 0x EE) // EE:FA:BC:BA:3D:AA it is a mac address of my transmitter nodemcu_2

{

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

w[i]=buf[i];

}

}

Serial.println();

}

void setup() {

Serial.begin(115200);

Serial.println();

WiFi.persistent(false);

WiFi.mode(WIFI_AP);

WiFi.softAP("ESPNOW", nullptr, 3);

WiFi.softAPdisconnect(false);

Serial.print("MAC address of this node is ");

Serial.println(WiFi.softAPmacAddress());

bool ok = WifiEspNow.begin();

if (!ok) {

Serial.println("WifiEspNow.begin() failed");

ESP.restart();

}

WifiEspNow.onReceive(printReceivedMessage, nullptr);

ok = WifiEspNow.addPeer(PEER);

if (!ok) {

Serial.println("WifiEspNow.addPeer() failed");

ESP.restart();

}

}

void loop()

{

delay(1000);

char msg[60];

Int len = snprintf(msg, sizeof(msg), "%s+nodemcu_1", v);

WifiEspNow.send(PEER, reinterpret_cast(msg), len);

delay(10);

msg[60];

len = snprintf(msg, sizeof(msg), "%s+ nodemcu_2", w);

WifiEspNow.send(PEER, reinterpret_cast(msg), len);

}

// DATA receiver

#include

#if defined(ESP8266)

#include

#elifdefined(ESP32)

#include

#endif

char v[255];

char w[255];

static uint8_t PEER[] {0xWW, 0xWW, 0xWW, 0xWW, 0xWW, 0xWW}; //put mac address your receiver

float a=0;

voidprintReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg)

{

Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

v[i]=buf[i];

}

Serial.println();

}

void setup() {

Serial.begin(115200);

Serial.println();

WiFi.persistent(false);

WiFi.mode(WIFI_AP);

WiFi.softAP("ESPNOW", nullptr, 3);

WiFi.softAPdisconnect(false);

Serial.print("MAC address of this node is ");

Serial.println(WiFi.softAPmacAddress());

bool ok = WifiEspNow.begin();

if (!ok) {

Serial.println("WifiEspNow.begin() failed");

ESP.restart();

}

WifiEspNow.onReceive(printReceivedMessage, nullptr);

ok = WifiEspNow.addPeer(PEER);

if (!ok) {

Serial.println("WifiEspNow.addPeer() failed");

ESP.restart();

}

}

void loop()

{

}

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The First Transmitter(nodemcu_1) Code

//Data transmitter_1

#include

#if defined(ESP8266)

#include

#elifdefined(ESP32)

#include

#endif

char v[255];

char w[255];

static uint8_t PEER[] {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // put mac address of your trans-receiver

float a=0;

voidprintReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {

Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

for (int i = 0; i < count; ++i) {

Serial.print(static_cast(buf[i]));

}

Serial.println();

}

void setup() {

Serial.begin(115200);

Serial.println();

WiFi.persistent(false);

WiFi.mode(WIFI_AP);

WiFi.softAP("ESPNOW", nullptr, 3);

WiFi.softAPdisconnect(false);

Serial.print("MAC address of this node is ");

Serial.println(WiFi.softAPmacAddress());

bool ok = WifiEspNow.begin();

if (!ok) {

Serial.println("WifiEspNow.begin() failed");

ESP.restart();

}

WifiEspNow.onReceive(printReceivedMessage, nullptr);

ok = WifiEspNow.addPeer(PEER);

if (!ok) {

Serial.println("WifiEspNow.addPeer() failed");

ESP.restart();

}

}

void loop()

{

charmsg[60];

a=a+0.1; //data to be transmitted

Intlen= snprintf(msg, sizeof(msg), "%f", a);

WifiEspNow.send(PEER, reinterpret_cast(msg), len);

delay(1000);

}

Step 2:

Be the First to Share

    Recommendations

    • LED Strip Speed Challenge

      LED Strip Speed Challenge
    • Sculpting Challenge

      Sculpting Challenge
    • Clocks Contest

      Clocks Contest

    Discussions