Funpack第八期-基于Arduino nano 33 BLE Sense和Wio Terminal的蓝牙环境监测站
Arduino nano 33 BLE Sense开发板采集温度、湿度、气压、光照、噪声等环境参数,并通过BLE发送数据到Wio Terminal,实现数据监测显示功能。也可通过手机上具有BLE功能的App显示数据。
标签
Arduino
BLE
Funpack参赛
Arduino Nano 33 BLE Sense
wio terminal
chinaking
更新2021-05-18
1411

一、准备

硬件:

Arduino nano 33 BLE Sense 开发板  1个

Wio Terminal终端                1个

软件:

Arduino开发环境

功能:

Arduino nano 33 BLE Sense开发板采集温度、湿度、气压、光照、噪声等环境参数,并通过BLE发送数据到Wio Terminal,实现数据监测显示功能。也可通过手机上具有BLE功能的App显示数据。

二、代码

2.1 BLE Server代码(对nano 33 ble sense编程)

下面是读温度、湿度、气压、光照(颜色)的代码

void read_Sensors() {
 temperature = HTS.readTemperature();
 humidity    = HTS.readHumidity();
 iTemperature=temperature *100;
 iHumidity=humidity*100;

 pressure = BARO.readPressure();
 iPressure=pressure*10000;
 
  while (! APDS.colorAvailable()) {
    delay(5);
  }
   APDS.readColor(r, g, b);
   irradiance=(r+g+b)/3;
     if ((r<20) && (g<20) && (b<20))  
  { 
    at_night=true;
  }
  else
  {  at_night=false;
  }
}

下面是读声音强度的代码

void onPDMdata() {
  // Query the number of available bytes
  int bytesAvailable = PDM.available();

  // Read into the sample buffer
  PDM.read(sampleBuffer, bytesAvailable);

  // 16-bit, 2 bytes per sample
  samplesRead = bytesAvailable / 2;
}
if (samplesRead) {
    // Print samples to the serial monitor or plotter
     for (int i = 0; i < samplesRead; i++) {
     // Serial.print("Sound Voltate:");
     // Serial.println(sampleBuffer[i]);
      unsigned int sum = 0;
      sum += pow(sampleBuffer[i], 2);
      rms = sqrt(double(sum) / (double(samplesRead) / 2.0));  //从PDM采样buffer计算RMS值(均方根值)
      Serial.print("RMS:");
      Serial.println(rms);
     float a=20.0 * (log10(rms));
     if(a<0) {
      a=0;}
     else
     {a=a;
      }
     noise =a+25;  
     iNoise= noise*100;
    }
    // Clear the read count
    samplesRead = 0;
  }

然后将数据通过BLE方式发出去

定义UUID

BLEService EnvironmentService("181A");
BLEUnsignedIntCharacteristic temperatureChar("2A6E", BLERead | BLENotify);
BLEUnsignedIntCharacteristic humidityChar("2A6F", BLERead | BLENotify);
BLEUnsignedIntCharacteristic pressureChar("2A6D", BLERead | BLENotify);
BLEUnsignedIntCharacteristic nightChar("2A88", BLERead | BLENotify);
BLEUnsignedIntCharacteristic noiseChar("2A99", BLERead | BLENotify);

写入数据值

BLE.setLocalName("Arduino Nano 33 BLE Sensors");
BLE.setAdvertisedService(EnvironmentService);
EnvironmentService.addCharacteristic(temperatureChar);
EnvironmentService.addCharacteristic(humidityChar);
EnvironmentService.addCharacteristic(pressureChar);
EnvironmentService.addCharacteristic(nightChar);
EnvironmentService.addCharacteristic(noiseChar);

BLE.addService(EnvironmentService);
temperatureChar.writeValue(iTemperature);
humidityChar.writeValue(iHumidity);
pressureChar.writeValue(iPressure);
nightChar.writeValue(at_night);
noiseChar.writeValue(iNoise);

2.2 BLE Client代码(对wio terminal编程)

