亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

Weather Station V 2.0

测量温度、压力和湿度房间。

故事

在这个项目中,我们将使用 Adafruit Starter Pack for Windows 10 IoT Core on Raspberry Pi 2 套件组件来创建一个使用传感器读取温度、压力的项目、湿度和海拔高度。

注意:该套件有两个版本,一个包含 BMP280 传感器,另一个包含 BME280。如果你有 BMP280,你会想去气象站 v1 项目 https://www.hackster.io/windows-iot/weather-station

硬件

按照下面“原理图”部分中的弗里茨图,将 Raspberry Pi2 连接到面包板和其他组件。

软件

您可以从https://github.com/ms-iot/adafruitsample 下载代码启动项目,我们将引导您完成所需代码的添加谈话到网络服务并在地图上获取您的图钉。什么地图?

打开“Lesson_203v2\StartSolution\lesson_203v2.sln”并打开 mainpage.xaml.cs 文件。

我们已经填写了一些方法作为您在此解决方案中的起点。如果您想继续前进,您可以在以下位置找到包含所有代码的解决方案:“Lesson_203v2\FullSolution\lesson_203v2.sln”

MainPage.xaml.cs

打开 MainPage.xaml.cs 文件。

添加对传感器 (BME280) 类的引用。

公共密封部分类 MainPage :Page{ // 为我们的传感器类 BME280 BME280 创建一个新对象;

现在我们在 OnNavigatedTo 方法中添加代码,该方法将为传感器创建一个新的 BME280 对象并初始化该对象。

//为我们的传感器创建一个新对象 classBME280 =new BME280Sensor();//初始化sensorawait BME280.Initialize();

接下来我们添加代码来做以下事情:

 //将它们初始化为 0.float temp =0;float pressure =0;float height =0;float height =0;//为海平面压力创建一个常数。 //这是基于你当地的海平面压力(单位:百帕斯卡)const float seaLevelPressure =1022.00f;//读取10个数据样本for (int i =0; i <10; i++){ temp =await BME280.ReadTemperature ();压力 =等待 BME280.ReadPreasure();海拔 =等待 BME280.ReadAltitude(seaLevelPressure);湿度 =等待 BME280.ReadHumidity(); //将值写入您的调试控制台 Debug.WriteLine("Temperature:" + temp.ToString() + " deg C"); Debug.WriteLine("湿度:" + 湿度.ToString() + " %"); Debug.WriteLine("压力:" + pressure.ToString() + " Pa"); Debug.WriteLine("海拔高度:" + height.ToString() + " m"); Debug.WriteLine("");}

BME280.cs

打开 BME280.cs 文件。

代码的第一部分是列出 BME280 中不同寄存器的地址。这些值可以在 BMP280 数据表中找到。

在 BME280 类中,在寄存器地址枚举后添加以下内容。

 //I2C busconst 友好名称的字符串 I2CControllerName ="I2C1";//创建一个I2C deviceprivate I2cDevice bme280 =null;//为传感器创建新的校准数据BME280_CalibrationData CalibrationData;/ /变量检查设备是否已初始化dbool init =false;

接下来在Initialize函数中添加如下代码:

//初始化BME280 sensorpublic async Task Initialize(){ Debug.WriteLine("BME280::Initialize"); try { //使用 BME280 的设备地址实例化 I2CConnectionSettings settings =new I2cConnectionSettings(BME280_Address); //设置连接的I2C总线速度为快速模式settings.BusSpeed =I2cBusSpeed.FastMode; //使用I2CBus设备选择器创建高级查询语法字符串 string aqs =I2cDevice.GetDeviceSelector(I2CControllerName); //使用 Windows.Devices.Enumeration.DeviceInformation 类创建一个使用高级查询语法字符串 DeviceInformationCollection dis =await DeviceInformation.FindAllAsync(aqs); 的集合//使用I2CBus的设备id和I2CConnectionSettings来实例化BME280 I2C设备 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);扔; }}

在Begin函数中加入如下代码:

私有异步任务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 签名:" + ReadBuffer[0].ToString()); //验证设备签名 if (ReadBuffer[0] !=BMP280_Signature) { Debug.WriteLine("BMP280::Begin Signature Mismatch.");返回; } //设置initalize变量为true init =true; //读取系数表 CalibrationData =await ReadCoefficeints(); //写控制寄存器 await WriteControlRegister(); //写湿度控制寄存器 await WriteControlRegisterHumidity();}

将以下代码添加到接下来的2个函数中以写入控制寄存器。

私有异步任务 WriteControlRegisterHumidity(){ byte[] WriteBuffer =new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROLHUMID, 0x03 }; bmp280.Write(WriteBuffer);等待 Task.Delay(1); return;}//向控制寄存器写入0x3F的方法private async Task WriteControlRegister(){ byte[] WriteBuffer =new byte[] { (byte)eRegisters.BMP280_REGISTER_CONTROL, 0x3F }; bmp280.Write(WriteBuffer);等待 Task.Delay(1);返回;}

