Exemplo n.º 1
0
        public EthernetManager(EthernetENC28J60 ethernetInterface = null)
        {
            isBuiltIn = ethernetInterface == null;

            if (!isBuiltIn)
            {
                ethernet = ethernetInterface;
                (ethernet as EthernetENC28J60).CableConnectivityChanged += (object sender, EthernetENC28J60.CableConnectivityEventArgs e) => { ethernet_CableConnectivityChanged(sender, e.IsConnected); };
            }
            else
            {
                ethernet = new EthernetBuiltIn();
                (ethernet as EthernetBuiltIn).CableConnectivityChanged += (object sender, EthernetBuiltIn.CableConnectivityEventArgs e) => { ethernet_CableConnectivityChanged(sender, e.IsConnected); };
            }

            ethernet.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(ethernet_NetworkAddressChanged);
        }
Exemplo n.º 2
0
        public void Start()
        {
            if (OpenWiFi() && EnableDHCP())
            {
                NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);

                WiFiNetworkInfo ni = SearchForNetwork();

                if (!Join(ni))
                {
                    if (Stopped != null)
                    {
                        Stopped(this, EventArgs.Empty);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public EthernetManager(EthernetENC28J60 ethernetInterface = null)
        {
            isBuiltIn = ethernetInterface == null;

            if (!isBuiltIn)
            {
                ethernet = ethernetInterface;
                (ethernet as EthernetENC28J60).CableConnectivityChanged += (object sender, EthernetENC28J60.CableConnectivityEventArgs e) => { ethernet_CableConnectivityChanged(sender, e.IsConnected); };
            }
            else
            {
                ethernet = new EthernetBuiltIn();
                (ethernet as EthernetBuiltIn).CableConnectivityChanged += (object sender, EthernetBuiltIn.CableConnectivityEventArgs e) => { ethernet_CableConnectivityChanged(sender, e.IsConnected); };
            }

            ethernet.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(ethernet_NetworkAddressChanged);
        }
Exemplo n.º 4
0
        public void Start()
        {
            if (OpenEthernet() && EnableDHCP())
            {
                NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet);

                // wait for cable:
                //if (isBuiltIn)
                //{
                //    if (!(ethernet as EthernetBuiltIn).IsCableConnected)
                //    {
                //        do
                //        {
                //            if (!(ethernet as EthernetBuiltIn).IsCableConnected)
                //                Debug.Print("Waiting for ethernet cable...");
                //            else
                //                break;
                //        }
                //        while (!blocker.WaitOne(500, false));
                //    }
                //}
                //else
                //{
                //    if (!(ethernet as EthernetENC28J60).IsCableConnected)
                //    {
                //        do
                //        {
                //            if (!(ethernet as EthernetENC28J60).IsCableConnected)
                //                Debug.Print("Waiting for ethernet cable...");
                //            else
                //                break;
                //        }
                //        while (!blocker.WaitOne(500, false));
                //    }
                //}

                //while (ethernet.NetworkInterface.IPAddress == "0.0.0.0")
                //{
                //    Debug.Print("Waiting for IPAddress");
                //    Thread.Sleep(250);
                //}
            }
        }
Exemplo n.º 5
0
        public WiFi(WiFi_RS21 module)
        {
            wifi = module.Interface;

            if (!wifi.IsOpen)
            {
                wifi.Open();
            }

            wifi.NetworkInterface.EnableDhcp();
            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);

            module.NetworkDown +=
                new GTM.Module.NetworkModule.NetworkEventHandler(Wifi_NetworkDown);
            module.NetworkUp +=
                new GTM.Module.NetworkModule.NetworkEventHandler(Wifi_NetworkUp);
            wifi.WirelessConnectivityChanged +=
                new WiFiRS9110.WirelessConnectivityChangedEventHandler(Interface_WirelessConnectivityChanged);
            wifi.NetworkAddressChanged +=
                new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a HomeOS device library instance supporting assocation with HomeOS hubs, using wifi, serial or storage devices (USB host, SD card)
        /// to transfer credentials.
        /// </summary>
        /// <param name="manufacturer">A string identifying the manufacturer, e.g. "Microsoft Research"</param>
        /// <param name="deviceTypeIdentifier">A string identifying the device type, e.g. "MoistureSensor"</param>
        /// <param name="setupWifiAuthCode">The authentication code used for wifi setup networks to prove that the caller has physical access to this device (it should be written on the device)</param>
        /// <param name="wifi">The instance of the wifi module.  Cannot be null.</param>
        /// <param name="led">The instance of an LED module to show the wifi connection state.  Can be null.  The LED will not be controlled if the doNotControlLed delegate returns true.</param>
        /// <param name="display">The instance of a display module to show the wifi connection state.  Can be null.  The display will not be controlled if the doNotControlLed delegate returns true.</param>
        /// <param name="serialPort">The serial port to use to receive wifi network credentials.  Can be null.</param>
        /// <param name="doNotManageWifi">A delegate specifying when NOT to manage wifi, e.g. allowing the program to stop wifi scanning and assocation for a time.  Can be null.</param>
        /// <param name="doNotControlScreen">A delegate specifying when NOT to control the screen (i.e. if your program wants to control the screen)</param>
        /// <param name="doNotControlLed">A delegate specifying when NOT to control the led (i.e. if your program wants to control the led)</param>
        /// <param name="enableWifiSetupNetworks">Enable wifi-based discovery using "setup" network</param>
        /// <param name="doUDPDiscovery">Listens and responds to UDP packets from HomeOS trying to discover this device, over both "setup" and home networks.  If this is off, the device will still beacon periodically over UDP.  Normally leave it on.</param>
        public HomeOSGadgeteerDevice(
            string manufacturer,
            string deviceTypeIdentifier,
            string setupWifiAuthCode,

            GTM.GHIElectronics.WiFi_RS21 wifi,
            GTM.GHIElectronics.MulticolorLed led = null,
            GTM.Module.DisplayModule display     = null,
            string serialPort               = null,
            BoolDelegate doNotManageWifi    = null,
            BoolDelegate doNotControlScreen = null,
            BoolDelegate doNotControlLed    = null,

            bool enableWifiSetupNetworks = true,
            bool doUDPDiscovery          = true

            )
        {
            if (manufacturer == null)
            {
                throw new ArgumentNullException("manufacturer");
            }
            if (deviceTypeIdentifier == null)
            {
                throw new ArgumentNullException("deviceTypeIdentifier");
            }
            if (setupWifiAuthCode == null)
            {
                throw new ArgumentNullException("setupAuthCode");
            }
            if (wifi == null)
            {
                throw new ArgumentNullException("wifi");
            }
            this.wifi    = wifi;
            this.led     = led;
            this.display = display;
            if (display != null)
            {
                display.SimpleGraphics.AutoRedraw = false;
            }

            if (serialPort != null)
            {
                new Thread(() => SerialReadThread(serialPort)).Start();
            }

            this.SkipWifiTimer      = doNotManageWifi ?? defaultFalse;
            this.DoNotControlScreen = doNotControlScreen ?? defaultFalse;
            this.DoNotControlLed    = doNotControlLed ?? defaultFalse;

            this.DoUDPDiscovery     = doUDPDiscovery;
            EnableWifiSetupNetworks = enableWifiSetupNetworks;
            this.SetupAuthCode      = setupWifiAuthCode;
            this.TypeIdentifier     = deviceTypeIdentifier;
            this.Manufacturer       = manufacturer;

            wifi.Interface.NetworkInterface.EnableDhcp();
            wifi.Interface.WirelessConnectivityChanged += Interface_WirelessConnectivityChanged;
            wifi.Interface.NetworkAddressChanged       += Interface_NetworkAddressChanged;

            if (!wifi.Interface.IsOpen)
            {
                wifi.Interface.Open();
            }
            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi.Interface);


            Thread thread = new Thread(DiscoveryThread);

            thread.Start();

            LoadData();

            wifiTimer       = new GT.Timer(WifiScanPeriod);
            wifiTimer.Tick += wifiTimer_Tick;
            wifiTimer.Start();

            WebServer.SetupWebEvent(webPath).WebEventReceived += new WebEvent.ReceivedWebEventHandler(CredentialsWebEventReceived);

            SetLed();
            SetScreen();

            GT.Program.BeginInvoke(new VoidDelegate(() => wifiTimer_Tick(wifiTimer)), null);
        }
Exemplo n.º 7
0
        private static void SetupNetwork()
        {
            const string suntimeUpdateFormat     = "{0} - {1}: {2}";
            const string suntimeUpdateTypeThread = "Thread";
            const string suntimeUpdateTypeInit   = "Init";
            const string suntimeUpdateSunrise    = "Sunrise";
            const string suntimeUpdateSunset     = "Sunset";
            int          currentDay = DateTime.Now.DayOfYear;

            Trace.TraceInformation("Setting up networking...");

            ethernet = new EthernetENC28J60(SPI.SPI_module.SPI2, Pin.P1_10, Pin.P2_11, Pin.P1_9, 4000);

            ethernet.NetworkAddressChanged +=
                (o, e) =>
            {
                if (ethernet.NetworkInterface.IPAddress == "0.0.0.0")
                {
                    return;
                }

                Trace.TraceInformation("IP Address: {0}", ethernet.NetworkInterface.IPAddress);

                time.EnsureTime(true);

                time.GetSunTimes(out sunrise, out sunset);
                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeInit, suntimeUpdateSunrise, sunrise.ToString());
                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeInit, suntimeUpdateSunset, sunset.ToString());

                //new System.Threading.Timer(
                //    (state) =>
                //    {
                //        time.GetSunTimes(out sunrise, out sunset);
                //        Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunrise, sunrise.ToString());
                //        Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunset, sunset.ToString());
                //    },
                //    null,
                //    DateTime.Today.AddHours(24) - DateTime.Now.AddMinutes(-5),
                //    new TimeSpan(24, 0, 0));

                try
                {
                    Trace.TraceInformation("Sunrise/set updater...");
                    new Thread(
                        () =>
                    {
                        while (true)
                        {
                            // 1 min? 5 mins? 1 hour?
                            Thread.Sleep(60000);

                            if (DateTime.Now.DayOfYear != currentDay)
                            {
                                currentDay = DateTime.Now.DayOfYear;

                                time.GetSunTimes(out sunrise, out sunset);
                                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunrise, sunrise.ToString());
                                Trace.TraceInformation(suntimeUpdateFormat, suntimeUpdateTypeThread, suntimeUpdateSunset, sunset.ToString());
                            }
                        }
                    }).Start();
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Sunrise/set updater exception: {0}", ex.Message);
                }
            };

            ethernet.CableConnectivityChanged +=
                (o, e) =>
            {
                Trace.TraceInformation("Network cable is {0}", e.IsConnected ? "connected" : "disconnected");
            };

            if (!ethernet.IsOpen)
            {
                ethernet.Open();
            }

            if (settings.Network.IPType == Constants.NetworkIPType.DHCP)
            {
                if (!ethernet.NetworkInterface.IsDhcpEnabled)
                {
                    ethernet.NetworkInterface.EnableDhcp();
                }
                else
                {
                    ethernet.NetworkInterface.RenewDhcpLease();
                }
            }
            else
            {
                var ip      = settings.Network.IPAddress;
                var subnet  = settings.Network.Subnet;
                var gateway = settings.Network.Gateway;

                ethernet.NetworkInterface.EnableStaticIP(ip, subnet, gateway);
                ethernet.NetworkInterface.EnableStaticDns(new string[] { gateway });
            }

            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet);
        }
