🧹 Simplifies websocket messages
This commit is contained in:
@@ -8,7 +8,7 @@ export const data = writable(new Float32Array(13))
|
|||||||
|
|
||||||
export const status:Writable<WebSocketStatus> = writable('CLOSED')
|
export const status:Writable<WebSocketStatus> = writable('CLOSED')
|
||||||
|
|
||||||
export const socket = writable(null)
|
export const socket:Writable<WebSocket> = writable(null)
|
||||||
|
|
||||||
export const connect = (url:string) => {
|
export const connect = (url:string) => {
|
||||||
status.set('CONNECTING')
|
status.set('CONNECTING')
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include "ArduinoJson.h"
|
||||||
|
|
||||||
extern uint8_t wsData[6];
|
extern uint8_t inputData[6];
|
||||||
|
|
||||||
|
struct websocket_message {
|
||||||
|
bool is_handled;
|
||||||
|
uint8_t identifier;
|
||||||
|
uint8_t* data;
|
||||||
|
size_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern websocket_message wsm;
|
||||||
|
|
||||||
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
|
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
|
||||||
@@ -1,70 +1,75 @@
|
|||||||
#include <WebsocketHandler.h>
|
#include <WebsocketHandler.h>
|
||||||
|
|
||||||
uint8_t wsData[6] = {};
|
uint8_t inputData[6] = {};
|
||||||
|
uint16_t servoData[12] = {};
|
||||||
|
|
||||||
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
|
esp_err_t parseControllerPacket(uint8_t* data, uint64_t lenght){
|
||||||
if(type == WS_EVT_CONNECT){
|
log_i("parsing controller packet");
|
||||||
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
|
for(size_t i=0; i < lenght; i++) {
|
||||||
client->printf("Hello Client %u :)", client->id());
|
inputData[i] = (uint8_t) data[i];
|
||||||
client->ping();
|
|
||||||
} else if(type == WS_EVT_DISCONNECT){
|
|
||||||
Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
|
|
||||||
} else if(type == WS_EVT_ERROR){
|
|
||||||
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
|
|
||||||
} else if(type == WS_EVT_PONG){
|
|
||||||
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
|
|
||||||
} else if(type == WS_EVT_DATA){
|
|
||||||
AwsFrameInfo * info = (AwsFrameInfo*)arg;
|
|
||||||
String msg = "";
|
|
||||||
if(info->final && info->index == 0 && info->len == len){
|
|
||||||
//the whole message is in a single frame and we got all of it's data
|
|
||||||
//Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
|
|
||||||
|
|
||||||
if(info->opcode == WS_TEXT){
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
msg += (char) data[i];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
char buff[6];
|
|
||||||
for(size_t i=0; i < info->len; i++) {
|
|
||||||
wsData[i] = (uint8_t) data[i];
|
|
||||||
//sprintf(buff, "%02x ", (uint8_t) data[i]);
|
|
||||||
//msg += buff ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//message is comprised of multiple frames or the frame is split into multiple packets
|
|
||||||
if(info->index == 0){
|
|
||||||
if(info->num == 0)
|
|
||||||
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
|
|
||||||
|
|
||||||
if(info->opcode == WS_TEXT){
|
|
||||||
for(size_t i=0; i < len; i++) {
|
|
||||||
msg += (char) data[i];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
char buff[3];
|
|
||||||
for(size_t i=0; i < len; i++) {
|
|
||||||
sprintf(buff, "%02x ", (uint8_t) data[i]);
|
|
||||||
msg += buff ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.printf("%s\n",msg.c_str());
|
|
||||||
|
|
||||||
if((info->index + len) == info->len){
|
|
||||||
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
|
|
||||||
if(info->final){
|
|
||||||
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
|
|
||||||
if(info->message_opcode == WS_TEXT)
|
|
||||||
client->text("I got your text message");
|
|
||||||
else
|
|
||||||
client->binary("I got your binary message");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t parseServoPacket(uint8_t* _data, uint64_t lenght){
|
||||||
|
log_i("parsing servo packet");
|
||||||
|
uint16_t* data = (uint16_t*)_data;
|
||||||
|
for(size_t i=0; i < lenght/2; i++) {
|
||||||
|
servoData[i] = data[i];
|
||||||
|
}
|
||||||
|
log_i("done parsing servo packet. There was %u int", lenght);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum data_packet_t {
|
||||||
|
CONTROLLER_PACKET,
|
||||||
|
SERVO_PACKET,
|
||||||
|
toggle,
|
||||||
|
};
|
||||||
|
|
||||||
|
void handleWebSocketBufferMessage(void* arg, uint8_t* data, size_t len, AsyncWebSocket* server, AsyncWebSocketClient* client) {
|
||||||
|
wsm.identifier = data[0];
|
||||||
|
wsm.data = data+1;
|
||||||
|
wsm.len = len;
|
||||||
|
wsm.is_handled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleWebSocketMessage(void* arg, uint8_t* data, size_t len, AsyncWebSocket* server, AsyncWebSocketClient* client) {
|
||||||
|
AwsFrameInfo* info = (AwsFrameInfo*)arg;
|
||||||
|
if(info->final && info->index == 0 && info->len == len){
|
||||||
|
if(info->opcode == WS_TEXT) return;
|
||||||
|
handleWebSocketBufferMessage(arg, data, len, server, client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleNewConnection(AsyncWebSocket* server, AsyncWebSocketClient* client){
|
||||||
|
log_i("ws[%s][%u] connect\n", server->url(), client->id());
|
||||||
|
|
||||||
|
StaticJsonDocument<600> json;
|
||||||
|
json["ssid"] = "Rune private network";
|
||||||
|
json["password"] = "a9b8c7d6e5f4g3H2I1";
|
||||||
|
json["sketchSize"] = ESP.getSketchSize();
|
||||||
|
json["sketchMD5"] = ESP.getSketchMD5();
|
||||||
|
json["freeSketchSize"] = ESP.getFreeSketchSpace();
|
||||||
|
json["chipCores"] = ESP.getChipCores();
|
||||||
|
json["chipModel"] = ESP.getChipModel();
|
||||||
|
json["chipRevision"] = ESP.getChipRevision();
|
||||||
|
json["cpuFreq"] = ESP.getCpuFreqMHz();
|
||||||
|
json["heapSize"] = ESP.getHeapSize();
|
||||||
|
json["psramSize"] = ESP.getPsramSize();
|
||||||
|
json["sdkVersion"] = ESP.getSdkVersion();
|
||||||
|
json["eFuseMac"] = ESP.getEfuseMac();
|
||||||
|
json["resetReason"] = esp_reset_reason();
|
||||||
|
|
||||||
|
char data[400];
|
||||||
|
size_t len = serializeJson(json, data);
|
||||||
|
client->text(data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
|
||||||
|
if (type == WS_EVT_CONNECT) handleNewConnection(server, client);
|
||||||
|
else if (type == WS_EVT_DISCONNECT) log_i("ws[%s][%u] disconnect\n", server->url(), client->id());
|
||||||
|
else if (type == WS_EVT_ERROR) log_i("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
|
||||||
|
else if (type == WS_EVT_PONG) log_i("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char*)data : "");
|
||||||
|
else if (type == WS_EVT_DATA) handleWebSocketMessage(arg, data, len, server, client);
|
||||||
|
}
|
||||||
|
|||||||
+20
-15
@@ -1,5 +1,7 @@
|
|||||||
#include <spot.h>
|
#include <spot.h>
|
||||||
|
|
||||||
|
websocket_message wsm;
|
||||||
|
|
||||||
Spot::Spot()
|
Spot::Spot()
|
||||||
: _events(EVENTSOURCE_PATH)
|
: _events(EVENTSOURCE_PATH)
|
||||||
, _ws(WEBSOCKET_PATH)
|
, _ws(WEBSOCKET_PATH)
|
||||||
@@ -56,21 +58,24 @@ uint8_t Spot::cpu_temperature(){
|
|||||||
esp_err_t Spot::broadcast_data(){
|
esp_err_t Spot::broadcast_data(){
|
||||||
if(millis() - _last_broadcast < 50) return ESP_OK;
|
if(millis() - _last_broadcast < 50) return ESP_OK;
|
||||||
|
|
||||||
size_t numContent = 13;
|
_mpu.update();
|
||||||
float content[numContent];
|
size_t numContent = 14;
|
||||||
content[0] = WiFi.RSSI();
|
float content[numContent] = {
|
||||||
content[1] = _mpu.getTemp();
|
WiFi.RSSI(),
|
||||||
content[2] = _mpu.getAccX();
|
_mpu.getTemp(),
|
||||||
content[3] = _mpu.getAccY();
|
_mpu.getAngleX(),
|
||||||
content[4] = _mpu.getAccZ();
|
_mpu.getAngleY(),
|
||||||
content[5] = _mpu.getAngleX();
|
_mpu.getAngleZ(),
|
||||||
content[6] = _mpu.getAngleY();
|
cpu_temperature(),
|
||||||
content[7] = _mpu.getAngleZ();
|
_leftUss.ping_cm(),
|
||||||
content[8] = cpu_temperature();
|
_rightUss.ping_cm(),
|
||||||
content[9] = _leftUss.ping_cm();
|
ESP.getFreeHeap(),
|
||||||
content[10] = _rightUss.ping_cm();
|
ESP.getFreePsram(),
|
||||||
content[11] = ESP.getFreeHeap();
|
ESP.getMinFreeHeap(),
|
||||||
content[12] = ESP.getFreePsram();
|
ESP.getMinFreePsram(),
|
||||||
|
ESP.getMaxAllocHeap(),
|
||||||
|
ESP.getMaxAllocPsram(),
|
||||||
|
};
|
||||||
|
|
||||||
uint8_t* buf = (uint8_t*) &content;
|
uint8_t* buf = (uint8_t*) &content;
|
||||||
size_t buf_len = sizeof(buf);
|
size_t buf_len = sizeof(buf);
|
||||||
|
|||||||
Reference in New Issue
Block a user