Skip to content

Commit 20e0a93

Browse files
authoredApr 21, 2020
Added BLE Notif
Added notification of Temp + Hpa.
1 parent 39f71bc commit 20e0a93

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed
 

‎M5_BMP280.ino

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <M5Stack.h>
1+
#include <ESP32-Chimera-Core.h>
22
#include "Free_Fonts.h"
33
#include "Seeed_BMP280.h"
44
#include <Wire.h>
@@ -11,7 +11,7 @@ BLECharacteristic *pCharacteristic;
1111
bool deviceConnected = false;
1212
BMP280 bmp280;
1313
double t0, t1;
14-
float seaLevel = 1010.4;
14+
float seaLevel = 1013.3;
1515
float readAltitude(float SL, float pressure) {
1616
float atmospheric = pressure / 100.0F;
1717
return 44330.0 * (1.0 - pow(atmospheric / SL, 0.1903));
@@ -23,13 +23,14 @@ float readAltitude(float SL, float pressure) {
2323
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
2424
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
2525
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
26-
26+
// TODO
27+
// Add notif characteristic
28+
// Add menu
2729

2830
class MyServerCallbacks: public BLEServerCallbacks {
2931
void onConnect(BLEServer* pServer) {
3032
deviceConnected = true;
3133
};
32-
3334
void onDisconnect(BLEServer* pServer) {
3435
deviceConnected = false;
3536
}
@@ -57,21 +58,22 @@ class MyCallbacks: public BLECharacteristicCallbacks {
5758
};
5859

5960
void buttons_test() {
60-
if (M5.BtnA.wasPressed()) {
61+
if (M5.BtnA.isPressed()) {
6162
Serial.printf("-0.10 HPa");
6263
seaLevel -= 0.1;
6364
displayBMP();
65+
delay(200);
6466
}
65-
if (M5.BtnB.wasPressed()) {
67+
if (M5.BtnB.isPressed()) {
6668
Serial.printf("B");
6769
}
68-
if (M5.BtnC.wasPressed()) {
70+
if (M5.BtnC.isPressed()) {
6971
Serial.printf("+0.10 HPa");
7072
seaLevel += 0.1;
7173
displayBMP();
74+
delay(200);
7275
}
7376
}
74-
7577
void setup() {
7678
Serial.begin(115200);
7779
delay(1000);
@@ -86,12 +88,10 @@ void setup() {
8688
Serial.println("Device error!");
8789
M5.Lcd.drawString(F("Starting BMP280 failed!"), 24, 57, GFXFF);
8890
M5.Lcd.drawString(F("Cannot do anything!"), 24, 82, GFXFF);
89-
M5.Lcd.drawJpgFile(SD, "/XMark20.jpg", 2, 55);
9091
while (1);
9192
}
9293
Serial.println(F("BMP280 init succeeded."));
9394
M5.Lcd.drawString(F("BMP280 init succeeded."), 24, 57, GFXFF);
94-
M5.Lcd.drawJpgFile(SD, "/Check20.jpg", 2, 55);
9595
// Create the BLE Device
9696
BLEDevice::init("BMP Sea Level Service");
9797
// Create the BLE Server
@@ -114,6 +114,8 @@ void setup() {
114114

115115
void displayBMP() {
116116
float pressure, temp, alt;
117+
char txString[23];
118+
String ret;
117119
M5.Lcd.fillRect(0, 100, 320, 137, TFT_WHITE);
118120
//get and print temperatures
119121
Serial.print("Temp: ");
@@ -123,15 +125,23 @@ void displayBMP() {
123125

124126
//get and print atmospheric pressure data
125127
Serial.print("Pressure: ");
126-
Serial.print(pressure = bmp280.getPressure());
127-
Serial.println("Pa");
128+
pressure = bmp280.getPressure();
129+
Serial.print(pressure / 100.0);
130+
Serial.println("HPa");
128131

129132
//get and print altitude data
130133
Serial.print("Altitude: ");
131134
alt = readAltitude(seaLevel, pressure);
132135
Serial.print(alt);
133136
Serial.println("m");
134137
Serial.println("\n");
138+
ret = String(temp, 2) + "C @ " + String(alt, 2) + "m";
139+
ret.toCharArray(txString, ret.length() + 1);
140+
pCharacteristic->setValue(txString);
141+
pCharacteristic->notify(); // Send the value to the app!
142+
Serial.print("*** Sent Value: ");
143+
Serial.print(txString);
144+
Serial.println(" ***");
135145

136146
uint8_t linePos = 110;
137147
M5.Lcd.setFreeFont(FSSB9);
@@ -156,8 +166,9 @@ void displayBMP() {
156166

157167
void loop() {
158168
t1 = millis() - t0;
159-
if (t1 > 4999) {
169+
if (t1 > 9999) {
160170
displayBMP();
171+
t0 = millis();
161172
}
162173
buttons_test();
163174
M5.update(); // 好importantですね!

0 commit comments

Comments
 (0)
Please sign in to comment.