Go Down

Topic: Communication between two NodeMcu WIFI modules (Read 2713 times) previous topic - next topic

mrhyde17

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:
Code: [Select]
#include <ESP8266WiFi.h>


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:
Code: [Select]
#include <ESP8266WiFi.h>

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");

}

 

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

mrhyde17

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?

PaulS

Quote
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".
The art of getting good answers lies in asking good questions.

Hackscribble

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
Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

mrhyde17

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

Hackscribble

#6
May 19, 2016, 01:34 pm Last Edit: May 19, 2016, 01:43 pm by Hackscribble
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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
#include <WiFiUdp.h>
WiFiUDP UDP;


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

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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);
}


Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

mrhyde17

#7
May 19, 2016, 05:16 pm Last Edit: May 19, 2016, 05:27 pm by mrhyde17
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:
Code: [Select]
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

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:
Code: [Select]
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

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:
Code: [Select]
Connecting to mrhyde17
..
WiFi connected
IP address:
192.168.43.35
PROGRAM: starting UDP
PROGRAM: UDP started

Hackscribble

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.

Code: [Select]
const char UDP_REMOTE_HOST[] =   "receiver";

Is the serial monitor output you posted from the sender or the receiver?
Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

mrhyde17

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

receiver monitor:
Code: [Select]
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?

Hackscribble

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:

Code: [Select]
packetBuffer[packetSize] = '\0';

That is a zero character after the \.


Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

Go Up