GxEPD2_SerialFlash_Loader.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // GxEPD2_SerialFlash_Loader : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. //
  3. // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
  4. //
  5. // Author: Jean-Marc Zingg
  6. //
  7. // Version: see library.properties
  8. //
  9. // Library: https://github.com/ZinggJM/GxEPD2
  10. //
  11. // this example uses the SerialFlash library from: https://github.com/PaulStoffregen/SerialFlash
  12. // with a modification for use with ESP32 or the STM32 package available here: https://github.com/ZinggJM/SerialFlash
  13. // download it as .zip file and install with Library Mananger method "Add .ZIP Library..."
  14. //
  15. // this example can run on ESP6266 or ESP32 using WiFi connection
  16. // Supporting Arduino Forum Topics (closed, read only):
  17. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  18. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  19. //
  20. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  21. #include <SerialFlash.h>
  22. // digital pin for flash chip CS pin:
  23. //const int FlashChipSelect = SS; // for standard slave select pin
  24. //const int FlashChipSelect = 5; // use D1 on my Wemos D1 mini wired for e-papers
  25. const int FlashChipSelect = 2; // as used with my ESP32 protoboard SD connector
  26. #if defined (ESP8266)
  27. #include <ESP8266WiFi.h>
  28. #endif
  29. #include <WiFiClient.h>
  30. #include <WiFiClientSecure.h>
  31. const char* ssid = "........";
  32. const char* password = "........";
  33. const int httpPort = 80;
  34. const int httpsPort = 443;
  35. const char* fp_api_github_com = "df b2 29 c6 a6 38 1a 59 9d c9 ad 92 2d 26 f5 3c 83 8f a5 87"; // as of 25.11.2020
  36. const char* fp_github_com = "5f 3f 7a c2 56 9f 50 a4 66 76 47 c6 a1 8c a0 07 aa ed bb 8e"; // as of 25.11.2020
  37. const char* fp_rawcontent = "70 94 de dd e6 c4 69 48 3a 92 70 a1 48 56 78 2d 18 64 e0 b7"; // as of 25.11.2020
  38. // note: the certificates have been moved to a separate header file, as R"CERT( destroys IDE Auto Format capability
  39. #include "GxEPD2_github_raw_certs.h"
  40. const char* certificate_rawcontent = cert_DigiCert_TLS_RSA_SHA256_2020_CA1; // ok, should work until 2031-04-13 23:59:59
  41. const char* host_rawcontent = "raw.githubusercontent.com";
  42. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  43. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  44. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/c/pic/";
  45. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/python/pic/";
  46. void setup()
  47. {
  48. Serial.begin(115200);
  49. Serial.println();
  50. Serial.println("GxEPD2_SerialFlash_Loader");
  51. #ifdef RE_INIT_NEEDED
  52. WiFi.persistent(true);
  53. WiFi.mode(WIFI_STA); // switch off AP
  54. WiFi.setAutoConnect(true);
  55. WiFi.setAutoReconnect(true);
  56. WiFi.disconnect();
  57. #endif
  58. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  59. {
  60. Serial.println();
  61. Serial.print("WiFi.getAutoConnect()=");
  62. Serial.println(WiFi.getAutoConnect());
  63. Serial.print("WiFi.SSID()=");
  64. Serial.println(WiFi.SSID());
  65. WiFi.mode(WIFI_STA); // switch off AP
  66. Serial.print("Connecting to ");
  67. Serial.println(ssid);
  68. WiFi.begin(ssid, password);
  69. }
  70. int ConnectTimeout = 30; // 15 seconds
  71. while (WiFi.status() != WL_CONNECTED)
  72. {
  73. delay(500);
  74. Serial.print(".");
  75. Serial.print(WiFi.status());
  76. if (--ConnectTimeout <= 0)
  77. {
  78. Serial.println();
  79. Serial.println("WiFi connect timeout");
  80. return;
  81. }
  82. }
  83. Serial.println();
  84. Serial.println("WiFi connected");
  85. // Print the IP address
  86. Serial.println(WiFi.localIP());
  87. setClock();
  88. if (!SerialFlash.begin(FlashChipSelect))
  89. {
  90. Serial.println("Unable to access SPI Flash chip");
  91. return;
  92. }
  93. Serial.println("SerialFlash started");
  94. eraseSerialFlash();
  95. listFiles();
  96. downloadBitmaps_200x200();
  97. downloadBitmaps_other();
  98. downloadBitmaps_test();
  99. listFiles();
  100. }
  101. void loop()
  102. {
  103. }
  104. void listFiles()
  105. {
  106. Serial.println("All Files on SPI Flash chip:");
  107. SerialFlash.opendir();
  108. while (1)
  109. {
  110. char filename[64];
  111. uint32_t filesize;
  112. if (SerialFlash.readdir(filename, sizeof(filename), filesize))
  113. {
  114. Serial.print(" ");
  115. Serial.print(filename);
  116. spaces(20 - strlen(filename));
  117. Serial.print(" ");
  118. Serial.print(filesize);
  119. Serial.print(" bytes");
  120. Serial.println();
  121. }
  122. else
  123. {
  124. Serial.println("no more files...");
  125. break; // no more files
  126. }
  127. }
  128. }
  129. void spaces(int num)
  130. {
  131. for (int i=0; i < num; i++)
  132. {
  133. Serial.print(" ");
  134. }
  135. }
  136. void downloadFile_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, const char* target, const char* certificate = certificate_rawcontent);
  137. void downloadBitmaps_200x200()
  138. {
  139. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, "logo200x200.bmp");
  140. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, "first200x200.bmp");
  141. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, "second200x200.bmp");
  142. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, "third200x200.bmp");
  143. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, "fourth200x200.bmp");
  144. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, "fifth200x200.bmp");
  145. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, "sixth200x200.bmp");
  146. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, "seventh200x200.bmp");
  147. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, "eighth200x200.bmp");
  148. }
  149. void downloadBitmaps_other()
  150. {
  151. downloadFile_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", "chanceflurries.bmp");
  152. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, "betty_1.bmp");
  153. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, "betty_4.bmp");
  154. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, "marilyn_240x240x8.bmp");
  155. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, "miniwoof.bmp");
  156. //downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, "test.bmp");
  157. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, "tiger.bmp");
  158. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, "tiger_178x160x4.bmp");
  159. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, "tiger_240x317x4.bmp");
  160. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, "tiger_320x200x24.bmp");
  161. //downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, "tiger16T.bmp");
  162. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, "woof.bmp");
  163. }
  164. void downloadBitmaps_test()
  165. {
  166. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, "output5.bmp");
  167. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, "output6.bmp");
  168. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, "tractor_1.bmp");
  169. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, "tractor_4.bmp");
  170. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, "tractor_8.bmp");
  171. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, "tractor_11.bmp");
  172. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, "tractor_44.bmp");
  173. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, "tractor_88.bmp");
  174. }
  175. void downloadFile_HTTP(const char* host, const char* path, const char* filename, const char* target)
  176. {
  177. WiFiClient client;
  178. Serial.print("connecting to ");
  179. Serial.println(host);
  180. if (!client.connect(host, httpPort))
  181. {
  182. Serial.println("connection failed");
  183. return;
  184. }
  185. Serial.print("requesting URL: ");
  186. Serial.println(String("http://") + host + path + filename);
  187. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  188. "Host: " + host + "\r\n" +
  189. "User-Agent: GxEPD2_SerialFlash_Loader\r\n" +
  190. "Connection: close\r\n\r\n");
  191. Serial.println("request sent");
  192. bool ok = false;
  193. int32_t content_length = 0;
  194. while (client.connected() || client.available())
  195. {
  196. String line = client.readStringUntil('\n');
  197. if (!ok)
  198. {
  199. ok = line.startsWith("HTTP/1.1 200 OK");
  200. if (ok) Serial.println(line);
  201. //if (!ok) Serial.println(line);
  202. }
  203. if (!ok) Serial.println(line);
  204. //Serial.println(line);
  205. if (line.startsWith("Content-Length: "))
  206. {
  207. content_length = line.substring(16, line.length()).toInt();
  208. Serial.print("content_length is "); Serial.println(content_length);
  209. }
  210. if (line == "\r")
  211. {
  212. Serial.println("headers received");
  213. break;
  214. }
  215. }
  216. if (!ok) return;
  217. uint8_t buffer[512];
  218. #if defined(ESP8266)
  219. client.peekBytes(buffer, 2);
  220. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  221. #endif
  222. size_t total = 0;
  223. //if (!SerialFlash.createErasable(target, content_length)) // would use much more space
  224. if (!SerialFlash.create(target, content_length))
  225. {
  226. Serial.print(target); Serial.println(" create failed");
  227. return;
  228. }
  229. SerialFlashFile file = SerialFlash.open(target);
  230. if (!file)
  231. {
  232. Serial.print(target); Serial.println(" open failed");
  233. return;
  234. }
  235. while (client.connected() || client.available())
  236. {
  237. size_t available = client.available();
  238. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  239. if (fetch > 0)
  240. {
  241. size_t got = client.read(buffer, fetch);
  242. file.write(buffer, got);
  243. total += got;
  244. }
  245. delay(1); // yield();
  246. }
  247. file.close();
  248. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  249. }
  250. void downloadFile_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, const char* target, const char* certificate)
  251. {
  252. // Use WiFiClientSecure class to create TLS connection
  253. #if defined (ESP8266)
  254. BearSSL::WiFiClientSecure client;
  255. BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
  256. #else
  257. WiFiClientSecure client;
  258. #endif
  259. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  260. Serial.print("connecting to "); Serial.println(host);
  261. #if defined (ESP8266)
  262. if (certificate) client.setTrustAnchors(&cert);
  263. else if (fingerprint) client.setFingerprint(fingerprint);
  264. else client.setInsecure();
  265. #elif defined (ESP32)
  266. if (certificate) client.setCACert(certificate);
  267. #endif
  268. if (!client.connect(host, httpsPort))
  269. {
  270. Serial.println("connection failed");
  271. return;
  272. }
  273. Serial.print("requesting URL: ");
  274. Serial.println(String("https://") + host + path + filename);
  275. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  276. "Host: " + host + "\r\n" +
  277. "User-Agent: GxEPD2_SerialFlash_Loader\r\n" +
  278. "Connection: close\r\n\r\n");
  279. Serial.println("request sent");
  280. bool ok = false;
  281. int32_t content_length = 0;
  282. while (client.connected() || client.available())
  283. {
  284. String line = client.readStringUntil('\n');
  285. if (!ok)
  286. {
  287. ok = line.startsWith("HTTP/1.1 200 OK");
  288. if (ok) Serial.println(line);
  289. //if (!ok) Serial.println(line);
  290. }
  291. if (!ok) Serial.println(line);
  292. //Serial.print("line: "); Serial.println(line);
  293. if (line.startsWith("Content-Length: "))
  294. {
  295. content_length = line.substring(16, line.length()).toInt();
  296. Serial.print("content_length is "); Serial.println(content_length);
  297. }
  298. if (line == "\r")
  299. {
  300. Serial.println("headers received");
  301. break;
  302. }
  303. }
  304. if (!ok) return;
  305. //return;
  306. uint8_t buffer[512];
  307. #if defined(ESP8266)
  308. client.peekBytes(buffer, 2);
  309. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  310. #endif
  311. size_t total = 0;
  312. //if (!SerialFlash.createErasable(target, content_length)) // would use much more space
  313. if (!SerialFlash.create(target, content_length))
  314. {
  315. Serial.print(target); Serial.println(" create failed");
  316. return;
  317. }
  318. SerialFlashFile file = SerialFlash.open(target);
  319. if (!file)
  320. {
  321. Serial.print(target); Serial.println(" open failed");
  322. return;
  323. }
  324. while (client.connected() || client.available())
  325. {
  326. // this doesn't work as expected, but it helps for long downloads
  327. int32_t start = millis();
  328. for (int16_t t = 0, dly = 50; t < 20; t++, dly += 50)
  329. {
  330. if (!(client.connected() || client.available())) break;
  331. if (client.available()) break; // read would not recover after having returned 0
  332. delay(dly);
  333. }
  334. if (!(client.connected() || client.available())) break;
  335. int32_t elapsed = millis() - start;
  336. if (elapsed > 250)
  337. {
  338. Serial.print("waited for available "); Serial.print(millis() - start); Serial.print(" ms @ "); Serial.println(total);
  339. }
  340. size_t available = client.available();
  341. if (0 == available)
  342. {
  343. Serial.print("download error: timeout on available() after "); Serial.print(total); Serial.println(" bytes");
  344. break; // don't hang forever
  345. }
  346. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  347. if (fetch > 0)
  348. {
  349. size_t got = client.read(buffer, fetch);
  350. file.write(buffer, got);
  351. total += got;
  352. }
  353. delay(1); // yield();
  354. if (total > 30000) delay(250); // helps for long downloads
  355. }
  356. file.close();
  357. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  358. }
  359. void eraseSerialFlash()
  360. {
  361. unsigned char id[5];
  362. SerialFlash.readID(id);
  363. unsigned long size = SerialFlash.capacity(id);
  364. unsigned long startMillis = millis();
  365. if (size > 0)
  366. {
  367. Serial.print("Flash Memory has ");
  368. Serial.print(size);
  369. Serial.println(" bytes.");
  370. Serial.println("Erasing ALL Flash Memory:");
  371. // Estimate the (lengthy) wait time.
  372. Serial.print(" estimated wait: ");
  373. int seconds = (float)size / eraseBytesPerSecond(id) + 0.5;
  374. Serial.print(seconds);
  375. Serial.println(" seconds.");
  376. Serial.println(" Yes, full chip erase is SLOW!");
  377. SerialFlash.eraseAll();
  378. unsigned long dotMillis = millis();
  379. unsigned char dotcount = 0;
  380. while (SerialFlash.ready() == false)
  381. {
  382. if (millis() - dotMillis > 1000)
  383. {
  384. dotMillis = dotMillis + 1000;
  385. Serial.print(".");
  386. dotcount = dotcount + 1;
  387. if (dotcount >= 60)
  388. {
  389. Serial.println();
  390. dotcount = 0;
  391. }
  392. }
  393. yield();
  394. }
  395. if (dotcount > 0) Serial.println();
  396. Serial.println("Erase completed");
  397. unsigned long elapsed = millis() - startMillis;
  398. Serial.print(" actual wait: ");
  399. Serial.print(elapsed / 1000ul);
  400. Serial.println(" seconds.");
  401. }
  402. }
  403. float eraseBytesPerSecond(const unsigned char *id)
  404. {
  405. if (id[0] == 0x20) return 152000.0; // Micron
  406. if (id[0] == 0x01) return 500000.0; // Spansion
  407. if (id[0] == 0xEF) return 419430.0; // Winbond
  408. if (id[0] == 0xC2) return 279620.0; // Macronix
  409. return 320000.0; // guess?
  410. }
  411. // Set time via NTP, as required for x.509 validation
  412. void setClock()
  413. {
  414. configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  415. Serial.print("Waiting for NTP time sync: ");
  416. time_t now = time(nullptr);
  417. while (now < 8 * 3600 * 2)
  418. {
  419. delay(500);
  420. Serial.print(".");
  421. now = time(nullptr);
  422. }
  423. Serial.println("");
  424. struct tm timeinfo;
  425. gmtime_r(&now, &timeinfo);
  426. Serial.print("Current time: ");
  427. Serial.print(asctime(&timeinfo));
  428. }