I made device that counting COCOA(COVID-19 Contact-Confirming Application) around M5StickC
M5StickC https://www.switch-science.com/catalog/5517/
Mr. MIKUMIN-P https://twitter.com/ksasao made sketch to find out COCOA
M5ATOM で接触確認アプリが有効かどうかを調べるやつをつくった。有効だと素早く点滅する。モバイルバッテリーで動作。 pic.twitter.com/aOdHc8Z5B6
接触確認アプリが有効になっているかを調べるアプリですGitHubhttps://gist.github.com/ksasao/0da6437d3eac9b2dbd675b6fee5d1117
I edited it.
It flows
execute pBLEScan->start(scanTime, false)
↓
execute automatically BLEAdvertisedDeviceCallbacks onResult() number of BLE devices found.
then count COCOA's UUID
↓
return from start()
it counts COCOA'S UUID in BLEAdvertisedDeviceCallbacks
// COCOA's UUID
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";
int deviceNum = 0;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if(advertisedDevice.haveServiceUUID()){
if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){
// if COCOA's UUID then deviceNum++
int rssi = advertisedDevice.getRSSI();
Serial.print("RSSI: ");
Serial.println(rssi);
Serial.print("ADDR: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
Serial.println("Found!");
deviceNum++;
}
}
}
};
void loop() {
deviceNum = 0;
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
pBLEScan->clearResults();
drawScreen();
}
Display number of COCOADisplay with M5StickC.
pls customize as you like.
void drawScreen() {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(RED);
M5.Lcd.print("COCOA Counter\n");
M5.Lcd.setTextSize(7);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.printf(" %2d",deviceNum); // Number of COCOA
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0, M5.Lcd.height() - 10);
// Voltage of battery(Auto power off near 3.3V)
M5.Lcd.printf("Bat:%5.1fV ", M5.Axp.GetBatVoltage());
// State of charge(Battery full makes 0)
M5.Lcd.printf("Charge:%5.1f\n",
M5.Axp.GetBatCurrent());
}
Whole of code with M5StickCPushing Button of "M5" makes POWER OFF
#include <Arduino.h>
#include <M5StickC.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 4; //BLEタイムアウト
BLEScan* pBLEScan;
// 接触確認アプリのUUID
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";
int deviceNum = 0;
bool dark = false;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if(advertisedDevice.haveServiceUUID()){
if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){
int rssi = advertisedDevice.getRSSI();
Serial.print("RSSI: ");
Serial.println(rssi);
Serial.print("ADDR: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
Serial.println("Found!");
deviceNum++;
}
}
}
};
void drawScreen() {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(RED);
M5.Lcd.print("COCOA Counter\n");
M5.Lcd.setTextSize(7);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.printf(" %2d",deviceNum);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0, M5.Lcd.height() - 10);
M5.Lcd.printf("Bat:%5.1fV ", M5.Axp.GetBatVoltage());
M5.Lcd.printf("Charge:%5.1f\n", M5.Axp.GetBatCurrent());
}
void Task1(void *pvParameters) {
while(1) {
deviceNum = 0;
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(deviceNum);
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
drawScreen();
}
}
void setup() {
M5.begin();
Serial.begin(115200);
Serial.println("Scanning...");
M5.Lcd.setRotation(1);
M5.Axp.ScreenBreath(8);
M5.Lcd.fillScreen(BLACK);
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(5000);
pBLEScan->setWindow(4999); // less or equal setInterval value
xTaskCreatePinnedToCore(Task1,"Task1", 4096, NULL, 3, NULL, 1);
}
void loop() {
M5.update();
if ( M5.BtnA.wasReleased() ) {
M5.Axp.PowerOff();
}
}
Comments