GxEPD2_Spiffs_Loader.ino 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // GxEPD2_Spiffs_Loader : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. // Supporting Arduino Forum Topics:
  12. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  13. // Good Dispay ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
  14. #include <FS.h>
  15. #include <ESP8266WiFi.h>
  16. #include <WiFiClient.h>
  17. #include <WiFiClientSecure.h>
  18. const char* ssid = "........";
  19. const char* password = "........";
  20. const int httpPort = 80;
  21. const int httpsPort = 443;
  22. 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";
  23. 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";
  24. const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
  25. const char* host_rawcontent = "raw.githubusercontent.com";
  26. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  27. void setup()
  28. {
  29. Serial.begin(115200);
  30. Serial.println();
  31. Serial.println("GxEPD2_Spiffs_Loader");
  32. #ifdef RE_INIT_NEEDED
  33. WiFi.persistent(true);
  34. WiFi.mode(WIFI_STA); // switch off AP
  35. WiFi.setAutoConnect(true);
  36. WiFi.setAutoReconnect(true);
  37. WiFi.disconnect();
  38. #endif
  39. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  40. {
  41. Serial.println();
  42. Serial.print("WiFi.getAutoConnect()=");
  43. Serial.println(WiFi.getAutoConnect());
  44. Serial.print("WiFi.SSID()=");
  45. Serial.println(WiFi.SSID());
  46. WiFi.mode(WIFI_STA); // switch off AP
  47. Serial.print("Connecting to ");
  48. Serial.println(ssid);
  49. WiFi.begin(ssid, password);
  50. }
  51. int ConnectTimeout = 30; // 15 seconds
  52. while (WiFi.status() != WL_CONNECTED)
  53. {
  54. delay(500);
  55. Serial.print(".");
  56. Serial.print(WiFi.status());
  57. if (--ConnectTimeout <= 0)
  58. {
  59. Serial.println();
  60. Serial.println("WiFi connect timeout");
  61. return;
  62. }
  63. }
  64. Serial.println();
  65. Serial.println("WiFi connected");
  66. // Print the IP address
  67. Serial.println(WiFi.localIP());
  68. SPIFFS.begin();
  69. Serial.println("SPIFFS started");
  70. listFiles();
  71. downloadBitmaps();
  72. listFiles();
  73. }
  74. void loop()
  75. {
  76. }
  77. void downloadBitmaps()
  78. {
  79. downloadFile_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", "chanceflurries.bmp");
  80. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, "logo200x200.bmp");
  81. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, "first200x200.bmp");
  82. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, "second200x200.bmp");
  83. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, "third200x200.bmp");
  84. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, "fourth200x200.bmp");
  85. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, "fifth200x200.bmp");
  86. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, "sixth200x200.bmp");
  87. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, "seventh200x200.bmp");
  88. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, "eighth200x200.bmp");
  89. }
  90. void downloadFile_HTTP(const char* host, const char* path, const char* filename, const char* target)
  91. {
  92. WiFiClient client;
  93. Serial.print("connecting to ");
  94. Serial.println(host);
  95. if (!client.connect(host, httpPort))
  96. {
  97. Serial.println("connection failed");
  98. return;
  99. }
  100. Serial.print("requesting URL: ");
  101. Serial.println(String("http://") + host + path + filename);
  102. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  103. "Host: " + host + "\r\n" +
  104. "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
  105. "Connection: close\r\n\r\n");
  106. Serial.println("request sent");
  107. bool ok = false;
  108. while (client.connected())
  109. {
  110. String line = client.readStringUntil('\n');
  111. if (!ok)
  112. {
  113. ok = line.startsWith("HTTP/1.1 200 OK");
  114. if (ok) Serial.println(line);
  115. //if (!ok) Serial.println(line);
  116. }
  117. if (!ok) Serial.println(line);
  118. //Serial.println(line);
  119. if (line == "\r")
  120. {
  121. Serial.println("headers received");
  122. break;
  123. }
  124. }
  125. if (!ok) return;
  126. uint8_t buffer[512];
  127. client.peekBytes(buffer, 2);
  128. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  129. size_t total = 0;
  130. fs::File file = SPIFFS.open(target, "w+");
  131. if (!file)
  132. {
  133. Serial.print(target); Serial.println(" open failed");
  134. return;
  135. }
  136. while (client.connected())
  137. {
  138. size_t available = client.available();
  139. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  140. if (fetch > 0)
  141. {
  142. size_t got = client.read(buffer, fetch);
  143. file.write(buffer, got);
  144. total += got;
  145. }
  146. delay(1); // yield();
  147. }
  148. file.close();
  149. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  150. }
  151. void downloadFile_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, const char* target)
  152. {
  153. // Use WiFiClientSecure class to create TLS connection
  154. WiFiClientSecure client;
  155. Serial.print("connecting to ");
  156. Serial.println(host);
  157. if (!client.connect(host, httpsPort))
  158. {
  159. Serial.println("connection failed");
  160. return;
  161. }
  162. if (fingerprint)
  163. {
  164. if (client.verify(fingerprint, host))
  165. {
  166. Serial.println("certificate matches");
  167. }
  168. else
  169. {
  170. Serial.println("certificate doesn't match");
  171. return;
  172. }
  173. }
  174. Serial.print("requesting URL: ");
  175. Serial.println(String("https://") + host + path + filename);
  176. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  177. "Host: " + host + "\r\n" +
  178. "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
  179. "Connection: close\r\n\r\n");
  180. Serial.println("request sent");
  181. bool ok = false;
  182. while (client.connected())
  183. {
  184. String line = client.readStringUntil('\n');
  185. if (!ok)
  186. {
  187. ok = line.startsWith("HTTP/1.1 200 OK");
  188. if (ok) Serial.println(line);
  189. //if (!ok) Serial.println(line);
  190. }
  191. if (!ok) Serial.println(line);
  192. //Serial.println(line);
  193. if (line == "\r")
  194. {
  195. Serial.println("headers received");
  196. break;
  197. }
  198. }
  199. if (!ok) return;
  200. uint8_t buffer[512];
  201. client.peekBytes(buffer, 2);
  202. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  203. size_t total = 0;
  204. fs::File file = SPIFFS.open(target, "w+");
  205. if (!file)
  206. {
  207. Serial.print(target); Serial.println(" open failed");
  208. return;
  209. }
  210. while (client.connected())
  211. {
  212. size_t available = client.available();
  213. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  214. if (fetch > 0)
  215. {
  216. size_t got = client.read(buffer, fetch);
  217. file.write(buffer, got);
  218. total += got;
  219. }
  220. delay(1); // yield();
  221. }
  222. file.close();
  223. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  224. }