-->
Page 3 of 6

Re: UDP Server Example

PostPosted: Mon May 04, 2015 8:14 am
by bernd331
Grob, I tried a lot of copying updates librarys. But nothing worked. So, as I described in 2 posts earlyer, I compiled the Arduino IDE new. Then all works fined. But gerardwr postet also a working solution.
What I have seen at my Computer was, that a complete uninstallation of the Arduino IDE is a bit fiddeling. Uninstall Arduino IDE, cleanup "documents" and cleanup "appdata". I had a new installed Laptop (Win10), so for me it was easy to begin from scratch.

Gerardwr, ;) I used Broadcast. Not Multicast. See here from the Internet:
------------------------------------------------------------
| TYPE | ASSOCIATIONS | SCOPE | EXAMPLE |
------------------------------------------------------------
| Unicast | 1 to 1 | Whole network | HTTP |
------------------------------------------------------------
| Broadcast | 1 to Many | Subnet | ARP |
------------------------------------------------------------
| Multicast | One/Many to Many | Defined horizon | SLP |
------------------------------------------------------------
| Anycast | Many to Few | Whole network | 6to4 |
------------------------------------------------------------


Unicast is used when two network nodes need to talk to each other. This is pretty straight forward, so I'm not going to spend much time on it. TCP by definition is a Unicast protocol, except when there is Anycast involved (more on that below).

When you need to have more than two nodes see the traffic, you have options.

If all of the nodes are on the same subnet, then broadcast becomes a viable solution. All nodes on the subnet will see all traffic. There is no TCP-like connection state maintained. Broadcast is a layer 2 feature in the Ethernet protocol, and also a layer 3 feature in IPv4.

Multicast is like a broadcast that can cross subnets, but unlike broadcast does not touch all nodes. Nodes have to subscribe to a multicast group to receive information. Multicast protocols are usually UDP protocols, since by definition no connection-state can be maintained. Nodes transmitting data to a multicast group do not know what nodes are receiving. By default, Internet routers do not pass Multicast traffic. For internal use, though, it is perfectly allowed; thus, "Defined horizon" in the above chart. Multicast is a layer 3 feature of IPv4 & IPv6.

To use anycast you advertise the same network in multiple spots of the Internet, and rely on shortest-path calculations to funnel clients to your multiple locations. As far the network nodes themselves are concerned, they're using a unicast connection to talk to your anycasted nodes. For more on Anycast, try: What is "anycast" and how is it helpful?. Anycast is also a layer 3 feature, but is a function of how route-coalescing happens.

To test, I use "Hercules Setup Utility by HW-Group". Nice tool for TCP, UDP and Serial under Windows. Luckily I have 4 Computers at home, so I can test Broadcasting.


Greets, Bernd

Re: UDP Server Example

PostPosted: Sun Jun 28, 2015 9:26 am
by mohfaza
Hi dear
hope to be good
well done you did a good job
I have a question about your job
you had said that you send UDP packet to 192.168.0.255
i want to know if it is ESP IP or some thing else IP
thank you

bernd331 wrote:Hi,

thanks for the answers. But finaly I compiled my "Arduino for ESP8266" by myself. It was suprisingly easy with this help:
viewtopic.php?f=26&t=2397
As Byozayturay in his post said: Takes a Long time. Round about an hour. But now all works fine under Windows 10 insider preview 10074 64Bit.

Maybe this code helps others. It's a UDP script which sends back "answer" to the Sender.

Also, when the ESP8266 starts, it broadcasts it's Chip-ID and IP-Address.

Code: Select all/*
 * 31 mar 2015
 * This sketch display UDP packets coming from an UDP client.
 * On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
 *
 * Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
 *
 */

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

extern "C"
{
  #include "user_interface.h" 
}

int status = WL_IDLE_STATUS;
const char* ssid = "UPC0864334";  //  your network SSID (name)
const char* pass = "secret";   // your network password

unsigned int localPort = 2390;    // local port to listen for UDP packets

IPAddress ipMulti (192,168,0,255);

byte packetBuffer[512]; //buffer to hold incoming and outgoing packets

WiFiUDP Udp;  // A UDP instance to let us send and receive packets over UDP

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // setting up Station AP
  WiFi.begin(ssid, pass);
 
  // 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("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);
  multicas_at_start_esp8266();
}

void loop()
{
  int noBytes = Udp.parsePacket();
  if ( noBytes ) {
    Serial.print(millis() / 1000);
    Serial.print(":Packet of ");
    Serial.print(noBytes);
    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,noBytes); // read the packet into the buffer

    // display the packet contents in HEX
    for (int i=1;i<=noBytes;i++){
      Serial.print(packetBuffer[i-1],HEX);
      if (i % 32 == 0){
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();
//    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Send answer to Packet-Sender
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Send Answer to Network (Broadcast)   
    Udp.write("answer");
    Udp.endPacket();
  } // end if
}

void multicas_at_start_esp8266() //When ESP8266 starts, Broadcast presence of ESP8255
{
  Udp.beginPacket(ipMulti, 2390);   
  Udp.write("ESP8266 ID#");
  Udp.print(system_get_chip_id());
  Udp.write("#ESP8266 IP#");
  Udp.println(WiFi.localIP());
  //Udp.writeln("answer");
  Udp.endPacket();
 
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}


To send a UDP broadcast, I simply send the UDP-Packet to the IP 192.168.0.255. My subnetmask is 255.255.255.0.

To test, I use "Hercules Setup Utility by HW-Group". Nice tool for TCP, UDP and Serial under Windows.

Greetz, Bernd

Re: UDP Server Example

PostPosted: Sun Feb 07, 2016 2:01 pm
by rezjat
gerardwr wrote:Please find attached a working UDP Server Example.

The picture below shows (on the right) the use of a Mac as UDP client sending messages, and the serial output of the UDP Server sketch on the ESP (on the left).



Hi, thanks for sharing! I am trying to use your programm but I get compilation errors due to missing queue.h, memory, etc... Which libraries did you use to get it compiled and working?

Thanks a lot.
Best regards.

Re: UDP Server Example

PostPosted: Mon Feb 08, 2016 8:04 am
by gerardwr
rezjat wrote:
gerardwr wrote:Please find attached a working UDP Server Example.

The picture below shows (on the right) the use of a Mac as UDP client sending messages, and the serial output of the UDP Server sketch on the ESP (on the left).



Hi, thanks for sharing! I am trying to use your programm but I get compilation errors due to missing queue.h, memory, etc... Which libraries did you use to get it compiled and working?

Thanks a lot.
Best regards.


The sketch is nearly 1 year old, so it was an early version of the Arduino ESP8266 environment from here:
[url]
https://github.com/esp8266/Arduino
[/url]

For your pleasure I just compiled it under Arduino 1.6.7 and the latest Stable version 2.0.0.

Still works OK, so the problem seems to be caused by your environment. I use the "Installing with Boards Manager" section on the linked site, but using Arduino 1.6.7.

Please note that for Arduino 1.6.7. the function "printWifiStatus" needs to be moved ABOVE the "Setup" function, for older Arduino versions (f.i. 1.6.4) thats not required. Arduino 1.6.7 is more strict :-(


Success!