Funpack第八期-基于Arduino Nano 33 BLE Sense的环境监测站
在LCD屏幕和微信小程序显示环境温度、湿度、气压、日照强度和周围噪声。
标签
Arduino Nano 33 BLE Sense
Funpack第八期
azjhong
更新2021-05-18
829

内容介绍

1.功能实现

本次任务主要实现了一个用于检测户外环境的小型监测站系统。检测环境参数有:

  • 周边环境温度(精度:±0.1°C, ±0.1°F)

  • 周边环境湿度(精度:±1%)

  • 大气压强(精度:±0.1kPa, ±0.1psi)

  • 日照强度(用于判断白天/夜晚)

  • 周边平均噪声(精度:±1dB)

2.硬件连接

Nano 33 BLE Sense

外接一个1.14寸LCD屏幕,使用NANO_33_BLE_SENSE的一个SPI接口。

1.14寸LCD屏幕引脚 板卡引脚
VCC/GND +3V3/GND
SCL D13/P0.13/SCK
SDA D11/P1.01/MOSI
DC D9/P0.27
CS D8/P0.21
RES D10/P1.02
BLK D7/P0.23

3.代码部分

检测环境数据通过两部分显示:第一是未连接BLE前只通过屏幕显示;第二是连接BLE后通过屏幕和微信小程序显示。

LCD显示

void display_process(void)
{
  tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); 
  tft.setCursor(0, 0);
  tft.print("WERTHER STATION");
  tft.drawLine(0, 16, tft.width() - 1, 16, ST77XX_YELLOW);
  
  temperature = HTS.readTemperature() + CALIBRATION_FACTOR;  
  Serial.print(F("TEMPERATURE:"));
  Serial.print(temperature, 1);
  Serial.println(F(" °C"));
  
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
  tft.setCursor(0, 20);
  tft.print("TEMPERATURE:");
  tft.print(temperature, 1);
  tft.drawCircle(200, 20, 3, ST77XX_RED);
  tft.setCursor(208, 20);
  tft.print("C");
  tft.drawLine(0, 36, tft.width() - 1, 36, ST77XX_YELLOW);

  humidity = HTS.readHumidity();
  Serial.print(F("HUMIDITY:"));
  Serial.print(humidity, 1);
  Serial.println(F(" %"));
  
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
  tft.setCursor(0, 40);
  tft.print("HUMIDITY:");
  tft.print(humidity, 1);
  tft.setCursor(160, 40);
  tft.print("%");
  tft.drawLine(0, 56, tft.width() - 1, 56, ST77XX_YELLOW);
  
  pressure = BARO.readPressure();
  Serial.print(F("PRESSURE:"));
  Serial.print(pressure, 1);
  Serial.println(F(" kPa"));
  
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
  tft.setCursor(0, 60);
  tft.print("PRESSURE:");
  tft.print(pressure, 1);
  tft.setCursor(170, 60);
  tft.print("kPa");
  tft.drawLine(0, 76, tft.width() - 1, 76, ST77XX_YELLOW);
  
  while (! APDS.colorAvailable()) 
  {
    delay(1);
  }
  APDS.readColor(r, g, b, a);
  Serial.print("R:");
  Serial.println(r);
  Serial.print("G:");
  Serial.println(g);
  Serial.print("B:");
  Serial.println(b);
  Serial.print("A:");
  Serial.println(a);
  if(a > 20)
  {
    
    Serial.println(F("DAY"));
    tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
    tft.setCursor(0, 80);
    tft.print("AMBIENT:DAY  ");
  }
  else
  {
    Serial.println(F("NIGHT"));
    tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
    tft.setCursor(0, 80);
    tft.print("AMBIENT:NIGHT");
  }
  tft.drawLine(0, 96, tft.width() - 1, 96, ST77XX_YELLOW);

  while (!samplesRead);
  uint16_t sample_max = 0;
  for (int i = 0; i < samplesRead; i++) 
  {
    if(sampleBuffer[i] < 0)
      sampleBuffer[i] = -sampleBuffer[i];
    if(sampleBuffer[i] > sample_max) 
      sample_max = sampleBuffer[i];
  }
  noise = 24 * log10(sample_max * 5); 
  samplesRead = 0;
  Serial.println(noise);
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
  tft.setCursor(0, 100);
  tft.print("NOISE:");
  uint8_t cnt = 0;
  uint16_t n = noise;
  while(n != 0)
  {
    n /= 10;
    ++cnt;
  }
  if(cnt == 2)
  {
      tft.print(" ");
      tft.print(noise, DEC);
  }
  else
  {
      tft.print(noise, DEC);
  }
  tft.setCursor(110, 100);
  tft.print("dB");
  tft.drawLine(0, 116, tft.width() - 1, 116, ST77XX_YELLOW);
}

外围设备蓝牙数据处理

