diff --git a/example/logger/logger.py b/example/logger/logger.py index de8a720..2aafd3f 100644 --- a/example/logger/logger.py +++ b/example/logger/logger.py @@ -70,7 +70,7 @@ class OdroidPowerLogger: # Write header header = [ - 'timestamp', 'uptime_sec', + 'timestamp', 'uptime_ms', 'vin_voltage', 'vin_current', 'vin_power', 'main_voltage', 'main_current', 'main_power', 'usb_voltage', 'usb_current', 'usb_power' @@ -97,10 +97,10 @@ class OdroidPowerLogger: # Process only if the payload type is 'sensor_data' if status_message.WhichOneof('payload') == 'sensor_data': sensor_data = status_message.sensor_data - ts_dt = datetime.fromtimestamp(sensor_data.timestamp) + ts_dt = datetime.fromtimestamp(sensor_data.timestamp_ms / 1000) ts_str = ts_dt.strftime('%Y-%m-%d %H:%M:%S') - print(f"--- {ts_str} (Uptime: {sensor_data.uptime_sec}s) ---") + print(f"--- {ts_str} (Uptime: {sensor_data.uptime_ms / 1000}s) ---") # Print data for each channel for name, channel in [('VIN', sensor_data.vin), ('MAIN', sensor_data.main), @@ -111,7 +111,7 @@ class OdroidPowerLogger: # Write to CSV if enabled if csv_writer: row = [ - ts_dt.isoformat(), sensor_data.uptime_sec, + ts_dt.isoformat(), sensor_data.uptime_ms, sensor_data.vin.voltage, sensor_data.vin.current, sensor_data.vin.power, sensor_data.main.voltage, sensor_data.main.current, sensor_data.main.power, sensor_data.usb.voltage, sensor_data.usb.current, sensor_data.usb.power diff --git a/main/proto/status.pb.h b/main/proto/status.pb.h index a5bc277..7de3c9c 100644 --- a/main/proto/status.pb.h +++ b/main/proto/status.pb.h @@ -25,8 +25,8 @@ typedef struct _SensorData { SensorChannelData main; bool has_vin; SensorChannelData vin; - uint32_t timestamp; - uint32_t uptime_sec; + uint64_t timestamp_ms; + uint64_t uptime_ms; } SensorData; /* Contains WiFi connection status */ @@ -85,8 +85,8 @@ extern "C" { #define SensorData_usb_tag 1 #define SensorData_main_tag 2 #define SensorData_vin_tag 3 -#define SensorData_timestamp_tag 4 -#define SensorData_uptime_sec_tag 5 +#define SensorData_timestamp_ms_tag 4 +#define SensorData_uptime_ms_tag 5 #define WifiStatus_connected_tag 1 #define WifiStatus_ssid_tag 2 #define WifiStatus_rssi_tag 3 @@ -111,8 +111,8 @@ X(a, STATIC, SINGULAR, FLOAT, power, 3) X(a, STATIC, OPTIONAL, MESSAGE, usb, 1) \ X(a, STATIC, OPTIONAL, MESSAGE, main, 2) \ X(a, STATIC, OPTIONAL, MESSAGE, vin, 3) \ -X(a, STATIC, SINGULAR, UINT32, timestamp, 4) \ -X(a, STATIC, SINGULAR, UINT32, uptime_sec, 5) +X(a, STATIC, SINGULAR, UINT64, timestamp_ms, 4) \ +X(a, STATIC, SINGULAR, UINT64, uptime_ms, 5) #define SensorData_CALLBACK NULL #define SensorData_DEFAULT NULL #define SensorData_usb_MSGTYPE SensorChannelData @@ -172,7 +172,7 @@ extern const pb_msgdesc_t StatusMessage_msg; #define LoadSwStatus_size 4 #define STATUS_PB_H_MAX_SIZE SensorData_size #define SensorChannelData_size 15 -#define SensorData_size 63 +#define SensorData_size 73 #ifdef __cplusplus } /* extern "C" */ diff --git a/main/service/monitor.c b/main/service/monitor.c index fe1ac09..1227125 100644 --- a/main/service/monitor.c +++ b/main/service/monitor.c @@ -4,6 +4,7 @@ #include "monitor.h" #include +#include #include #include "climit.h" #include "esp_log.h" @@ -89,9 +90,10 @@ static void send_pb_message(const pb_msgdesc_t* fields, const void* src_struct) static void sensor_timer_callback(void* arg) { - int64_t uptime_us = esp_timer_get_time(); - uint32_t uptime_sec = (uint32_t)(uptime_us / 1000000); - uint32_t timestamp = (uint32_t)time(NULL); + struct timeval tv; + gettimeofday(&tv, NULL); + uint64_t timestamp_ms = (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec / 1000; + uint64_t uptime_ms = (uint64_t)esp_timer_get_time() / 1000; StatusMessage message = StatusMessage_init_zero; message.which_payload = StatusMessage_sensor_data_tag; @@ -120,8 +122,8 @@ static void sensor_timer_callback(void* arg) // datalog_add(timestamp, channel_data_log); - sensor_data->timestamp = timestamp; - sensor_data->uptime_sec = uptime_sec; + sensor_data->timestamp_ms = timestamp_ms; + sensor_data->uptime_ms = uptime_ms; send_pb_message(StatusMessage_fields, &message); } diff --git a/page/src/chart.js b/page/src/chart.js index 484e0ab..e8b169a 100644 --- a/page/src/chart.js +++ b/page/src/chart.js @@ -216,7 +216,7 @@ function updateSingleChart(chart, metric, data, timeLabel) { * @param {Object} data - The new sensor data object from the WebSocket. */ export function updateCharts(data) { - const timeLabel = new Date(data.timestamp * 1000).toLocaleTimeString(); + const timeLabel = new Date(data.timestamp).toLocaleTimeString(); updateSingleChart(charts.power, 'power', data, timeLabel); updateSingleChart(charts.voltage, 'voltage', data, timeLabel); diff --git a/page/src/main.js b/page/src/main.js index f4a5539..af442fb 100644 --- a/page/src/main.js +++ b/page/src/main.js @@ -88,13 +88,13 @@ function onWsMessage(event) { USB: sensorData.usb, MAIN: sensorData.main, VIN: sensorData.vin, - timestamp: sensorData.timestamp + timestamp: sensorData.timestampMs }; updateSensorUI(sensorPayload); // Update uptime separately from the sensor data payload - if (sensorData.uptimeSec !== undefined) { - updateUptimeUI(sensorData.uptimeSec); + if (sensorData.uptimeMs !== undefined) { + updateUptimeUI(sensorData.uptimeMs / 1000); } } break; diff --git a/page/src/utils.js b/page/src/utils.js index 41ea475..d48a26b 100644 --- a/page/src/utils.js +++ b/page/src/utils.js @@ -25,6 +25,7 @@ export function debounce(func, delay) { * @returns {string} The formatted uptime string. */ export function formatUptime(totalSeconds) { + totalSeconds = Math.floor(totalSeconds); const days = Math.floor(totalSeconds / 86400); const hours = Math.floor((totalSeconds % 86400) / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); diff --git a/proto/status.proto b/proto/status.proto index 540662a..6a7efde 100644 --- a/proto/status.proto +++ b/proto/status.proto @@ -12,8 +12,8 @@ message SensorData { SensorChannelData usb = 1; SensorChannelData main = 2; SensorChannelData vin = 3; - uint32 timestamp = 4; - uint32 uptime_sec = 5; + uint64 timestamp_ms = 4; + uint64 uptime_ms = 5; } // Contains WiFi connection status