Communication between two NodeMcu WIFI modules

Hello everybody,

I have got my two NodeMcu modules and I would like to send data between them. First module I set as AP and second as a client. Looks like that connects well but I have problem what to do next. Simply I want to send a short string sth like "hello" from one module and read it on second one.

Can anyone help how to do this?

AP code:

#include 


const char *ssid = "TEST";
const char *password = "grubychuj";



void setup() {
  
  delay(1000);
  Serial.begin(115200);
  Serial.println("siema");
  WiFi.softAP(ssid, password);
  pinMode(2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(1000);
  Serial.println("eloszka");

}

Client code:

#include 

const char *ssid = "TEST";
const char *password = "grubychuj";

void setup() {

  pinMode(2, OUTPUT);
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(1000);
  Serial.println("eloszka");

}

Simply I want to send a short string sth like "hello" from one module and read it on second one.

WiFi handles very structured communications without wires. "Sending something like hello" is NOT something that can happen. An access point is something that allows two other devices to communicate, through the access point.

Thank you for answer. I have seen example in lua where somebody makes two esp8266 to talk with each other. Is that "hello" able to do when I also creat a server on one of them?

Is that "hello" able to do when I also creat a server on one of them?

If one of them is a server, the other one (the client) can make a GET request. The GET request could include the word "hello".

Hi mrhyde17

You could try using UDP to send packets of data between the nodeMCUs.

There is an example program called WifiUdpSendReceiveString under File - Examples - Wifi in the Arduino IDE.

I use UDP to link two nodeMCUs running in client mode connected to a separate access point. I don't know for sure if it will work on the nodeMCU that you have set as an access point.

Regards

Ray

Hi Hackscribble,

Thanks for information. I have old router in my box so maybe thats really a point to do this. Could you share with me your codes which you put into yours nodemcu's?

Best Regards Hyde

The UDP code is part of much larger programs, so here are some extracts that may help. The code checks for received UDP packets on a defined port. If a received packet contains a defined string, the code takes some action.

First. I define configuration on the sender:

const uint16_t UDP_LOCAL_PORT =  8050;
const uint16_t UDP_REMOTE_PORT = 8051;
const char UDP_REMOTE_HOST[] =   "receiver";
char TRIGGER_STRING[] = "somestring";

I have the host name of the receiving nodeMCU ("receiver") configured on my home router. If you need to use an IP address, then you will need to change "receiver" to the IP address.

Configuration on the receiver:

const uint16_t UDP_LOCAL_PORT =  8051; // Must match UDP_REMOTE_PORT on sender
char TRIGGER_STRING[] = "somestring";  // Must match TRIGGER_STRING on sender

On both devices, I declare a global instance of the UDP class:

#include 
WiFiUDP UDP;

UDP is started by this code in setup() on both devices:

Serial.println("PROGRAM: starting UDP");
if (UDP.begin(UDP_LOCAL_PORT) == 1)
{
  Serial.println("PROGRAM: UDP started");
}
else
{
  Serial.println("PROGRAM: UDP not started");
}

On the sender, I use this code to send the trigger string to the receiver:

Serial.println("PROGRAM: sending trigger");
UDP.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
UDP.write(TRIGGER_STRING);
UDP.endPacket();

On the receiver, this code checks received UDP packets to see if they contain the trigger string:

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
int packetSize = UDP.parsePacket();
if (packetSize > 0)
{
  Serial.print("PROGRAM: received UDP packet of size ");
  Serial.println(packetSize);
  Serial.print("PROGRAM: from ");
  IPAddress remote = UDP.remoteIP();
  for (int i = 0; i < 4; i++)
  {
    Serial.print(remote[i], DEC);
    if (i < 3)
    {
      Serial.print(".");
    }
  }
  Serial.print(", port ");
  Serial.println(UDP.remotePort());
  UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  Serial.print("PROGRAM: contents: ");
  Serial.println(packetBuffer);
  if (strncmp(packetBuffer, TRIGGER_STRING, strlen(TRIGGER_STRING)) == 0)
  {
    Serial.println("PROGRAM: trigger received");
    // DO YOUR STUFF HERE
  }
  delay(10);
}

Thanj you so much for sharing your code. I made a test to check if it sends that text to antoher one but unfortunately something is wrong. If you find a while please take a look at that codes and scan form serial monitor:

sender code:

#include 
#include 

WiFiUDP UDP;


const char *ssid = "mrhyde17";
const char *password = "abc89";

const uint16_t UDP_LOCAL_PORT =  8050;
const uint16_t UDP_REMOTE_PORT = 8051;
const char UDP_REMOTE_HOST[] =   "receiver";
char TRIGGER_STRING[] = "somestring";


void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


Serial.println("PROGRAM: starting UDP");
if (UDP.begin(UDP_LOCAL_PORT) == 1)
{
  Serial.println("PROGRAM: UDP started");
}
else
{
  Serial.println("PROGRAM: UDP not started");
}

  
}

