Exemplo n.º 1
0
    static void Main(string[] args)
    {
        Console.WriteLine("Hello DHT!");

        DHTSensor dht = new DHTSensor(26, DhtType.Dht22);

        while (true)
        {
            // You have 2 ways to read the data, both are equivalent
            // First way to read the data
            bool readret = dht.ReadData();
            if (readret)
            {
                Console.WriteLine($"Temperature: {dht.Temperature.Celsius.ToString("0.00")} °C, Humidity: {dht.Humidity.ToString("0.00")} %");
            }
            else
            {
                Console.WriteLine("Error reading the sensor");
            }
            Thread.Sleep(1000);

            // Second way to read the data
            Temperature Temp;
            double      Hum;
            if (dht.TryGetTemperatureAndHumidity(out Temp, out Hum))
            {
                Console.WriteLine($"Temperature: {Temp.Celsius.ToString("0.00")} °C, Humidity: {Hum.ToString("0.00")} %");
            }
            else
            {
                Console.WriteLine("Error reading the sensor");
            }
            Thread.Sleep(1000);
        }
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var pin = 17;
            var lightTimeInMilliseconds = 1000;
            var dimTimeInMilliseconds   = 1000;

            var tempSensor = new DHTSensor(27, DhtType.Dht11);

            bool result = false;

            /*
             * I2cConnectionSettings i2cSettings = new I2cConnectionSettings(1, 0x39);
             * // get I2cDevice (in Linux)
             * UnixI2cDevice i2cDevice = new UnixI2cDevice(i2cSettings);
             * var lightSensor = new TMD2771(i2cDevice);
             * lightSensor.init();
             */
            /////////

            /*
             * void callback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
             * {
             *  Console.WriteLine("************Presed**************");
             * }
             */
            /////////////


            GpioController controller = new GpioController();

            controller.OpenPin(22, PinMode.Input);
            //controller.RegisterCallbackForPinValueChangedEvent(22, PinEventTypes.Rising, callback);

            controller.OpenPin(pin, PinMode.Output);

            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
            {
                controller.Dispose();
            };

            //Console.WriteLine("enter URL");
            //myURL = Console.ReadLine();
            //Console.WriteLine(myURL);
            myURL = "http://ehsankianifar-001-site1.itempurl.com/api/TempData";

            while (true)
            {
                controller.Write(pin, PinValue.High);
                Thread.Sleep(lightTimeInMilliseconds);
                controller.Write(pin, PinValue.Low);
                Thread.Sleep(dimTimeInMilliseconds);

                result = tempSensor.TryGetTemperatureAndHumidity(out temperature, out humidity);
                Console.WriteLine($"Temp in centigrad {temperature}");
                Console.WriteLine($"humidity in percent {humidity}");
                //Console.WriteLine($"result {result}");
                //Console.WriteLine($"light sensor c0 {lightSensor.ReadC0()}");
                //Console.WriteLine($"light sensor c1 {lightSensor.ReadC1()}");
                //Console.WriteLine($"light sensor id {lightSensor.ReadId()}");

                sendDataAsync();
                //TestAsync();
            }
        }