Connect to Klipper (although websockets are broken :( )

This commit is contained in:
suchmememanyskill
2023-11-12 00:18:54 +01:00
parent 20e1cdbc46
commit bf892d6855
9 changed files with 195 additions and 29 deletions

View File

@@ -14,34 +14,31 @@ board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = lib_deps =
SPI SPI
lvgl/lvgl@^8.3.9 lvgl/lvgl@^8.3.9
https://github.com/Bodmer/TFT_eSPI.git https://github.com/Bodmer/TFT_eSPI.git
https://github.com/PaulStoffregen/XPT2046_Touchscreen.git https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
bblanchon/ArduinoJson@^6.21.3
links2004/WebSockets@^2.4.1
build_flags = build_flags =
-DLV_CONF_PATH="../../../../src/conf/lv_conf.h" -DLV_CONF_PATH="../../../../src/conf/lv_conf.h"
-DUSER_SETUP_LOADED=1 -DUSER_SETUP_LOADED=1
#-DILI9341_DRIVER=1 -DILI9341_2_DRIVER=1
-DILI9341_2_DRIVER=1 -DTFT_INVERSION_ON=1
-DTFT_INVERSION_ON=1 -DTFT_WIDTH=240
-DTFT_WIDTH=240 -DTFT_HEIGHT=320
-DTFT_HEIGHT=320 -DTFT_BL=21
-DTFT_BL=21 -DTFT_BACKLIGHT_ON=HIGH
-DTFT_BACKLIGHT_ON=HIGH -DTFT_MISO=12
-DTFT_MISO=12 -DTFT_MOSI=13
-DTFT_MOSI=13 -DTFT_SCLK=14
-DTFT_SCLK=14 -DTFT_CS=15
-DTFT_CS=15 -DTFT_DC=2
-DTFT_DC=2 -DTFT_RST=-1
-DTFT_RST=-1
-DLOAD_GCLD=1 -DLOAD_GCLD=1
-DLOAD_FONT2=1 -DLOAD_FONT2=1
#-DLOAD_FONT4=1
#-DLOAD_FONT6=1
#-DLOAD_FONT7=1
#-DLOAD_FONT8=1
-DLOAD_GFXFF=1 -DLOAD_GFXFF=1
-DSMOOTH_FONT=1 -DSMOOTH_FONT=1
-DSPI_FREQUENCY=55000000 -DSPI_FREQUENCY=55000000
-DSPI_READ_FREQUENCY=20000000 -DSPI_READ_FREQUENCY=20000000
-DSPI_TOUCH_FREQUENCY=2500000 -DSPI_TOUCH_FREQUENCY=2500000

View File

@@ -1,7 +1,7 @@
#ifndef _GLOBAL_CONFIG_INIT #ifndef _GLOBAL_CONFIG_INIT
#define _GLOBAL_CONFIG_INIT #define _GLOBAL_CONFIG_INIT
#define CONFIG_VERSION 1 #define CONFIG_VERSION 80
typedef struct _GLOBAL_CONFIG { typedef struct _GLOBAL_CONFIG {
unsigned char version; unsigned char version;
@@ -10,6 +10,7 @@ typedef struct _GLOBAL_CONFIG {
struct { struct {
bool screenCalibrated : 1; bool screenCalibrated : 1;
bool wifiConfigured : 1; bool wifiConfigured : 1;
bool ipConfigured : 1;
}; };
}; };
float screenCalXOffset; float screenCalXOffset;
@@ -19,6 +20,9 @@ typedef struct _GLOBAL_CONFIG {
char wifiSSID[32]; char wifiSSID[32];
char wifiPassword[64]; char wifiPassword[64];
char klipperHost[64];
unsigned short klipperPort;
} GLOBAL_CONFIG; } GLOBAL_CONFIG;
extern GLOBAL_CONFIG global_config; extern GLOBAL_CONFIG global_config;

View File

@@ -0,0 +1,41 @@
#include <WebSocketsClient.h>
#include "../conf/global_config.h"
WebSocketsClient webSocket;
void websocket_event(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
Serial.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
break;
case WStype_TEXT:
Serial.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
break;
}
}
void websocket_process(){
webSocket.loop();
}
void websocket_setup(){
webSocket.begin("192.168.0.122", global_config.klipperPort, "/websocket");
webSocket.onEvent(websocket_event);
webSocket.setReconnectInterval(5000);
}

View File

@@ -0,0 +1,2 @@
void websocket_process();
void websocket_setup();

View File

