| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // GxEPD2_Spiffs_Loader : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
- // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
- //
- // Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
- //
- // Author: Jean-Marc Zingg
- //
- // Version: see library.properties
- //
- // Library: https://github.com/ZinggJM/GxEPD2
- // Supporting Arduino Forum Topics:
- // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
- // Good Dispay ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
- #include <FS.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <WiFiClientSecure.h>
- const char* ssid = "........";
- const char* password = "........";
- const int httpPort = 80;
- const int httpsPort = 443;
- const char* fp_api_github_com = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
- const char* fp_github_com = "ca 06 f5 6b 25 8b 7a 0d 4f 2b 05 47 09 39 47 86 51 15 19 84";
- const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
- const char* host_rawcontent = "raw.githubusercontent.com";
- const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
- void setup()
- {
- Serial.begin(115200);
- Serial.println();
- Serial.println("GxEPD2_Spiffs_Loader");
- #ifdef RE_INIT_NEEDED
- WiFi.persistent(true);
- WiFi.mode(WIFI_STA); // switch off AP
- WiFi.setAutoConnect(true);
- WiFi.setAutoReconnect(true);
- WiFi.disconnect();
- #endif
- if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
- {
- Serial.println();
- Serial.print("WiFi.getAutoConnect()=");
- Serial.println(WiFi.getAutoConnect());
- Serial.print("WiFi.SSID()=");
- Serial.println(WiFi.SSID());
- WiFi.mode(WIFI_STA); // switch off AP
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- }
- int ConnectTimeout = 30; // 15 seconds
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- Serial.print(WiFi.status());
- if (--ConnectTimeout <= 0)
- {
- Serial.println();
- Serial.println("WiFi connect timeout");
- return;
- }
- }
- Serial.println();
- Serial.println("WiFi connected");
- // Print the IP address
- Serial.println(WiFi.localIP());
- SPIFFS.begin();
- Serial.println("SPIFFS started");
- listFiles();
- downloadBitmaps();
- listFiles();
- }
- void loop()
- {
- }
- void downloadBitmaps()
- {
- downloadFile_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", "chanceflurries.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, "logo200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, "first200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, "second200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, "third200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, "fourth200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, "fifth200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, "sixth200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, "seventh200x200.bmp");
- downloadFile_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, "eighth200x200.bmp");
- }
- void downloadFile_HTTP(const char* host, const char* path, const char* filename, const char* target)
- {
- WiFiClient client;
- Serial.print("connecting to ");
- Serial.println(host);
- if (!client.connect(host, httpPort))
- {
- Serial.println("connection failed");
- return;
- }
- Serial.print("requesting URL: ");
- Serial.println(String("http://") + host + path + filename);
- client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- bool ok = false;
- while (client.connected())
- {
- String line = client.readStringUntil('\n');
- if (!ok)
- {
- ok = line.startsWith("HTTP/1.1 200 OK");
- if (ok) Serial.println(line);
- //if (!ok) Serial.println(line);
- }
- if (!ok) Serial.println(line);
- //Serial.println(line);
- if (line == "\r")
- {
- Serial.println("headers received");
- break;
- }
- }
- if (!ok) return;
- uint8_t buffer[512];
- client.peekBytes(buffer, 2);
- Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
- size_t total = 0;
- fs::File file = SPIFFS.open(target, "w+");
- if (!file)
- {
- Serial.print(target); Serial.println(" open failed");
- return;
- }
- while (client.connected())
- {
- size_t available = client.available();
- size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
- if (fetch > 0)
- {
- size_t got = client.read(buffer, fetch);
- file.write(buffer, got);
- total += got;
- }
- delay(1); // yield();
- }
- file.close();
- Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
- }
- void downloadFile_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, const char* target)
- {
- // Use WiFiClientSecure class to create TLS connection
- WiFiClientSecure client;
- Serial.print("connecting to ");
- Serial.println(host);
- if (!client.connect(host, httpsPort))
- {
- Serial.println("connection failed");
- return;
- }
- if (fingerprint)
- {
- if (client.verify(fingerprint, host))
- {
- Serial.println("certificate matches");
- }
- else
- {
- Serial.println("certificate doesn't match");
- return;
- }
- }
- Serial.print("requesting URL: ");
- Serial.println(String("https://") + host + path + filename);
- client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- bool ok = false;
- while (client.connected())
- {
- String line = client.readStringUntil('\n');
- if (!ok)
- {
- ok = line.startsWith("HTTP/1.1 200 OK");
- if (ok) Serial.println(line);
- //if (!ok) Serial.println(line);
- }
- if (!ok) Serial.println(line);
- //Serial.println(line);
- if (line == "\r")
- {
- Serial.println("headers received");
- break;
- }
- }
- if (!ok) return;
- uint8_t buffer[512];
- client.peekBytes(buffer, 2);
- Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
- size_t total = 0;
- fs::File file = SPIFFS.open(target, "w+");
- if (!file)
- {
- Serial.print(target); Serial.println(" open failed");
- return;
- }
- while (client.connected())
- {
- size_t available = client.available();
- size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
- if (fetch > 0)
- {
- size_t got = client.read(buffer, fetch);
- file.write(buffer, got);
- total += got;
- }
- delay(1); // yield();
- }
- file.close();
- Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
- }
|