| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include <ESP8266WiFi.h>
- #include <WebSocketClient.h>
- #include <PS2X_lib.h> //for v1.6
- #define PS2_DAT 1 //14
- #define PS2_CMD 3 //15
- #define PS2_SEL 12 //16
- #define PS2_CLK 15 //17
- PS2X ps2x;
- int error = 0;
- byte type = 0;
- byte vibrate = 0;
- int PlyStnRStickUpDn = 0; //读取PS2右侧摇杆向上/向下的值.
- int PlyStnRStickLtRt = 0; //读取PS2右侧摇杆向左/向右的值.
- int PlyStnLStickUpDn = 0; //读取PS2左侧摇杆向上/向下的值.
- int PlyStnLStickLtRt = 0; //读取PS2左侧摇杆向左/向右的值.
- /******************************************************************/
- const char* ssid = "无线信号";
- const char* password = "无线密码";
- char path[] = "/wss";
- char host[] = "www.xxx.com"; // 服务器端域名地址
- WebSocketClient webSocketClient;
- WiFiClient client;
- void(*resetFunc) (void) = 0;
- void setup(void) {
- Serial.begin(115200);
- ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
- delay(5000);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("loading...");
- }
- delay(5000);
-
- if (client.connect(host, 80)) {
- Serial.println("Wss-Udp-ok!");
- } else {
- Serial.println("Wss-Udp-No!");
- while(1) {
- }
- }
- webSocketClient.path = path;
- webSocketClient.host = host;
- if (webSocketClient.handshake(client)) {
- Serial.println("Handshake Ok");
- } else {
- Serial.println("Handshake No.");
- while(1) {
- }
- }
-
- }
- void loop() {
- String data;
-
- PlyStnLStickUpDn = ps2x.Analog(PSS_LY); //左侧摇杆上/下
- PlyStnLStickLtRt = ps2x.Analog(PSS_LX); //左侧摇杆左/右
- PlyStnRStickUpDn = ps2x.Analog(PSS_RY); //右侧摇杆上/下
- PlyStnRStickLtRt = ps2x.Analog(PSS_RX); //右侧摇杆左/右
-
- ps2x.read_gamepad();
-
- if(ps2x.ButtonPressed(RED_FRET)){
- webSocketClient.sendData("2");
- }
- if(ps2x.ButtonPressed(YELLOW_FRET)){
- webSocketClient.sendData("1");
- }
- if(ps2x.ButtonPressed(BLUE_FRET)){
- webSocketClient.sendData("3");
- }
- if(ps2x.ButtonPressed(ORANGE_FRET)){
- webSocketClient.sendData("4");
- }
- if(ps2x.Button(UP_STRUM)){
- webSocketClient.sendData("UP");
- }
- if(ps2x.Button(DOWN_STRUM)){
- webSocketClient.sendData("DOWN");
- }
-
- if(ps2x.Button(RIGHT_STRUM)){
- webSocketClient.sendData("RIGHT");
- }
- if(ps2x.Button(LEFT_STRUM)){
- webSocketClient.sendData("LEFT");
- }
-
- if(ps2x.Button(PSB_START)){
- webSocketClient.sendData("START");
- }
- if(ps2x.Button(PSB_SELECT)){
- webSocketClient.sendData("SELECT");
- }
- if(ps2x.Button(PSB_L2)){
- webSocketClient.sendData("L2");
- }
- if(ps2x.Button(PSB_R2)){
- webSocketClient.sendData("R2");
- }
- if(ps2x.Button(PSB_L1)) {
- webSocketClient.sendData("K-" + String(PlyStnLStickUpDn));
- }
- if(ps2x.Button(PSB_R1)) {
- webSocketClient.sendData("W-" + String(PlyStnRStickUpDn));
- }
-
- if (client.connected()) {
- webSocketClient.getData(data);
- if (data.length() > 0) {
- if(data == "server-ok"){
- webSocketClient.sendData("Hello!");
- }
- if(data == "username or password"){
- webSocketClient.sendData("admin:123456");
- }
- if(data == "reset"){
- Serial.println("EPS-Reset ...");
- resetFunc();
- }
- }
- }else{
- resetFunc();
- }
- delay(50);
- }
|