#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
WiFiClient wificlient;
String ssid="TP-LINK_C97D"; //你的wifif名
String wifipwd="19805282968"; //你的wifi密码
void setup() {
  Serial.begin(115200);
  delay(500);
  WiFi.begin(ssid.c_str(), wifipwd.c_str());
  delay(500);
  WiFi.mode(WIFI_STA);
  Serial.println("Wifi->connet");
  int ii = 0;
  for (ii; ii < 20; ii++) {
    delay(500);
    Serial.print(" . ");
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Wifi 连接成功");
      break;
    }
  }
  String upUrl = esp8266post();
  if (upUrl.length() > 10) {
    upUrl.trim(); 
    while (1) {
      ESPhttpUpdate.onStart(update_started);
      ESPhttpUpdate.onEnd(update_finished);
      ESPhttpUpdate.onProgress(update_progress);
      ESPhttpUpdate.onError(update_error);
      ESPhttpUpdate.update(wificlient, upUrl);

    }
    ESP.restart();
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}
void update_started() {
  Serial.println("CALLBACK:  HTTP update process started");
}

void update_finished() {
  Serial.println("CALLBACK:  HTTP update process finished");
}
String esp8266post() {
  HTTPClient http;
  String url = "http://jyan.nobiggie.cn/query/vs";
  http.begin(wificlient, url);
  http.setTimeout(3000);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  String postData = "vs=abc1124267212";
  int httpResponseCode = http.POST(postData);
  if (httpResponseCode > 0) {
    String response = http.getString();
    if (response != "false") {
      return response;
    }
  }
  return "";
}
void update_progress(int cur, int total) {
  Serial.printf("CALLBACK:  HTTP update process at %d of %d bytes...\n", cur, total);
}

void update_error(int err) {
  Serial.printf("CALLBACK:  HTTP update fatal error code %d\n", err);
}