Stasiun Cuaca Canggih v2.0: Pemantauan Suhu, Tekanan, Kelembaban & Ketinggian Real-Time
Bangun stasiun cuaca kompak yang mengukur suhu, tekanan, kelembaban, dan ketinggian dengan akurat di ruang tinggal Anda.
Cerita
Menggunakan Adafruit Starter Pack untuk Windows 10 IoT Core pada Raspberry Pi 2, proyek ini menunjukkan cara mengantarmuka sensor BME280 (atau BMP280) untuk membaca data lingkungan dan mempublikasikannya ke layanan cloud.
Jika Anda memiliki BMP280, ikuti panduan Stasiun Cuaca v1 sebagai gantinya: Stasiun Cuaca v1.
Hardware
Hubungkan Raspberry Pi 2 ke breadboard dan pasang sensor BME280 sesuai dengan diagram Fritzing di bagian “Skematika” di bawah.
Perangkat Lunak
Unduh kode starter dari GitHub – ms‑iot/adafruitsample. Kami akan memandu Anda menambahkan kode yang diperlukan untuk berkomunikasi dengan sensor dan mempublikasikan pembacaan Anda.
Buka Lesson_203v2\StartSolution\lesson_203v2.sln dan edit mainpage.xaml.cs.
Versi yang selesai sepenuhnya tersedia di Lesson_203v2\FullSolution\lesson_203v2.sln.
MainPage.xaml.cs
Tambahkan referensi ke kelas BME280:
public sealed partial class MainPage : Page { // Create a new object for our sensor class BME280 BME280; Inisialisasi sensor di OnNavigatedTo:
// Create a new object for our sensor class BME280 = new BME280Sensor(); // Initialize the sensor await BME280.Initialize();
Selanjutnya, tambahkan kode untuk mengumpulkan dan mencatat data:
- Buat variabel untuk suhu, tekanan, kelembaban, dan ketinggian, diinisialisasi ke 0.
- Tetapkan konstanta tekanan permukaan laut (default 1013.25 hPa).
- Baca keempat pengukuran sepuluh kali dan keluarkan ke konsol debug.
//Initialize them to 0. float temp = 0; float pressure = 0; float altitude = 0; float humidity = 0; //Create a constant for pressure at sea level. const float seaLevelPressure = 1022.00f; //Read 10 samples of the data for (int i = 0; i < 10; i++){ temp = await BME280.ReadTemperature(); pressure = await BME280.ReadPreasure(); altitude = await BME280.ReadAltitude(seaLevelPressure); humidity = await BME280.ReadHumidity(); //Write the values to your debug console Debug.WriteLine("Temperature: " + temp.ToString() + " deg C"); Debug.WriteLine("Humidity: " + humidity.ToString() + " %"); Debug.WriteLine("Pressure: " + pressure.ToString() + " Pa"); Debug.WriteLine("Altitude: " + altitude.ToString() + " m"); Debug.WriteLine("\n"); } BME280.cs
Buka BME280.cs dan tambahkan berikut setelah enum alamat register:
//String for the friendly name of the I2C bus const string I2CControllerName = "I2C1"; //Create an I2C device private I2cDevice bme280 = null; //Create new calibration data for the sensor BME280_CalibrationData CalibrationData; //Variable to check if device is initialized bool init = false;
Tingkatkan metode Initialize untuk menyiapkan koneksi I2C:
public async Task Initialize() { Debug.WriteLine("BME280::Initialize"); try { I2cConnectionSettings settings = new I2cConnectionSettings(BME280_Address); settings.BusSpeed = I2cBusSpeed.FastMode; string aqs = I2cDevice.GetDeviceSelector(I2CControllerName); DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs); bme280 = await I2cDevice.FromIdAsync(dis[0].Id, settings); if (bme280 == null) { Debug.WriteLine("Device not found"); } } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace); throw; } } Tambahkan rutinitas Begin untuk memverifikasi tanda sensor dan memuat data kalibrasi:
private async Task Begin() { Debug.WriteLine("BME280::Begin"); byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CHIPID }; byte[] ReadBuffer = new byte[] { 0xFF }; bmp280.WriteRead(WriteBuffer, ReadBuffer); Debug.WriteLine("BME280 Signature: " + ReadBuffer[0].ToString()); if (ReadBuffer[0] != BMP280_Signature) { Debug.WriteLine("BMP280::Begin Signature Mismatch."); return; } init = true; CalibrationData = await ReadCoefficeints(); await WriteControlRegister(); await WriteControlRegisterHumidity(); } Helper register kontrol:
private async Task WriteControlRegisterHumidity() { byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROLHUMID, 0x03 }; bmp280.Write(WriteBuffer); await Task.Delay(1); } private async Task WriteControlRegister() { byte[] WriteBuffer = new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROL, 0x3F }; bmp280.Write(WriteBuffer); await Task.Delay(1); } Fungsi utilitas untuk membaca data register:
private UInt16 ReadUInt16_LittleEndian(byte register) { UInt16 value = 0; byte[] writeBuffer = new byte[] { 0x00 }; byte[] readBuffer = new byte[] { 0x00, 0x00 }; writeBuffer[0] = register; bmp280.WriteRead(writeBuffer, readBuffer); int h = readBuffer[1] << 8; int l = readBuffer[0]; value = (UInt16)(h + l); return value; } private byte ReadByte(byte register) { byte value = 0; byte[] writeBuffer = new byte[] { 0x00 }; byte[] readBuffer = new byte[] { 0x00 }; writeBuffer[0] = register; bmp280.WriteRead(writeBuffer, readBuffer); value = readBuffer[0]; return value; } Fungsi kalibrasi dan kompensasi diambil langsung dari datasheet BMP280 dan sudah disediakan.
Implementasikan ReadTemperature untuk mengembalikan suhu sensor dalam derajat Celsius:
public async Task<float> ReadTemperature() { if (!init) await Begin(); byte tmsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_MSB); byte tlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_LSB); byte txlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_XLSB); Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4); double temp = BMP280_compensate_T_double(t); return (float)temp; } Lengkapi ReadPreasure (catat kesalahan ketik asli; kami mempertahankannya untuk kompatibilitas):
public async Task<float> ReadPreasure() { if (!init) await Begin(); if (t_fine == Int32.MinValue) await ReadTemperature(); byte tmsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_MSB); byte tlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_LSB); byte txlsb = ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_XLSB); Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4); Int64 pres = BMP280_compensate_P_Int64(t); return ((float)pres) / 256; } Terakhir, implementasikan ReadAltitude untuk menghitung ketinggian menggunakan rumus barometrik:
public async Task<float> ReadAltitude(float seaLevel) { if (!init) await Begin(); float pressure = await ReadPreasure(); pressure /= 100; return 44330.0f * (1.0f - (float)Math.Pow((pressure / seaLevel), 0.1903f)); } Proyek Anda sekarang siap untuk diterapkan!
Output yang Diharapkan
Suhu: 24,46189 derajat C
Kelembaban: 54,372 %
Tekanan: 99738,73 Pa
Ketinggian: 205,1726 m
Sumber: Stasiun Cuaca V 2.0