산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Manufacturing Technology >> 제조공정

고급 기상 관측소 v2.0: 실시간 온도, 기압, 습도, 고도 모니터링

거실에서 온도, 기압, 습도, 고도를 정확하게 측정하는 콤팩트한 기상 관측소를 만드세요.

스토리

라즈베리파이 2용 윈도우 10 IoT Core 아다프루트 스타터 팩을 사용해 BME280 센서(또는 BMP280)와 인터페이스해 환경 데이터를 읽고 클라우드 서비스에 발행하는 방법을 시연합니다.

BMP280을 소유한 경우 대신 Weather Station v1 가이드 참조: Weather Station v1.

하드웨어

라즈베리파이 2를 브레드보드에 연결하고 "Schematics" 섹션의 프리칭 다이어그램에 따라 BME280 센서를 배선하세요.

소프트웨어

GitHub – ms-iot/adafruitsample에서 스타터 코드 다운로드. 센서와 통신하고 읽기 값을 발행하는 데 필요한 코드 추가 과정을 안내합니다.

Lesson_203v2\StartSolution\lesson_203v2.sln을 열고 mainpage.xaml.cs를 편집하세요.

완전히 완성된 버전은 Lesson_203v2\FullSolution\lesson_203v2.sln에서 확인 가능합니다.

MainPage.xaml.cs

BME280 클래스 참조 추가:

public sealed partial class MainPage : Page {
    // 센서 클래스용 새 객체 생성
    BME280 BME280;

OnNavigatedTo에서 센서 초기화:

// 센서 클래스용 새 객체 생성
BME280 = new BME280Sensor();
// 센서 초기화
await BME280.Initialize();

데이터 수집 및 로깅 코드 추가:

// 0으로 초기화
float temp = 0; float pressure = 0; float altitude = 0; float humidity = 0;
// 해수면 기압 상수
const float seaLevelPressure = 1022.00f;
// 데이터 10 샘플 읽기
for (int i = 0; i < 10; i++){
    temp = await BME280.ReadTemperature();
    pressure = await BME280.ReadPreasure();
    altitude = await BME280.ReadAltitude(seaLevelPressure);
    humidity = await BME280.ReadHumidity();
    // 디버그 콘솔에 값 출력
    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

BME280.cs를 열고 레지스터 주소 enum 다음에 다음 추가:

// I2C 버스 친숙한 이름 문자열
const string I2CControllerName = "I2C1";
// I2C 디바이스 생성
private I2cDevice bme280 = null;
// 센서용 새 보정 데이터 생성
BME280_CalibrationData CalibrationData;
// 디바이스 초기화 여부 확인 변수
bool init = false;

Initialize 메서드 향상해 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;
    }
}

Begin 루틴 추가해 센서 시그니처 확인 및 보정 데이터 로드:

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();
}

제어 레지스터 헬퍼:

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);
}

레지스터 데이터 읽기 유틸리티 함수:

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;
}

보정 및 보상 함수는 BMP280 데이터시트에서 직접 파생되었으며 이미 제공됩니다.

ReadTemperature 구현으로 센서 온도(섭씨) 반환:

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;
}

ReadPreasure 완성 (원본 오타 유지로 호환성 보장):

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;
}

마지막으로 기압 공식으로 고도 계산하는 ReadAltitude 구현:

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));
}

이제 프로젝트 배포 준비 완료!

예상 출력

Temperature: 24.46189 deg C

Humidity: 54.372 %

Pressure: 99738.73 Pa

Altitude: 205.1726 m

출처: Weather Station V 2.0

제조공정

  1. 라즈베리 파이 태양광 기상 관측소 DIY 제작
  2. 라즈베리 파이 2와 ADS-WS1로 야외 기상 관측소 구축하기
  3. 라즈베리 파이 기상 관측소 만들기: 매일 이메일로 날씨 데이터 전송
  4. 라즈베리 파이 3와 PubNub로 원격 기상 모니터링하기
  5. 기상 관측소를 CWOP와 통합하기: 실용 가이드
  6. 날씨 모니터 – 라즈베리파이 DIY 기상 관측소 (드롭박스 업로드)
  7. 컴팩트 기상 관측소: 아두이노 나노 & ESP8266, BMP280 기압계 & OLED/노키아 LCD
  8. 1만 원대 휴대용 아두이노 날씨 스테이션 – 나노, DHT22, BMP180, LCD로 직접 제작
  9. eDOT: 내장 IR 리모컨·자동 밝기 조절 갖춘 정밀 아두이노 시계·기상 관측소
  10. 아두이노 & ThingSpeak로 실시간 날씨 모니터링 IoT 구축