This commit is contained in:
Laila van Reenen 2024-08-15 15:06:13 +02:00
parent 3cda1316ae
commit 07144c04eb
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769
7 changed files with 42 additions and 24 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/.vscode /.vscode
build build
.cache

View File

@ -183,9 +183,9 @@ async def new_client(clientId, ws):
"""handler for every new client connection""" """handler for every new client connection"""
client = { "id": clientId, "boat": None, "ws": ws, "state": "active" } client = { "id": clientId, "boat": None, "ws": ws, "state": "active" }
if client['id'] == ADMIN_ID: if client['id'] == ADMIN_ID:
print("new client connected: admin") print("INFO: new client connected: admin")
else: else:
print("new client connected: " + client['id']) print("INFO: new client connected: " + client['id'])
for clie in Clients: for clie in Clients:
if clie['id'] == client['id']: if clie['id'] == client['id']:
if client['state'] == "kicked": if client['state'] == "kicked":
@ -197,7 +197,7 @@ async def new_client(clientId, ws):
async def new_boat(boatId, name, ws): async def new_boat(boatId, name, ws):
"""handler for every new boat connection""" """handler for every new boat connection"""
boat = { "id": boatId, "name": name, "ws": ws, "state": BOAT_STATE_AVAILABLE, "lastMsg": 0, "log": [] } boat = { "id": boatId, "name": name, "ws": ws, "state": BOAT_STATE_AVAILABLE, "lastMsg": 0, "log": [] }
print("new boat connected: " + boatId) print("INFO: new boat connected: " + boatId)
for i, bo in enumerate(Boats): for i, bo in enumerate(Boats):
if bo['id'] == boat['id']: if bo['id'] == boat['id']:
bo['state'] = BOAT_STATE_TERMINATED bo['state'] = BOAT_STATE_TERMINATED
@ -210,6 +210,7 @@ async def run(ws, path):
"""hadeler for every new websocket connection""" """hadeler for every new websocket connection"""
client = None client = None
try: try:
print("INFO: new connection")
async for msg in ws: async for msg in ws:
for message in msg.split("\n"): for message in msg.split("\n"):
if len(message) == 0: if len(message) == 0:
@ -218,7 +219,6 @@ async def run(ws, path):
if len(message) == 3 and message[1] == "4675": if len(message) == 3 and message[1] == "4675":
client = await new_client(message[0], ws) client = await new_client(message[0], ws)
if client is not None: if client is not None:
try: try:
async for msg in ws: async for msg in ws:
for message in msg.split("\n"): for message in msg.split("\n"):

1
rx_esp32/.gitignore vendored
View File

@ -10,3 +10,4 @@ build
/sdkconfig /sdkconfig
/dependencies.lock /dependencies.lock
/src/config.h /src/config.h
*.old

View File

@ -29,7 +29,7 @@ void adc_init()
adc_continuous_new_handle(&adc_config, &adc_handle); adc_continuous_new_handle(&adc_config, &adc_handle);
adc_continuous_config_t dig_cfg = { adc_continuous_config_t dig_cfg = {
.sample_freq_hz = 100 * 6, .sample_freq_hz = 611, //100 * 6,
.conv_mode = ADC_CONV_SINGLE_UNIT_1, .conv_mode = ADC_CONV_SINGLE_UNIT_1,
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE2, .format = ADC_DIGI_OUTPUT_FORMAT_TYPE2,
}; };

View File

