- websocket client on ESP32
- telemetry protocol builder - server-side message handling - database migrations - telemetry persistence
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#include "sender_task.h"
|
||||
#include "sampler_task.h"
|
||||
#include "ringbuf.h"
|
||||
#include "protocol.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include "ws.h"
|
||||
|
||||
extern "C" {
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@@ -18,21 +20,35 @@ static void sender_task(void *arg) {
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // 1 Гц
|
||||
|
||||
char buf[512];
|
||||
|
||||
ringbuf_t *rb = sampler_get_buffer();
|
||||
ringbuf_copy(rb, data);
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
// формируем JSON
|
||||
printf("{\"v\":1,\"t\":\"t\",\"id\":%lu,\"ts\":%lld,\"d\":1,\"p\":{",
|
||||
(unsigned long)msg_id++, (long long)now);
|
||||
printf("\"m\":\"v\",\"s\":\"adc35\",\"u\":\"raw\",\"v\":[");
|
||||
int len = build_telemetry(
|
||||
buf,
|
||||
sizeof(buf),
|
||||
msg_id++,
|
||||
now,
|
||||
1, // device_id (пока захардкожен)
|
||||
"v", // metric
|
||||
"adc35", // source
|
||||
"raw", // unit
|
||||
data,
|
||||
RINGBUF_SIZE
|
||||
);
|
||||
|
||||
for (int i = 0; i < RINGBUF_SIZE; i++) {
|
||||
printf("[%d,%d]%s", i, data[i], (i < RINGBUF_SIZE - 1) ? "," : "");
|
||||
if (len > 0) {
|
||||
if (ws_is_connected()) {
|
||||
ws_send(buf);
|
||||
} else {
|
||||
printf("%s\n", buf); // fallback
|
||||
}
|
||||
} else {
|
||||
printf("build_telemetry failed\n");
|
||||
}
|
||||
|
||||
printf("]}}\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user