Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

UDP and ESP8266

WiFi ESP8266 Broadcast

  • Please log in to reply
3 replies to this topic

#1 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 28 May 2015 - 04:38 PM

Been playing around with UDP broadcasting as a way of developing a low overhead sensor network.  The basic idea is that the sensors are not transferring any "secure" data, just simple stuff like temperature etc.  The sensors would broadcast the data over WiFi and so the data should be reasonably secure as the sensor need to be logged on to the WiFi network in the first place.

 

Speed is proving to be an issue though.  I'm getting random responses from UDP broadcasts, sometime data is received in 2 seconds, sometime 15 and other any mixture of responses in between.

 

C# code is:

using Microsoft.SPOT;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace NETMFUDPServer
{
    public class Program
    {
        public static void Main()
        {
            //
            //  Wait until we have an IP address.
            //
            while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
            {
                Thread.Sleep(10);
            }
            Debug.Print("IP Address: " + IPAddress.GetDefaultLocalAddress());

            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 11000);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(endPoint);
            while (true)
            {
                if (socket.Poll(1000, SelectMode.SelectRead))
                {
                    byte[] buffer = new byte[socket.Available];
                    int bytesRead = socket.ReceiveFrom(buffer, ref endPoint);

                    int length = buffer.Length;
                    char[] text = new char[length];

                    for (int i = 0; i < length; i++)
                    {
                        text[i] = (char) buffer[i];
                    }
                    Debug.Print("Message received from " + endPoint.ToString() + ": " + new string(text));
                }
            }
        }
    }
}

I know the ESP code is triggering every second as I have a console App on the PC which confirms that data is being transmitted every second.

 

Anyone with more experience with UDP who could help with optimisation or advise?  I know NETMF is a little slower than full .NET but the period between packets being picked up can be the best part of 15 seconds.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#2 HSven1611

HSven1611

    Member

  • Members
  • PipPip
  • 14 posts

Posted 29 May 2015 - 07:14 AM

Hi

 

I'm doing something like that:

  public UdpClient(int port)
        {
            LocalPort = port;
            try
            {
                Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                IPEndPoint broadcastEndPoint = new IPEndPoint(System.Net.IPAddress.Any, LocalPort);

                Socket.Bind(broadcastEndPoint);
            }
            catch
            {
                throw;
            }

            new Thread(new ThreadStart(threadStartTarget)).Start();
        }

        private void threadStartTarget()
        {
            try
            {
                EndPoint remoteEndPoint = new IPEndPoint(System.Net.IPAddress.Any, 0);

                while (true)
                {

                    int availableBytes = Socket.Available;
                    if (availableBytes > 0)
                    {
                        byte[] receiveBuffer = new byte[availableBytes];
                        Socket.ReceiveFrom(receiveBuffer, ref remoteEndPoint);

                        // My own data received Event
                        OnDataReceived(remoteEndPoint, receiveBuffer);                       
                        
                    }

                    // This is recommends in thread loops to help prevent lock-ups
                    Thread.Sleep(10);
                }
            }
            catch (Exception e)
            {
                // catch all exceptions
                Debug.Print(e.ToString());
            }
            finally
            {
                Debug.Print("UDP server thread finished.");
            }
        }

I remember having issues with that Poll() method too... Don't know how that thing should work.



#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 29 May 2015 - 08:50 AM

I have tried eliminating the code which converts the packet to a string and I'm sill getting long pauses between packets.

 

The Poll method does return quickly, it seems to be the ReceiveFrom method which is consuming the time.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 bghavami

bghavami

    New Member

  • Members
  • Pip
  • 2 posts

Posted 07 January 2016 - 04:33 PM

I hope you have solved the problem by now, but if you haven't yet here is the solution.

 

      private static int nPort = 12345;
      private static EndPoint EndPointRemote;
      private static int nBufSize = 1024;

      public void Start()
      {
         //
         //  Wait until we have an IP address.
         //
         while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
         {
               Thread.Sleep(10);
         }
         Debug.Print("IP Address: " + IPAddress.GetDefaultLocalAddress());

         EndPointRemote = new IPEndPoint(IPAddress.Any, nPort);
         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         socket.Bind(EndPointRemote);
         int bytesRead;
         byte[] buffer = new byte[nBufSize + 1];

         while (true)
         {
            if( (bytesRead = socket.ReceiveFrom(buffer, nBufSize, SocketFlags.None, ref EndPointRemote)) > 0 )
            {
               Debug.Print("Message received from " + EndPointRemote.ToString() + ": " + new string(System.Text.Encoding.UTF8.GetChars(buffer, 0, bytesRead)));
            }
            Thread.Sleep(0);
         }
      }

 

Let me know if this work for you.

 

Bizhan.







Also tagged with one or more of these keywords: WiFi, ESP8266, Broadcast

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.