void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
  JsonStreamingParser parser;
  parser.setListener(this);
  WiFiClient client;

  // http://api.thingspeak.com/channels/CHANNEL_ID/feeds.json?results=2&api_key=API_KEY
  const char host[] = "api.thingspeak.com";
  String url = "/channels/" + channelId +"/feeds.json?results=1&api_key=" + readApiKey;

  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }


  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  int retryCounter = 0;
  while(!client.available()) {
    Serial.println(".");
    delay(1000);
    retryCounter++;
    if (retryCounter > 10) {
      return;
    }
  }

  int pos = 0;
  boolean isBody = false;
  char c;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      c = client.read();
      if (c == '{' || c == '[') {
        isBody = true;
      }
      if (isBody) {
        parser.parse(c);
      }
    }
  }
}
void TimeClient::updateTime() {
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect("google.com", httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // This will send the request to the server
  client.print(String("GET / HTTP/1.1\r\n") +
               String("Host: google.com\r\n") +
               String("Connection: close\r\n\r\n"));
  int repeatCounter = 0;
  while(!client.available() && repeatCounter < 10) {
    delay(1000); 
    Serial.println(".");
    repeatCounter++;
  }

  String line;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      line = client.readStringUntil('\n');
      line.toUpperCase();
      // example: 
      // date: Thu, 19 Nov 2015 20:25:40 GMT
      if (line.startsWith("DATE: ")) {
        Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31));
        int parsedHours = line.substring(23, 25).toInt();
        int parsedMinutes = line.substring(26, 28).toInt();
        int parsedSeconds = line.substring(29, 31).toInt();
        Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds));

        localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
        Serial.println(localEpoc);
        localMillisAtUpdate = millis();
      }
    }
  }

}
void WundergroundClient::doUpdate(String url) {
  JsonStreamingParser parser;
  parser.setListener(this);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect("api.wunderground.com", httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: api.wunderground.com\r\n" +
               "Connection: close\r\n\r\n");
  int retryCounter = 0;
  while(!client.available()) {
    delay(1000);
    retryCounter++;
    if (retryCounter > 10) {
      return;
    }
  }

  int pos = 0;
  boolean isBody = false;
  char c;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      c = client.read();
      if (c == '{' || c == '[') {
        isBody = true;
      }
      if (isBody) {
        parser.parse(c);
      }
    }
  }
}
Example #4
0
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version) {

    t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
    WiFiClient tcp;
    DEBUG_HTTP_UPDATE("[httpUpdate] connected to %s:%u %s .... ", host, port, url);

    if(!tcp.connect(host, port)) {
        DEBUG_HTTP_UPDATE("failed.\n");
        return ret;
    }
    DEBUG_HTTP_UPDATE("ok.\n");

    // set Timeout for readBytesUntil and readStringUntil
    tcp.setTimeout(2000);
    tcp.setNoDelay(true);

    String req = "GET ";

    req += url;
    req += " HTTP/1.1\r\n"
            "Host: ";
    req += host;
    req += "\r\n"
            "Connection: close\r\n"
            "User-Agent: ESP8266-http-Update\r\n"
            "x-ESP8266-STA-MAC: ";
    req += WiFi.macAddress();
    req += "\r\n"
            "x-ESP8266-AP-MAC: ";
    req += WiFi.softAPmacAddress();
    req += "\r\n"
            "x-ESP8266-free-space: ";
    req += ESP.getFreeSketchSpace();
    req += "\r\n"
            "x-ESP8266-sketch-size: ";
    req += ESP.getSketchSize();
    req += "\r\n"
            "x-ESP8266-chip-size: ";
    req += ESP.getFlashChipRealSize();
    req += "\r\n"
           "x-ESP8266-sdk-version: ";
    req += ESP.getSdkVersion();

    if(current_version[0] != 0x00) {
        req += "\r\n"
               "x-ESP8266-version: ";
        req += current_version;
    }

    req += "\r\n"
           "\r\n";

    tcp.write(req.c_str(), req.length());

    uint32_t code = 0;
    size_t len = 0;

    while(true) {
        String headerLine = tcp.readStringUntil('\n');
        headerLine.trim(); // remove \r

        if(headerLine.length() > 0) {
            DEBUG_HTTP_UPDATE("[httpUpdate][Header] RX: %s\n", headerLine.c_str());
            if(headerLine.startsWith("HTTP/1.")) {
                // 9 = lenght of "HTTP/1.x "
                code = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
            } else if(headerLine.startsWith("Content-Length: ")) {
                // 16 = lenght of "Content-Length: "
                len = headerLine.substring(16).toInt();
            }
        } else {
            break;
        }
    }

    DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
    DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
    DEBUG_HTTP_UPDATE("[httpUpdate]  - code: %d\n", code);
    DEBUG_HTTP_UPDATE("[httpUpdate]  - len: %d\n", len);

    DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
    DEBUG_HTTP_UPDATE("[httpUpdate]  - free Space: %d\n", ESP.getFreeSketchSpace());
    DEBUG_HTTP_UPDATE("[httpUpdate]  - current Sketch Size: %d\n", ESP.getSketchSize());

    if(current_version[0] != 0x00) {
        DEBUG_HTTP_UPDATE("[httpUpdate]  - current version: %s\n", current_version);
    }

    switch(code) {
        case 200:  ///< OK (Start Update)
            if(len > 0) {
                if(len > ESP.getFreeSketchSpace()) {
                    ret = HTTP_UPDATE_FAILED;
                    DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
                } else {

                    WiFiUDP::stopAll();
                    WiFiClient::stopAllExcept(&tcp);

                    delay(100);

                    if(ESP.updateSketch(tcp, len, false, false)) {
                        ret = HTTP_UPDATE_OK;
                        DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
                        tcp.stop();
                        ESP.restart();
                    } else {
                        ret = HTTP_UPDATE_FAILED;
                        DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
                    }
                }
            } else {
                ret = HTTP_UPDATE_FAILED;
                DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0?!\n");
            }
            break;
        case 304:
            ///< Not Modified (No updates)
            ret = HTTP_UPDATE_NO_UPDATES;
            break;
        case 403:
            ///< Forbidden
            // todo handle login
        default:
            ret = HTTP_UPDATE_FAILED;
            DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code);
            break;
    }

    return ret;
}