Exemplo n.º 8
0
        /*
         * bool direction = true;
         * double servoValue;
         */

        // This method is run when the mainboard is powered up or reset.
        void ProgramStarted()
        {
            /*******************************************************************************************
            *  Modules added in the Program.gadgeteer designer view are used by typing
            *  their name followed by a period, e.g.  button.  or  camera.
            *
            *  Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
            *   button.ButtonPressed +=<tab><tab>
            *
            *  If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
            *   GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
            *   timer.Tick +=<tab><tab>
            *   timer.Start();
            *******************************************************************************************/

            // setup a servo on pin 7 of the extender and turn it on
            //servo = this.extender.SetupPWMOutput(GT.Socket.Pin.Seven);
            //servo.Active = true;

            // setup compass
            //compass.SetGain(Compass.Gain.Gain2);
            compass.MeasurementComplete += new GTM.Seeed.Compass.MeasurementCompleteEventHandler(compass_MeasurementComplete);

            // setup accelerometer
            //accelerometer.Calibrate();
            //accelerometer.SaveCalibration();

            /*
             * Debug.Print(accelerometer.LoadCalibration().ToString());
             * accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
             */



            // setup wifi
            if (!wifi.Interface.IsOpen)
            {
                wifi.Interface.Open();
            }

            //if (!wifi.Interface.NetworkInterface.IsDhcpEnabled)
            //    wifi.Interface.NetworkInterface.EnableDhcp();
            wifi.Interface.NetworkInterface.EnableStaticIP("172.20.10.5", "255.255.255.240", "172.20.10.1");

            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi.Interface);

            //wifi.Interface.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);
            wifi.Interface.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);

            // create an ad hoc connection for the smartphone to connect to
            wifi.Interface.StartAdHocHost("Via", SecurityMode.WEP, "123tolmin", 11);


            Debug.Print("------------------------------------------------");
            Debug.Print("IP Address:   " + wifi.Interface.NetworkInterface.IPAddress);
            Debug.Print("DHCP Enabled: " + wifi.Interface.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Subnet Mask:  " + wifi.Interface.NetworkInterface.SubnetMask);
            Debug.Print("Gateway:      " + wifi.Interface.NetworkInterface.GatewayAddress);
            Debug.Print("DNS server:   " + wifi.Interface.NetworkInterface.DnsAddresses[0]);
            Debug.Print("------------------------------------------------");
            Debug.Print("Actvated: " + wifi.Interface.IsActivated);
            Debug.Print("Link connected: " + wifi.Interface.IsLinkConnected);
            Debug.Print("------------------------------------------------");


            /*
             * // setup web server
             * WebServer.StartLocalServer(wifi.Interface.NetworkInterface.IPAddress, 80);
             * collectData = WebServer.SetupWebEvent("data"); // SEND DATA TO: http://{ip}/data
             * collectData.WebEventReceived += new WebEvent.ReceivedWebEventHandler(collectData_WebEventReceived);
             */

            // puts servo to default position for 5 seconds to sync it with the arrow piece
            //servo.SetPulse(20 * 1000 * 1000, 150 * 10 * 1000);
            //Mainboard.SetDebugLED(true);
            //Thread.Sleep(5000);
            //Mainboard.SetDebugLED(false);

            // create a timer robot
            GT.Timer timer = new GT.Timer(100);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            timer.Start();

            // convert coordinates to radians
            currentLatitude      = currentLatitude * MathEx.PI / 180;
            currentLongitude     = currentLongitude * MathEx.PI / 180;
            destinationLatitude  = destinationLatitude * MathEx.PI / 180;
            destinationLongitude = destinationLongitude * MathEx.PI / 180;

            // tell everyone that we have started
            Debug.Print("Program Started");
        }