diff --git a/firmware/SensorLogger.cpp b/firmware/SensorLogger.cpp index 8ef22f3..98ee1d6 100644 --- a/firmware/SensorLogger.cpp +++ b/firmware/SensorLogger.cpp @@ -47,6 +47,8 @@ public: void onDisconnect(BLEServer* server) override { logger_->bluetoothConnected_ = false; + logger_->commandBuffer_ = ""; + logger_->lastAdvertise_ = millis(); server->startAdvertising(); } @@ -88,12 +90,18 @@ void SensorLogger::begin(uint8_t soilPin) { readAndStore(); lastMeasure_ = millis(); + lastAdvertise_ = millis(); } bool SensorLogger::update() { processBluetooth(); unsigned long now = millis(); + if (!bluetoothConnected_ && now - lastAdvertise_ >= kAdvertiseRetryIntervalMs) { + lastAdvertise_ = now; + BLEDevice::startAdvertising(); + } + if (now - lastMeasure_ >= kMeasureIntervalMs) { lastMeasure_ = now; readAndStore(); @@ -140,12 +148,11 @@ size_t SensorLogger::logCount() const { return logCount_; } -bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const { +bool SensorLogger::formatTimestamp(int64_t timestamp, char* buffer, size_t bufferSize) const { if (bufferSize == 0) { return false; } - int64_t timestamp = currentTimestamp(); if (timeSynchronized_) { time_t unixTimestamp = static_cast(timestamp); struct tm date; @@ -177,6 +184,10 @@ bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const { return true; } +bool SensorLogger::getCurrentTime(char* buffer, size_t bufferSize) const { + return formatTimestamp(currentTimestamp(), buffer, bufferSize); +} + void SensorLogger::readAndStore() { sensors_event_t humidityEvent; sensors_event_t temperatureEvent; @@ -230,6 +241,23 @@ void SensorLogger::receiveBluetoothData(const String& data) { } } +void SensorLogger::applyHistoricalTimeAdjustment(int64_t epoch) { + if (historicalAdjustmentApplied_ || timeSynchronized_) { + return; + } + + const int64_t elapsedSeconds = millis() / 1000UL; + historicalEpochOffset_ = epoch - elapsedSeconds; + for (size_t index = 0; index < logCount_; ++index) { + size_t logIndex = (logStart_ + index) % kMaxLogEntries; + const int64_t storedSeconds = log_[logIndex].timestamp; + if (storedSeconds <= elapsedSeconds) { + log_[logIndex].timestamp = storedSeconds + historicalEpochOffset_; + } + } + historicalAdjustmentApplied_ = true; +} + void SensorLogger::processCommand(const String& command) { if (command == "GET_LOG") { sendLog(); @@ -257,6 +285,7 @@ void SensorLogger::processCommand(const String& command) { epoch = static_cast(mktime(&date)); } if (epoch != 0) { + applyHistoricalTimeAdjustment(static_cast(epoch)); epochOffset_ = epoch - millis() / 1000UL; timeSynchronized_ = true; txCharacteristic_->setValue("OK_TIME\n"); @@ -269,17 +298,22 @@ void SensorLogger::processCommand(const String& command) { } void SensorLogger::sendLog() { - sendNotification("timestamp,temp,humid,light,soil,device\n"); + sendNotification("タイムスタンプ,温度,湿度,土壌水分,光量,デバイス名\n"); for (size_t index = 0; index < logCount_; ++index) { size_t logIndex = (logStart_ + index) % kMaxLogEntries; const SensorReading& reading = log_[logIndex]; - char line[128]; - snprintf(line, sizeof(line), "%lld,%.2f,%.2f,%.2f,%u,%s\n", - reading.timestamp, + char timestamp[32]; + char line[160]; + const bool timestampValid = formatTimestamp(reading.timestamp, timestamp, sizeof(timestamp)); + if (!timestampValid) { + timestamp[0] = '\0'; + } + snprintf(line, sizeof(line), "%s,%.2f,%.2f,%u,%.2f,%s\n", + timestamp, reading.temperature, reading.humidity, - reading.illuminance, reading.soilMoisture, + reading.illuminance, bluetoothName_.c_str()); sendNotification(line); delay(20); diff --git a/firmware/SensorLogger.h b/firmware/SensorLogger.h index fedfff1..67d3ad2 100644 --- a/firmware/SensorLogger.h +++ b/firmware/SensorLogger.h @@ -24,11 +24,13 @@ public: bool hasReading() const; size_t logCount() const; bool getCurrentTime(char* buffer, size_t bufferSize) const; + bool formatTimestamp(int64_t timestamp, char* buffer, size_t bufferSize) const; const char* bluetoothName() const; bool isBluetoothConnected() const; private: - static constexpr unsigned long kMeasureIntervalMs = 3600000UL; + static constexpr unsigned long kMeasureIntervalMs = 10000UL; + static constexpr unsigned long kAdvertiseRetryIntervalMs = 500UL; void readAndStore(); void processBluetooth(); @@ -37,6 +39,7 @@ private: void sendNotification(const char* data); void sendLog(); void clearLog(); + void applyHistoricalTimeAdjustment(int64_t epoch); int64_t currentTimestamp() const; BLEServer* server_ = nullptr; @@ -47,8 +50,11 @@ private: size_t logCount_ = 0; uint8_t soilPin_ = 0; unsigned long lastMeasure_ = 0; + unsigned long lastAdvertise_ = 0; int64_t epochOffset_ = 0; + int64_t historicalEpochOffset_ = 0; bool timeSynchronized_ = false; + bool historicalAdjustmentApplied_ = false; bool bluetoothConnected_ = false; String bluetoothName_; String commandBuffer_;