Fix device info

This commit is contained in:
Christoph Hagen 2023-07-03 09:43:47 +02:00
parent 7c2162a5bf
commit b082384dd7
5 changed files with 78 additions and 70 deletions

View File

@ -40,4 +40,6 @@ void temperatureConfigure();
void temperaturePerformUpdate(Temperature* temperatures); void temperaturePerformUpdate(Temperature* temperatures);
uint8_t* getSensorAddress(uint8_t index);
void copySensorAddress(uint8_t index, uint8_t* buffer); void copySensorAddress(uint8_t index, uint8_t* buffer);

View File

@ -226,96 +226,94 @@ uint16_t readNumberFromReceivedBuffer(uint8_t* buffer) {
return *((uint16_t*) buffer + 1); return *((uint16_t*) buffer + 1);
} }
void inline addToBluetoothBuffer(const void* data, size_t byteCount) {
memcpy(bluetoothDataBuffer + bluetoothDataCount, data, byteCount);
bluetoothDataCount += byteCount;
}
void inline addValueToBluetoothBuffer8(uint8_t value) {
bluetoothDataBuffer[bluetoothDataCount] = value;
bluetoothDataCount += sizeof(uint8_t);
}
void inline addValueToBluetoothBuffer16(uint16_t value) {
addToBluetoothBuffer(&value, sizeof(uint16_t));
}
void inline addValueToBluetoothBuffer32(uint32_t value) {
addToBluetoothBuffer(&value, sizeof(uint32_t));
}
void inline addSensorToBluetoothBuffer(uint8_t index) {
// Temperature sensor addresses
uint8_t* address = getSensorAddress(index);
addToBluetoothBuffer(address, TEMPERATURE_SENSOR_ADDRESS_SIZE);
// The last temperatures
uint8_t tempByte = getLastTemperature(index);
addValueToBluetoothBuffer8(tempByte);
// Time since measurement of sensor
uint16_t timeSeconds = getTimeSinceValidTemperature(index);
addValueToBluetoothBuffer16(timeSeconds);
}
void fillInfo() { void fillInfo() {
uint16_t value; uint16_t value16;
uint32_t value32; uint32_t value32;
bluetoothDataCount = 0;
// The unique ID generated on power-on // The unique ID generated on power-on
value32 = getUniqueID(); value32 = getUniqueID();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t)); addValueToBluetoothBuffer32(value32);
bluetoothDataCount += sizeof(uint32_t);
// the number of bytes as a uint16_t (2 bytes) // the number of bytes as a uint16_t (2 bytes)
value = getTotalNumberOfStoredBytes(); value16 = getTotalNumberOfStoredBytes();
memcpy(bluetoothDataBuffer, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount = sizeof(uint16_t);
// the number of seconds until the next measurement as a uint16_t (2 bytes) // the number of seconds until the next measurement as a uint16_t (2 bytes)
value = secondsUntilNextTemperatureMeasurement(); value16 = secondsUntilNextTemperatureMeasurement();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// the number of seconds between measurements as a uint16_t (2 bytes) // the number of seconds between measurements as a uint16_t (2 bytes)
value = temperatureMeasurementIntervalSeconds; value16 = temperatureMeasurementIntervalSeconds;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// the number of measurements as a uint16_t (2 bytes) // the number of measurements as a uint16_t (2 bytes)
value = getNumberOfMeasurements(); value16 = getNumberOfMeasurements();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// Total number of measurements since start // Total number of measurements since start
value32 = getTotalNumberOfMeasurements(); value32 = getTotalNumberOfMeasurements();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t)); addValueToBluetoothBuffer32(value32);
bluetoothDataCount += sizeof(uint32_t);
// the maximum number of bytes that can be copied // the maximum number of bytes that can be copied
value = bluetoothMaxDataSize; value16 = bluetoothMaxDataSize;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// Total storage size of device // Total storage size of device
value = rtcStorageSize; value16 = rtcStorageSize;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// the number of seconds since power on as a uint32_t (4 bytes) // the number of seconds since power on as a uint32_t (4 bytes)
value32 = time(NULL); value32 = time(NULL);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t)); addValueToBluetoothBuffer32(value32);
bluetoothDataCount += sizeof(uint32_t);
// Start time of current recording // Start time of current recording
value32 = getStartTimeOfCurrentRecording(); value32 = getStartTimeOfCurrentRecording();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t)); addValueToBluetoothBuffer32(value32);
bluetoothDataCount += sizeof(uint32_t);
// Checksum for all data // Checksum for all data
value = getDataChecksum(); value16 = getDataChecksum();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); addValueToBluetoothBuffer16(value16);
bluetoothDataCount += sizeof(uint16_t);
// The wakeup cause for the current run // The wakeup cause for the current run
bluetoothDataBuffer[bluetoothDataCount] = getWakeupCause(); uint8_t wakeCause = getWakeupCause();
bluetoothDataCount += sizeof(uint8_t); addValueToBluetoothBuffer8(wakeCause);
// Sensor 0 // Sensor 0
addSensorToBluetoothBuffer(0);
// Temperature sensor addresses addSensorToBluetoothBuffer(1);
copySensorAddress(0, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
// The last temperatures
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(0);
bluetoothDataCount += sizeof(uint8_t);
// Time since measurement of sensor 0
value = getTimeSinceValidTemperature(0);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
// Sensor 1
copySensorAddress(1, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(1);
bluetoothDataCount += sizeof(uint8_t);
// Time since measurement of sensor 1
value = getTimeSinceValidTemperature(1);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
setResponse(BluetoothResponse::success); setResponse(BluetoothResponse::success);
} }