将以下代码添加到 ReadUInt16_LittleEndian 函数中:

//从寄存器中读取16位值并以小端格式返回的方法private UInt16 ReadUInt16_LittleEndian(byte register){ UInt16 value =0;字节[] writeBuffer =新字节[] { 0x00 }; byte[] readBuffer =new byte[] { 0x00, 0x00 }; writeBuffer[0] =寄存器; bmp280.WriteRead(writeBuffer, readBuffer); int h =readBuffer[1] <<8; int l =readBuffer[0];值 =(UInt16)(h + l);返回值;}

将以下代码添加到 ReadByte 函数以从寄存器读取 8 位数据。

//从寄存器中读取8位值的方法private byte ReadByte(byte register){ byte value =0;字节[] writeBuffer =新字节[] { 0x00 }; byte[] readBuffer =new byte[] { 0x00 }; writeBuffer[0] =寄存器; bmp280.WriteRead(writeBuffer, readBuffer);值 =读取缓冲区 [0];返回值;}

接下来的 3 个功能已为您完成。可以在数据表中找到编写这些函数所需的信息。

ReadCoefficeints:这是从寄存器地址读取所有校准数据的函数。

BMP280_compensate_T_double:在此函数中,使用 BMP280 数据表中的补偿公式计算以 ºC 为单位的温度。

BMP280_compensate_P_Int64:在此函数中,Pa 中的压力使用 BMP280 数据表中的补偿公式计算。

添加以下代码以完成 ReadTemperature 函数。

public async Task ReadTemperature(){ //确保I2C设备已初始化 if (!init) await Begin(); //从BMP280寄存器中读取温度的MSB、LSB和bits 7:4 (XLSB) byte tmsb =ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_MSB);字节 tlsb =ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_LSB);字节 txlsb =ReadByte((byte)eRegisters.BMP280_REGISTER_TEMPDATA_XLSB); // bits 7:4 //将值组合成一个 32 位整数 Int32 t =(tmsb <<12) + (tlsb <<4) + (txlsb>> 4); //将原始值转换为以摄氏度为单位的温度 double temp =BMP280_compensate_T_double(t); //将温度作为浮点值返回 return (float)temp;}

重复同样的步骤完成ReadPressure功能。

 public async Task ReadPeasure() { //确保 I2C 设备已初始化 if (!init) await Begin(); //先读取温度加载t_fine值进行补偿 if (t_fine ==Int32.MinValue) { await ReadTemperature(); } //从BMP280寄存器中读取压力的MSB、LSB和bits 7:4 (XLSB) byte tmsb =ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_MSB);字节 tlsb =ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_LSB);字节 txlsb =ReadByte((byte)eRegisters.BMP280_REGISTER_PRESSUREDATA_XLSB); // bits 7:4 //将值组合成一个 32 位整数 Int32 t =(tmsb <<12) + (tlsb <<4) + (txlsb>> 4); //将原始值转换为Pa Int64 pres =BMP280_compensate_P_Int64(t); //以浮点值形式返回温度 return ((float)pres) / 256;}

最终完成ReadAltitude功能:

//以百帕斯卡(hPa)为单位的海平面压力作为参数,使用当前压力计算海拔的方法。public async Task ReadAltitude(float seaLevel){ //确定I2C 设备初始化 if (!init) await Begin(); //先读取压力 float pressure =await ReadPreasure(); //将压力转换为百帕斯卡(hPa)压力 /=100; //使用国际气压公式计算并返回海拔高度 return 44330.0f * (1.0f - (float)Math.Pow((pressure / seaLevel), 0.1903f));}

您的项目现在可以部署了!

预期输出

温度:24.46189 摄氏度

湿度:54.372 %

压力:99738.73 Pa

海拔:205.1726 米

来源: 气象站 V 2.0


制造工艺

  1. 基于树莓派的气象站
  2. Raspberry Pi 2 气象站
  3. Raspberry Pi 气象站
  4. 使用树莓派远程监测天气
  5. 与您的气象站一起加入 IOT – CWOP
  6. Weather Monitor
  7. Arduino + ESP 气象箱
  8. $10 便携式 Arduino 气象站 (AWS)
  9. eDOT - 基于 Arduino 的精密时钟和气象站
  10. ThingSpeak Arduino 气象站
  11. 当地气象站
  12. 便携式温度站