示例#1
0
        private static void TestSocket(string host, string url, int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity, string commonName = null)
        {
            var buffer = new byte[512];
            var id     = wifi.OpenSocket(host, port, connectionType, connectionSecurity, commonName);

            var cont = true;

            while (cont)
            {
                var start = DateTime.UtcNow;

                wifi.WriteSocket(id, Encoding.UTF8.GetBytes($"GET {url} HTTP/1.1\r\nHost: {host}\r\n\r\n"));

                Thread.Sleep(100);

                var total = 0;
                var first = true;
                while ((wifi.QuerySocket(id) is var avail && avail > 0) || first || total < 120)
                {
                    if (avail > 0)
                    {
                        first = false;

                        var read = wifi.ReadSocket(id, buffer, 0, Math.Min(avail, buffer.Length));

                        total += read;

                        Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read));
                    }

                    Thread.Sleep(100);
                }

                Debug.WriteLine($"\r\nRead: {total:N0} in {(DateTime.UtcNow - start).TotalMilliseconds:N0}ms");

                WaitForButton();
            }

            wifi.CloseSocket(id);
        }
示例#2
0
        static void Main()
        {
            TouchSensor     touch = new TouchSensor(FEZ.GpioPin.D7);
            LightSensor     light = new LightSensor(FEZ.AdcChannel.A3);
            ServoMotor      servo = new ServoMotor(FEZ.PwmPin.Controller4.Id, FEZ.PwmPin.Controller4.D5);
            Buzzer          buzz  = new Buzzer(FEZ.GpioPin.D4);
            LcdRgbBacklight lcd   = new LcdRgbBacklight();

            host = "192.168.1.152";
            port = 80;

            var buffer = new byte[512];
            var cont   = GpioController.GetDefault();
            var reset  = cont.OpenPin(FEZ.GpioPin.WiFiReset);
            var irq    = cont.OpenPin(FEZ.GpioPin.WiFiInterrupt);
            var spi    = SpiDevice.FromId(FEZ.SpiBus.WiFi, SPWF04SxInterface.GetConnectionSettings(FEZ.GpioPin.WiFiChipSelect));

            connected    = false;
            socketOpened = false;
            garageLed    = cont.OpenPin(FEZ.GpioPin.D2);
            servo.SetPosition(180);
            garageLed.SetDriveMode(GpioPinDriveMode.Output);

            wifi = new SPWF04SxInterface(spi, irq, reset);

            wifi.IndicationReceived += (s, e) => Debug.WriteLine($"WIND: {Program.WindToName(e.Indication)} {e.Message}");
            wifi.ErrorReceived      += (s, e) => Debug.WriteLine($"ERROR: {e.Error} {e.Message}");

            wifi.TurnOn();
            //wifi.JoinNetwork("GHI", "ghi555wifi.");

            lcd.Clear();
            lcd.SetBacklightRGB(100, 100, 100);
            lcd.Write("Time:");

            while (!connected)
            {
                ListenWind();
                Thread.Sleep(200);
            }

            StringBuilder builder = new StringBuilder();

            while (connected)
            {
                if (!socketOpened)
                {
                    id           = wifi.OpenSocket(host, port, SPWF04SxConnectionyType.Tcp, SPWF04SxConnectionSecurityType.None);
                    socketOpened = true;
                }

                var hour   = DateTime.UtcNow.Hour;
                var minute = DateTime.UtcNow.Minute;
                var second = DateTime.UtcNow.Second;
                lcd.SetCursor(7, 1);
                lcd.Write($"{hour}:{minute}:{second}");

                if (touch.IsTouched())
                {
                    wifi.WriteSocket(id, Encoding.UTF8.GetBytes("Someone wants to open the garage"));
                }

                if (light.ReadLightLevel() > 60 && isDoorOpened == true)
                {
                    //Debug.WriteLine(light.ReadLightLevel().ToString());
                    wifi.WriteSocket(id, Encoding.UTF8.GetBytes("Car in the garage"));
                    while (light.ReadLightLevel() > 60)
                    {
                        Thread.Sleep(50);
                    }
                    wifi.WriteSocket(id, Encoding.UTF8.GetBytes("You can close the garage"));
                }

                if (wifi.QuerySocket(id) is var avail && avail > 0)
                {
                    wifi.ReadSocket(id, buffer, 0, Math.Min(avail, buffer.Length));

                    for (var k = 0; k < buffer.Length; k++)
                    {
                        if (buffer[k] != 0)
                        {
                            char result = (char)buffer[k];
                            builder.Append(result);
                            buffer[k] = 0;
                        }
                    }
                    Debug.WriteLine(builder.ToString());
                }
                string command = builder.ToString();
                builder.Clear();

                switch (command)
                {
                case "open":
                    buzz.Beep();
                    servo.SetPosition(0);
                    garageLed.Write(GpioPinValue.High);
                    isDoorOpened = true;
                    break;

                case "close":
                    buzz.Beep();
                    servo.SetPosition(180);
                    garageLed.Write(GpioPinValue.Low);
                    break;

                default:
                    break;
                }
                Thread.Sleep(100);
            }
        }
