/// <inheritdoc/> public override void Initialize() { this.LogStartInitialization(); this.Device = this.Controller.GetI2CConnection(this.DeviceId); var resetCommand = new byte[] { CCS811SWRESET, 0x11, 0xE5, 0x72, 0x8A }; this.Device.Write(resetCommand); Thread.Sleep(100); byte hwId = (byte)ByteOperations.GetUChar(this.ReadRawByte(CCS811HWID)); if (hwId != CCS811ID) { throw new System.Exception(string.Format("Incorrect ID, expected {0}, got {1}", CCS811HWID, hwId)); } // check internal status this.CheckError("Init"); // app valid var status = this.ReadRawByte(CCS811STATUS); this.LogMessage(string.Format("Status afer INIT OK? {0}", status == 16)); if ((status & 1) << 4 != 0) { this.CheckError("App not valid"); } this.Device.WriteByte(CCS811APPSTART); Thread.Sleep(100); this.CheckError("Start"); this.WriteByte(CCS811MEASMODE, CCS811MEAS1S); this.CheckError("MEAS"); Thread.Sleep(1500); var bytes = this.ReadRawBytes(CCS811BASELINE, 2); this.LogMessage(string.Format("Baseline: {0}", (bytes[0] << 8) | bytes[1])); // data availabe? this.LogMessage(string.Format("Data available: {0}", !((this.ReadRawByte(CCS811STATUS) & 1) << 3 == 0))); this.CheckError("READ"); this.LogMessage("Warm up"); for (int i = 0; i < this.warmupCount; i++) { var data = this.ReadRawBytes(CCS811ALGRESULTDATA, 4); this.LogMessage($"RawBytes: {ByteOperations.PrintByteArray(data)}"); Thread.Sleep(5000); } this.CheckError("WARMUP"); this.LogMessage("CCS811 Ready"); this.LogStartSuccess(); }
/// <inheritdoc/> public override ISensorSample TakeReading(WeatherObservation observation) { this.LogTakeReadingStart(); var sensorResult = new SensorSample(this.SensorName); var data = this.ReadRawBytes(CCS811ALGRESULTDATA, 4); sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(data)); var co2 = (data[0] << 8) | data[1]; var tvoc = (data[2] << 8) | data[3]; sensorResult.AddFinalObservation(co2, "CO2", ObservationUnits.PartsPerMillion); sensorResult.AddFinalObservation(tvoc, "TVOC", ObservationUnits.PartsPerBillion); observation.CO2 = co2; observation.TVOC = tvoc; this.LogTakeReadingComplete(); return(sensorResult); }
/// <inheritdoc/> public override ISensorSample TakeReading(WeatherObservation observation) { this.LogTakeReadingStart(); var sensorResult = new SensorSample(this.SensorName); // Initialize for reading this.Device.Write(new byte[] { REGCONTROLHUM, OVERSAMPLEHUM }); var control = GetControlCode(); this.Device.Write(new byte[] { REGCONTROL, (byte)control }); // Read calibaration data var cal1 = this.ReadBytes(0x88, 24); var cal2 = this.ReadBytes(0xA1, 1); var cal3 = this.ReadBytes(0xE1, 7); sensorResult.AddDiagnostic("Calibration data (1/2/3)"); sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal1)); sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal2)); sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal3)); var calibrationData = ExtractcalibrationData(cal1, cal2, cal3); // Pause per spec var wait_time = GetWaitTime(); Thread.Sleep((int)wait_time / 1000); // Read raw data var data = this.ReadBytes(REGDATA, 8); sensorResult.AddDiagnostic("Raw data"); sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(data)); var rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); double rawTemperature = CalculateTemperature(sensorResult, calibrationData, rawTemp, out double tFine); // Refine pressure and adjust for temperature var rawPressure = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); double pressureValue = CalculatePressure(sensorResult, calibrationData, rawPressure, tFine); // Refine humidity var rawHumidity = (data[6] << 8) | data[7]; double humidityValue = CalculateHumidity(sensorResult, calibrationData, rawHumidity, tFine); sensorResult.AddFinalObservation(rawTemperature + this.calibrationOffset, "TEMPERATURE", ObservationUnits.DegreesCelcius); sensorResult.AddFinalObservation(pressureValue, "PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(humidityValue, "HUMIDITY", ObservationUnits.Percentage); var pressure = new Pressure(pressureValue, UnitsNet.Units.PressureUnit.Hectopascal); var humidity = new RelativeHumidity(humidityValue, UnitsNet.Units.RelativeHumidityUnit.Percent); var temperature = new Temperature(rawTemperature, UnitsNet.Units.TemperatureUnit.DegreeCelsius); var actualAltitude = new Length(this.altitudeInMeters, UnitsNet.Units.LengthUnit.Meter); var altitudeCalculated = WeatherHelper.CalculateAltitude(pressure); double absHumidity = WeatherHelper.CalculateAbsoluteHumidity(temperature, humidity).GramsPerCubicMeter; double dewPoint = WeatherHelper.CalculateDewPoint(temperature, humidity).DegreesCelsius; double heatIndex = WeatherHelper.CalculateHeatIndex(temperature, humidity).DegreesCelsius; double vapourPressure = WeatherHelper.CalculateActualVaporPressure(temperature, humidity).Hectopascals; double barometricPressure = WeatherHelper.CalculateBarometricPressure(pressure, temperature, actualAltitude, humidity).Hectopascals; double vapourPressureOverIce = WeatherHelper.CalculateSaturatedVaporPressureOverIce(temperature).Hectopascals; double vapourPressureOverWater = WeatherHelper.CalculateSaturatedVaporPressureOverWater(temperature).Hectopascals; double seaLevelPressure = WeatherHelper.CalculateSeaLevelPressure(pressure, actualAltitude, temperature).Hectopascals; sensorResult.AddFinalObservation(temperature.DegreesCelsius + this.calibrationOffset, "TEMPERATURE", ObservationUnits.DegreesCelcius); sensorResult.AddFinalObservation(pressure.Hectopascals, "PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(barometricPressure, "BAROMETRIC PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(seaLevelPressure, "SEA LEVEL PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(vapourPressureOverIce, "OVER ICE PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(vapourPressureOverWater, "OVER WATER PRESSURE", ObservationUnits.HectoPascal); sensorResult.AddFinalObservation(humidity.Percent, "RELATIVE HUMIDITY", ObservationUnits.Percentage); sensorResult.AddFinalObservation(absHumidity, "ABSOLUTE HUMIDITY", ObservationUnits.GramsPerCubicMeter); sensorResult.AddFinalObservation(vapourPressure, "VAPOUR PRESSURE", ObservationUnits.Percentage); sensorResult.AddFinalObservation(altitudeCalculated.Meters, "CALCULATED ALTITUDE", ObservationUnits.Meters); sensorResult.AddFinalObservation(actualAltitude.Meters, "ACTUAL ALTITUDE", ObservationUnits.Meters); sensorResult.AddFinalObservation(heatIndex, "HEAT INDEX", ObservationUnits.DegreesCelcius); sensorResult.AddFinalObservation(dewPoint, "DEW POINT", ObservationUnits.DegreesCelcius); observation.Temperature1 = temperature.DegreesCelsius + this.calibrationOffset; observation.Pressure = pressure.Hectopascals; observation.BarometricPressure = barometricPressure; observation.SealevelPressure = seaLevelPressure; observation.OverIcePressure = vapourPressureOverIce; observation.OverWaterPressure = vapourPressureOverWater; observation.RelativeHumidity = humidity.Percent; observation.AbsoluteHumidity = absHumidity; observation.ActualAltitude = actualAltitude.Meters; observation.CalculatedAltitude = altitudeCalculated.Meters; observation.HeatIndex = heatIndex; observation.DewPoint = dewPoint; this.LogTakeReadingComplete(); return(sensorResult); }