@@ -1,6 +1,8 @@
#include "conf/global_config.h" #include "conf/global_config.h"
#include "conf/screen_driver.h" #include "conf/screen_driver.h"
#include "ui/wifi_setup.h" #include "ui/wifi_setup.h"
#include "ui/ip_setup.h"
#include "core/websocket_setup.h"
#include "lvgl.h" #include "lvgl.h"
static void event_handler(lv_event_t * e){ static void event_handler(lv_event_t * e){
@@ -22,6 +24,8 @@ void setup() {
Serial.println("Screen init done"); Serial.println("Screen init done");
wifi_init(); wifi_init();
ip_setup();
websocket_setup();
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
@@ -34,9 +38,17 @@ void setup() {
label = lv_label_create(btn1); label = lv_label_create(btn1);
lv_label_set_text(label, "Reset Configuration"); lv_label_set_text(label, "Reset Configuration");
lv_obj_center(label); lv_obj_center(label);
lv_obj_t * slider = lv_slider_create(lv_scr_act());
lv_obj_set_width(slider, 200);
lv_obj_align(slider, LV_ALIGN_CENTER, 0, 40);
lv_slider_set_range(slider, 0, 100);
lv_slider_set_value(slider, 50, LV_ANIM_OFF);
} }
void loop(){ void loop(){
wifi_ok();
websocket_process();
lv_timer_handler(); lv_timer_handler();
lv_task_handler(); lv_task_handler();
} }

View File

@@ -0,0 +1,101 @@
#include "ip_setup.h"
#include "../conf/global_config.h"
#include "lvgl.h"
#include <TFT_eSPI.h>
#include <HTTPClient.h>
bool connect_ok = false;
lv_obj_t * ipEntry;
lv_obj_t * portEntry;
lv_obj_t * label = NULL;
bool verify_ip(){
HTTPClient client;
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/info";
int httpCode;
try {
Serial.println(url);
client.begin(url.c_str());
httpCode = client.GET();
return httpCode == 200;
}
catch (...) {
Serial.println("Failed to connect");
return false;
}
}
static void ta_event_cb(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * ta = lv_event_get_target(e);
lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e);
if(code == LV_EVENT_FOCUSED) {
lv_keyboard_set_textarea(kb, ta);
lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
}
else if(code == LV_EVENT_DEFOCUSED) {
lv_keyboard_set_textarea(kb, NULL);
lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
}
else if (code == LV_EVENT_READY)
{
strcpy(global_config.klipperHost, lv_textarea_get_text(ipEntry));
global_config.klipperPort = atoi(lv_textarea_get_text(portEntry));
bool result = verify_ip();
if (result)
{
global_config.ipConfigured = true;
WriteGlobalConfig();
connect_ok = true;
}
else
{
lv_label_set_text(label, "Failed to connect");
}
}
}
void ip_setup_inner(){
lv_obj_clean(lv_scr_act());
lv_obj_t * keyboard = lv_keyboard_create(lv_scr_act());
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Enter Klipper IP and Port");
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 10 + 2);
ipEntry = lv_textarea_create(lv_scr_act());
lv_textarea_set_one_line(ipEntry, true);
lv_textarea_set_max_length(ipEntry, 57);
lv_textarea_set_text(ipEntry, "");
lv_obj_align(ipEntry, LV_ALIGN_TOP_LEFT, 10, 40);
lv_obj_add_event_cb(ipEntry, ta_event_cb, LV_EVENT_ALL, keyboard);
lv_obj_set_size(ipEntry, TFT_HEIGHT - 20 - 100, 60);
portEntry = lv_textarea_create(lv_scr_act());
lv_textarea_set_one_line(portEntry, true);
lv_textarea_set_max_length(portEntry, 5);
lv_textarea_set_text(portEntry, "80");
lv_obj_align(portEntry, LV_ALIGN_TOP_LEFT, TFT_HEIGHT - 20 - 80, 40);
lv_obj_add_event_cb(portEntry, ta_event_cb, LV_EVENT_ALL, keyboard);
lv_obj_set_size(portEntry, 90, 60);
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
lv_keyboard_set_textarea(keyboard, ipEntry);
}
void ip_setup(){
connect_ok = false;
if (global_config.ipConfigured && verify_ip()){
return;
}
ip_setup_inner();
while (!connect_ok)
{
lv_timer_handler();
lv_task_handler();
}
}

View File

@@ -0,0 +1 @@
void ip_setup();

View File

@@ -1,4 +1,5 @@
#include "lvgl.h" #include "lvgl.h"
#include "wifi_setup.h"
#include "../conf/global_config.h" #include "../conf/global_config.h"
#include "WiFi.h" #include "WiFi.h"
@@ -149,4 +150,10 @@ void wifi_init(){
lv_timer_handler(); lv_timer_handler();
lv_task_handler(); lv_task_handler();
} }
}
void wifi_ok(){
if (WiFi.status() != WL_CONNECTED){
wifi_init();
}
} }

View File

@@ -1 +1,2 @@
void wifi_init(); void wifi_init();
void wifi_ok();