Exemple #1
0
        public BarometerSensor(IPConnection ipConnection, string uid, int sensorCallbackPeriod, Station station)
        {
            _tinfluxWeatherStation = station;
            var bricklet = new BrickletBarometer(uid, ipConnection);

            // Register callback to function BrickletCb
            bricklet.AirPressureCallback += BrickletCb;

            // Note: The callback is only called every "sensorCallbackPeriod"
            // if the value has changed since the last call!
            bricklet.SetAirPressureCallbackPeriod(sensorCallbackPeriod);
        }
    static void Main()
    {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        BrickletBarometer b = new BrickletBarometer(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT); // Connect to brickd
        // Don't use device before ipcon is connected

        // Register air pressure callback to function AirPressureCB
        b.AirPressure += AirPressureCB;

        // Set period for air pressure callback to 1s (1000ms)
        // Note: The air pressure callback is only called every second
        //       if the air pressure has changed since the last call!
        b.SetAirPressureCallbackPeriod(1000);

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
    static void EnumerateCB(IPConnection sender, string UID, string connectedUID, char position,
	                        short[] hardwareVersion, short[] firmwareVersion,
	                        int deviceIdentifier, short enumerationType)
    {
        if(enumerationType == IPConnection.ENUMERATION_TYPE_CONNECTED ||
           enumerationType == IPConnection.ENUMERATION_TYPE_AVAILABLE)
        {
            if(deviceIdentifier == BrickletLCD20x4.DEVICE_IDENTIFIER)
            {
                try
                {
                    brickletLCD = new BrickletLCD20x4(UID, ipcon);
                    brickletLCD.ClearDisplay();
                    brickletLCD.BacklightOn();
                    System.Console.WriteLine("LCD 20x4 initialized");
                }
                catch(TinkerforgeException e)
                {
                    System.Console.WriteLine("LCD 20x4 init failed: " + e.Message);
                    brickletLCD = null;
                }
            }
            else if(deviceIdentifier == BrickletAmbientLight.DEVICE_IDENTIFIER)
            {
                try
                {
                    brickletAmbientLight = new BrickletAmbientLight(UID, ipcon);
                    brickletAmbientLight.SetIlluminanceCallbackPeriod(1000);
                    brickletAmbientLight.Illuminance += IlluminanceCB;
                    System.Console.WriteLine("Ambient Light initialized");
                }
                catch(TinkerforgeException e)
                {
                    System.Console.WriteLine("Ambient Light init failed: " + e.Message);
                    brickletAmbientLight = null;
                }
            }
            else if(deviceIdentifier == BrickletAmbientLightV2.DEVICE_IDENTIFIER)
            {
                try
                {
                    brickletAmbientLightV2 = new BrickletAmbientLightV2(UID, ipcon);
                    brickletAmbientLightV2.SetConfiguration(BrickletAmbientLightV2.ILLUMINANCE_RANGE_64000LUX,
                                                            BrickletAmbientLightV2.INTEGRATION_TIME_200MS);
                    brickletAmbientLightV2.SetIlluminanceCallbackPeriod(1000);
                    brickletAmbientLightV2.Illuminance += IlluminanceV2CB;
                    System.Console.WriteLine("Ambient Light 2.0 initialized");
                }
                catch(TinkerforgeException e)
                {
                    System.Console.WriteLine("Ambient Light 2.0 init failed: " + e.Message);
                    brickletAmbientLightV2 = null;
                }
            }
            else if(deviceIdentifier == BrickletHumidity.DEVICE_IDENTIFIER)
            {
                try
                {
                    brickletHumidity = new BrickletHumidity(UID, ipcon);
                    brickletHumidity.SetHumidityCallbackPeriod(1000);
                    brickletHumidity.Humidity += HumidityCB;
                    System.Console.WriteLine("Humidity initialized");
                }
                catch(TinkerforgeException e)
                {
                    System.Console.WriteLine("Humidity init failed: " + e.Message);
                    brickletHumidity = null;
                }
            }
            else if(deviceIdentifier == BrickletBarometer.DEVICE_IDENTIFIER)
            {
                try
                {
                    brickletBarometer = new BrickletBarometer(UID, ipcon);
                    brickletBarometer.SetAirPressureCallbackPeriod(1000);
                    brickletBarometer.AirPressure += AirPressureCB;
                    System.Console.WriteLine("Barometer initialized");
                }
                catch(TinkerforgeException e)
                {
                    System.Console.WriteLine("Barometer init failed: " + e.Message);
                    brickletBarometer = null;
                }
            }
        }
    }
 static void EnumerateCB(IPConnection sender, string UID, string connectedUID, char position,
                         short[] hardwareVersion, short[] firmwareVersion,
                         int deviceIdentifier, short enumerationType)
 {
     if (enumerationType == IPConnection.ENUMERATION_TYPE_CONNECTED ||
         enumerationType == IPConnection.ENUMERATION_TYPE_AVAILABLE)
     {
         if (deviceIdentifier == BrickletLCD20x4.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletLCD = new BrickletLCD20x4(UID, ipcon);
                 brickletLCD.ClearDisplay();
                 brickletLCD.BacklightOn();
                 System.Console.WriteLine("LCD 20x4 initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("LCD 20x4 init failed: " + e.Message);
                 brickletLCD = null;
             }
         }
         else if (deviceIdentifier == BrickletAmbientLight.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletAmbientLight = new BrickletAmbientLight(UID, ipcon);
                 brickletAmbientLight.SetIlluminanceCallbackPeriod(1000);
                 brickletAmbientLight.Illuminance += IlluminanceCB;
                 System.Console.WriteLine("Ambient Light initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("Ambient Light init failed: " + e.Message);
                 brickletAmbientLight = null;
             }
         }
         else if (deviceIdentifier == BrickletAmbientLightV2.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletAmbientLightV2 = new BrickletAmbientLightV2(UID, ipcon);
                 brickletAmbientLightV2.SetConfiguration(BrickletAmbientLightV2.ILLUMINANCE_RANGE_64000LUX,
                                                         BrickletAmbientLightV2.INTEGRATION_TIME_200MS);
                 brickletAmbientLightV2.SetIlluminanceCallbackPeriod(1000);
                 brickletAmbientLightV2.Illuminance += IlluminanceV2CB;
                 System.Console.WriteLine("Ambient Light 2.0 initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("Ambient Light 2.0 init failed: " + e.Message);
                 brickletAmbientLightV2 = null;
             }
         }
         else if (deviceIdentifier == BrickletHumidity.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletHumidity = new BrickletHumidity(UID, ipcon);
                 brickletHumidity.SetHumidityCallbackPeriod(1000);
                 brickletHumidity.Humidity += HumidityCB;
                 System.Console.WriteLine("Humidity initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("Humidity init failed: " + e.Message);
                 brickletHumidity = null;
             }
         }
         else if (deviceIdentifier == BrickletHumidityV2.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletHumidityV2 = new BrickletHumidityV2(UID, ipcon);
                 brickletHumidityV2.SetHumidityCallbackConfiguration(1000, true, 'x', 0, 0);
                 brickletHumidityV2.HumidityCallback += HumidityV2CB;
                 System.Console.WriteLine("Humidity 2.0 initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("Humidity 2.0 init failed: " + e.Message);
                 brickletHumidityV2 = null;
             }
         }
         else if (deviceIdentifier == BrickletBarometer.DEVICE_IDENTIFIER)
         {
             try
             {
                 brickletBarometer = new BrickletBarometer(UID, ipcon);
                 brickletBarometer.SetAirPressureCallbackPeriod(1000);
                 brickletBarometer.AirPressure += AirPressureCB;
                 System.Console.WriteLine("Barometer initialized");
             }
             catch (TinkerforgeException e)
             {
                 System.Console.WriteLine("Barometer init failed: " + e.Message);
                 brickletBarometer = null;
             }
         }
     }
 }