예제 #1
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);
            }
        }