#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
// 定义摇杆通道1的ADC值
int joystickChannel1Raw = 0; // 第一个摇杆通道的原始ADC值
int joystickChannel1Smooth = 0; // 第一个摇杆通道的平滑输出值
int joystickChannel1Min = 0x7fffffff; // 定义ADC最小值
int joystickChannel1Center = 2048; // 摇杆置中时的ADC值
int joystickChannel1Max = 0; //定义ADC最大值
int joystickChannel1Out = 0; // 摇杆1最终输出值
// 定义摇杆通道2的ADC值
int joystickChannel2Raw = 0; // 第二个摇杆通道的原始ADC值
int joystickChannel2Smooth = 0; // 第二个摇杆通道的平滑输出值
int joystickChannel2Min = 0x7fffffff; //定义ADC最小值
int joystickChannel2Center = 0; // 定义ADC中间值
int joystickChannel2Max = 0; //定义ADC最大值
int joystickChannel2Out = 0; // 摇杆2最终输出值
// 设置WiFi接入点的SSID和密码
const char* ssid = "ESP32_AP";
const char* password = "12345678";
// 创建Web服务器和WebSocket服务器实例
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
void ADCcompu() {
static uint32_t ADCadd;
joystickChannel1Raw = analogRead(2);
joystickChannel2Raw = analogRead(3);
joystickChannel1Smooth = joystickChannel1Raw + joystickChannel1Smooth * 0.95;
joystickChannel2Smooth = joystickChannel2Raw + joystickChannel2Smooth * 0.95;
if (ADCadd == 0-1) {
if (joystickChannel1Min > joystickChannel1Smooth)
joystickChannel1Min = joystickChannel1Smooth;
if (joystickChannel1Max < joystickChannel1Smooth)
joystickChannel1Max = joystickChannel1Smooth;
if (joystickChannel2Min > joystickChannel2Smooth)
joystickChannel2Min = joystickChannel2Smooth;
if (joystickChannel2Max < joystickChannel2Smooth)
joystickChannel2Max = joystickChannel2Smooth;
} else if (ADCadd == 200) {
joystickChannel1Center = joystickChannel1Smooth;
joystickChannel2Center = joystickChannel2Smooth;
ADCadd = 0-1;
} else
ADCadd++;
if (joystickChannel1Center < joystickChannel1Smooth) //中间值处理
joystickChannel1Out = map(joystickChannel1Smooth, joystickChannel1Center, joystickChannel1Max, 0, 1000);
else
joystickChannel1Out = map(joystickChannel1Smooth, joystickChannel1Min, joystickChannel1Center, -1000, 0);
if (joystickChannel2Center < joystickChannel2Smooth) //中间值处理
joystickChannel2Out = map(joystickChannel2Smooth, joystickChannel2Center, joystickChannel2Max, 0, 1000);
else
joystickChannel2Out = map(joystickChannel2Smooth, joystickChannel2Min, joystickChannel2Center, -1000, 0);
}
void setup() {
Serial.begin(115200);
pinMode(2, INPUT);
pinMode(3, INPUT);
analogSetAttenuation(ADC_11db);
// 初始化WiFi接入点
delay(100);
WiFi.softAP(ssid);
for (int i = 0; i < 256; i++)
ADCcompu();
delay(100);
// 打印接入点的IP地址
Serial.println();
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
// 设置Web服务器处理根URL请求
server.on("/", handleRoot);
// 启动WebSocket服务器
webSocket.begin();
Serial.println("WebSocket server started.");
// 启动Web服务器
server.begin();
Serial.println("HTTP server started.");
}
void loop() {
ADCcompu();
// 处理WebSocket客户端连接
webSocket.loop();
// 处理Web服务器请求
server.handleClient();
// 每秒发送两个随机数
static unsigned long lastTime = 0;
if (millis() - lastTime >= 10) {
lastTime = millis();
sendRandomCoordinates();
}
}
void handleRoot() {
// 发送HTML页面内容
String html = "<html><body><div style='position:relative;width:1080px;height:1080px;border:1px solid black;'>";
html += "<div id='circle' style='position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:50px;height:50px;border-radius:50%;background-color:blue;'></div>";
html += "</div><script>";
html += "var circle = document.getElementById('circle');";
html += "var socket = new WebSocket('ws://' + window.location.hostname + ':81/');";
html += "socket.onmessage = function(event) {";
html += "var data = JSON.parse(event.data);";
html += "circle.style.left = data.x + 'px';";
html += "circle.style.top = data.y + 'px';";
html += "};";
html += "</script></body></html>";
server.send(200, "text/html", html);
}
void sendRandomCoordinates() {
// 生成两个随机数(假设正方形大小为300x300,圆圈大小为50x50)
int x = map(joystickChannel2Out, -1000, 1000, 50, 1030);
int y = map(joystickChannel1Out, -1000, 1000, 1030, 50);
// 创建JSON格式的字符串
String data = "{\"x\":";
data += x;
data += ",\"y\":";
data += y;
data += "}";
// 发送数据到所有连接的WebSocket客户端
webSocket.broadcastTXT(data);
}
ESP32C3最好的socket入门例程----基于socket通信的摇杆位置校正显示
最新推荐文章于 2024-01-23 22:29:20 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)