Skip to content

Commit

Permalink
Merge pull request #608 from Paciente8159/websockets
Browse files Browse the repository at this point in the history
Implemented WebSockets for µCNC
  • Loading branch information
Paciente8159 committed Feb 3, 2024
2 parents 7d5e3e6 + d57013d commit 58f35c5
Show file tree
Hide file tree
Showing 15 changed files with 570 additions and 96 deletions.
11 changes: 11 additions & 0 deletions data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p align="center">
<img src="https://github.com/Paciente8159/uCNC/blob/master/docs/logo.png?raw=true">
</p>

# µCNC

µCNC - Universal CNC firmware for microcontrollers

# µCNC data

Insert your files to be written to the board file system here. For example the index.html.gz file for web plugins
2 changes: 1 addition & 1 deletion dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ dependencies:
source:
type: idf
version: 4.4.4
manifest_hash: 748d9ec66daab24a00486d8debd2679ce41df5cbbb709cf23e88f269990f02b2
manifest_hash: dcf4d39b94252de130019eadceb989d72b0dbc26b552cfdcbb50f6da531d2b92
target: esp32
version: 1.0.0
40 changes: 31 additions & 9 deletions uCNC/src/hal/boards/esp32/esp32.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ board_build.f_cpu = 240000000L
board_build.partitions = min_spiffs.csv
monitor_filters=esp32_exception_decoder
upload_speed = 921600
lib_deps = SPI, Wire, Wifi, WebServer, HTTPUpdatedServer, BluetoothSerial
debug_tool=esp-prog
lib_deps =
https://github.com/Links2004/arduinoWebSockets/archive/refs/heads/master.zip
SPI
Wire
Wifi
WebServer
HTTPUpdatedServer
BluetoothSerial
Hash


##################
# Arduino builds #
Expand All @@ -25,25 +33,29 @@ extends = common_esp32
board = wemos_d1_uno32
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_WEMOS_D1_R32 -DMCU_FLASH_FS=1
board_build.filesystem = littlefs
lib_deps = ${common_esp32.lib_deps}, LittleFS
lib_deps = ${common_esp32.lib_deps}
LittleFS

[env:ESP32-MKS-DLC32]
extends = common_esp32
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_MKS_DLC32 -DMCU_FLASH_FS=1
board_build.filesystem = littlefs
lib_deps = ${common_esp32.lib_deps}, LittleFS
lib_deps = ${common_esp32.lib_deps}
LittleFS

[env:ESP32-MKS-TINYBEE]
extends = common_esp32
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_MKS_TINYBEE -DMCU_FLASH_FS=1
board_build.filesystem = littlefs
lib_deps = ${common_esp32.lib_deps}, LittleFS
lib_deps = ${common_esp32.lib_deps}
LittleFS

[env:ESP32-Shield-V3]
extends = common_esp32
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_ESP32_SHIELD_V3 -DMCU_FLASH_FS=1
board_build.filesystem = littlefs
lib_deps = ${common_esp32.lib_deps}, LittleFS
lib_deps = ${common_esp32.lib_deps}
LittleFS

#################
# ESPIDF builds #
Expand All @@ -54,16 +66,26 @@ extends = common_esp32
framework = arduino, espidf
board = wemos_d1_uno32
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_WEMOS_D1_R32
lib_deps = ${common_esp32.lib_deps}, SPIFFS
lib_deps = ${common_esp32.lib_deps}
SPIFFS

[env:ESP32IDF-MKS-DLC32]
extends = common_esp32
framework = arduino, espidf
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_MKS_DLC32
lib_deps = ${common_esp32.lib_deps}, SPIFFS
lib_deps = ${common_esp32.lib_deps}
SPIFFS

[env:ESP32IDF-MKS-TINYBEE]
extends = common_esp32
framework = arduino, espidf
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_MKS_TINYBEE
lib_deps = ${common_esp32.lib_deps}, SPIFFS
lib_deps = ${common_esp32.lib_deps}
SPIFFS

[env:ESP32IDF-Shield-V3]
extends = common_esp32
framework = arduino, espidf
build_flags = ${common_esp32.build_flags} -DBOARD=BOARD_ESP32_SHIELD_V3
lib_deps = ${common_esp32.lib_deps}
SPIFFS
16 changes: 13 additions & 3 deletions uCNC/src/hal/boards/esp8266/esp8266.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ platform = espressif8266
framework = arduino
board = d1
build_src_filter = +<*>-<src/hal/mcu/virtual>
; lib_deps =
; https://github.com/tzapu/WiFiManager/archive/refs/heads/master.zip
lib_deps =
SPI
EEPROM
ESP8266WiFi
ESP8266WebServer
ESP8266HTTPUpdateServer
LittleFS
Hash
https://github.com/Links2004/arduinoWebSockets/archive/refs/heads/master.zip
build_flags = -DBOARD=BOARD_WEMOS_D1 -DENABLE_WIFI
upload_speed = 256000
board_build.f_flash = 80000000L
board_build.f_cpu = 160000000L
board_build.ldscript=eagle.flash.2m512.ld
board_build.flash_mode = qio
board_build.ldscript=eagle.flash.4m2m.ld
board_build.filesystem = littlefs
12 changes: 12 additions & 0 deletions uCNC/src/hal/boards/rp2040/rp2040.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,16 @@ build_flags = -DBOARD=BOARD_RPI_PICO
[env:RP2040-PICO-W]
extends = common_rp2040
board = rpipicow
lib_deps =
SPI
Wire
Wifi
WebServer
HTTPUpdatedServer
BluetoothSerial
Hash
DNSServer
SerialBT
LittleFS
https://github.com/Links2004/arduinoWebSockets/archive/refs/heads/master.zip
build_flags = -DBOARD=BOARD_RPI_PICO_W -DENABLE_WIFI -DENABLE_BLUETOOTH -DPIO_FRAMEWORK_ARDUINO_ENABLE_BLUETOOTH
151 changes: 134 additions & 17 deletions uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ uint16_t bt_settings_offset;
#include <HTTPUpdateServer.h>
#include <Update.h>

#ifndef WIFI_PORT
#define WIFI_PORT 23
#ifndef TELNET_PORT
#define TELNET_PORT 23
#endif

#ifndef WEBSERVER_PORT
#define WEBSERVER_PORT 80
#endif

#ifndef WEBSOCKET_PORT
#define WEBSOCKET_PORT 8080
#endif

#ifndef WEBSOCKET_MAX_CLIENTS
#define WEBSOCKET_MAX_CLIENTS 2
#endif

#ifndef WIFI_USER
Expand All @@ -60,13 +72,13 @@ uint16_t bt_settings_offset;
#define WIFI_PASS "pass"
#endif

WebServer web_server(80);
WebServer web_server(WEBSERVER_PORT);
HTTPUpdateServer httpUpdater;
const char *update_path = "/firmware";
const char *update_username = WIFI_USER;
const char *update_password = WIFI_PASS;
#define MAX_SRV_CLIENTS 1
WiFiServer telnet_server(WIFI_PORT);
WiFiServer telnet_server(TELNET_PORT);
WiFiClient server_client;

typedef struct
Expand Down Expand Up @@ -394,18 +406,12 @@ extern "C"
#define FLASH_FS SPIFFS
#endif

// call to the webserver initializer
DECL_MODULE(endpoint)
{
#ifndef CUSTOM_OTA_ENDPOINT
httpUpdater.setup(&web_server, update_path, update_username, update_password);
#endif
FLASH_FS.begin();
web_server.begin();
}

void endpoint_add(const char *uri, uint8_t method, endpoint_delegate request_handler, endpoint_delegate file_handler)
{
if (!method)
{
method = 255;
}
web_server.on(uri, (HTTPMethod)method, request_handler, file_handler);
}

Expand Down Expand Up @@ -449,15 +455,110 @@ extern "C"

#endif

#if defined(ENABLE_WIFI) && defined(MCU_HAS_WEBSOCKETS)
#include "WebSocketsServer.h"
#include "../../../modules/websocket.h"
WebSocketsServer socket_server(WEBSOCKET_PORT);

WEAK_EVENT_HANDLER(websocket_client_connected)
{
DEFAULT_EVENT_HANDLER(websocket_client_connected);
}

WEAK_EVENT_HANDLER(websocket_client_disconnected)
{
DEFAULT_EVENT_HANDLER(websocket_client_disconnected);
}

