Example #1
0
  void getTimeFromGoogle(String &time) {
    if (DEBUG) Serial << F("Check time from google") << endl;
    if (waitForWifi(7000) != WL_CONNECTED) return;
    HTTPClient http;
    const char *headers[1] = {"Date"};
    http.begin(F("http://google.com/"));
    http.collectHeaders(headers, 1);
    int rc = http.sendRequest("HEAD");
    if (rc < 0) return;
    time = http.header("Date");
    //const char *d1 = http.header("Date").c_str();

  }
void TimeClient::updateTime() {
  HTTPClient http;

  String url = "http://www.google.com/";
  const char * headerkeys[] = {"Date"} ;
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  // Based on Arduino core BasicHttpClient.ino example
  // https://github.com/esp8266/Arduino/blob/1588b45a8a15e4d3f1b42f052fc41590e9bec0bb/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino
  // configure http client and url
  http.begin(url); //HTTP
  http.collectHeaders(headerkeys,headerkeyssize);
  // start connection and send HTTP header
  int httpCode = http.GET();
  // httpCode will be negative on error
  if(httpCode > 0) {
	  // HTTP header has been send and Server response header has been handled
	  USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
	  // file found at server
	  if ((httpCode == HTTP_CODE_OK)|| (httpCode == HTTP_CODE_FOUND)) {
		String payload = http.header("Date");
		USE_SERIAL.println(payload);
		http.end();
        payload.toUpperCase();
        // example:
      	// date: Thu, 19 Nov 2015 20:25:40 GMT
      	if (payload!=NULL) {
          Serial.println(payload.substring(17, 19) + ":" + payload.substring(20, 22) + ":" +payload.substring(23, 25));
          int parsedHours = payload.substring(17, 19).toInt();
          int parsedMinutes = payload.substring(20, 22).toInt();
          int parsedSeconds = payload.substring(23, 25).toInt();
          Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds));
          localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
          Serial.println(localEpoc);
          localMillisAtUpdate = millis();
        }
      }
  }
}
Example #3
0
void http_access_test() {
	HTTPClient* httpClient = new HTTPClient();
	
	HTTPHeader* httpHeader = new HTTPHeader();
	
	httpHeader->insert(
		std::string("Content-Type"), 
		std::string("application/x-www-form-urlencoded")
	);
	
	httpHeader->insert(
		std::string("Connection"), 
		std::string("close")
	);
	
	httpClient->get(
		new std::string("http://www.ex-studio.info/"), 
		httpHeader
	);
		
	int status_code = httpClient->status_code();
	std::cout << "Status code: " << status_code << std::endl;
	if(status_code != 200) {
		delete httpHeader;
		delete httpClient;
		return;
	}
	
	HTTPHeader* recvHeader = httpClient->header();
	std::string* recvBody = httpClient->body();
	std::cout << "Date is " << recvHeader->get("Date") << std::endl;
	std::cout << "body: " << *recvBody << std::endl;
	
	delete httpHeader;
	delete httpClient;
	return;
}