[code]
/*Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
/*
Thanks to https://gist.github.com/ksasao/0da6437d3eac9b2dbd675b6fee5d1117
by https://gist.github.com/ksasao
*/
/*
SD card wrie routine
https://raspberrypi.mongonta.com/howto-write-csv-to-sdcard-on-m5stack/
*/
//////////////////////////////////////////////////////////////////////////////////
#include <M5Stack.h>
#include <BLEDevice.h>
//#include <BLEUtils.h>
//#include <BLEScan.h>
//#include <BLEAdvertisedDevice.h>
//////////////////////////////////////////////////////////////////////////////////
/*
note: need add library Adafruit_BMP280 & Adafruit_SHT31 from library manage
*/
#include <Adafruit_SHT31.h>
#include <Wire.h> //The SHT31 uses I2C comunication.
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
//////////////////////////////////////////////////////////////////////////////////
#include <SPI.h>
#include <SD.h>
//////////////////////////////////////////////////////////////////////////////////
Adafruit_SHT31 sht31 = Adafruit_SHT31();
Adafruit_BMP280 bme;
//time out in seconds
int scanTime = 5;
BLEScan* pBLEScan;
const int chipSelect = 4;
//接触確認アプリのUUID
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";
int cocoaCnt = 0;
//log file name
const char* logfile = "/datalog.txt";
//文字列
const char* sumdevStr = "Sum";
const char* cocoaStr = " Cocoa";
const char* envStr = "Temp: %3.1fC\r\nHumid: %3.1f%%\r\nPress: %3.0fhPa\r\n";
//RTC買うまで固定
const char* dateStr = "2020.07.15 10:10:00 ";
//Sensor flag
bool hasSHT31 = false;
bool hasBMP280 = false;
float tmp = 100;
float hum = 0;
float pressure = 0;
//Other flags,Params
bool onBeep = true;
int bright = 20;
int brightPitch = 10;
//////////////////////////////////////////////////////////////////////////////////
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveServiceUUID()) {
if (strncmp(advertisedDevice.getServiceUUID().toString().c_str(), uuid, 36) == 0) {
cocoaCnt++;
if (onBeep) {
M5.Speaker.beep();
delay(10);
M5.Speaker.mute();
}
M5.Lcd.setCursor(0, 110);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(GREEN, BLACK);
M5.Lcd.printf("%d ", cocoaCnt);
M5.Lcd.setTextSize(1);
}
}
M5.Lcd.println(advertisedDevice.toString().c_str());
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(WHITE, BLACK);
}
};
//////////////////////////////////////////////////////////////////////////////////
void setup() {
// Initialize the M5Stack
M5.begin();
M5.Power.begin();
M5.Lcd.setBrightness(bright);
M5.Lcd.setTextSize(1);
M5.Lcd.println("Hello!, cacoa BLE Scan");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
Wire.begin();
Serial.println(F("ENV2 Unit(SHT31 and BMP280) test..."));
if (sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
hasSHT31 = true;
Serial.println("Found SHT31");
Serial.print("Heater Enabled State: ");
if (sht31.isHeaterEnabled())
Serial.println("ENABLED");
else
Serial.println("DISABLED");
}
if (bme.begin(0x76)) {
hasBMP280 = true;
Serial.println("Found a valid BMP280 sensor");
}
}
void loop() {
// print all found BLE devices
M5.Lcd.setTextSize(1);
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
// clear screen and set cursor to the top
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.println(dateStr);
// print counts of BLE devices
int sumdev = foundDevices.getCount();
M5.Lcd.setTextSize(3);
M5.Lcd.print(sumdevStr);
M5.Lcd.setTextColor(RED, BLACK);
M5.Lcd.print(sumdev);
// print count of cocoa APPs
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.print(cocoaStr);
M5.Lcd.setTextColor(GREEN, BLACK);
M5.Lcd.println(cocoaCnt);
// print env2 data
if (hasSHT31) {
tmp = sht31.readTemperature();
hum = sht31.readHumidity();
}
if (hasBMP280) {
pressure = bme.readPressure() / 100;
// hPa = Pa / 100;
}
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.printf(envStr, tmp, hum, pressure);
// print to the serial port too
Serial.println(dateStr);
Serial.print(sumdevStr);
Serial.println(sumdev);
Serial.print(cocoaStr);
Serial.println(cocoaCnt);
Serial.printf(envStr, tmp, hum, pressure);
// SDカードへの書き込み処理(ファイル追加モード)
// SD.beginはM5.begin内で処理されているので不要
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dfile = SD.open(logfile, FILE_APPEND);
// if the file is available, write to it:
if (dfile) {
dfile.println(dateStr);
dfile.print(sumdevStr);
dfile.print(sumdev);
dfile.print(cocoaStr);
dfile.println(cocoaCnt);
dfile.printf(envStr, tmp, hum, pressure);
dfile.close();
}
//Button controll
M5.Lcd.setTextSize(3);
M5.Lcd.println("");
M5.Lcd.setTextColor(BLUE, WHITE);
M5.Lcd.println("A: Beep on/off");
M5.Lcd.println("B: DOWN bright");
M5.Lcd.println("C: UP bright");
int timer = 100;
while (timer--) {
if (M5.BtnA.wasReleased()) { //Aボタンでbeepをon/off切り替える
onBeep = !onBeep;
} else if (M5.BtnB.wasReleased()) { //Bボタンで輝度を下げる
bright -= brightPitch;
} else if (M5.BtnC.wasReleased()) { //Cボタンで輝度を上げる
bright += brightPitch;
}
M5.Lcd.setBrightness(bright);
delay(70);
M5.update(); // update button state
}
//init for next loop
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setCursor(0, 110);
M5.Lcd.setTextSize(1);
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
cocoaCnt = 0;
}
// Arranged and written by 柴田(ひ)
[/code]
0 件のコメント:
コメントを投稿