- Sat Mar 24, 2018 10:02 pm
#74891
Any way to make two or more esp communicate with mdns or similar technique? No computer involved in the set up so no bonjour. Strictly esp talking to each other over home Wi-Fi.
I have looked at this. I have not worked things out yet but it is what I had planned for my system. This is the way I think I will go about it.
I will use mDNS as a way to discover devices and then make a list of devices and their IP addresses. After that I will use unicast to communicate to devices on the list.
mDNS uses multicast and from my tests I don't always see a response to a discovery request.
What you should try is the mDNS-SD_Extended.ino example on one ESP and the mDNS_Web_Server.ino example on one or more ESPs. Make sure that you have the query request have matching setups in the other devices.
Code: Select allint n = MDNS.queryService("esp", "tcp"); // Send out query for [u]esp[/u] [u]tcp[/u] services
That is in the mDNS-SD_Extended.ino example. Make sure that the two fields match what you have in the other devices. The mDNS_webserver example has the ESP set for the following, and it will not match the above query.
Code: Select all // Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
I'm also going to throw this out there. It is code that displays the packets that an ESP hears on the multicast address and port used by mDNS. I use this rather than wireshark when I just want to see real time traffic in a terminal window.
Code: Select all// Displays mDNS packets on multicast - 224.0.0.251:5353
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
int status = WL_IDLE_STATUS;
const char* ssid = "MTS61752";
const char* password = "6900979039";
const char* WiFi_hostname = "esp27.107";
unsigned int localPort = 12345; // local port to listen for UDP packets
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
//////////////////////////////////////////////////////////////////////
// A UDP instance to let us send and receive packets over UDP
//////////////////////////////////////////////////////////////////////
WiFiUDP Udp;
// Multicast declarations
IPAddress ipMulti(224, 0, 0, 251);
unsigned int portMulti = 5353; // local port to listen on
//=====================================================
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.println("");
Serial.println("mDNS-display.ino");
Serial.print("Udp Multicast mDNS listener");
Serial.print("");
WiFi.hostname(WiFi_hostname);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30) {
break;
}
}
Serial.println();
printWifiStatus();
Serial.println("Connected to wifi");
Serial.print("");
Serial.print("Udp Multicast mDNS listener at : ");
Serial.print(ipMulti);
Serial.print(":");
Serial.println(portMulti);
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
Serial.print("");
}
//=====================================================
void loop()
{
int numBytes = Udp.parsePacket(); // number of bytes
if ( numBytes ) // if > 0
{
/////// UDP packet can be a multicast packet or specific to this device's own IP
Serial.print(millis() / 1000);
Serial.print("seconds :Packet of ");
Serial.print(numBytes);
Serial.print(" received from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
//////////////////////////////////////////////////////////////////////
// We've received a packet, read the data from it
//////////////////////////////////////////////////////////////////////
Udp.read(packetBuffer, numBytes); // read the packet into the buffer
// display the packet contents in HEX
for (int i = 1; i <= numBytes; i++) {
if (packetBuffer[i - 1] < 16) {
Serial.print("0"); // leading 0 if < 10h
}
Serial.print(packetBuffer[i - 1], HEX);
if (i % 32 == 0) {
Serial.println();
}
else Serial.print(' ');
} // end for
Serial.println();
Serial.println("--------------------------");
//////////////////////////////////////////////////////////////////////
// send a reply, to the IP address and port that sent us the packet we received
// the receipient will get a packet with this device's specific IP and port
//////////////////////////////////////////////////////////////////////
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
//Udp.write("My ChipId:");
//Udp.print(ESP.getChipId());
//Udp.endPacket();
// Serial.println(ESP.getChipId()); //??? why
} // end if
delay(20);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}