定义UUID

static BLEUUID serviceUUID(0x181a); // service UUID
static BLEUUID characteristicUUID_temperature(0x2a6e);//Temperature characteristic UUID
static BLEUUID characteristicUUID_humidity(0x2a6f);//humidity characteristic UUID
static BLEUUID characteristicUUID_pressure(0x2a6d);//气压 characteristic UUID
static BLEUUID characteristicUUID_night(0x2a88);//白天黑夜 characteristic UUID
static BLEUUID characteristicUUID_noise(0x2a99);//分贝 characteristic UUID

指定要连接的BLE Servise的mac

static BLEAdvertisedDevice* myDevice;
uint8_t bd_addr[6] = {0x98, 0xAA, 0x9B, 0xDF, 0xD7, 0x60};//BLE Server的MAC地址,注意Aruduino里和手机APP搜到的MAC顺序是反的,可能是大端小端原因(60:D7:DF:9B:AA:98)
BLEAddress BattServer(bd_addr);

读传感器数据

 //读温度  Read Temperature
   pRemoteCharacteristic1=pClient->getService(serviceUUID)->getCharacteristic(characteristicUUID_temperature);
   value_t= pRemoteCharacteristic1->readUInt16();
   f_Temperature=value_t/100.00;
    Serial.print(f_Temperature);
    Serial.println("℃");

  //读湿度  Read Humidity
   pRemoteCharacteristic2=pClient->getService(serviceUUID)->getCharacteristic(characteristicUUID_humidity);
   value_h= pRemoteCharacteristic2->readUInt16();
   f_Humidity=value_h/100.00;
    Serial.print(f_Humidity);
    Serial.println("%");

    //读气压  Read Humidity
   pRemoteCharacteristic3=pClient->getService(serviceUUID)->getCharacteristic(characteristicUUID_pressure);
   value_p= pRemoteCharacteristic3->readUInt32();
   f_Pressure=value_p/10000.00;
    Serial.print(f_Pressure);
    Serial.println("kPa");

    //读光照  
   pRemoteCharacteristic4=pClient->getService(serviceUUID)->getCharacteristic(characteristicUUID_night);
   value_y= pRemoteCharacteristic4->readUInt8();
    if (value_y==1)
  {
    Serial.println("Night");
    }
   else
    {
    Serial.println("Daytime");
    }

    //读噪声  Read Noise
   pRemoteCharacteristic5=pClient->getService(serviceUUID)->getCharacteristic(characteristicUUID_noise);
   value_n= pRemoteCharacteristic5->readUInt32();
   f_Noise=value_n/100.00;
    Serial.print(f_Noise);
    Serial.println("dB");
    Serial.println(" ");

屏幕显示测量数据

    //屏幕显示
  tft.drawFloat(f_Temperature,2, 10, 50, 1);//文字方向,X,Y,字号
  tft.drawString("C", 110, 50, 1);

  tft.drawFloat(f_Humidity,2, 180, 50, 1);
  tft.drawString("%", 280, 50, 1);

  tft.drawFloat(f_Pressure,2, 10, 100, 1);//文字方向,X,Y,字号
  tft.drawString("kPa", 130, 100, 1);

  if (value_y==1)
  {
    tft.drawString("Night  ",10, 150, 1);
    }
   else
    {
    tft.drawString("Daytime",10, 150, 1);
    }
  
  tft.drawFloat(f_Noise,2, 10, 200, 1);//文字方向,X,Y,字号
  tft.drawString("dB", 130, 200, 1);
 

三、心得

时光匆匆,funpack都到第8期了。有几个小建议:

3.1 建议后续任务能和前几期的板卡结合起来使用,减少开发板积灰,拓展应用范围。

3.2 建议将活动和竞赛结合起来,增强竞技性,优秀作品可以增加投票环节。

 

 

附件下载
BLE_Server_Nano33.ino
BLE_Client_Wio.ino
团队介绍
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2024 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号