View File

@ -15,8 +15,6 @@ constexpr uint32_t delayWaitingForNextMeasurementMS = 50;
// Time is measured in seconds since power-on via RTC clock // Time is measured in seconds since power-on via RTC clock
RTC_DATA_ATTR uint32_t nextTimeToMeasureTemperatureSeconds = 0; RTC_DATA_ATTR uint32_t nextTimeToMeasureTemperatureSeconds = 0;
RTC_DATA_ATTR bool isFirstStart = true;
// The unique ID generated whenever the device is powered on // The unique ID generated whenever the device is powered on
RTC_DATA_ATTR uint32_t uniqueID; RTC_DATA_ATTR uint32_t uniqueID;
@ -134,11 +132,15 @@ void enableLED() {
} }
void setup() { void setup() {
esp_sleep_wakeup_cause_t wakeupCause = esp_sleep_get_wakeup_cause();
wakeupCauseByte = static_cast<uint8_t>(wakeupCause);
bool isFirstStart = wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED;
// No need for serial output in production // No need for serial output in production
Serial.begin(serialBaudRate); Serial.begin(serialBaudRate);
if (isFirstStart) { if (isFirstStart) {
// Generate unique ID on device start // Power on, generate unique ID on device start
uniqueID = random(); uniqueID = random();
} }
@ -154,8 +156,6 @@ void setup() {
storageConfigure(isFirstStart); storageConfigure(isFirstStart);
// Configure bluetooth if wake button was pressed // Configure bluetooth if wake button was pressed
esp_sleep_wakeup_cause_t wakeupCause = esp_sleep_get_wakeup_cause();
wakeupCauseByte = static_cast<uint8_t>(wakeupCause);
if (wakeupCause == ESP_SLEEP_WAKEUP_EXT0 || isFirstStart) { if (wakeupCause == ESP_SLEEP_WAKEUP_EXT0 || isFirstStart) {
Serial.println("First start or button press"); Serial.println("First start or button press");
bluetoothConfigure(); bluetoothConfigure();
@ -167,7 +167,6 @@ void setup() {
temperatureConfigure(); temperatureConfigure();
Serial.println("Setup complete"); Serial.println("Setup complete");
isFirstStart = false;
} }
void loop() { void loop() {

View File

@ -62,6 +62,12 @@ uint8_t byteForAbsoluteTemperature(Temperature* temp) {
return converted; return converted;
} }
void saveByte(uint8_t byte) {
data[dataIndex] = byte;
dataIndex += 1;
rtcDataSum += byte; // Update checksum
}
void saveTemperatures(Temperature* temperatures) { void saveTemperatures(Temperature* temperatures) {
if (numberOfMeasurements == 0) { if (numberOfMeasurements == 0) {
// Adjust start time if new measurement is begun // Adjust start time if new measurement is begun
@ -74,16 +80,15 @@ void saveTemperatures(Temperature* temperatures) {
// First: Write timestamp // First: Write timestamp
// Timestamp is number of measurement intervals since start of recording // Timestamp is number of measurement intervals since start of recording
uint16_t timestamp = (time(NULL) - startTimeOfCurrentRecording) / 60; uint16_t timestamp = (time(NULL) - startTimeOfCurrentRecording) / 60;
memcpy(data + dataIndex, &timestamp, sizeof(uint16_t)); uint8_t* ptr = (uint8_t*) &timestamp;
rtcDataSum += data[dataIndex] + data[dataIndex+1]; // Update checksum saveByte(ptr[0]);
dataIndex += sizeof(uint16_t); saveByte(ptr[1]);
// Then: Write bytes for each sensor
for (uint8_t sensorIndex = 0; sensorIndex < temperatureSensorCount; sensorIndex += 1) { for (uint8_t sensorIndex = 0; sensorIndex < temperatureSensorCount; sensorIndex += 1) {
Temperature* temp = &temperatures[sensorIndex]; Temperature* temp = &temperatures[sensorIndex];
uint8_t byte = byteForAbsoluteTemperature(temp); uint8_t byte = byteForAbsoluteTemperature(temp);
data[dataIndex] = byte; saveByte(byte);
dataIndex += 1;
rtcDataSum += byte; // Update checksum
lastMeasurements[sensorIndex] = byte; lastMeasurements[sensorIndex] = byte;
if (temp->status == TemperatureStatus::temperatureIsValid) { if (temp->status == TemperatureStatus::temperatureIsValid) {
// Only update if temperature is valid // Only update if temperature is valid

View File

@ -89,6 +89,10 @@ void removeSensorAtIndex(uint8_t sensorIndex) {
availableSensorCount -= 1; availableSensorCount -= 1;
} }
uint8_t* getSensorAddress(uint8_t index) {
return sensors[index].address;
}
void copySensorAddress(uint8_t index, uint8_t* buffer) { void copySensorAddress(uint8_t index, uint8_t* buffer) {
memcpy(buffer, sensors[index].address, TEMPERATURE_SENSOR_ADDRESS_SIZE); memcpy(buffer, sensors[index].address, TEMPERATURE_SENSOR_ADDRESS_SIZE);
} }