/// <summary>
        /// Initializes the ModuleClient and sets up the callback to receive
        /// messages containing temperature information
        /// </summary>
        static async Task Init()
        {
            MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

            ITransportSettings[] settings = { mqttSetting };

            // Open a connection to the Edge runtime
            grovePiPlus           = new GrovePiPlus(1);
            ledButtonDevice       = new GrovePiPlusBlueLEDButton(grovePiPlus, 4, 5);
            barometerSensorDevice = new BarometerBME280(1);
            barometerSensorDevice.Initialize();
#if USE_LIGHT_SENSE
            lightSensor = new GrovePiLightSensor(grovePiPlus, 0);
#endif
#if USE_CO2_SENSE
            co2Sensor = new CO2SensorMHZ19B();
#endif
            Console.WriteLine("Sensing Device Initialized");

            iotHubConnector     = new ModuleClientConnector(settings, "command-input", "telemetry-output");
            sensingDeviceClient = new EnvironmentSensingDeviceClient(iotHubConnector, barometerSensorDevice, ledButtonDevice, lightSensor, co2Sensor);

            var tokenSource = new CancellationTokenSource();
            var ct          = tokenSource.Token;
            await sensingDeviceClient.Initialize(ct);

            Console.WriteLine("IoT Hub module client initialized.");
        }
Пример #2
0
        public EnvironmentSensingDeviceClient(EG.IoT.Utils.IoTHubConnector connector, BarometerBME280 sensor, GrovePiPlusBlueLEDButton ledButton, GrovePiLightSensor lightSensor, CO2SensorMHZ19B co2Sensor)
        {
            iothubClient      = connector;
            envSensorDevice   = sensor;
            ledButtonDevice   = ledButton;
            lightSensorDevice = lightSensor;
            co2SensorDevice   = co2Sensor;

            telemetryConfig = new TelemetryConfig()
            {
                telemetryCycleMSec   = 1000,
                temperatureAvailable = true,
                humidityAvailable    = true,
                pressureAvailable    = true,
                lightSenseAvailable  = (lightSensor != null),
                co2SensorAvailable   = (co2Sensor != null)
            };
        }
        static void Main(string[] args)
        {
#if (REMOTE_DEBUG)
            // for remote debug attaching
            for (; ;)
            {
                Console.WriteLine("waiting for debugger attach");
                if (Debugger.IsAttached)
                {
                    break;
                }
                System.Threading.Thread.Sleep(1000);
            }
#endif
            if (args.Length != 1)
            {
                Console.WriteLine("Command line:");
                Console.WriteLine("dotnet run iothub-device-connection-string");
                return;
            }
            string iothubcs = args[0];
            Console.WriteLine("Environment monitoring device with BME280 sensor.");
            Console.WriteLine($"Connection string of IoT Hub Device:{iothubcs}");

            var groveShield    = new EG.IoT.Grove.GrovePiPlus(1);
            var gblueLedButton = new EG.IoT.Grove.GrovePiPlusBlueLEDButton(groveShield, 4, 5);

            for (int i = 0; i < 3; i++)
            {
                gblueLedButton.TurnOn();
                Thread.Sleep(1000);
                gblueLedButton.TurnOff();
                Thread.Sleep(1000);
            }

            var bme280 = new EG.IoT.Grove.BarometerBME280(1);
            bme280.Initialize();
            bme280.Read();
            var temperature = bme280.ReadTemperature();
            var humidity    = bme280.ReadHumidity();
            var pressure    = bme280.ReadPressure();
            Console.WriteLine($"T={temperature}C,H={humidity}%,P={pressure}hPa");

#if USE_LIGHT_SENSE
            lightSensor = new GrovePiLightSensor(groveShield, 0);
            var lightSensorValue      = lightSensor.SensorValue();
            var lightSensorResistance = lightSensor.Resitance(lightSensorValue);
            Console.WriteLine($"lightSensor={lightSensorValue},resistance={lightSensorResistance}");
#endif

            co2Sensor = new CO2SensorMHZ19B()
            {
                Port = "/dev/serial0"
            };
            if (co2Sensor.Initialize())
            {
                for (int i = 0; i < 3; i++)
                {
                    var co2 = co2Sensor.SensorValue();
                    Console.WriteLine($"CO2:{co2} ppm");
                    Thread.Sleep(1000);
                }
            }

            EG.IoT.Utils.IoTHubConnector iothubClient = new EG.IoT.Utils.DeviceClientConnector(iothubcs);
            var sensingDevice = new EG.IoT.EnvironmentSensing.EnvironmentSensingDeviceClient(iothubClient, bme280, gblueLedButton, lightSensor, co2Sensor);

            var tokenSource = new CancellationTokenSource();
            var ct          = tokenSource.Token;

            sensingDevice.Initialize(ct).Wait();

            var key = Console.ReadLine();
            Console.WriteLine("End Apps");
            tokenSource.Cancel();
            Thread.Sleep(5000);

            sensingDevice.Terminate().Wait();
        }