COCOA COUNTER SPIFFS LOG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <M5StickC.h> | |
#include <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEScan.h> | |
#include <BLEAdvertisedDevice.h> | |
#include "FS.h" | |
#include "SPIFFS.h" | |
/* You only need to format SPIFFS the first time you run a | |
test or else use the SPIFFS plugin to create a partition | |
https://github.com/me-no-dev/arduino-esp32fs-plugin */ | |
#define FORMAT_SPIFFS_IF_FAILED true | |
//==================SPIFFS=================================--- | |
String dataStr; | |
String dataStr_1; | |
char char_array[]={}; | |
int datalen; | |
int timing; | |
void writeFile(fs::FS &fs, const char * path, const char * message){ | |
Serial.printf("Writing file: %s\r\n", path); | |
File file = fs.open(path, FILE_WRITE); | |
if(!file){ | |
Serial.println("- failed to open file for writing"); | |
return; | |
} | |
if(file.print(message)){ | |
Serial.println("- file written"); | |
} else { | |
Serial.println("- write failed"); | |
} | |
file.close(); | |
} | |
void appendFile(fs::FS &fs, const char * path, const char * message){ | |
//Serial.printf("Appending to file: %s\r\n", path); | |
File file = fs.open(path, FILE_APPEND); | |
if(!file){ | |
Serial.println("- failed to open file for appending"); | |
return; | |
} | |
if(file.print(message)){ | |
//Serial.println("- message appended"); | |
} else { | |
Serial.println("- append failed"); | |
} | |
file.close(); | |
} | |
void readFile(fs::FS &fs, const char * path){ | |
Serial.printf("Reading file: %s\r\n", path); | |
File file = fs.open(path); | |
if(!file || file.isDirectory()){ | |
Serial.println("- failed to open file for reading"); | |
return; | |
} | |
Serial.println("- read from file:"); | |
while(file.available()){ | |
Serial.write(file.read()); | |
} | |
file.close(); | |
} | |
void deleteFile(fs::FS &fs, const char * path){ | |
Serial.printf("Deleting file: %s\r\n", path); | |
if(fs.remove(path)){ | |
Serial.println("- file deleted"); | |
} else { | |
Serial.println("- delete failed"); | |
} | |
} | |
//=========================================================================== | |
int scanTime = 4; //BLEタイムアウト | |
BLEScan* pBLEScan; | |
// 接触確認アプリのUUID | |
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb"; | |
int deviceNum = 0; | |
int deviceNum_1=0; | |
int Mtime=0; | |
int Mtime_1=0; | |
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { | |
void onResult(BLEAdvertisedDevice advertisedDevice) { | |
if(advertisedDevice.haveServiceUUID()){ | |
//Serial.println(advertisedDevice.getAddress().toString().c_str()); | |
if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){ | |
int rssi = advertisedDevice.getRSSI(); | |
Serial.print("-------rssi="); | |
Serial.println(rssi); | |
int Dbm = advertisedDevice.getTXPower(); | |
String adrs=advertisedDevice.getAddress().toString().c_str(); | |
Serial.print("adrs="); | |
Serial.println(adrs); | |
timing=int(millis()/1000); | |
Serial.print("timing="); | |
Serial.println(timing); | |
deviceNum++; | |
Serial.print("deviceNum="); | |
Serial.println(deviceNum); | |
dataStr=""; | |
dataStr=String(timing)+","+String(rssi)+","+String(adrs)+","+String(deviceNum)+"\n\r"; | |
Serial.print("dataStr="); | |
Serial.println(dataStr); | |
int dataS_len=dataStr.length()+1; | |
char char_array[dataS_len]; | |
dataStr.toCharArray(char_array,dataS_len); | |
Serial.print("char_array="); | |
Serial.println(char_array); | |
appendFile(SPIFFS, "/hello.txt",char_array); | |
delay(1000); | |
} | |
} | |
} | |
}; | |
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(3); | |
M5.Lcd.setTextColor(WHITE); | |
M5.Lcd.printf(" %2d",deviceNum); | |
M5.Lcd.setCursor(0,M5.Lcd.height() - 40); | |
M5.Lcd.setTextSize(2); | |
M5.Lcd.printf("Log:%5d sec",timing); | |
/* | |
M5.Lcd.setTextSize(1); | |
M5.Lcd.setCursor(0, M5.Lcd.height() - 20); | |
M5.Lcd.printf("Bat:%5.1fV ", M5.Axp.GetBatVoltage()); | |
M5.Lcd.printf("Charge:%5.1f\n", M5.Axp.GetBatCurrent()); | |
*/ | |
} | |
void Task1(void *pvParameters) { | |
// loop()に書くとBLEスキャン中M5.update()が実行されなくてボタンが取れないのでマルチスレッド化している | |
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(12);//best brightness | |
M5.Lcd.fillScreen(BLACK); | |
//====SPIFFS Init============================= | |
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ | |
Serial.println("SPIFFS Mount Failed"); | |
return; | |
} | |
writeFile(SPIFFS, "/hello.txt", "sec,rssi,address,devNum\n\r "); | |
//============================================= | |
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); // スキャン間隔5秒 | |
pBLEScan->setWindow(4999); // less or equal setInterval value | |
xTaskCreatePinnedToCore(Task1,"Task1", 4096, NULL, 3, NULL, 1); | |
} | |
void loop() { | |
M5.update(); | |
if ( M5.BtnB.wasReleased() ) { | |
readFile(SPIFFS, "/hello.txt"); | |
} | |
if ( M5.BtnA.wasReleased() ) { | |
deleteFile(SPIFFS, "/hello.txt"); | |
esp_restart(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment