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);
uint8_t* getSensorAddress(uint8_t index);
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);
}
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() {
uint16_t value;
uint16_t value16;
uint32_t value32;
bluetoothDataCount = 0;
// The unique ID generated on power-on
value32 = getUniqueID();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t);
addValueToBluetoothBuffer32(value32);
// the number of bytes as a uint16_t (2 bytes)
value = getTotalNumberOfStoredBytes();
memcpy(bluetoothDataBuffer, &value, sizeof(uint16_t));
bluetoothDataCount = sizeof(uint16_t);
value16 = getTotalNumberOfStoredBytes();
addValueToBluetoothBuffer16(value16);
// the number of seconds until the next measurement as a uint16_t (2 bytes)
value = secondsUntilNextTemperatureMeasurement();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = secondsUntilNextTemperatureMeasurement();
addValueToBluetoothBuffer16(value16);
// the number of seconds between measurements as a uint16_t (2 bytes)
value = temperatureMeasurementIntervalSeconds;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = temperatureMeasurementIntervalSeconds;
addValueToBluetoothBuffer16(value16);
// the number of measurements as a uint16_t (2 bytes)
value = getNumberOfMeasurements();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = getNumberOfMeasurements();
addValueToBluetoothBuffer16(value16);
// Total number of measurements since start
value32 = getTotalNumberOfMeasurements();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t);
addValueToBluetoothBuffer32(value32);
// the maximum number of bytes that can be copied
value = bluetoothMaxDataSize;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = bluetoothMaxDataSize;
addValueToBluetoothBuffer16(value16);
// Total storage size of device
value = rtcStorageSize;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = rtcStorageSize;
addValueToBluetoothBuffer16(value16);
// the number of seconds since power on as a uint32_t (4 bytes)
value32 = time(NULL);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t);
addValueToBluetoothBuffer32(value32);
// Start time of current recording
value32 = getStartTimeOfCurrentRecording();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t);
addValueToBluetoothBuffer32(value32);
// Checksum for all data
value = getDataChecksum();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
value16 = getDataChecksum();
addValueToBluetoothBuffer16(value16);
// The wakeup cause for the current run
bluetoothDataBuffer[bluetoothDataCount] = getWakeupCause();
bluetoothDataCount += sizeof(uint8_t);
uint8_t wakeCause = getWakeupCause();
addValueToBluetoothBuffer8(wakeCause);
// Sensor 0
// Temperature sensor addresses
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);
addSensorToBluetoothBuffer(0);
addSensorToBluetoothBuffer(1);
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
RTC_DATA_ATTR uint32_t nextTimeToMeasureTemperatureSeconds = 0;
RTC_DATA_ATTR bool isFirstStart = true;
// The unique ID generated whenever the device is powered on
RTC_DATA_ATTR uint32_t uniqueID;
@ -134,11 +132,15 @@ void enableLED() {
}
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
Serial.begin(serialBaudRate);
if (isFirstStart) {
// Generate unique ID on device start
// Power on, generate unique ID on device start
uniqueID = random();
}
@ -154,8 +156,6 @@ void setup() {
storageConfigure(isFirstStart);
// 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) {
Serial.println("First start or button press");
bluetoothConfigure();
@ -167,7 +167,6 @@ void setup() {
temperatureConfigure();
Serial.println("Setup complete");
isFirstStart = false;
}
void loop() {

View File

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

View File

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