Attempt at implementing PB sending for ESP32
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user