BLE広告名

This commit is contained in:
Koichi Ito 2026-09-20 18:33:22 +09:00
parent 5e052dba93
commit 787f236fc6
3 changed files with 56 additions and 8 deletions

View File

@ -39,11 +39,19 @@ private:
class SensorLoggerServerCallbacks : public BLEServerCallbacks { class SensorLoggerServerCallbacks : public BLEServerCallbacks {
public: public:
void onConnect(BLEServer*) override {} explicit SensorLoggerServerCallbacks(SensorLogger* logger) : logger_(logger) {}
void onConnect(BLEServer*) override {
logger_->bluetoothConnected_ = true;
}
void onDisconnect(BLEServer* server) override { void onDisconnect(BLEServer* server) override {
logger_->bluetoothConnected_ = false;
server->startAdvertising(); server->startAdvertising();
} }
private:
SensorLogger* logger_;
}; };
void SensorLogger::begin(uint8_t soilPin) { void SensorLogger::begin(uint8_t soilPin) {
@ -61,7 +69,7 @@ void SensorLogger::begin(uint8_t soilPin) {
BLEDevice::init(bluetoothName_.c_str()); BLEDevice::init(bluetoothName_.c_str());
server_ = BLEDevice::createServer(); server_ = BLEDevice::createServer();
server_->setCallbacks(new SensorLoggerServerCallbacks()); server_->setCallbacks(new SensorLoggerServerCallbacks(this));
BLEService* service = server_->createService(kNusServiceUuid); BLEService* service = server_->createService(kNusServiceUuid);
BLECharacteristic* rxCharacteristic = service->createCharacteristic( BLECharacteristic* rxCharacteristic = service->createCharacteristic(
kNusRxUuid, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR); kNusRxUuid, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
@ -94,6 +102,24 @@ bool SensorLogger::update() {
return false; return false;
} }
void SensorLogger::refreshSensors() {
SensorReading reading = {};
sensors_event_t humidityEvent;
sensors_event_t temperatureEvent;
if (aht20Ready) {
aht20.getEvent(&humidityEvent, &temperatureEvent);
reading.temperature = temperatureEvent.temperature;
reading.humidity = humidityEvent.relative_humidity;
}
if (bh1750Ready) {
reading.illuminance = bh1750.readLightLevel();
}
reading.soilMoisture = analogRead(soilPin_);
reading.timestamp = latest_.timestamp;
latest_ = reading;
}
const SensorReading& SensorLogger::latest() const { const SensorReading& SensorLogger::latest() const {
return latest_; return latest_;
} }
@ -102,6 +128,14 @@ bool SensorLogger::hasReading() const {
return logCount_ != 0; return logCount_ != 0;
} }
const char* SensorLogger::bluetoothName() const {
return bluetoothName_.c_str();
}
bool SensorLogger::isBluetoothConnected() const {
return bluetoothConnected_;
}
size_t SensorLogger::logCount() const { size_t SensorLogger::logCount() const {
return logCount_; return logCount_;
} }

View File

@ -19,10 +19,13 @@ public:
void begin(uint8_t soilPin); void begin(uint8_t soilPin);
bool update(); bool update();
void refreshSensors();
const SensorReading& latest() const; const SensorReading& latest() const;
bool hasReading() const; bool hasReading() const;
size_t logCount() const; size_t logCount() const;
bool getCurrentTime(char* buffer, size_t bufferSize) const; bool getCurrentTime(char* buffer, size_t bufferSize) const;
const char* bluetoothName() const;
bool isBluetoothConnected() const;
private: private:
static constexpr unsigned long kMeasureIntervalMs = 3600000UL; static constexpr unsigned long kMeasureIntervalMs = 3600000UL;
@ -46,8 +49,10 @@ private:
unsigned long lastMeasure_ = 0; unsigned long lastMeasure_ = 0;
int64_t epochOffset_ = 0; int64_t epochOffset_ = 0;
bool timeSynchronized_ = false; bool timeSynchronized_ = false;
bool bluetoothConnected_ = false;
String bluetoothName_; String bluetoothName_;
String commandBuffer_; String commandBuffer_;
friend class SensorLoggerCallbacks; friend class SensorLoggerCallbacks;
friend class SensorLoggerServerCallbacks;
}; };

View File

@ -31,24 +31,32 @@ void updateDisplay() {
char sensorLine[32]; char sensorLine[32];
char timeLine[24]; char timeLine[24];
if (sensorLogger.isBluetoothConnected()) {
u8g2.setDrawColor(1);
u8g2.drawBox(0, 0, 128, 10);
u8g2.setDrawColor(0);
}
u8g2.drawStr(0, 9, sensorLogger.bluetoothName());
u8g2.setDrawColor(1);
if (sensorLogger.getCurrentTime(timeLine, sizeof(timeLine))) { if (sensorLogger.getCurrentTime(timeLine, sizeof(timeLine))) {
u8g2.drawStr(0, 9, timeLine); u8g2.drawStr(0, 20, timeLine);
} else { } else {
u8g2.drawStr(0, 9, "RTC: --/--/-- --:--:--"); u8g2.drawStr(0, 20, "RTC: --/--/-- --:--:--");
} }
snprintf(sensorLine, sizeof(sensorLine), "Log: %u", static_cast<unsigned int>(sensorLogger.logCount())); snprintf(sensorLine, sizeof(sensorLine), "Log: %u", static_cast<unsigned int>(sensorLogger.logCount()));
u8g2.drawStr(0, 20, sensorLine); u8g2.drawStr(0, 31, sensorLine);
snprintf(sensorLine, sizeof(sensorLine), "T:%5.1fC H:%5.1f%%", snprintf(sensorLine, sizeof(sensorLine), "T:%5.1fC H:%5.1f%%",
reading.temperature, reading.humidity); reading.temperature, reading.humidity);
u8g2.drawStr(0, 32, sensorLine); u8g2.drawStr(0, 42, sensorLine);
snprintf(sensorLine, sizeof(sensorLine), "Light: %6.1f lx", reading.illuminance); snprintf(sensorLine, sizeof(sensorLine), "Light: %6.1f lx", reading.illuminance);
u8g2.drawStr(0, 44, sensorLine); u8g2.drawStr(0, 53, sensorLine);
snprintf(sensorLine, sizeof(sensorLine), "Soil: %4u", reading.soilMoisture); snprintf(sensorLine, sizeof(sensorLine), "Soil: %4u", reading.soilMoisture);
u8g2.drawStr(0, 56, sensorLine); u8g2.drawStr(0, 64, sensorLine);
u8g2.sendBuffer(); u8g2.sendBuffer();
return; return;
} }
@ -171,6 +179,7 @@ void loop() {
if ((!bluetoothConnected || !musicPlaying) if ((!bluetoothConnected || !musicPlaying)
&& (newReading || now - lastDisplayUpdate >= 1000UL)) { && (newReading || now - lastDisplayUpdate >= 1000UL)) {
lastDisplayUpdate = now; lastDisplayUpdate = now;
sensorLogger.refreshSensors();
updateDisplay(); updateDisplay();
} }
} }