add ws
This commit is contained in:
parent
71615ead86
commit
1f1683fe2c
@ -16,4 +16,5 @@ monitor_speed = 115200
|
||||
|
||||
lib_deps =
|
||||
adafruit/Adafruit GFX Library
|
||||
adafruit/Adafruit SSD1306
|
||||
adafruit/Adafruit SSD1306
|
||||
links2004/WebSockets
|
||||
@ -4,17 +4,20 @@
|
||||
#include "web_server.h"
|
||||
#include "controller.h"
|
||||
#include "robot_state.h"
|
||||
#include "ws_server.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
actuatorsInit();
|
||||
webServerInit();
|
||||
wsInit();
|
||||
|
||||
robot.lastCmdMs = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
webServerLoop();
|
||||
wsLoop();
|
||||
controllerUpdate();
|
||||
}
|
||||
|
||||
@ -52,10 +52,30 @@ const char INDEX_HTML[] PROGMEM = R"HTML(
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let ws = null;
|
||||
|
||||
function wsConnect(){
|
||||
ws = new WebSocket(`ws://${location.hostname}:81`);
|
||||
|
||||
ws.onopen = () => {
|
||||
st('WS CONNECTED');
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
st('WS DISCONNECTED');
|
||||
setTimeout(wsConnect, 1000);
|
||||
};
|
||||
|
||||
ws.onmessage = (e) => {
|
||||
const j = JSON.parse(e.data);
|
||||
st(`MODE=${j.mode} | L=${j.speedL} R=${j.speedR} | UP=${Math.floor(j.uptime/1000)}s`);
|
||||
};
|
||||
}
|
||||
|
||||
const st = (t) => document.getElementById('st').textContent = t;
|
||||
let holdTimer = null;
|
||||
|
||||
async function cmd(c){
|
||||
async function cmd_old(c){
|
||||
try{
|
||||
const r = await fetch(`/cmd?c=${c}`);
|
||||
st(await r.text());
|
||||
@ -64,7 +84,13 @@ const char INDEX_HTML[] PROGMEM = R"HTML(
|
||||
}
|
||||
}
|
||||
|
||||
async function setSpeed(v){
|
||||
function cmd(c){
|
||||
if (ws && ws.readyState === 1) {
|
||||
ws.send(`MOVE:${c}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function setSpeed_old(v){
|
||||
document.getElementById('spv').textContent = v;
|
||||
try{
|
||||
const r = await fetch(`/speed?l=${v}&r=${v}`);
|
||||
@ -74,6 +100,13 @@ const char INDEX_HTML[] PROGMEM = R"HTML(
|
||||
}
|
||||
}
|
||||
|
||||
function setSpeed(v){
|
||||
document.getElementById('spv').textContent = v;
|
||||
if (ws && ws.readyState === 1) {
|
||||
ws.send(`SPEED:${v}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function setMode(m){
|
||||
try{
|
||||
const r = await fetch(`/mode?m=${m}`);
|
||||
@ -140,6 +173,7 @@ const char INDEX_HTML[] PROGMEM = R"HTML(
|
||||
slider.addEventListener('input', ()=> setSpeed(slider.value));
|
||||
|
||||
st('Ready');
|
||||
wsConnect();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
79
src/ws_server.cpp
Normal file
79
src/ws_server.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include <WebSocketsServer.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "ws_server.h"
|
||||
#include "controller.h"
|
||||
#include "robot_state.h"
|
||||
|
||||
static WebSocketsServer ws(81); // порт 81
|
||||
|
||||
static uint32_t lastWsCmdMs = 0;
|
||||
|
||||
void wsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||
switch (type) {
|
||||
|
||||
case WStype_CONNECTED:
|
||||
lastWsCmdMs = millis();
|
||||
break;
|
||||
|
||||
case WStype_DISCONNECTED:
|
||||
controllerStop();
|
||||
break;
|
||||
|
||||
case WStype_TEXT: {
|
||||
String msg = String((char*)payload);
|
||||
|
||||
// простейший протокол
|
||||
// MOVE:FWD / MOVE:STOP / SPEED:150
|
||||
|
||||
if (msg.startsWith("MOVE:")) {
|
||||
String cmd = msg.substring(5);
|
||||
cmd.toUpperCase();
|
||||
|
||||
robot.lastCmdMs = millis();
|
||||
lastWsCmdMs = millis();
|
||||
|
||||
if (cmd == "STOP") {
|
||||
controllerStop();
|
||||
} else {
|
||||
controllerMove(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
else if (msg.startsWith("SPEED:")) {
|
||||
int v = msg.substring(6).toInt();
|
||||
controllerSetSpeed(v, v);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void wsInit() {
|
||||
ws.begin();
|
||||
ws.onEvent(wsEvent);
|
||||
}
|
||||
|
||||
void wsLoop() {
|
||||
ws.loop();
|
||||
|
||||
// если WebSocket умер — стоп
|
||||
if (robot.mode == MODE_MANUAL &&
|
||||
millis() - lastWsCmdMs > 400) {
|
||||
controllerStop();
|
||||
}
|
||||
}
|
||||
|
||||
void wsBroadcastStatus() {
|
||||
String json = "{";
|
||||
json += "\"mode\":\"" + String(modeToStr(robot.mode)) + "\",";
|
||||
json += "\"speedL\":" + String(robot.speedL) + ",";
|
||||
json += "\"speedR\":" + String(robot.speedR) + ",";
|
||||
json += "\"uptime\":" + String(millis());
|
||||
json += "}";
|
||||
|
||||
ws.broadcastTXT(json);
|
||||
}
|
||||
4
src/ws_server.h
Normal file
4
src/ws_server.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
void wsInit();
|
||||
void wsLoop();
|
||||
void wsBroadcastStatus();
|
||||
Loading…
x
Reference in New Issue
Block a user