Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        static void Main()
        {
            var distance = new DistanceSensor(Netduino3.GpioPin.D8);
            //SoundSensor sound = new SoundSensor(Netduino3.AdcChannel.A0);
            LightSensor       light  = new LightSensor(Netduino3.AdcChannel.A1);
            RotaryAngleSensor rotary = new RotaryAngleSensor(Netduino3.AdcChannel.A2);
            TemperatureSensor temp   = new TemperatureSensor(Netduino3.AdcChannel.A3);
            TouchSensor       touch  = new TouchSensor(Netduino3.GpioPin.D2);
            LedSocket         led    = new LedSocket(Netduino3.GpioPin.D3);
            Button            btn    = new Button(Netduino3.GpioPin.D5);
            Relay             rly    = new Relay(Netduino3.GpioPin.D6);
            Buzzer            buzz   = new Buzzer(Netduino3.GpioPin.D7);
            //var rgb = new LcdRgbBacklight();
            //rgb.EnableDisplay(true);
            Random rnd     = new Random();
            int    counter = 0;
            bool   Touched = false;

            while (true)
            {
                counter++;
                Thread.Sleep(100);
                //rgb.GoHome();
                //rgb.SetCursor(0, 0);
                //rgb.Clear();
                //Debug.WriteLine("sound:"+sound.ReadLevel());
                Debug.WriteLine("light:" + light.ReadLightLevel());
                Debug.WriteLine("rotary:" + rotary.GetAngle());
                Debug.WriteLine("temp:" + temp.ReadTemperature());
                Debug.WriteLine("distance:" + distance.MeasureInCentimeters() + "cm");
                if (touch.IsTouched() && !Touched)
                {
                    Touched = true;
                    //rgb.Write("turn on light");
                    led.TurnOn();
                }
                else if (!touch.IsTouched() && Touched)
                {
                    Touched = false;
                    //rgb.Write("turn off light");
                    led.TurnOff();
                }
                if (btn.IsPressed())
                {
                    rly.TurnOn();
                    buzz.TurnOn();
                }
                else
                {
                    buzz.TurnOff();
                    rly.TurnOff();
                }

                /*
                 * if (counter > 50)
                 * {
                 *  rgb.SetBacklightRGB((byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255));
                 *  counter = 0;
                 * }*/
            }
        }