Attempt at implementing PB sending for ESP32
This commit is contained in:
@@ -20,6 +20,8 @@ class Websocket : public CommAdapterBase {
|
||||
|
||||
void emit(const char *event, JsonVariant &payload, const char *originId = "", bool onlyToSameOrigin = false);
|
||||
|
||||
void emit_raw(const char *event, uint8_t* payload, size_t event_length, size_t payload_length);
|
||||
|
||||
private:
|
||||
PsychicWebSocketHandler _socket;
|
||||
PsychicHttpServer &_server;
|
||||
|
||||
@@ -19,6 +19,17 @@ void Websocket::emit(const char *event, JsonVariant &payload, const char *origin
|
||||
CommAdapterBase::emit(event, payload, originId, onlyToSameOrigin);
|
||||
}
|
||||
|
||||
void Websocket::emit_raw(const char *event, uint8_t* payload, size_t event_length, size_t payload_length) {
|
||||
size_t total_len = payload_length + event_length + 1;
|
||||
uint8_t* buf = (uint8_t*) malloc(total_len + 1);
|
||||
memcpy(buf, event, event_length);
|
||||
buf[event_length+1] = ',';
|
||||
memcpy(buf + event_length+2, payload, payload_length);
|
||||
|
||||
send(buf, total_len, -1);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void Websocket::onWSOpen(PsychicWebSocketClient *client) {
|
||||
ESP_LOGI("EventSocket", "ws[%s][%u] connect", client->remoteIP().toString().c_str(), client->socket());
|
||||
ping(client->socket());
|
||||
|
||||
+26
-4
@@ -17,6 +17,10 @@
|
||||
#include <mdns_service.h>
|
||||
#include <system_service.h>
|
||||
|
||||
// Temporary includes
|
||||
#include <pb_encode.h>
|
||||
#include "platform_shared/imu_report.pb.h"
|
||||
|
||||
#include <www_mount.hpp>
|
||||
|
||||
// Communication
|
||||
@@ -225,15 +229,33 @@ void IRAM_ATTR serviceLoopEntry(void *) {
|
||||
setupEventSocket();
|
||||
|
||||
ESP_LOGI("main", "Service control task started");
|
||||
float temp = 0;
|
||||
for (;;) {
|
||||
wifiService.loop();
|
||||
apService.loop();
|
||||
EXECUTE_EVERY_N_MS(2000, system_service::emitMetrics(socket));
|
||||
EXECUTE_EVERY_N_MS(500, {
|
||||
JsonDocument doc;
|
||||
JsonVariant results = doc.to<JsonVariant>();
|
||||
peripherals.getIMUResult(results);
|
||||
socket.emit(EVENT_IMU, results);
|
||||
// JsonDocument doc;
|
||||
// JsonVariant results = doc.to<JsonVariant>();
|
||||
// peripherals.getIMUResult(results);
|
||||
// socket.emit(EVENT_IMU, results);
|
||||
|
||||
// TESTING PB EMITTING!!
|
||||
IMUReport report;
|
||||
report.x = 1;
|
||||
report.y = 2;
|
||||
report.z = 3;
|
||||
report.temp = temp;
|
||||
temp += 0.01;
|
||||
report.success = true;
|
||||
|
||||
uint8_t buffer[IMUReport_size];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
bool status = pb_encode(&stream, &IMUReport_msg, &report);
|
||||
if (!status) {
|
||||
// PRINT ERROR HERE!
|
||||
}
|
||||
socket.emit_raw(EVENT_IMU, buffer, strlen(EVENT_IMU), IMUReport_size);
|
||||
});
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
|
||||
message IMUReport {
|
||||
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
float z = 3;
|
||||
float temp = 4;
|
||||
bool success = 5;
|
||||
}
|
||||
+6
-2
@@ -94,10 +94,14 @@ build_flags =
|
||||
-D register=
|
||||
-std=gnu++2a
|
||||
-Ofast
|
||||
-ffunction-sections
|
||||
-fdata-sections
|
||||
-ffunction-sections
|
||||
-fdata-sections
|
||||
-Wl,--gc-sections
|
||||
-I submodules/nanopb
|
||||
build_unflags = -std=gnu++11
|
||||
build_src_filter =
|
||||
+<*>
|
||||
+<../../submodules/nanopb/pb_*.c>
|
||||
build_src_flags =
|
||||
-Wformat=2
|
||||
-Wformat-truncation
|
||||
|
||||
Reference in New Issue
Block a user