ESP12F_socket.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <ESP8266WiFi.h>
  2. #include <PS2X_lib.h>
  3. #include <ArduinoJson.h>
  4. //网络配置
  5. const char* ssid = "test";
  6. const char* password = "dd88888888";
  7. //sockt服务器地址
  8. const char* host = "caner.top";
  9. const uint16_t port = 49800;
  10. volatile uint16_t state = 0;
  11. //遥控引脚定义
  12. #define PS2_DAT 12 //14
  13. #define PS2_CMD 13 //15
  14. #define PS2_SEL 14 //16
  15. #define PS2_CLK 16 //17
  16. int Controller = 0; //控制器
  17. //遥控数据
  18. StaticJsonDocument<300> doc;
  19. //实例化
  20. WiFiClient client;
  21. PS2X ps2x;
  22. // WIFI连接
  23. void netConnect() {
  24. WiFi.begin(ssid, password);
  25. while (WiFi.status() != WL_CONNECTED) {
  26. delay(500);
  27. Serial.println("WIFI connection...");
  28. }
  29. Serial.println("WiFi connected IP:");
  30. Serial.println(WiFi.localIP());
  31. }
  32. //socekt 连接
  33. void soConnect() {
  34. // 判断是否有网
  35. if (WiFi.status() == WL_CONNECTED) {
  36. //若未连接到服务端,则客户端进行连接。
  37. if (client.connected()) {
  38. //增加状态
  39. if (state == 0) {
  40. Serial.println("socket connected!");
  41. state = 1;
  42. // 连接控制器
  43. ControllerInt();
  44. }
  45. // 发送
  46. PS2Control();
  47. } else {
  48. // 判断状态
  49. if (state == 1) {
  50. Serial.println("socket restart connection...");
  51. state = 0;
  52. // 需要增加控制器主动断开
  53. }
  54. // 连接服务器
  55. Serial.println("socket connection...");
  56. client.connect(host, port);
  57. delay(1000);
  58. }
  59. } else {
  60. // 重新连接网络
  61. netConnect();
  62. }
  63. }
  64. //控制器初始化
  65. void ControllerInt() {
  66. Serial.println("scan controller.");
  67. Controller = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
  68. while (Controller != 0) {
  69. delay(1000);
  70. Serial.print(".");
  71. Controller = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
  72. }
  73. Serial.println("");
  74. Serial.println("Controller connected!");
  75. }
  76. //PS2操作并发送信息
  77. void PS2Control() {
  78. ps2x.read_gamepad();
  79. // 上
  80. // if (ps2x.Button(PSB_PAD_UP) || ps2x.Button(PSB_TRIANGLE)){
  81. // client.println("UP");
  82. // Serial.println("UP");
  83. // }
  84. // 右
  85. // if (ps2x.Button(PSB_PAD_RIGHT) || ps2x.ButtonPressed(PSB_CIRCLE)){
  86. // client.println("RIGHT");
  87. // Serial.println("RIGHT");
  88. // }
  89. // 左
  90. // if (ps2x.Button(PSB_PAD_LEFT) || ps2x.ButtonReleased(PSB_SQUARE)){
  91. // client.println("LEFT");
  92. // Serial.println("LEFT");
  93. // }
  94. // 下
  95. // if (ps2x.Button(PSB_PAD_DOWN) || ps2x.NewButtonState(PSB_CROSS)){
  96. // client.println("DOWN");
  97. // Serial.println("DOWN");
  98. // }
  99. // 功能键
  100. // if (ps2x.NewButtonState()) {
  101. // if (ps2x.Button(PSB_L3))
  102. // Serial.println("L3 pressed");
  103. // if (ps2x.Button(PSB_R3))
  104. // Serial.println("R3 pressed");
  105. // if (ps2x.Button(PSB_L2))
  106. // Serial.println("L2 pressed");
  107. // if (ps2x.Button(PSB_R2)){
  108. // Serial.println("R2 pressed");
  109. // client.println("STOP");
  110. // Serial.println("STOP");
  111. // }
  112. // }
  113. // 车站加速+转向加速
  114. doc["type"] = "conctrl";
  115. doc["data"]["v0"] = 128;
  116. doc["data"]["v1"] = 128;
  117. doc["data"]["v2"] = 128;
  118. doc["data"]["v3"] = 128;
  119. if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) {
  120. //print stick values if either is TRUE
  121. // Serial.print("Stick Values:");
  122. // Serial.print(ps2x.Analog(PSS_LY), DEC);
  123. // Serial.print(",");
  124. // Serial.print(ps2x.Analog(PSS_LX), DEC);
  125. // Serial.print(",");
  126. // Serial.print(ps2x.Analog(PSS_RY), DEC);
  127. // Serial.print(",");
  128. // Serial.println(ps2x.Analog(PSS_RX), DEC);
  129. // 摇杆
  130. doc["data"]["v0"] = ps2x.Analog(PSS_RX);
  131. doc["data"]["v1"] = ps2x.Analog(PSS_RY);
  132. doc["data"]["v2"] = ps2x.Analog(PSS_LX);
  133. doc["data"]["v3"] = ps2x.Analog(PSS_LY);
  134. }
  135. // 循环频率过大过小会粘包|分包
  136. serializeJson(doc, Serial);
  137. Serial.println();
  138. serializeJson(doc, client);
  139. // client.println(doc);
  140. }
  141. //初始区
  142. void setup() {
  143. Serial.begin(115200);
  144. netConnect();
  145. }
  146. //轮询区
  147. void loop() {
  148. soConnect();
  149. delay(40);
  150. }