void loop() {
  // put your main code here, to run repeatedly:
pinMode(2, LOW);
delay(1000);
Serial.println("PROGRAM: sending trigger");
UDP.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
UDP.write(TRIGGER_STRING);
UDP.endPacket();

pinMode(2, HIGH);
delay(1000);

}

receiver code:

#include 
#include 

WiFiUDP UDP;


const char *ssid = "mrhyde17";
const char *password = "abc89";


const uint16_t UDP_LOCAL_PORT =  8051; // Must match UDP_REMOTE_PORT on sender
char TRIGGER_STRING[] = "somestring";  // Must match TRIGGER_STRING on sender

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


Serial.println("PROGRAM: starting UDP");
if (UDP.begin(UDP_LOCAL_PORT) == 1)
{
  Serial.println("PROGRAM: UDP started");
}
else
{
  Serial.println("PROGRAM: UDP not started");
}

}

void loop() {
  // put your main code here, to run repeatedly:
  char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
int packetSize = UDP.parsePacket();
if (packetSize > 0)
{
  Serial.print("PROGRAM: received UDP packet of size ");
  Serial.println(packetSize);
  Serial.print("PROGRAM: from ");
  IPAddress remote = UDP.remoteIP();
  for (int i = 0; i < 4; i++)
  {
    Serial.print(remote[i], DEC);
    if (i < 3)
    {
      Serial.print(".");
    }
  }
  Serial.print(", port ");
  Serial.println(UDP.remotePort());
  UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  Serial.print("PROGRAM: contents: ");
  Serial.println(packetBuffer);
  if (strncmp(packetBuffer, TRIGGER_STRING, strlen(TRIGGER_STRING)) == 0)
  {
    Serial.println("PROGRAM: trigger received");
    // DO YOUR STUFF HERE
  }
  delay(10);
}

}

monitor:

Connecting to mrhyde17
..
WiFi connected
IP address: 
192.168.43.35
PROGRAM: starting UDP
PROGRAM: UDP started

On the sender, you need to configure the host name or IP address for the receiver. This still has the host name I use; you probably need to change it to the IP address of your receiver.

const char UDP_REMOTE_HOST[] =   "receiver";

Is the serial monitor output you posted from the sender or the receiver?

I have corrected the ip and looks like that esp's started to talk but i also get a gabage.

receiver monitor:

PROGRAM: received UDP packet of size 10
PROGRAM: from 192.168.43.151, port 8050
PROGRAM: contents: somestringöRk'ô�¸
PROGRAM: trigger received

Do you know how to get rid them?

I think the UDP library sends the string without a terminating null character. When you use Serial.print(), this expects a null to mark the end of the string, so it carries on past the received data and prints out whatever is in memory until it happens to hit a null.

Try adding this before you print:

packetBuffer[packetSize] = '\0';

That is a zero character after the .

Hi,

I've been working in a library to give a solution for this situation. Please have a look to this link.

http://abetoo.com/wiki/knowledge-base/arduino_send_messages

The schema is very simple, you only need a channel id for any single device connected, and then you only have to send a message to the device you want to communicate with, just using the channel id as identifier.

Am a doing a college project Rfid tracker using Nodemcu v3. i am using 2 rfid readers and 2 nodemcu. I want the datas of the 2 rfid to be a posted on a single web server. each nodemcu creates an ip address where my data transfered on the server. each data is on a new ip address. i want the data of the 2 nodemcu to be posted on one server. is that possible . here is my code 1st rfid with nodemcu code

include //RFID Library

include // Serial Protocol Interface

include

include // Sets up a server

define SS_PIN 15 // Slave Select pin (D8)

define RST_PIN 5 // Reset Pin (D1)

MFRC522 mfrc522(SS_PIN, RST_PIN); ESP8266WebServer server;

char* ssid = "Oppo a57"; // Mobile Hotspot name char* password = "12345678@"; // Connect to the Wifi network

void setup() {

Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 Serial.println("Approximate your card to the reader..."); // Prints on new line Serial.println();

WiFi.begin(ssid,password);

while(WiFi.status()!=WL_CONNECTED) // Wait for connection { Serial.print("."); delay(100); } Serial.println(""); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); //Print the local IP Address

server.begin(); // Start the server }

void loop() { server.handleClient(); //Handle incoming https requests if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } Serial.print("UID tag :"); String content= ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) // mfrc522.uid.size --> PRINTS UID SIZE { Serial.print(mfrc522.uid.uidByte < 0x10 ? " 0" : " "); // mfrc522.uid.uidByte --> PRINTS BYTES OF UID _ Serial.print(mfrc522.uid.uidByte*, HEX); content.concat(String(mfrc522.uid.uidByte *< 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte*, HEX)); } Serial.println(); //Serial.print("Message : ");*_

* content.toUpperCase();*

if (content.substring(1) == "E0 5D 12 A4" ) // Compares the string { * Serial.println("Dr.Ayesha entered in Room 1");* * server.on("/",{server.send(200,"text/plain","Dr.Ayesha entered in Room 1");}); // Posted on the web server* }

/*else if ( content.substring(1) == "3E 0E AB 59" ) * {* * Serial.println("Dr.Santosh entered in Room 1" );* * server.on("/",{server.send(200,"text/plain","Dr.Zenul entered in Room 1");});* _ }/ *else if ( content.substring(1) == "50 DD 20 A4" ) * { Serial.println("Dr.Zenul entered in Room 1" ); *server.on("/",{server.send(200,"text/plain"," Dr.Zenul entered in Room 1");}); * }*_

else * {* * Serial.println("Invalid");* * }* delay(3000); } 2nd rfid with nodemcu code #include //RFID Library #include // Serial Protocol Interface #include #include // Sets up a server #define SS_PIN 15 // Slave Select pin (D8) #define RST_PIN 5 // Reset Pin (D1) MFRC522 mfrc522(SS_PIN, RST_PIN); ESP8266WebServer server; char* ssid = "Oppo a57"; // Mobile Hotspot name char* password = "12345678@"; // Connect to the Wifi network void setup() {

* Serial.begin(9600); // Initiate a serial communication* * SPI.begin(); // Initiate SPI bus* * mfrc522.PCD_Init(); // Initiate MFRC522* * Serial.println("Approximate your card to the reader..."); // Prints on new line* * Serial.println();*

* WiFi.begin(ssid,password);*

* while(WiFi.status()!=WL_CONNECTED) // Wait for connection* * {* * Serial.print(".");* * delay(100);* * }* * Serial.println("");* * Serial.print("IP Address: ");* * Serial.println(WiFi.localIP()); //Print the local IP Address* server.begin(); // Start the server } void loop() { server.handleClient(); //Handle incoming https requests * if ( ! mfrc522.PICC_IsNewCardPresent()) _ { return; } // Select one of the cards* * if ( ! mfrc522.PICC_ReadCardSerial())* * { return; } Serial.print("UID tag :"); String content= ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) // mfrc522.uid.size --> PRINTS UID SIZE* * {_ Serial.print(mfrc522.uid.uidByte _< 0x10 ? " 0" : " "); // mfrc522.uid.uidByte *--> PRINTS BYTES OF UID Serial.print(mfrc522.uid.uidByte*, HEX); content.concat(String(mfrc522.uid.uidByte *< 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte*, HEX)); } Serial.println(); //Serial.print("Message : ");*_

* content.toUpperCase();*

if (content.substring(1) == "E0 5D 12 A4" ) // Compares the string { * Serial.println("Dr.Ayesha entered in Room 2");* * server.on("/",{server.send(200,"text/plain"," Dr.Ayesha entered in Room 2");}); // Posted on the web server* }

/*else if ( content.substring(1) == "3E 0E AB 59" ) * {* * Serial.println("Operation Theatre" );* * server.on("/",{server.send(200,"text/plain","Operation Theatre");});* _ }/ *else if ( content.substring(1) == "50 DD 20 A4" ) * { Serial.println("Dr.Zenul entered in Room 1" ); *server.on("/",{server.send(200,"text/plain","Dr.Zenul entered in Room 1");}); * }*_

else * {* * Serial.println("Invalid ");* * }* delay(3000); } Please help me out by getting both the datas on a server