Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Auto-find brick via wifi #3

Open
BrianPeek opened this issue Aug 30, 2015 · 1 comment
Open

Auto-find brick via wifi #3

BrianPeek opened this issue Aug 30, 2015 · 1 comment

Comments

@BrianPeek
Copy link
Owner

This issue was imported from CodePlex

peekb wrote 2013-11-24 at 23:46
Hello, great job with this new EV3 API. I just made a small start project using the API but I have a small question. I used:

_brick = new Brick(new NetworkCommunication("192.168.178.19")); to make a wifi connection. It works but is there a way to get the used IP adres automaticly?

I looked at the sample but I can not understand how it works...... (not enough C# skills yet!)

greeting from Holland André

nuwans wrote 2013-12-01 at 13:27
Hello, this API has not implemented a way to get the ip address automatically yet. But I found a way to do that using some documentation found in monobrick web site. Basically, EV3 broadcast an identification UDP message on port 3015. This happens every
few seconds after it connects to the router. It has the string called "EV3" in it. So listen to this message on this port using UDP and then if the message contains the string "EV3" get the ip address from the receiving message. This is
what I do now until that functionality is integrated to the API. I guess there is a similar functionality to discover the Bluetooth port instead we having to provide it to the application.

The code I have is pretty simple

private const int listenPort = 3015;

    private static void StartListener()
    {
        bool done = false;

        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                byte[] bytes = listener.Receive(ref groupEP);
                Console.WriteLine("Received broadcast from {0} :\n {1}\n", groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length));

                if (Encoding.ASCII.GetString(bytes, 0, bytes.Length).Contains("EV3"))
                {
                    Console.WriteLine("EV3 IP="+groupEP.Address.ToString());

                }

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
@UnderDev
Copy link

UnderDev commented Mar 7, 2017

Universal Windows Platform Solution

Hi, Im currently using this API as part of a project to control an Ev3 Robot with a Myo Armband. Iv tried to use this workaround in my UWP App, but unfortunately UdpClient is not supported.

It suggests using DatagramSocket's instead. So just in case anyone runs into the same problem as me. Here is the Code i used in the UWP App to get the IP address Automatically.

       private async void FindHostIP()
        {
            //Open up a socket
            DatagramSocket listener = new DatagramSocket();

            //Add MessageReceived Event
            listener.MessageReceived += MessageReceived;

            //Important for async access
            CoreApplication.Properties.Add("listener", listener);

            // Start listen operation.
            try
            {
                listener.Control.InboundBufferSizeInBytes = 67;
                await listener.BindServiceNameAsync("3015");
            }
            catch (Exception)
            {
                //Oops Something Went Wrong
            }
        }

        //Message is Received Every 10 seconds , Code Must be added to handle Repeats
        async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
        {   
            try
            {
                IOutputStream outputStream = await socket.GetOutputStreamAsync(
                    eventArguments.RemoteAddress,
                    eventArguments.RemotePort);
                _Ev3IpAddress = eventArguments.RemoteAddress;

                //Pass the RemoteAddress to the BrickInit() method
                BrickInit(eventArguments.RemoteAddress);
            }
            catch (Exception)
            {
                //Oops Something Went Wrong
            }
        }

        private void BrickInit(HostName RemoteAddress)
        {
                _brick = new Brick(new NetworkCommunication(RemoteAddress.CanonicalName));
                _brick.BrickChanged += _brick_BrickChanged;
                //....Add other events here etc
            }
        }

Thanks For the API @BrianPeek , Having lots of fun using this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants