Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FS.h"
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- WiFiClientSecure client;
- const char* ssid = "AC";
- const char* password = "23saller45";
- String url = "http://osvathmark.myqnapcloud.com:8022/image/jpeg.cgi";
- String localfile = "/downloaded.000";
- const char* httpusername = "admin";
- const char* httppassword = "ccc";
- const int buffersize = 1024;
- void downloadFile(String url, String filename, boolean auth) {
- if (!SPIFFS.begin()) {
- Serial.println("File system mount error!");
- return;
- }
- Serial.println("Downloading " + url + " and saving as " + filename);
- if (SPIFFS.exists(filename) == true) { // check file exists
- SPIFFS.remove(filename); // ha létezik a fájl, akkor töröljük
- }
- if(WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- Serial.print("[HTTP] begin...\n");
- http.begin(url);
- if (auth) http.setAuthorization(httpusername, httppassword);
- int httpCode = http.GET();
- if(httpCode > 0) {
- File f = SPIFFS.open(filename, "w+");
- if (!f) {
- Serial.println("file open failed");
- return;
- }
- Serial.printf("[HTTP] GET... code: %d\n", httpCode);
- if(httpCode == HTTP_CODE_OK) {
- int len, total = http.getSize(); // get lenght of document (is -1 when Server sends no Content-Length header)
- len = total;
- uint8_t buff[buffersize] = { 0 }; // create buffer for read
- WiFiClient * stream = http.getStreamPtr(); // get tcp stream
- while(http.connected() && (len > 0 || len == -1)) { // read all data from server
- size_t size = stream->available(); // get available data size
- if(size) {
- int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // read up to 128 byte
- f.write(buff, c); // write it to file
- if(len > 0) len -= c;
- Serial.println(String(total-len) + " / " + String(total));
- }
- delay(1);
- }
- if (len==0) Serial.print("[HTTP] EOF. OK.\n"); else Serial.print("[HTTP] Connection closed!\n");
- }
- f.close();
- } else Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- http.end();
- }
- else Serial.println("WiFi is not connected!");
- }
- void setup() {
- Serial.begin(115200);
- delay(10);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- downloadFile("http://oscomputer.hu/download/esp8266/edit.htm.gz","/edit.htm.gz", false);
- downloadFile("http://oscomputer.hu/download/esp8266/favicon.ico","/favicon.ico", false);
- downloadFile("http://oscomputer.hu/download/esp8266/graphs.js.gz","/graphs.js.gz", false);
- downloadFile("http://oscomputer.hu/download/esp8266/index.htm","/index.htm", false);
- delay(30000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement