| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include <ESP8266WiFi.h>
- #include <PS2X_lib.h>
- #include <ArduinoJson.h>
- //网络配置
- const char* ssid = "test";
- const char* password = "dd88888888";
- //sockt服务器地址
- const char* host = "caner.top";
- const uint16_t port = 49800;
- volatile uint16_t state = 0;
- //遥控引脚定义
- #define PS2_DAT 12 //14
- #define PS2_CMD 13 //15
- #define PS2_SEL 14 //16
- #define PS2_CLK 16 //17
- int Controller = 0; //控制器
- //遥控数据
- StaticJsonDocument<300> doc;
- //实例化
- WiFiClient client;
- PS2X ps2x;
- // WIFI连接
- void netConnect() {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("WIFI connection...");
- }
- Serial.println("WiFi connected IP:");
- Serial.println(WiFi.localIP());
- }
- //socekt 连接
- void soConnect() {
- // 判断是否有网
- if (WiFi.status() == WL_CONNECTED) {
- //若未连接到服务端,则客户端进行连接。
- if (client.connected()) {
- //增加状态
- if (state == 0) {
- Serial.println("socket connected!");
- state = 1;
- // 连接控制器
- ControllerInt();
- }
- // 发送
- PS2Control();
- } else {
- // 判断状态
- if (state == 1) {
- Serial.println("socket restart connection...");
- state = 0;
- // 需要增加控制器主动断开
- }
- // 连接服务器
- Serial.println("socket connection...");
- client.connect(host, port);
- delay(1000);
- }
- } else {
- // 重新连接网络
- netConnect();
- }
- }
- //控制器初始化
- void ControllerInt() {
- Serial.println("scan controller.");
- Controller = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
- while (Controller != 0) {
- delay(1000);
- Serial.print(".");
- Controller = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
- }
- Serial.println("");
- Serial.println("Controller connected!");
- }
- //PS2操作并发送信息
- void PS2Control() {
- ps2x.read_gamepad();
- // 上
- // if (ps2x.Button(PSB_PAD_UP) || ps2x.Button(PSB_TRIANGLE)){
- // client.println("UP");
- // Serial.println("UP");
- // }
- // 右
- // if (ps2x.Button(PSB_PAD_RIGHT) || ps2x.ButtonPressed(PSB_CIRCLE)){
- // client.println("RIGHT");
- // Serial.println("RIGHT");
- // }
- // 左
- // if (ps2x.Button(PSB_PAD_LEFT) || ps2x.ButtonReleased(PSB_SQUARE)){
- // client.println("LEFT");
- // Serial.println("LEFT");
- // }
- // 下
- // if (ps2x.Button(PSB_PAD_DOWN) || ps2x.NewButtonState(PSB_CROSS)){
- // client.println("DOWN");
- // Serial.println("DOWN");
- // }
- // 功能键
- // if (ps2x.NewButtonState()) {
- // if (ps2x.Button(PSB_L3))
- // Serial.println("L3 pressed");
- // if (ps2x.Button(PSB_R3))
- // Serial.println("R3 pressed");
- // if (ps2x.Button(PSB_L2))
- // Serial.println("L2 pressed");
- // if (ps2x.Button(PSB_R2)){
- // Serial.println("R2 pressed");
- // client.println("STOP");
- // Serial.println("STOP");
- // }
- // }
- // 车站加速+转向加速
- doc["type"] = "conctrl";
- doc["data"]["v0"] = 128;
- doc["data"]["v1"] = 128;
- doc["data"]["v2"] = 128;
- doc["data"]["v3"] = 128;
- if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) {
- //print stick values if either is TRUE
- // Serial.print("Stick Values:");
- // Serial.print(ps2x.Analog(PSS_LY), DEC);
- // Serial.print(",");
- // Serial.print(ps2x.Analog(PSS_LX), DEC);
- // Serial.print(",");
- // Serial.print(ps2x.Analog(PSS_RY), DEC);
- // Serial.print(",");
- // Serial.println(ps2x.Analog(PSS_RX), DEC);
- // 摇杆
- doc["data"]["v0"] = ps2x.Analog(PSS_RX);
- doc["data"]["v1"] = ps2x.Analog(PSS_RY);
- doc["data"]["v2"] = ps2x.Analog(PSS_LX);
- doc["data"]["v3"] = ps2x.Analog(PSS_LY);
- }
- // 循环频率过大过小会粘包|分包
- serializeJson(doc, Serial);
- Serial.println();
- serializeJson(doc, client);
- // client.println(doc);
- }
- //初始区
- void setup() {
- Serial.begin(115200);
- netConnect();
- }
- //轮询区
- void loop() {
- soConnect();
- delay(40);
- }
|