核心器件
1. Arduino UNO
2. BMP280
可测量温度,气压(气压可换算为高度)。功耗为为微瓦级。
3. OLED 128x32
4. RC2202A
目前最低功耗的蓝牙模块(功耗微瓦级),只支持从模式。
接线方式
代码
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP280.h>
#include <SoftwareSerial.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define RX 3
#define TX 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_BMP280 bmp;
SoftwareSerial BT(RX, TX);
static const unsigned char PROGMEM degree[] = {//“°”字模 16x16
0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x24,0x00,0x24,0x00,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
static const unsigned char PROGMEM yes[] = {//“√”字模 10x10
0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x02,0x00,0x02,0x00,0x1A,0x00,0x0C,0x00,
0x04,0x00,0x00,0x00,
};
static const unsigned char PROGMEM no[] = {//“×”字模 10x10
0x00,0x00,0x00,0x00,0x21,0x00,0x12,0x00,0x0C,0x00,0x0C,0x00,0x12,0x00,0x21,0x00,
0x00,0x00,0x00,0x00,
};
void setup() {
//Serial.begin(9600);
BT.begin(9600);
bmp.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
display.setTextColor(WHITE);
display.setTextSize(1);
display.print("Xidian University");
display.setCursor(26, 18);
display.setTextSize(2);
display.println("LRCTEST");
display.display();
delay(3000);
//Serial.println("Everything is ready!");
}
void loop() {
display.clearDisplay();
float T = bmp.readTemperature();
float P = bmp.readPressure()/100;
float A = bmp.readAltitude(1004);
//打印温度
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.setTextSize(1);
display.print("Temperature");
display.setCursor(0, 18);
display.setTextSize(2);
display.print(T, 1);
display.drawBitmap(40, 17, degree, 16, 16, 1);
display.setCursor(56, 18);
display.setTextSize(2);
display.print("C");
//打印气压
display.setTextSize(1);
display.setCursor(78, 0);
display.print(P, 1);
display.print("hPa");
//打印海拔
display.setCursor(78, 9);
display.print("Alt ");
display.print(A, 0);
display.print("m");
//蓝牙
display.setCursor(108, 25);
display.print("BLE");
if (BT.available()) {
BT.print("温度: ");
BT.print(T, 1);
BT.print("°C, ");
BT.print("气压: ");
BT.print(P, 1);
BT.print("hPa, ");
BT.print("海拔: ");
BT.print(A, 0);
BT.print("m");
//显示BLE可连接
display.drawBitmap(98, 23, yes, 10, 10, 1);
} else {
//显示BLE不可连接
display.drawBitmap(98, 23, no, 10, 10, 1);
}
display.display();
delay(5000);
}
效果图
备注
BMP280的SDO引脚没有连接,此时该模块的地址是0x76(默认是0x77),需要手动在库文件修改。详情参考。还有就是不要下载最新的BMP280库,老版本库的代码便于修改地址。