GxEPD2_SerialFlash_Loader.ino 16 KB

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