void ble_process(void)
{
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  if(central)
  {
    Serial.print("Connected to central MAC: ");
    Serial.println(central.address()); // Central's BT address:
    digitalWrite(LEDB, LOW); // Turn on the LED to indicate the connection
    while (central.connected()) 
    {
      long currentMillis = millis();
      // After UPDATE_FREQUENCY ms have passed, check temperature & humidity
      if (currentMillis - previousMillis >= UPDATE_FREQUENCY) 
      {
        previousMillis = currentMillis;
        if (temperature != previousTemperature) 
        { // If reading has changed
          Serial.print("Temperature: ");
          Serial.println(temperature);
          tempCharacteristic.writeValue((int16_t)(temperature * 100)); // Update characteristic
          previousTemperature = temperature;          // Save value
        }
        if (humidity != previousHumidity) 
        { // If reading has changed
            Serial.print("Humidity: ");
            Serial.println(humidity);
            humidCharacteristic.writeValue((uint16_t)(humidity * 100));
            previousHumidity = humidity;
        }
        if (pressure != previousPressure) 
        { // If reading has changed
          Serial.print("Pressure: ");
          Serial.println(pressure);
          pressureCharacteristic.writeValue((uint16_t)(pressure * 100));
          previousPressure = pressure;
        }     
        if(a > 20)
        {
          Serial.print("Day a: ");
          Serial.println(a);
          colorCharacteristic.writeValue((uint8_t)true);
        }
        else
        {
          Serial.print("Night a: ");
          Serial.println(a);
          colorCharacteristic.writeValue((uint8_t)false);
        }
        if(noise != previousNoise) 
        { // If reading has changed
          Serial.print("Noise: ");
          Serial.println(noise);
          soundCharacteristic.writeValue((uint16_t)(noise));
          previousNoise = noise;
        } 
        display_process();
      }
    }
    digitalWrite(LEDB, HIGH); // When the central disconnects, turn off the LED
    Serial.print("Disconnected from central MAC: ");
    Serial.println(central.address());
   }
}

主控设备蓝牙数据处理

   /* 监听低功耗蓝牙设备的特征值变化事件 */
   onBLECharacteristicValueChange: function(environmentTypeValue) {
      var that = this;
      wx.onBLECharacteristicValueChange(function(res) {
         console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`);

         switch(res.characteristicId)
         {
            case that.data.tempertureCharId:
               var int16View = new Int16Array(res.value);
               console.log(int16View[0]);
               /* 接收的当前温度数据并转化实际温度显示值 */
               that.data.current_temperture = int16View[0] / 100;
               break;
            case that.data.humidityCharId:
               var uint16View = new Uint16Array(res.value);
               console.log(uint16View[0]);
               /* 接收的当前湿度数据并转化实际湿度显示值 */
               that.data.current_humidity = uint16View[0] / 100;
               break;
            case that.data.pressureCharId:
               var uint16View = new Uint16Array(res.value);
               console.log(uint16View[0]);
               /* 接收的当前气压数据并转化实际气压显示值 */
               that.data.current_pressure = uint16View[0] / 100;
               break;
            case that.data.ambientCharId:
               var uint8View = new Uint8Array(res.value);
               console.log(uint8View[0]);
               /* 接收的当前光强数据并转化实际光强显示值 */
               (uint8View[0] == true) ? (that.data.current_ambient = "白天") : (that.data.current_ambient = "夜晚");
               break;
            case that.data.noiseCharId:
               var uint16view = new Uint16Array(res.value);
               console.log(uint16view[0]);
               /* 接收的当前噪声数据并转化实际噪声显示值 */
               that.data.current_noise = uint16view[0];
               break;
            default:
               break;
         }
         that.setData({
            /* 定义一个当前温度变量 */
            current_temperture: that.data.current_temperture,
            current_humidity: that.data.current_humidity,
            current_pressure: that.data.current_pressure,
            current_ambient: that.data.current_ambient,
            current_noise: that.data.current_noise,
         })
      })
   },

4.功能演示

本次在LCD屏幕和微信小程序上显示了:周边环境温度、周边环境湿度、大气压强、判断白天和夜晚以及周边平均噪声。

FseYkT3UaPdmb_8g_ZR81MrchC4U

5.心得体会

很高兴参加这次funpack第八期活动,这期也是我第一次玩起Arduino开发板。Arduino给我的感触是第一方便上手,IDE极其简洁,下载即用;第二具有丰富的库供我们使用,库里还有很多例程可以参考;第三是具有许多开源硬件项目可以参考使用。这期活动我使用nano 33 ble sense蓝牙功能,刚好在第四期活动我写一个BLE微信小程序,拿到这期做一些小改动,就可以很快实现在微信小程序显示环境数据的功能。另外我还对比了一下同一个ST7789驱动屏幕显示在Arduino下开发比在MAX32660下开发是多么简单呀~

最后,感谢硬禾学堂和得捷电子,让我能在空余时间能更多参与有趣项目学习,也感谢群里小伙伴提供很多种实现题目功能的思路,感谢大家一路的折腾与陪伴,谢谢!

代码工程链接:https://github.com/Azjhong/funpack

团队介绍

电子爱好者
团队成员
电子小白

评论

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