GxEPD2_WiFi_Example.ino 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // GxEPD2_WiFi_Example : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
  5. //
  6. // BMP handling code extracts taken from: https://github.com/prenticedavid/MCUFRIEND_kbv/tree/master/examples/showBMP_kbv_Uno
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. //
  14. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  15. // Supporting Arduino Forum Topics (closed, read only):
  16. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  17. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  18. //
  19. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  20. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  21. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  22. // enable or disable GxEPD2_GFX base class
  23. #define ENABLE_GxEPD2_GFX 0
  24. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  25. //#include <GFX.h>
  26. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  27. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  28. #include <GxEPD2_BW.h>
  29. #include <GxEPD2_3C.h>
  30. #include <GxEPD2_4C.h>
  31. #include <GxEPD2_7C.h>
  32. #include "GxEPD2_WiFi_DisplaySelection.h"
  33. #if defined (ESP8266)
  34. #include <ESP8266WiFi.h>
  35. #else
  36. #include <WiFi.h>
  37. #endif
  38. #include <WiFiClient.h>
  39. #include <WiFiClientSecure.h>
  40. const char* ssid = "........";
  41. const char* password = "........";
  42. const int httpPort = 80;
  43. const int httpsPort = 443;
  44. // note: the certificates have been moved to a separate header file, as R"CERT( destroys IDE Auto Format capability
  45. #include "GxEPD2_github_raw_certs.h"
  46. const char* certificate_rawcontent = github_io_pem; // ok, should work until Fri, 14 Mar 2025 23:59:59 GMT
  47. const char* host_rawcontent = "raw.githubusercontent.com";
  48. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  49. const char* path_workcontent = "/ZinggJM/GxEPD2/work_in_progress/extras/bitmaps/";
  50. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  51. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/c/pic/";
  52. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/python/pic/";
  53. const char* fp_rawcontent = "97:D8:C5:70:0F:12:24:6C:88:BC:FA:06:7E:8C:A7:4D:A8:62:67:28"; // SHA-1 as of 12.04.2024
  54. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  55. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  56. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  57. const char* certificate = certificate_rawcontent);
  58. // draws BMP bitmap according to set orientation
  59. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  60. void showBitmapFrom_HTTPS_Buffered(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  61. const char* certificate = certificate_rawcontent);
  62. void showNative4cFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  63. void showNative4cFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  64. const char* certificate = certificate_rawcontent);
  65. #if defined(ESP32)
  66. // uncomment next line to use HSPI for EPD (and VSPI for SD), e.g. with Waveshare ESP32 Driver Board
  67. //#define USE_HSPI_FOR_EPD
  68. #endif
  69. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  70. // SPIClassRP2040(spi_inst_t *spi, pin_size_t rx, pin_size_t cs, pin_size_t sck, pin_size_t tx);
  71. // uncomment next line for use with Waveshare Pico-ePaper-2.9.
  72. SPIClassRP2040 SPIn(spi1, 12, 13, 10, 11); // need be valid pins for same SPI channel, else fails blinking 4 long 4 short
  73. #endif
  74. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  75. SPIClass hspi(HSPI);
  76. #endif
  77. void setup()
  78. {
  79. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  80. hspi.begin(13, 12, 14, 15); // remap hspi for EPD (swap pins)
  81. display.epd2.selectSPI(hspi, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  82. #endif
  83. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  84. delay(2000); // USB serial available
  85. display.epd2.selectSPI(SPIn, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  86. #endif
  87. Serial.begin(115200);
  88. Serial.println();
  89. Serial.println("GxEPD2_WiFi_Example");
  90. //display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  91. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  92. if (display.pages() > 1)
  93. {
  94. delay(100);
  95. Serial.println();
  96. Serial.print("pages = "); Serial.print(display.pages()); Serial.print(" page height = "); Serial.println(display.pageHeight());
  97. delay(1000);
  98. }
  99. Serial.println();
  100. WiFi.mode(WIFI_STA); // switch off AP
  101. Serial.print("Connecting to ");
  102. Serial.println(ssid);
  103. WiFi.begin(ssid, password);
  104. int ConnectTimeout = 60; // 30 seconds
  105. while (WiFi.status() != WL_CONNECTED)
  106. {
  107. delay(500);
  108. Serial.print(".");
  109. Serial.print(WiFi.status());
  110. if (--ConnectTimeout <= 0)
  111. {
  112. Serial.println();
  113. Serial.println("WiFi connect timeout");
  114. return;
  115. }
  116. }
  117. Serial.println();
  118. Serial.println("WiFi connected");
  119. // Print the IP address
  120. Serial.println(WiFi.localIP());
  121. setClock();
  122. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || false)
  123. {
  124. //drawBitmapsBuffered_200x200();
  125. //drawBitmapsBuffered_other();
  126. drawBitmapsBuffered_7C();
  127. }
  128. else
  129. {
  130. drawBitmaps_200x200();
  131. drawBitmaps_other();
  132. }
  133. //drawBitmaps_test();
  134. //drawBitmapsBuffered_test();
  135. Serial.println("GxEPD2_WiFi_Example done");
  136. }
  137. void loop(void)
  138. {
  139. }
  140. void drawBitmaps_200x200()
  141. {
  142. int16_t x = (display.width() - 200) / 2;
  143. int16_t y = (display.height() - 200) / 2;
  144. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  145. delay(2000);
  146. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  147. delay(2000);
  148. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  149. delay(2000);
  150. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  151. delay(2000);
  152. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  153. delay(2000);
  154. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  155. delay(2000);
  156. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  157. delay(2000);
  158. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  159. delay(2000);
  160. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  161. delay(2000);
  162. }
  163. void drawBitmaps_other()
  164. {
  165. int16_t w2 = display.width() / 2;
  166. int16_t h2 = display.height() / 2;
  167. //showBitmapFrom_HTTP("www.packescape.com", "/img/assets/", "IniciMenusTV2.bmp", w2 - 200, h2 - 150, false);
  168. //delay(2000);
  169. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  170. delay(2000);
  171. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  172. delay(2000);
  173. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  174. delay(2000);
  175. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  176. delay(2000);
  177. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  178. delay(2000);
  179. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  180. delay(2000);
  181. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  182. delay(2000);
  183. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  184. delay(2000);
  185. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  186. delay(2000);
  187. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  188. delay(2000);
  189. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  190. delay(2000);
  191. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  192. delay(2000);
  193. }
  194. void drawBitmaps_test()
  195. {
  196. int16_t w2 = display.width() / 2;
  197. int16_t h2 = display.height() / 2;
  198. //showBitmapFrom_HTTPS(host_rawcontent, path_workcontent, "z0gs/screenshot.bmp", fp_rawcontent, 0, 0); delay(2000); return;
  199. //showNative4cFrom_HTTPS(host_rawcontent, path_workcontent, "z0gs/screenshot.bmp", fp_rawcontent, 0, 0); delay(2000); return;
  200. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  201. delay(2000);
  202. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  203. delay(2000);
  204. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  205. delay(2000);
  206. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  207. delay(2000);
  208. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  209. delay(2000);
  210. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0); // format 1: BI_RLE8 is not supported
  211. //delay(2000);
  212. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  213. delay(2000);
  214. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  215. delay(2000);
  216. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  217. delay(2000);
  218. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "liberato640x384.bmp", fp_rawcontent, 0, 0);
  219. //delay(2000);
  220. }
  221. void drawBitmapsBuffered_200x200()
  222. {
  223. int16_t x = (display.width() - 200) / 2;
  224. int16_t y = (display.height() - 200) / 2;
  225. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  226. delay(2000);
  227. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  228. delay(2000);
  229. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  230. delay(2000);
  231. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  232. delay(2000);
  233. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  234. delay(2000);
  235. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  236. delay(2000);
  237. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  238. delay(2000);
  239. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  240. delay(2000);
  241. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  242. delay(2000);
  243. }
  244. void drawBitmapsBuffered_other()
  245. {
  246. int16_t w2 = display.width() / 2;
  247. int16_t h2 = display.height() / 2;
  248. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  249. delay(2000);
  250. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  251. delay(2000);
  252. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  253. delay(2000);
  254. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  255. delay(2000);
  256. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  257. delay(2000);
  258. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  259. delay(2000);
  260. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  261. delay(2000);
  262. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  263. delay(2000);
  264. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  265. delay(2000);
  266. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  267. delay(2000);
  268. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  269. delay(2000);
  270. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  271. delay(2000);
  272. }
  273. void drawBitmapsBuffered_7C()
  274. {
  275. if ((display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46))
  276. {
  277. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, 0, 0);
  278. delay(2000);
  279. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, 0, 0);
  280. delay(2000);
  281. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, 0, 0);
  282. delay(2000);
  283. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, 0, 0);
  284. delay(2000);
  285. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, 0, 0);
  286. delay(2000);
  287. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, 0, 0);
  288. delay(2000);
  289. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, 0, 0);
  290. delay(2000);
  291. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, 0, 0);
  292. delay(2000);
  293. }
  294. }
  295. void drawBitmapsBuffered_test()
  296. {
  297. int16_t w2 = display.width() / 2;
  298. int16_t h2 = display.height() / 2;
  299. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  300. delay(2000);
  301. }
  302. static const uint16_t input_buffer_pixels = 800; // may affect performance
  303. //static const uint16_t input_buffer_pixels = 960; // may affect performance
  304. static const uint16_t max_row_width = 1872; // for up to 7.8" display 1872x1404
  305. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  306. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  307. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  308. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  309. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  310. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  311. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  312. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  313. {
  314. WiFiClient client;
  315. bool connection_ok = false;
  316. bool valid = false; // valid format to be handled
  317. bool flip = true; // bitmap is stored bottom-to-top
  318. uint32_t startTime = millis();
  319. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  320. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  321. Serial.print("connecting to "); Serial.println(host);
  322. if (!client.connect(host, httpPort))
  323. {
  324. Serial.println("connection failed");
  325. return;
  326. }
  327. Serial.print("requesting URL: ");
  328. Serial.println(String("http://") + host + path + filename);
  329. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  330. "Host: " + host + "\r\n" +
  331. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  332. "Connection: close\r\n\r\n");
  333. Serial.println("request sent");
  334. while (client.connected())
  335. {
  336. String line = client.readStringUntil('\n');
  337. if (!connection_ok)
  338. {
  339. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  340. if (connection_ok) Serial.println(line);
  341. }
  342. if (!connection_ok) Serial.println(line);
  343. if (line == "\r")
  344. {
  345. Serial.println("headers received");
  346. break;
  347. }
  348. }
  349. if (!connection_ok) return;
  350. // Parse BMP header
  351. if (read16(client) == 0x4D42) // BMP signature
  352. {
  353. uint32_t fileSize = read32(client);
  354. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  355. uint32_t imageOffset = read32(client); // Start of image data
  356. uint32_t headerSize = read32(client);
  357. uint32_t width = read32(client);
  358. int32_t height = (int32_t) read32(client);
  359. uint16_t planes = read16(client);
  360. uint16_t depth = read16(client); // bits per pixel
  361. uint32_t format = read32(client);
  362. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  363. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  364. {
  365. Serial.print("File size: "); Serial.println(fileSize);
  366. Serial.print("Image Offset: "); Serial.println(imageOffset);
  367. Serial.print("Header size: "); Serial.println(headerSize);
  368. Serial.print("Bit Depth: "); Serial.println(depth);
  369. Serial.print("Image size: ");
  370. Serial.print(width);
  371. Serial.print('x');
  372. Serial.println(abs(height));
  373. // BMP rows are padded (if needed) to 4-byte boundary
  374. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  375. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  376. if (height < 0)
  377. {
  378. height = -height;
  379. flip = false;
  380. }
  381. uint16_t w = width;
  382. uint16_t h = height;
  383. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  384. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  385. if (w <= max_row_width) // handle with direct drawing
  386. {
  387. valid = true;
  388. uint8_t bitmask = 0xFF;
  389. uint8_t bitshift = 8 - depth;
  390. uint16_t red, green, blue;
  391. bool whitish = false;
  392. bool colored = false;
  393. if (depth == 1) with_color = false;
  394. if (depth <= 8)
  395. {
  396. if (depth < 8) bitmask >>= depth;
  397. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  398. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  399. {
  400. blue = client.read();
  401. green = client.read();
  402. red = client.read();
  403. client.read();
  404. bytes_read += 4;
  405. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  406. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  407. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  408. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  409. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  410. color_palette_buffer[pn / 8] |= colored << pn % 8;
  411. }
  412. }
  413. display.clearScreen();
  414. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  415. bytes_read += skip(client, rowPosition - bytes_read);
  416. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  417. {
  418. if (!connection_ok || !(client.connected() || client.available())) break;
  419. delay(1); // yield() to avoid WDT
  420. uint32_t in_remain = rowSize;
  421. uint32_t in_idx = 0;
  422. uint32_t in_bytes = 0;
  423. uint8_t in_byte = 0; // for depth <= 8
  424. uint8_t in_bits = 0; // for depth <= 8
  425. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  426. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  427. uint32_t out_idx = 0;
  428. for (uint16_t col = 0; col < w; col++) // for each pixel
  429. {
  430. yield();
  431. if (!connection_ok || !(client.connected() || client.available())) break;
  432. // Time to read more pixel data?
  433. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  434. {
  435. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  436. uint32_t got = read8n(client, input_buffer, get);
  437. while ((got < get) && connection_ok)
  438. {
  439. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  440. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  441. got += gotmore;
  442. connection_ok = gotmore > 0;
  443. }
  444. in_bytes = got;
  445. in_remain -= got;
  446. bytes_read += got;
  447. in_idx = 0;
  448. }
  449. if (!connection_ok)
  450. {
  451. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  452. break;
  453. }
  454. switch (depth)
  455. {
  456. case 32:
  457. blue = input_buffer[in_idx++];
  458. green = input_buffer[in_idx++];
  459. red = input_buffer[in_idx++];
  460. in_idx++; // skip alpha
  461. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  462. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  463. break;
  464. case 24:
  465. blue = input_buffer[in_idx++];
  466. green = input_buffer[in_idx++];
  467. red = input_buffer[in_idx++];
  468. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  469. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  470. break;
  471. case 16:
  472. {
  473. uint8_t lsb = input_buffer[in_idx++];
  474. uint8_t msb = input_buffer[in_idx++];
  475. if (format == 0) // 555
  476. {
  477. blue = (lsb & 0x1F) << 3;
  478. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  479. red = (msb & 0x7C) << 1;
  480. }
  481. else // 565
  482. {
  483. blue = (lsb & 0x1F) << 3;
  484. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  485. red = (msb & 0xF8);
  486. }
  487. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  488. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  489. }
  490. break;
  491. case 1:
  492. case 2:
  493. case 4:
  494. case 8:
  495. {
  496. if (0 == in_bits)
  497. {
  498. in_byte = input_buffer[in_idx++];
  499. in_bits = 8;
  500. }
  501. uint16_t pn = (in_byte >> bitshift) & bitmask;
  502. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  503. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  504. in_byte <<= depth;
  505. in_bits -= depth;
  506. }
  507. break;
  508. }
  509. if (whitish)
  510. {
  511. // keep white
  512. }
  513. else if (colored && with_color)
  514. {
  515. out_color_byte &= ~(0x80 >> col % 8); // colored
  516. }
  517. else
  518. {
  519. out_byte &= ~(0x80 >> col % 8); // black
  520. }
  521. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  522. {
  523. output_row_color_buffer[out_idx] = out_color_byte;
  524. output_row_mono_buffer[out_idx++] = out_byte;
  525. out_byte = 0xFF; // white (for w%8!=0 border)
  526. out_color_byte = 0xFF; // white (for w%8!=0 border)
  527. }
  528. } // end pixel
  529. int16_t yrow = y + (flip ? h - row - 1 : row);
  530. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  531. } // end line
  532. Serial.print("downloaded in ");
  533. Serial.print(millis() - startTime);
  534. Serial.println(" ms");
  535. display.refresh();
  536. }
  537. Serial.print("bytes read "); Serial.println(bytes_read);
  538. }
  539. }
  540. client.stop();
  541. if (!valid)
  542. {
  543. Serial.println("bitmap format not handled.");
  544. }
  545. }
  546. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color, const char* certificate)
  547. {
  548. // Use WiFiClientSecure class to create TLS connection
  549. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  550. BearSSL::WiFiClientSecure client;
  551. BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
  552. #else
  553. WiFiClientSecure client;
  554. #endif
  555. bool connection_ok = false;
  556. bool valid = false; // valid format to be handled
  557. bool flip = true; // bitmap is stored bottom-to-top
  558. uint32_t startTime = millis();
  559. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  560. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  561. Serial.print("connecting to "); Serial.println(host);
  562. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  563. if (certificate) client.setTrustAnchors(&cert);
  564. else if (fingerprint) client.setFingerprint(fingerprint);
  565. else client.setInsecure();
  566. #elif defined (ESP32)
  567. if (certificate) client.setCACert(certificate);
  568. #endif
  569. if (!client.connect(host, httpsPort))
  570. {
  571. Serial.println("connection failed");
  572. return;
  573. }
  574. Serial.print("requesting URL: ");
  575. Serial.println(String("https://") + host + path + filename);
  576. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  577. "Host: " + host + "\r\n" +
  578. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  579. "Connection: close\r\n\r\n");
  580. Serial.println("request sent");
  581. while (client.connected())
  582. {
  583. String line = client.readStringUntil('\n');
  584. if (!connection_ok)
  585. {
  586. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  587. if (connection_ok) Serial.println(line);
  588. }
  589. if (!connection_ok) Serial.println(line);
  590. if (line == "\r")
  591. {
  592. Serial.println("headers received");
  593. break;
  594. }
  595. }
  596. if (!connection_ok) return;
  597. // Parse BMP header
  598. uint16_t signature = 0;
  599. for (int16_t i = 0; i < 50; i++)
  600. {
  601. if (!client.available()) delay(100);
  602. else signature = read16(client);
  603. if (signature == 0x4D42) break;
  604. }
  605. if (signature == 0x4D42) // BMP signature
  606. {
  607. uint32_t fileSize = read32(client);
  608. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  609. uint32_t imageOffset = read32(client); // Start of image data
  610. uint32_t headerSize = read32(client);
  611. uint32_t width = read32(client);
  612. int32_t height = (int32_t) read32(client);
  613. uint16_t planes = read16(client);
  614. uint16_t depth = read16(client); // bits per pixel
  615. uint32_t format = read32(client);
  616. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  617. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  618. {
  619. Serial.print("File size: "); Serial.println(fileSize);
  620. Serial.print("Image Offset: "); Serial.println(imageOffset);
  621. Serial.print("Header size: "); Serial.println(headerSize);
  622. Serial.print("Bit Depth: "); Serial.println(depth);
  623. Serial.print("Image size: ");
  624. Serial.print(width);
  625. Serial.print('x');
  626. Serial.println(abs(height));
  627. // BMP rows are padded (if needed) to 4-byte boundary
  628. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  629. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  630. if (height < 0)
  631. {
  632. height = -height;
  633. flip = false;
  634. }
  635. uint16_t w = width;
  636. uint16_t h = height;
  637. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  638. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  639. if (w <= max_row_width) // handle with direct drawing
  640. {
  641. valid = true;
  642. uint8_t bitmask = 0xFF;
  643. uint8_t bitshift = 8 - depth;
  644. uint16_t red, green, blue;
  645. bool whitish = false;
  646. bool colored = false;
  647. if (depth == 1) with_color = false;
  648. if (depth <= 8)
  649. {
  650. if (depth < 8) bitmask >>= depth;
  651. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  652. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  653. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  654. {
  655. blue = client.read();
  656. green = client.read();
  657. red = client.read();
  658. client.read();
  659. bytes_read += 4;
  660. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  661. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  662. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  663. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  664. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  665. color_palette_buffer[pn / 8] |= colored << pn % 8;
  666. }
  667. }
  668. display.clearScreen();
  669. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  670. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  671. bytes_read += skip(client, rowPosition - bytes_read);
  672. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  673. {
  674. if (!connection_ok || !(client.connected() || client.available())) break;
  675. delay(1); // yield() to avoid WDT
  676. uint32_t in_remain = rowSize;
  677. uint32_t in_idx = 0;
  678. uint32_t in_bytes = 0;
  679. uint8_t in_byte = 0; // for depth <= 8
  680. uint8_t in_bits = 0; // for depth <= 8
  681. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  682. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  683. uint32_t out_idx = 0;
  684. for (uint16_t col = 0; col < w; col++) // for each pixel
  685. {
  686. yield();
  687. if (!connection_ok || !(client.connected() || client.available())) break;
  688. // Time to read more pixel data?
  689. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  690. {
  691. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  692. uint32_t got = read8n(client, input_buffer, get);
  693. while ((got < get) && connection_ok)
  694. {
  695. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  696. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  697. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  698. got += gotmore;
  699. connection_ok = gotmore > 0;
  700. }
  701. in_bytes = got;
  702. in_remain -= got;
  703. bytes_read += got;
  704. in_idx = 0;
  705. }
  706. if (!connection_ok)
  707. {
  708. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  709. break;
  710. }
  711. switch (depth)
  712. {
  713. case 32:
  714. blue = input_buffer[in_idx++];
  715. green = input_buffer[in_idx++];
  716. red = input_buffer[in_idx++];
  717. in_idx++; // skip alpha
  718. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  719. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  720. break;
  721. case 24:
  722. blue = input_buffer[in_idx++];
  723. green = input_buffer[in_idx++];
  724. red = input_buffer[in_idx++];
  725. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  726. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  727. break;
  728. case 16:
  729. {
  730. uint8_t lsb = input_buffer[in_idx++];
  731. uint8_t msb = input_buffer[in_idx++];
  732. if (format == 0) // 555
  733. {
  734. blue = (lsb & 0x1F) << 3;
  735. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  736. red = (msb & 0x7C) << 1;
  737. }
  738. else // 565
  739. {
  740. blue = (lsb & 0x1F) << 3;
  741. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  742. red = (msb & 0xF8);
  743. }
  744. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  745. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  746. }
  747. break;
  748. case 1:
  749. case 2:
  750. case 4:
  751. case 8:
  752. {
  753. if (0 == in_bits)
  754. {
  755. in_byte = input_buffer[in_idx++];
  756. in_bits = 8;
  757. }
  758. uint16_t pn = (in_byte >> bitshift) & bitmask;
  759. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  760. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  761. in_byte <<= depth;
  762. in_bits -= depth;
  763. }
  764. break;
  765. }
  766. if (whitish)
  767. {
  768. // keep white
  769. }
  770. else if (colored && with_color)
  771. {
  772. out_color_byte &= ~(0x80 >> col % 8); // colored
  773. }
  774. else
  775. {
  776. out_byte &= ~(0x80 >> col % 8); // black
  777. }
  778. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  779. {
  780. output_row_color_buffer[out_idx] = out_color_byte;
  781. output_row_mono_buffer[out_idx++] = out_byte;
  782. out_byte = 0xFF; // white (for w%8!=0 border)
  783. out_color_byte = 0xFF; // white (for w%8!=0 border)
  784. }
  785. } // end pixel
  786. int16_t yrow = y + (flip ? h - row - 1 : row);
  787. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  788. } // end line
  789. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  790. Serial.print("bytes read "); Serial.println(bytes_read);
  791. display.refresh();
  792. }
  793. }
  794. }
  795. client.stop();
  796. if (!valid)
  797. {
  798. Serial.println("bitmap format not handled.");
  799. }
  800. }
  801. #include "GxEPD2_WiFi_Buffered.h"
  802. #include "GxEPD2_WiFi_Native4c.h"
  803. uint16_t read16(WiFiClient& client)
  804. {
  805. // BMP data is stored little-endian, same as Arduino.
  806. uint16_t result;
  807. ((uint8_t *)&result)[0] = client.read(); // LSB
  808. ((uint8_t *)&result)[1] = client.read(); // MSB
  809. return result;
  810. }
  811. uint32_t read32(WiFiClient& client)
  812. {
  813. // BMP data is stored little-endian, same as Arduino.
  814. uint32_t result;
  815. ((uint8_t *)&result)[0] = client.read(); // LSB
  816. ((uint8_t *)&result)[1] = client.read();
  817. ((uint8_t *)&result)[2] = client.read();
  818. ((uint8_t *)&result)[3] = client.read(); // MSB
  819. return result;
  820. }
  821. #if USE_BearSSL
  822. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  823. {
  824. int32_t remain = bytes;
  825. uint32_t start = millis();
  826. while ((client.connected() || client.available()) && (remain > 0))
  827. {
  828. if (client.available())
  829. {
  830. client.read();
  831. remain--;
  832. }
  833. else delay(10);
  834. if (millis() - start > 2000) break; // don't hang forever
  835. }
  836. return bytes - remain;
  837. }
  838. uint32_t read8n(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  839. {
  840. int32_t remain = bytes;
  841. uint32_t start = millis();
  842. while ((client.connected() || client.available()) && (remain > 0))
  843. {
  844. if (client.available())
  845. {
  846. int16_t v = client.read();
  847. *buffer++ = uint8_t(v);
  848. remain--;
  849. }
  850. else delay(10);
  851. if (millis() - start > 2000) break; // don't hang forever
  852. }
  853. return bytes - remain;
  854. }
  855. #endif
  856. uint32_t skip(WiFiClient& client, int32_t bytes)
  857. {
  858. int32_t remain = bytes;
  859. uint32_t start = millis();
  860. while ((client.connected() || client.available()) && (remain > 0))
  861. {
  862. if (client.available())
  863. {
  864. client.read();
  865. remain--;
  866. }
  867. else delay(1);
  868. if (millis() - start > 2000) break; // don't hang forever
  869. }
  870. return bytes - remain;
  871. }
  872. uint32_t read8n(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  873. {
  874. int32_t remain = bytes;
  875. uint32_t start = millis();
  876. while ((client.connected() || client.available()) && (remain > 0))
  877. {
  878. if (client.available())
  879. {
  880. int16_t v = client.read();
  881. *buffer++ = uint8_t(v);
  882. remain--;
  883. }
  884. else delay(1);
  885. if (millis() - start > 2000) break; // don't hang forever
  886. }
  887. return bytes - remain;
  888. }
  889. // Set time via NTP, as required for x.509 validation
  890. void setClock()
  891. {
  892. configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  893. Serial.print("Waiting for NTP time sync: ");
  894. time_t now = time(nullptr);
  895. while (now < 8 * 3600 * 2)
  896. {
  897. delay(500);
  898. Serial.print(".");
  899. now = time(nullptr);
  900. }
  901. Serial.println("");
  902. struct tm timeinfo;
  903. gmtime_r(&now, &timeinfo);
  904. Serial.print("Current time: ");
  905. Serial.print(asctime(&timeinfo));
  906. }