@ -14,12 +14,12 @@
#include "utils.h" #include "utils.h"
#include "commands.h" #include "commands.h"
#include "wifi.h" #include "wifi.h"
#include "led.h"
#include "servos.h" #include "servos.h"
#include "logger.h" #include "logger.h"
#include "ws.h" #include "ws.h"
#if TARGET == TARGET_TX #if TARGET == TARGET_TX
#include "adc.h" #include "led.h"
// #include "adc.h"
#endif #endif
bool volatile running = true; bool volatile running = true;
@ -91,7 +91,7 @@ CLI_charOutFn charOut_ws[3] = {
&charOut_ws0, &charOut_ws0,
&charOut_ws1, &charOut_ws1,
&charOut_ws2 &charOut_ws2
} };
#endif #endif
void app_main() { void app_main() {
@ -117,7 +117,7 @@ void app_main() {
wifiInit(); wifiInit();
#if TARGET == TARGET_TX #if TARGET == TARGET_TX
adc_init(); // adc_init();
#endif #endif
running = true; running = true;
@ -162,8 +162,10 @@ void app_main() {
{ {
#if TARGET == TARGET_TX #if TARGET == TARGET_TX
led_setRGB(0, 20, 0); led_setRGB(0, 20, 0);
#endif MainState = STATE_WIFI_CONNECTED;
#elif TARGET == TARGET_RX
MainState = STATE_WS_CONNECTING; MainState = STATE_WS_CONNECTING;
#endif
LOG_I("main: connected to ap SSID '%s'", WIFI_SSID); LOG_I("main: connected to ap SSID '%s'", WIFI_SSID);
} }
else if ((bits & WIFI_FAIL_BIT) != 0) else if ((bits & WIFI_FAIL_BIT) != 0)
@ -178,7 +180,7 @@ void app_main() {
else if (bits != 0) else if (bits != 0)
{ {
#if TARGET == TARGET_TX #if TARGET == TARGET_TX
led_setRGB(2, 0, 0); led_setRGB(20, 0, 0);
#endif #endif
// MainState = STATE_WIFI_CONNECTING; // MainState = STATE_WIFI_CONNECTING;
LOG_C("main: UNEXPECTED EVENT (bits: 0x%04x)", bits); LOG_C("main: UNEXPECTED EVENT (bits: 0x%04x)", bits);
@ -190,8 +192,9 @@ void app_main() {
switch (WSStates[WS_conn_proc]) switch (WSStates[WS_conn_proc])
{ {
case STATE_WS_CONNECTING: case STATE_WS_CONNECTING:
ws_client[WS_conn_proc] = ws_connect(WS_URL); ws_client[WS_conn_proc] = ws_connect(WS_URL, BoatId + WS_conn_proc, WS_DEV_CODE_CLIENT);
WSStates[WS_conn_proc] = STATE_WS_WAIT_CONNECTION; WSStates[WS_conn_proc] = STATE_WS_WAIT_CONNECTION;
LOG_I("main: websocket %d: connecting", WS_conn_proc);
break; break;
case STATE_WS_WAIT_CONNECTION: case STATE_WS_WAIT_CONNECTION:
if (ws_client[WS_conn_proc]->connected) if (ws_client[WS_conn_proc]->connected)
@ -199,6 +202,7 @@ void app_main() {
cli_ws_client[WS_conn_proc] = CLI_init((CLI_charOutFn)&charOut_ws[WS_conn_proc], cmdList); cli_ws_client[WS_conn_proc] = CLI_init((CLI_charOutFn)&charOut_ws[WS_conn_proc], cmdList);
cli_ws_client[WS_conn_proc].echo = false; cli_ws_client[WS_conn_proc].echo = false;
MainState = STATE_IDEL; MainState = STATE_IDEL;
LOG_I("main: websocket %d: connected", WS_conn_proc);
} }
break; break;
@ -217,16 +221,21 @@ void app_main() {
ws_sendData(ws_client[WS_conn_proc]); ws_sendData(ws_client[WS_conn_proc]);
break; break;
} }
// WS_conn_proc++;
// if (WS_conn_proc >= 3)
// {
// WS_conn_proc = 0;
// }
ADC_Data_t data; // ADC_Data_t data;
if (adc_read(&data)) // if (adc_read(&data))
{ // {
LOG_D("ADC_DATA: %d, %d, %d, %d, %d, %d", data.value[0], data.value[1], data.value[2], data.value[3], data.value[4], data.value[5]); // LOG_D("ADC_DATA: %d, %d, %d, %d, %d, %d", data.value[0], data.value[1], data.value[2], data.value[3], data.value[4], data.value[5]);
} // }
break; break;
#elif TARGET == TARGET_RX #elif TARGET == TARGET_RX
case STATE_WS_CONNECTING: case STATE_WS_CONNECTING:
ws_client = ws_connect(WS_URL); ws_client = ws_connect(WS_URL, BoatId, WS_DEV_CODE_BOAT);
MainState = STATE_WS_WAIT_CONNECTION; MainState = STATE_WS_WAIT_CONNECTION;
break; break;
case STATE_WS_WAIT_CONNECTION: case STATE_WS_WAIT_CONNECTION:

View File

@ -19,7 +19,7 @@ static void ws_event_handler(void* handler_args, esp_event_base_t base, int32_t
client->connected = true; client->connected = true;
char str[100]; char str[100];
snprintf(&str[0], 100, "%04d;3440;" BOAT_NAME "\n", BoatId); snprintf(&str[0], 100, "%04u;%04u;" BOAT_NAME "\n", client->id, client->dev_code);
esp_websocket_client_send_text(client->handle, &str[0], strlen(str), 1000); esp_websocket_client_send_text(client->handle, &str[0], strlen(str), 1000);
break; break;
@ -111,9 +111,11 @@ static void ws_event_handler(void* handler_args, esp_event_base_t base, int32_t
} }
} }
ws_client_t ws_connect(char *url) ws_client_t ws_connect(char *url, uint16_t id, uint16_t dev_code)
{ {
ws_client_t client = malloc(sizeof(ws_client_data_t)); ws_client_t client = malloc(sizeof(ws_client_data_t));
client->id = id;
client->dev_code = dev_code;
client->rxBuffer_rp = 0; client->rxBuffer_rp = 0;
client->rxBuffer_wp = 0; client->rxBuffer_wp = 0;
client->txBuffer_wp = 0; client->txBuffer_wp = 0;

View File

@ -7,6 +7,9 @@
#include "config.h" #include "config.h"
#define WS_DEV_CODE_BOAT 3440
#define WS_DEV_CODE_CLIENT 4675
typedef struct { typedef struct {
esp_websocket_client_handle_t handle; esp_websocket_client_handle_t handle;
int connected; int connected;
@ -15,10 +18,12 @@ typedef struct {
uint16_t rxBuffer_rp; uint16_t rxBuffer_rp;
unsigned char txBuffer[WS_TX_BUFFER_LEN]; unsigned char txBuffer[WS_TX_BUFFER_LEN];
uint16_t txBuffer_wp; uint16_t txBuffer_wp;
uint16_t id;
uint16_t dev_code;
} ws_client_data_t; } ws_client_data_t;
typedef ws_client_data_t* ws_client_t; typedef ws_client_data_t* ws_client_t;
ws_client_t ws_connect(char *url); ws_client_t ws_connect(char *url, uint16_t id, uint16_t dev_code);
int ws_getchar(ws_client_t client); int ws_getchar(ws_client_t client);
void ws_putchar(ws_client_t client, char c); void ws_putchar(ws_client_t client, char c);
void ws_sendData(ws_client_t client); void ws_sendData(ws_client_t client);