Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include "FS.h"
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266HTTPClient.h>
  4. WiFiClientSecure client;
  5.  
  6. const char* ssid = "AC";
  7. const char* password = "23saller45";
  8. String url = "http://osvathmark.myqnapcloud.com:8022/image/jpeg.cgi";
  9. String localfile = "/downloaded.000";
  10. const char* httpusername = "admin";
  11. const char* httppassword = "ccc";
  12. const int buffersize = 1024;
  13.  
  14. void downloadFile(String url, String filename, boolean auth) {
  15. if (!SPIFFS.begin()) {
  16. Serial.println("File system mount error!");
  17. return;
  18. }
  19. Serial.println("Downloading " + url + " and saving as " + filename);
  20. if (SPIFFS.exists(filename) == true) { // check file exists
  21. SPIFFS.remove(filename); // ha létezik a fájl, akkor töröljük
  22. }
  23. if(WiFi.status() == WL_CONNECTED) {
  24. HTTPClient http;
  25. Serial.print("[HTTP] begin...\n");
  26. http.begin(url);
  27. if (auth) http.setAuthorization(httpusername, httppassword);
  28. int httpCode = http.GET();
  29. if(httpCode > 0) {
  30. File f = SPIFFS.open(filename, "w+");
  31. if (!f) {
  32. Serial.println("file open failed");
  33. return;
  34. }
  35. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  36. if(httpCode == HTTP_CODE_OK) {
  37. int len, total = http.getSize(); // get lenght of document (is -1 when Server sends no Content-Length header)
  38. len = total;
  39. uint8_t buff[buffersize] = { 0 }; // create buffer for read
  40. WiFiClient * stream = http.getStreamPtr(); // get tcp stream
  41. while(http.connected() && (len > 0 || len == -1)) { // read all data from server
  42. size_t size = stream->available(); // get available data size
  43. if(size) {
  44. int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // read up to 128 byte
  45. f.write(buff, c); // write it to file
  46. if(len > 0) len -= c;
  47. Serial.println(String(total-len) + " / " + String(total));
  48. }
  49. delay(1);
  50. }
  51. if (len==0) Serial.print("[HTTP] EOF. OK.\n"); else Serial.print("[HTTP] Connection closed!\n");
  52. }
  53. f.close();
  54. } else Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  55. http.end();
  56. }
  57. else Serial.println("WiFi is not connected!");
  58. }
  59.  
  60. void setup() {
  61. Serial.begin(115200);
  62. delay(10);
  63. Serial.println();
  64. Serial.print("Connecting to ");
  65. Serial.println(ssid);
  66. WiFi.begin(ssid, password);
  67. while (WiFi.status() != WL_CONNECTED) {
  68. delay(500);
  69. Serial.print(".");
  70. }
  71. Serial.println("");
  72. Serial.println("WiFi connected");
  73. Serial.print("IP address: ");
  74. Serial.println(WiFi.localIP());
  75. }
  76.  
  77. void loop() {
  78. downloadFile("http://oscomputer.hu/download/esp8266/edit.htm.gz","/edit.htm.gz", false);
  79. downloadFile("http://oscomputer.hu/download/esp8266/favicon.ico","/favicon.ico", false);
  80. downloadFile("http://oscomputer.hu/download/esp8266/graphs.js.gz","/graphs.js.gz", false);
  81. downloadFile("http://oscomputer.hu/download/esp8266/index.htm","/index.htm", false);
  82. delay(30000);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement