How to use Wifi RS21 to set up an ad-hoc network?

See reply 22. Adhoc is a bad idea with mobile devices

@ SkierHiker - Wish you luck ;-)ā€¦ For ADHOC mode at your Cobra set your IP address to start with 169.254. Then you will be able to communicate with your PC or an Apple device. Android device are more trickyā€¦ One will work, others wonā€™tā€¦ I had some android devices working by manually set their WiFi settings. When a Windows PC is connected to your ADHOC device it will try to obtain a IP address using DHCPā€¦(if obtain DHCP ā€¦ is enabled). Since DHCP is not working in adhoc modeā€¦ it will not receive an IP address and automatically falls back to an IP in the range 169.254.x.x Once the PC is using this fall back IP it will work perfectly.

I donā€™t quite buy that Gusā€¦ the AR Parrot quadcopters use ad-hoc just fine with mobile devices.

@ Gus -

I think Google agrees with you since they implement WiFi Direct. Any thoughts of GHI getting onto the WiFi Direct bandwagon? I think youā€™re going to find that more and more people are becoming interested in controlling their gadgets directly with their iPhones and WiFi is the only easy option. Bluetooth on an Apple device requires their special security chip which they charge a license fee.

And Iā€™m still wondering how GoPro uses WiFi with Android phones.

Maybe that is the case but when we tried it we had all kind of problems. Also, one of our customers had similar problems as well.

@ RobvanSchelven -

I tried your RunServer but Iā€™m unable to communicate with my browser. Even Ping does not work. The only difference is that Iā€™m using the on-board WiFi on the Cobra-II. I figure thereā€™s no point in trying to get it to talk TCP until I can get HTTP to work. Here is my code. Any suggestions?


    public class Program
    {
        private static InterruptPort IntButton = new InterruptPort((Cpu.Pin)G120.Pin.P0_22, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
        private static Boolean DoOnce = false;

        private static SPI.SPI_module SPIModule = SPI.SPI_module.SPI2;
        private static Cpu.Pin ChipSelect = G120.Pin.P1_10;
        private static Cpu.Pin ExtInterrupt = G120.Pin.P2_11; 
        private static Cpu.Pin Reset = G120.Pin.P1_9;
        private static UInt32 ClockRate = 4000;

        private static String SSID = "TestWiFi";
        private static SecurityMode SecurityType = SecurityMode.Open;
        private static String SecurityKey = ""; 
        private static UInt32 WiFiChannel = 10;
        private static String IPAddress = "169.254.0.200";
        private static String SubNetMask = "255.255.0.0";
        private static String Gateway = "169.254.0.1";

        public static void Main()
        {
            try
            {
                Thread.Sleep(5000); // allow MFDeploy to start

                IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);

                WiFiRS9110 WiFi = new WiFiRS9110(SPIModule, ChipSelect, ExtInterrupt, Reset, ClockRate);

                if (!WiFi.IsOpen)
                    WiFi.Open();

                WiFi.StartAdHocHost(SSID, SecurityType, SecurityKey, WiFiChannel);
                WiFi.NetworkInterface.EnableStaticIP(IPAddress, SubNetMask, Gateway);

                WiFi.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(WiFi_Changed);

            }
            catch (Exception ex)
            {
                Debug.Print("Main Exception=" + ex.ToString());
            }
        }

        static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
        {
            if (!DoOnce)
            {
                DoOnce = true;
                RunServer();
            }
        }

        private static void WiFi_Changed(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            Debug.Print("isConnected = " + e.IsConnected);
            Debug.Print("Info = " + e.NetworkInformation); // this is null
        }

        internal static void RunServer()
        {
            try
            {
                Debug.Print("Starting HTTP Server");
                HttpListener listener = new HttpListener("http");
                listener.Start();

                while (true)
                {
                    HttpListenerResponse response = null;
                    HttpListenerContext context = null;

                    try
                    {
                        Debug.Print("Waiting for HTTP Response");
                        context = listener.GetContext();
                        response = context.Response;
                        Debug.Print("Received HTTP Response");

                        HttpListenerRequest request = context.Request;
                        switch (request.HttpMethod.ToUpper())
                        {
                            case "GET": ProcessClientGetRequest(context); break;
                        }

                        if (response != null)
                        {
                            response.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (context != null)
                            context.Close();
                        Debug.Print("RunServer Exception=" + ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print("RunServer Exception=" + ex.ToString());
            }
        }

        private static void ProcessClientGetRequest(HttpListenerContext context)
        {
            try
            {
                Debug.Print("Processing HTTP Client Request");
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                Byte[] outBuffer = Encoding.UTF8.GetBytes("Hello World");
                Debug.Print("HTTP URL=" + request.RawUrl);
                response.OutputStream.Write(outBuffer, 0, outBuffer.Length);
            }
            catch (Exception ex)
            {
                Debug.Print("ProcessClientGetRequest Exception=" + ex.ToString());
            }
        }
    }

@ SkierHiker - add NetworkInterfaceExtension.AssignNetworkingStackTo(WiFi); after WiFi.Open(ā€¦)

@ RobvanSchelven -

The good news is that did the trick! The bad news is that I was planning on taking the afternoon off but now Iā€™m on a mission :).

@ SkierHiker - Live is not easyā€¦ sometimes :wink: Good Luck