#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
WiFiClient wificlient;
//14 13 12 4 都是高电平
int advance = 14;
int recoil = 12;
int turnLeft = 13;
int turnRight = 0;
int stop=2;
int speedA = 5;
int speedB = 4;
String serverUrl = "http://192.168.168.2/xyzgo";
String State = "■";
bool speedBL=false;
void setup() {
  pinMode(stop,INPUT_PULLUP);
  pinMode(advance, INPUT_PULLUP);
  pinMode(recoil, INPUT_PULLUP);
  pinMode(turnLeft, INPUT_PULLUP);
  pinMode(turnRight, INPUT_PULLUP);
  pinMode(speedA, INPUT_PULLUP);
  pinMode(speedB, INPUT_PULLUP);
  delay(500);
  Serial.begin(115200);
  WiFi.begin("esp32WebServer192.168.168.2", "www.360xyz.cn");
  WiFi.mode(WIFI_STA);
  delay(5000);
  int i=0;
  for (i; i < 20; i++) {
    delay(500);
    Serial.print(" . ");
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("WIFI连接成功");
    }
  }
  Serial.println("WIFI连接失败");
}

void loop() {
  
  if (digitalRead(advance) == HIGH && digitalRead(recoil) == HIGH && digitalRead(turnLeft) == HIGH && digitalRead(turnRight) == HIGH && digitalRead(speedA) == HIGH && digitalRead(speedB) == HIGH) {
    if (State != "■") {
      MyHttpPost(serverUrl, "xyz=■");
      State="■";
    }
  } else {
    if (digitalRead(stop) ==LOW) {
      MyHttpPost(serverUrl, "xyz=■");
      State = "■";
      Serial.print(" 停 ");
    }
    if (digitalRead(advance) ==LOW) {
      MyHttpPost(serverUrl, "xyz=↑");
      State = "↑";
      Serial.print(" 上 ");
    }
    if (digitalRead(recoil) ==LOW) {
      MyHttpPost(serverUrl, "xyz=↓");
      State = "↓";
      Serial.print(" 下 ");
    }
    if (digitalRead(turnLeft) ==LOW) {
      MyHttpPost(serverUrl, "xyz=←");
      State = "←";
      Serial.print(" 左 ");
    }
    if (digitalRead(turnRight) ==LOW) {
      MyHttpPost(serverUrl, "xyz=→");
      State = "→";
      Serial.print(" 右 ");
    }
    if (digitalRead(speedA) ==LOW) {
      if(speedBL==false){
        speedBL=true;
        MyHttpPost(serverUrl, "xyz=speedA");
        State = "speedA";
        Serial.print(" ++++++ ");
        delay(1000);
        speedBL=false;
      }
    }
    if (digitalRead(speedB) ==LOW) {
      if(speedBL==false){
        speedBL=true;
        MyHttpPost(serverUrl, "xyz=speedB");
        State = "speedB";
        Serial.print(" ---- ");
        delay(1000);
        speedBL=false;
      }
    }
  }
}
void MyHttpPost(String url, String postStr) {
  HTTPClient http;
  String response;
  http.begin(wificlient, url);
  http.setTimeout(2000);
  //需要传输的数据
  char dataBuff[postStr.length()];
  strcpy(dataBuff, postStr.c_str());
  //头信息，需要的反注释就可以
  //http.addHeader("Content-Type", "application/json;charset=utf-8");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpCode = http.POST(dataBuff);
  if (httpCode == 200) {
    response = http.getString();
   Serial.println(response);
  }
  http.end();
}