示例#3
0
        static void Main()
        {
            var buffer = new byte[512];

            DIRA = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D2);
            DIRA.SetDriveMode(GpioPinDriveMode.Output);

            DIRB = GpioController.GetDefault().OpenPin(FEZ.GpioPin.D4);
            DIRB.SetDriveMode(GpioPinDriveMode.Output);
            var cont = GpioController.GetDefault();

            var reset = cont.OpenPin(FEZ.GpioPin.WiFiReset);
            var irq   = cont.OpenPin(FEZ.GpioPin.WiFiInterrupt);
            var spi   = SpiDevice.FromId(FEZ.SpiBus.WiFi, SPWF04SxInterface.GetConnectionSettings(FEZ.GpioPin.WiFiChipSelect));

            led1 = cont.OpenPin(FEZ.GpioPin.Led1);
            btn1 = cont.OpenPin(FEZ.GpioPin.Btn1);

            led1.SetDriveMode(GpioPinDriveMode.Output);
            btn1.SetDriveMode(GpioPinDriveMode.InputPullUp);

            wifi = new SPWF04SxInterface(spi, irq, reset);
            wifi.TurnOn();
            //wifi.JoinNetwork("GHI", "ghi555wifi.");

            Debug.WriteLine("WaitForButton");
            WaitForButton();

            var speed = 90;
            var id    = wifi.OpenSocket("192.168.1.152", 80, SPWF04SxConnectionyType.Tcp, SPWF04SxConnectionSecurityType.None);

            StringBuilder builder = new StringBuilder();

            while (true)
            {
                if (wifi.QuerySocket(id) is var avail && avail > 0)
                {
                    wifi.ReadSocket(id, buffer, 0, Math.Min(avail, buffer.Length));

                    for (var k = 0; k < buffer.Length; k++)
                    {
                        if (buffer[k] != 0)
                        {
                            char result = (char)buffer[k];
                            builder.Append(result);
                            buffer[k] = 0;
                        }
                    }
                    Debug.WriteLine(builder.ToString());
                }
                string command = builder.ToString();
                builder.Clear();

                switch (command)
                {
                case "forvard":
                    SetMotorDuty(Servo.A, speed, Direction.Forvard);
                    SetMotorDuty(Servo.B, speed, Direction.Forvard);
                    break;

                case "backward":
                    SetMotorDuty(Servo.A, speed, Direction.Back);
                    SetMotorDuty(Servo.B, speed, Direction.Back);
                    break;

                case "left":
                    SetMotorDuty(Servo.A, speed, Direction.Forvard);
                    StopMotor(Servo.B);
                    Thread.Sleep(500);
                    break;

                case "right":
                    SetMotorDuty(Servo.B, speed, Direction.Forvard);
                    StopMotor(Servo.A);
                    Thread.Sleep(500);
                    break;

                case "stop":
                    StopMotor(Servo.B);
                    StopMotor(Servo.A);
                    Thread.Sleep(500);
                    break;

                default:
                    break;
                }
                Thread.Sleep(100);
            }
        }