Main Site Documentation

Next step to create a WiFi socket between PC and FEZ II Cobra?


#1

We are trying to create a peer-to-peer connection between a FEZ Cobra II (WiFi) and a desktop computer. We have completed the following steps:

[ul]CreateWifiDriver[/ul]
[ul]OpenTheWifiDriver[/ul]
[ul]ConfigureIpAddress[/ul]
[ul]SetupAdHocHost[/ul]
[ul]Wait for the wireless connectivity to change.[/ul]
[ul]CreateSocket[/ul]

The problem is that we are stuck after creating the socket. We don’t know how then to connect to the socket from the desktop computer.

Once we’ve created the socket (by running the code shown below), we try to connect as follows. We open PowerShell on the desktop computer and run:

[ul]netsh interface ip delete arpcache[/ul]
[ul]netsh wlan show networks mode=bssid[/ul]

This output shows that the SSID that we created is visible. This is good. Our ad-hoc network is broadcasting.Then in Windows 8 we do the following:

[ul]open Network and Sharing Center[/ul]
[ul]Set up a new connection or network[/ul]
[ul]Manually connect to a wireless network[/ul]
[ul]Network name: YOURSSID[/ul]
[ul]Security type: No authentication (Open)[/ul]
[ul]Leave other defaults[/ul]
[ul]Next[/ul]

And the we run the following in PowerShell:

[ul]netsh wlan connect ssid=YOURSSID name=PROFILENAME[/ul]
[ul]e.g. netsh wlan connect ssid=MyAdHocHost129 name=MyAdHocHost129[/ul]

The results of the PowerShell command say:

This is good too! The Desktop Computer is connected to the Cobra WiFi. Hooray! Now, the problem is, now that we made a successful connection request with the Cobra II WiFi, what do we do next to create a socket? When we try to create a socket, the listener just sits waiting for a client request, and we haven’t been able to figure out how to contact the listener. Here is our full code.


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using GHI.Hardware.G120;
using GHI.Premium.Net;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace MFConsoleApplication3
{
    public class Program
    {
        private static WiFiRS9110 _wifi;
        private static readonly ManualResetEvent mre = new ManualResetEvent(false);

        private const string AdHocHostName = "MyAdHocHost";
        private const string IpAddress = "169.254.0.200";
        private const string SubnetMask = "255.255.0.0";
        private const string GatewayAddress = "169.254.0.1";

        public static void Main()
        {
            PrintMethodNameForDebugging("Main");

            try
            {
                _wifi = CreateWifiDriver();
                OpenTheWifiDriver(_wifi);
                DisconnectTheWifiDriver(_wifi);
                AddEventListenersToTheWifiDriver(_wifi);
                ConfigureIpAddress(_wifi, false);

                new Thread(CreateSocket).Start();
                SetupAdHocHost(_wifi, AdHocHostName);

                Debug.Print("Exit");

                /* --------------------------------------
                 
                 * Open PowerShell and run:
                 
                 netsh interface ip delete arpcache
                 netsh wlan show networks mode=bssid
                 
                 * Then open Network and Sharing Center
                 * Set up a new connection or network
                 * Manually connect to a wireless network
                 * Network name: YOURSSID
                 * Security type: No authentication (Open)
                 * Leave other defaults
                 * Next
                 
                 * Open PowerShell and run:
                 
                 netsh wlan connect ssid=YOURSSID name=PROFILENAME
                 e.g. netsh wlan connect ssid=MyAdHocHost129 name=MyAdHocHost129
                 
                 * "Connection request was completed successfully."
                 * This means that the Desktop Computer is connected to the Cobra WiFi.
                 
                 * However, we haven't been able to send data via TCP.
                 * I.e. the CreateSocket() method never goes past Waiting for client...
                 
                 
                 -------------------------------------- */
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
        }

        private static void CreateSocket()
        {
            mre.WaitOne();
            PrintMethodNameForDebugging("CreateSocket");

            if (_wifi.IsLinkConnected)
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress hostIp = IPAddress.Parse(_wifi.NetworkInterface.IPAddress);
                var hostEndPoint = new IPEndPoint(hostIp, 80);
                socket.Bind(hostEndPoint);

                // does this need to wait too?
                socket.Listen(1);
                Debug.Print("Waiting for client...");

                Socket client = socket.Accept();
                var clientEndPoint = client.RemoteEndPoint as IPEndPoint;
                if (clientEndPoint != null)
                {
                    Debug.Print("Connected with " + clientEndPoint.Address + " at port " + clientEndPoint.Port);
                }
            }
        }

        private static void SetupAdHocHost(WiFiRS9110 wifi, string hostName)
        {
            PrintMethodNameForDebugging("SetupAdHocHost");

            hostName += DateTime.Now.Ticks.ToString().Substring(0, 3);
            Debug.Print("hostName:" + hostName);
            wifi.StartAdHocHost(hostName, SecurityMode.Open, "", 10);
        }

        // See more at: https://www.ghielectronics.com/community/forum/topic?id=10374&page=3#sthash.j13kLPi4.dpuf
        private static void ConfigureIpAddress(WiFiRS9110 wifi, bool doDhcp)
        {
            PrintMethodNameForDebugging("ConfigureIpAddress");

            // the ipAddress is apparently finicky
            if (doDhcp)
            {
                // dynamic means that we need a DHCP server on the LAN
                Debug.Print("EnableDhcp");
                if (!wifi.NetworkInterface.IsDhcpEnabled)
                {
                    wifi.NetworkInterface.EnableDhcp();
                }
            }
            else
            {
                // static apparently works well with laptop using MS-Windows as long you use an IP address 169.255.x.x
                Debug.Print("EnableStaticIp");
                wifi.NetworkInterface.EnableStaticIP(IpAddress, SubnetMask, GatewayAddress);
                wifi.NetworkInterface.EnableStaticDns(new string[] { "192.168.1.1" });
            }
        }

        private static void wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            PrintMethodNameForDebugging("wifi_NetworkAddressChanged");

            PrintNetworkInterfaceDetails(_wifi);
        }

        private static void wifi_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            PrintMethodNameForDebugging("wifi_WirelessConnectivityChanged");

            Debug.Print("IsConnected:" + e.IsConnected);
            Debug.Print("NetworkInformation:" + e.NetworkInformation); // this is null   

            PrintNetworkInterfaceDetails(_wifi);

            mre.Set();
        }

        private static void AddEventListenersToTheWifiDriver(WiFiRS9110 wifi)
        {
            PrintMethodNameForDebugging("AddEventListenersToTheWifiDriver");

            wifi.NetworkAddressChanged += wifi_NetworkAddressChanged;
            wifi.WirelessConnectivityChanged += wifi_WirelessConnectivityChanged;
        }

        private static void DisconnectTheWifiDriver(WiFiRS9110 wifi)
        {
            PrintMethodNameForDebugging("DisconnectTheWifiDriver");

            if (wifi.IsLinkConnected)
            {
                wifi.Disconnect();
            }
        }

        private static void OpenTheWifiDriver(WiFiRS9110 wifi)
        {
            PrintMethodNameForDebugging("OpenTheWifiDriver");

            if (!wifi.IsOpen)
            {
                // open the driver
                wifi.Open();
            }
        }

        private static WiFiRS9110 CreateWifiDriver()
        {
            PrintMethodNameForDebugging("CreateWifiDriver");

            // the wifi module is MOD2 and we connect via an SPI
            const SPI.SPI_module mod2 = SPI.SPI_module.SPI2;

            // why do we choose 1_10 as the chipset pin? 
            const Cpu.Pin chipSelect = Pin.P1_10;

            // why do we choose 2_11 as the external interupt pin?
            const Cpu.Pin externalInterupt = Pin.P2_11;

            // why do we choose 1_9 as the reset pin?
            const Cpu.Pin reset = Pin.P1_9;

            // why do we choose 4000?
            const uint clockRateKhz = 4000;

            // create a wifi object
            var wifi = new WiFiRS9110(mod2, chipSelect, externalInterupt, reset, clockRateKhz);

            return wifi;
        }

        private static void PrintNetworkInterfaceDetails(WiFiRS9110 wifi)
        {
            PrintMethodNameForDebugging("PrintNetworkInterfaceDetails");

            Debug.Print("GatewayAddress:" + wifi.NetworkInterface.GatewayAddress);
            Debug.Print("IPAddress:" + wifi.NetworkInterface.IPAddress);
            Debug.Print("SubnetMask:" + wifi.NetworkInterface.SubnetMask);
            Debug.Print("DnsAddresses");
            foreach (var s in wifi.NetworkInterface.DnsAddresses)
            {
                Debug.Print(s);
            }
            Debug.Print("IsDhcpEnabled:" + wifi.NetworkInterface.IsDhcpEnabled);
            Debug.Print("IsDynamicDnsEnabled:" + wifi.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType:" + wifi.NetworkInterface.NetworkInterfaceType);
        }

        private static void PrintMethodNameForDebugging(string header)
        {
            Debug.Print("\n\n");
            Debug.Print(header);
            Debug.Print("--");
        }
    }
}


#2

I have not done adhoc WiFi with a NETMF device…

The first thing I would do is try to ping the Cobra from the PC. This would verify that both are on the same adhoc network and can communicate.

Also, for the listening endpoint, I would use IPAddress.Any.

What messages are appearing in Console window?


#3

@ Mike -

Thank you for the reply. My replies follow your quotes.

Fair enough. Do you think it’s a possible way to setup a peer-to-peer socket for data transfer and remote procedure calls?

We have tried pinging and receive a “request timed out” message when we try this IP Address:

[ul]ping 169.254.0.200 # the static ip addres we assigned [/ul]

Does that mean we are not on the same ad-hoc network? Is there another IP address that we could ping?

Sure. We will try that. Maybe this will fix thing ping timeout result.

Do you mean the Debug.Print(…) output of Visual Studio or the console window output after our netsh and ping commands? I can post it all if it’s helpful.


#4

If you can not ping the Cobra from the PC, then an adhoc network has not been established.

I don’t remember seeing any other questions in the past about adhoc. My initial though was adhoc was not implemented. But, I see the method in the Premium documentation.

Maybe someone from GHI can comment on adhoc support, and if it is implemented, provide some sample code.


#5

@ Mike - I will forward this topic on the GHI support. Thanks for your help!