1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00
DarioGHub 9701d3afb6
Add setClientTimeout() to ESP8266HTTPUpdate (#8740)
Shortening up the default hard-coded 8000 ms timeout, will save energy when module can't reach its update server, especially on battery powered projects, and otherwise provide adaptability to the local wlan. The ESPhttpUpdate object is auto-created by #including ESP8266HTTPUpdate. ESP8266HTTPUpdate also provides a constructor with param httpClientTimeout, but to use it required destroying the auto ESPhttpUpdate object, or creating a secondESPhttpUpdate(timeout) object.
2022-12-06 13:02:57 +03:00

86 lines
2.3 KiB
C++

/**
httpUpdate.ino
Created on: 27.11.2015
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#ifndef APSSID
#define APSSID "APSSID"
#define APPSK "APPSK"
#endif
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(false);
Serial.println();
ESPhttpUpdate.setClientTimeout(2000); // default was 8000
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(APSSID, APPSK);
}
void update_started() {
Serial.println("CALLBACK: HTTP update process started");
}
void update_finished() {
Serial.println("CALLBACK: HTTP update process finished");
}
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);
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
// The line below is optional. It can be used to blink the LED on the board during flashing
// The LED will be on during download of one buffer of data from the network. The LED will
// be off during writing that buffer to flash
// On a good connection the LED should flash regularly. On a bad connection the LED will be
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
// Add optional callback notifiers
ESPhttpUpdate.onStart(update_started);
ESPhttpUpdate.onEnd(update_finished);
ESPhttpUpdate.onProgress(update_progress);
ESPhttpUpdate.onError(update_error);
t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://server/file.bin");
// Or:
// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED: Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); break;
case HTTP_UPDATE_NO_UPDATES: Serial.println("HTTP_UPDATE_NO_UPDATES"); break;
case HTTP_UPDATE_OK: Serial.println("HTTP_UPDATE_OK"); break;
}
}
}