WEAK_EVENT_HANDLER(websocket_client_receive)
{
DEFAULT_EVENT_HANDLER(websocket_client_receive);
}

WEAK_EVENT_HANDLER(websocket_client_error)
{
DEFAULT_EVENT_HANDLER(websocket_client_error);
}

void websocket_send(uint8_t clientid, uint8_t *data, size_t length, uint8_t flags)
{
switch (flags & WS_SEND_TYPE)
{
case WS_SEND_TXT:
if (flags & WS_SEND_BROADCAST)
{
socket_server.broadcastTXT(data, length);
}
else
{
socket_server.sendTXT(clientid, data, length);
}
break;
case WS_SEND_BIN:
if (flags & WS_SEND_BROADCAST)
{
socket_server.broadcastTXT(data, length);
}
else
{
socket_server.sendTXT(clientid, data, length);
}
break;
case WS_SEND_PING:
if (flags & WS_SEND_BROADCAST)
{
socket_server.broadcastPing(data, length);
}
else
{
socket_server.sendPing(clientid, data, length);
}
break;
}
}

void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
{
websocket_event_t event = {num, (uint32_t)socket_server.remoteIP(num), type, payload, length};
switch (type)
{
case WStype_DISCONNECTED:
EVENT_INVOKE(websocket_client_disconnected, &event);
break;
case WStype_CONNECTED:
EVENT_INVOKE(websocket_client_connected, &event);
break;
case WStype_ERROR:
EVENT_INVOKE(websocket_client_error, &event);
break;
case WStype_TEXT:
case WStype_BIN:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
case WStype_PING:
case WStype_PONG:
EVENT_INVOKE(websocket_client_receive, &event);
break;
}
}
#endif

#ifdef ENABLE_WIFI
void mcu_wifi_task(void *arg)
{
WiFi.begin();
telnet_server.begin();
telnet_server.setNoDelay(true);
#if !defined(MCU_HAS_ENDPOINTS)
#ifndef CUSTOM_OTA_ENDPOINT
httpUpdater.setup(&web_server, update_path, update_username, update_password);
#endif
FLASH_FS.begin();
web_server.begin();
#ifdef MCU_HAS_WEBSOCKETS
socket_server.begin();
socket_server.onEvent(webSocketEvent);
#endif
WiFi.disconnect();

Expand Down Expand Up @@ -495,7 +596,13 @@ extern "C"

for (;;)
{
web_server.handleClient();
if (wifi_settings.wifi_on)
{
web_server.handleClient();
#ifdef MCU_HAS_WEBSOCKETS
socket_server.loop();
#endif
}
taskYIELD();
}
}
Expand All @@ -517,7 +624,13 @@ extern "C"
settings_save(wifi_settings_offset, (uint8_t *)&wifi_settings, sizeof(wifi_settings_t));
}

xTaskCreatePinnedToCore(mcu_wifi_task, "wifiTask", 4069, NULL, 3, NULL, CONFIG_ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(mcu_wifi_task, "wifiTask", 4069, NULL, 1, NULL, 0);
// taskYIELD();

// #ifdef MCU_HAS_WEBSOCKETS
// socket_server.begin();
// socket_server.onEvent(webSocketEvent);
// #endif
#endif
#ifdef ENABLE_BLUETOOTH
bt_settings_offset = settings_register_external_setting(1);
Expand Down Expand Up @@ -733,6 +846,10 @@ extern "C"
#endif
}
}

// #ifdef MCU_HAS_WEBSOCKETS
// socket_server.loop();
// #endif
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions uCNC/src/hal/mcus/esp32/mcumap_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2704,8 +2704,13 @@ extern "C"
#endif
#ifdef ENABLE_WIFI
#define MCU_HAS_WIFI
#ifndef DISABLE_ENDPOINTS
#define MCU_HAS_ENDPOINTS
#endif
#ifndef DISABLE_WEBSOCKETS
#define MCU_HAS_WEBSOCKETS
#endif
#endif
#ifdef ENABLE_BLUETOOTH
#define MCU_HAS_BLUETOOTH
#endif
Expand Down
Loading

0 comments on commit 58f35c5

Please sign in to comment.