ESP8266 - Send String between two NodeMCU under SAME WiFi

Hi :slight_smile:

I'm making a project with 7 NodeMCU.

Each NodeMCU (ESP8266) will have a static IP Address, like 10.6.1.XX.

Each NodeMCU (ESP8266) will act as a Server and not as a Client (I'll have to handle some HTML GET and POST function), and just One of them will send String data to the others.

Simply, on those who will recieve the string value, there will be something like this:

if (string = VALUE) {
    do Thing A
}
else {
    do Thing C
}

Is there a Library to send data or it must be constructed in the program?
Knowing the IP address of each one, is there a way to say something like:

Send STRING to 10.6.1.XX ?

Thanks in advance for any response! :slight_smile:

do you use the ESP8266WebServer library?

Juraj:
do you use the ESP8266WebServer library?

Yes :slight_smile:

#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <stdlib.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <NTPClient.h>
#include <Servo.h>
#include <DFPlayer.h>

then add an server.on() handle to receive the String

server.on("/api", handleApi)

void handleApi() {
String command = server.arg("command");
...

in client send a http request as "/api?command=" + theString

see WiFiClient example or use the HttpClient library

Can I do something like:

String Command = "AAA";
WBServer.arg("ArgCommand") = Command;

Shalla:
Can I do something like:

String Command = "AAA";
WBServer.arg("ArgCommand") = Command;

you send a request with a Client, not with Server

I think Juraj covered it already, but just in case, the 6 nodeMCU's run ESPwebserver, and the one is acting as a client. oh and eh.. this

if (string = VALUE) {
    do Thing A
}
else {
    do Thing C
}

must be a typo ("==")

I had to make a change to the project, now all the nodeMCUs will send and receive strings.

Do I still have to use the same method?
However, they will all be connected to an external WiFi

What's the easiest way to get them talking to each other?

Shalla:
I had to make a change to the project, now all the nodeMCUs will send and receive strings.

Do I still have to use the same method?
However, they will all be connected to an external WiFi

What's the easiest way to get them talking to each other?

will they still run a WebServer?

Juraj:
will they still run a WebServer?

Yes

I think you will be better of using UDP packet

Never used UDP packets,I started reading something about it only today.

The things I need to do are:

From just a single NodeMCU, send a different packet to each other NodeMCU.

In those who receive it (if the packet corresponds to a specific string, N.B. string will be different in each receiving NodeMCU for securuty reasons) I’ll activate a trigger.
Here, I did not understand if I will have to set a static IP to every NodeMCU and send packet to a specific IP or if I can differentiate the UDP packets through their content

Also, on the others NodeMCU, i need to read a sensor distance, and if the distance is > or < of X, I’ll send a UDP packet the the single NodeMCU hosting a Web Server with a HTML page on it.

Can I ask your help with the code?

Actually, the packets from the NodeMCU hosting the WebSite will be activated by this trigger:

void setup() {
  WBServer.on("/6A1/chiudi", seiauno);
  }

void seiauno() {
  String s = "";
  s += "<!DOCTYPE html><html>";
  s += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  s += "<link rel=\"icon\" href=\"data:,\">";
  s += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
  s += ".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;";
  s += "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
  s += "</style></head>";
  s += "<body><h1>
Comando di chiusura inviato alla porta 6A1.

Attendere qualche secondo.</h1>";
  s += "<p><a href=\"/\"><button class=\"button\">Torna alla pagina iniziale</button></a></p>";
  WBServer.send(200, "text/html", s);  

  Here i Need to send the UDP packet to a specific NodeMCU called 6A1

  }

there will be a void for each other NodeMCU with a specific UDP packet

Here, I did not understand if I will have to set a static IP to every NodeMCU and send packet to a specific IP or if I can differentiate the UDP packets through their content

I have never done anything with UDP packets, but from what i know you have to send the packet to a specific destination. I would use the content to verify it's validity. You can ask for help but i am a busy man.

Deva_Rishi:
I think you will be better of using UDP packet

why?

Juraj:
why?

She wants to send & receive a String, that is what UDP-packet is meant for. (in fact the UDP example is called UDPSendReceiveString) Unfortunately i did not find the specific example under ESPwifi, but there is one under the general WIFI and there is one under Ethernet(esp8266) shouldn’t be to difficult to create one that that works. But on the ESP forum a correct example was posted. I would post a link but that would mean you would have to create an account before you could download the code (and somehow my F-Secure complained when i did that so there is a warning about that forum.) but i’ll post it here

// Ref : http://www.esp8266.com/viewtopic.php?f=28&t=2295&p=13730#p13730

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUDP port;

char packetBuffer[255];
unsigned int localPort = 9999;


void setup() {
  Serial.begin(115200);
  WiFi.begin("yourssid", "yourpassword");
  port.begin(localPort);
}

void loop() {
  int packetSize = port.parsePacket();
  // Serial.println(packetSize);
  if (packetSize) {
    int len = port.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len-1] = 0;
    Serial.println(packetBuffer);
    port.beginPacket(port.remoteIP(),port.remotePort());
    port.write("Your UDP packet was received OK\r\n");
    port.endPacket();
  }
  delay(500);
}

Anyhow, my philosophy is 'if you want a wheel, use a wheel, if you want a steering wheel use a steering wheel. sure a normal wheel may also work, but a steering wheel is meant for that purpose. (and your car will look less like a Flintstone car)
On another note, are you sure you just want to login to the page on the ‘main’ nodeMCU, or would it be even better to control the whole thing with an APP on your phone ? (That would require a whole different programming language of course and a whole new IDE and loads of other things, but it would look even more slick in the end.)

@Deva_Rishi,

  1. UDP doesn’t guarantee the delivery
  2. they have already a TCP server started (web server)
  3. the esp8266 webserver will handle the request and hand them the string

Juraj:
@Deva_Rishi,

  1. UDP doesn't guarantee the delivery

Neither does the webserver, both can wait for a response.

Deva_Rishi:
Neither does the webserver, both can wait for a response.

lookup the difference between UDP and TCP

http://www.tcpipguide.com/free/t_UDPOperation.htm

So how can I manage to use the WiFi or the HTTP Library to manage strings in both ways?

Shalla:
So how can I manage to use the WiFi or the HTTP Library to manage strings in both ways?

comment #3 is still valid