WeatherMatrix.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <FastLED.h>
  2. #include <ArduinoJson.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WiFiMulti.h>
  6. #include <WiFiClient.h>
  7. #include <Array.h>
  8. #define LED_PIN 0
  9. #define COLOR_ORDER GRB
  10. #define CHIPSET WS2811
  11. #define BRIGHTNESS 10
  12. #ifndef STASSID
  13. #define STASSID "FNT-Gast"
  14. #define STAPSK "Laserjet09"
  15. #endif
  16. const char* ssid = STASSID;
  17. const char* password = STAPSK;
  18. // networking and json
  19. String apiUrl;
  20. int status = WL_IDLE_STATUS;
  21. ESP8266WiFiMulti WiFiMulti;
  22. WiFiClient client;
  23. HTTPClient http;
  24. boolean tempView = false;
  25. int respCode = 0;
  26. unsigned long startMillis1;
  27. unsigned long currentMillis1;
  28. unsigned long startMillis2;
  29. unsigned long currentMillis2;
  30. boolean firstIteration = true;
  31. void connectWiFi() {
  32. // We start by connecting to a WiFi network
  33. WiFi.mode(WIFI_STA);
  34. WiFiMulti.addAP(ssid, password);
  35. Serial.println();
  36. Serial.println();
  37. Serial.print("Wait for WiFi... ");
  38. while (WiFiMulti.run() != WL_CONNECTED) {
  39. Serial.print(".");
  40. delay(500);
  41. }
  42. Serial.println("");
  43. Serial.println("WiFi connected");
  44. Serial.println("IP address: ");
  45. Serial.println(WiFi.localIP());
  46. delay(500);
  47. }
  48. String buildAPIUrl (double lat, double lon, const String& apiKey) {
  49. const String latStr(lat, 4);
  50. const String lonStr(lon, 4);
  51. return String("http://api.openweathermap.org/data/2.5/weather?lat=" +
  52. latStr + "&lon=" + lonStr + "&appid=" + apiKey + "&units=metric");
  53. }
  54. // Params for width and height
  55. const uint8_t kMatrixWidth = 16;
  56. const uint8_t kMatrixHeight = 16;
  57. uint16_t XY( uint8_t x, uint8_t y)
  58. {
  59. uint16_t i;
  60. if ( x & 0x01) {
  61. i = kMatrixHeight * (kMatrixWidth - (x + 1)) + y;
  62. } else {
  63. i = kMatrixHeight * (kMatrixWidth - x) - (y + 1);
  64. }
  65. return i;
  66. }
  67. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  68. CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
  69. CRGB* const leds( leds_plus_safety_pixel + 1);
  70. uint16_t XYsafe( uint8_t x, uint8_t y)
  71. {
  72. if ( x >= kMatrixWidth) return -1;
  73. if ( y >= kMatrixHeight) return -1;
  74. return XY(x, y);
  75. }
  76. uint8_t sun_yx[16][16] = {
  77. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  78. {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
  79. {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0},
  80. {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  81. {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
  82. {0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0},
  83. {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
  84. {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0},
  85. {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
  86. {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
  87. {0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0},
  88. {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
  89. {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  90. {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0},
  91. {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
  92. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  93. };
  94. uint8_t cloud_yx[16][16] = {
  95. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  96. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  97. {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
  98. {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  99. {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  100. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  101. {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0},
  102. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  103. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  104. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  105. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  106. {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
  107. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  108. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  109. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  110. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  111. };
  112. uint8_t rain_yx[16][16] = {
  113. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  114. {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
  115. {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  116. {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  117. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  118. {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
  119. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
  120. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  121. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  122. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  123. {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
  124. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  125. {0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  126. {0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0},
  127. {0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
  128. {0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0}
  129. };
  130. uint8_t cloudSun_yx[16][16] = {
  131. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  132. {0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
  133. {0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
  134. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
  135. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
  136. {0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
  137. {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  138. {0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
  139. {0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
  140. {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
  141. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
  142. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  143. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  144. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  145. {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
  146. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  147. };
  148. uint8_t celsius[5][16] = {
  149. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1},
  150. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
  151. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
  152. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
  153. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  154. };
  155. uint8_t empty[11][8] = {
  156. {0, 0, 0, 0, 0, 0, 0, 0},
  157. {0, 0, 0, 0, 0, 0, 0, 0},
  158. {0, 0, 0, 0, 0, 0, 0, 0},
  159. {0, 0, 0, 0, 0, 0, 0, 0},
  160. {0, 0, 0, 0, 0, 0, 0, 0},
  161. {0, 0, 0, 0, 0, 0, 0, 0},
  162. {0, 0, 0, 0, 0, 0, 0, 0},
  163. {0, 0, 0, 0, 0, 0, 0, 0},
  164. {0, 0, 0, 0, 0, 0, 0, 0},
  165. {0, 0, 0, 0, 0, 0, 0, 0},
  166. {0, 0, 0, 0, 0, 0, 0, 0}
  167. };
  168. uint8_t zero[11][8] = {
  169. {0, 0, 1, 1, 1, 1, 0, 0},
  170. {0, 1, 0, 0, 0, 0, 1, 0},
  171. {0, 1, 0, 0, 0, 0, 1, 0},
  172. {0, 1, 0, 0, 0, 0, 1, 0},
  173. {0, 1, 0, 0, 0, 0, 1, 0},
  174. {0, 1, 0, 0, 0, 0, 1, 0},
  175. {0, 1, 0, 0, 0, 0, 1, 0},
  176. {0, 1, 0, 0, 0, 0, 1, 0},
  177. {0, 1, 0, 0, 0, 0, 1, 0},
  178. {0, 1, 0, 0, 0, 0, 1, 0},
  179. {0, 0, 1, 1, 1, 1, 0, 0}
  180. };
  181. uint8_t one[11][8] = {
  182. {0, 0, 0, 1, 0, 0, 0, 0},
  183. {0, 0, 1, 1, 0, 0, 0, 0},
  184. {0, 1, 0, 1, 0, 0, 0, 0},
  185. {0, 0, 0, 1, 0, 0, 0, 0},
  186. {0, 0, 0, 1, 0, 0, 0, 0},
  187. {0, 0, 0, 1, 0, 0, 0, 0},
  188. {0, 0, 0, 1, 0, 0, 0, 0},
  189. {0, 0, 0, 1, 0, 0, 0, 0},
  190. {0, 0, 0, 1, 0, 0, 0, 0},
  191. {0, 0, 0, 1, 0, 0, 0, 0},
  192. {0, 0, 1, 1, 1, 0, 0, 0}
  193. };
  194. uint8_t two[11][8] = {
  195. {0, 0, 0, 0, 0, 0, 0, 0},
  196. {0, 0, 1, 1, 1, 1, 0, 0},
  197. {0, 1, 0, 0, 0, 0, 1, 0},
  198. {0, 1, 0, 0, 0, 0, 1, 0},
  199. {0, 0, 0, 0, 0, 0, 1, 0},
  200. {0, 0, 0, 0, 0, 0, 1, 0},
  201. {0, 0, 0, 0, 0, 1, 0, 0},
  202. {0, 0, 0, 1, 1, 0, 0, 0},
  203. {0, 0, 1, 0, 0, 0, 0, 0},
  204. {0, 1, 0, 0, 0, 0, 0, 0},
  205. {0, 1, 1, 1, 1, 1, 1, 0}
  206. };
  207. uint8_t three[11][8] = {
  208. {0, 0, 1, 1, 1, 0, 0, 0},
  209. {0, 1, 0, 0, 0, 1, 0, 0},
  210. {0, 0, 0, 0, 0, 0, 1, 0},
  211. {0, 0, 0, 0, 0, 0, 1, 0},
  212. {0, 0, 0, 0, 0, 1, 0, 0},
  213. {0, 0, 0, 1, 1, 0, 0, 0},
  214. {0, 0, 0, 0, 0, 1, 0, 0},
  215. {0, 0, 0, 0, 0, 0, 1, 0},
  216. {0, 0, 0, 0, 0, 0, 1, 0},
  217. {0, 1, 0, 0, 0, 1, 0, 0},
  218. {0, 0, 1, 1, 1, 0, 0, 0}
  219. };
  220. uint8_t four[11][8] = {
  221. {0, 1, 0, 0, 0, 0, 0, 0},
  222. {0, 1, 0, 0, 0, 0, 0, 0},
  223. {0, 1, 0, 0, 0, 0, 0, 0},
  224. {0, 1, 0, 0, 1, 0, 0, 0},
  225. {0, 1, 0, 0, 1, 0, 0, 0},
  226. {0, 1, 0, 0, 1, 0, 0, 0},
  227. {0, 1, 1, 1, 1, 1, 1, 0},
  228. {0, 0, 0, 0, 1, 0, 0, 0},
  229. {0, 0, 0, 0, 1, 0, 0, 0},
  230. {0, 0, 0, 0, 1, 0, 0, 0},
  231. {0, 0, 0, 0, 1, 0, 0, 0}
  232. };
  233. uint8_t five[11][8] = {
  234. {0, 1, 1, 1, 1, 1, 1, 0},
  235. {0, 1, 0, 0, 0, 0, 0, 0},
  236. {0, 1, 0, 0, 0, 0, 0, 0},
  237. {0, 1, 0, 0, 0, 0, 0, 0},
  238. {0, 1, 0, 0, 0, 0, 0, 0},
  239. {0, 0, 1, 1, 1, 1, 0, 0},
  240. {0, 0, 0, 0, 0, 0, 1, 0},
  241. {0, 0, 0, 0, 0, 0, 1, 0},
  242. {0, 0, 0, 0, 0, 0, 1, 0},
  243. {0, 1, 0, 0, 0, 0, 1, 0},
  244. {0, 0, 1, 1, 1, 1, 0, 0}
  245. };
  246. uint8_t six[11][8] = {
  247. {0, 0, 1, 1, 1, 1, 0, 0},
  248. {0, 1, 0, 0, 0, 0, 1, 0},
  249. {0, 1, 0, 0, 0, 0, 0, 0},
  250. {0, 1, 0, 0, 0, 0, 0, 0},
  251. {0, 1, 0, 0, 0, 0, 0, 0},
  252. {0, 1, 1, 1, 1, 1, 0, 0},
  253. {0, 1, 0, 0, 0, 0, 1, 0},
  254. {0, 1, 0, 0, 0, 0, 1, 0},
  255. {0, 1, 0, 0, 0, 0, 1, 0},
  256. {0, 1, 0, 0, 0, 0, 1, 0},
  257. {0, 0, 1, 1, 1, 1, 0, 0}
  258. };
  259. uint8_t seven[11][8] = {
  260. {0, 1, 1, 1, 1, 1, 1, 0},
  261. {0, 0, 0, 0, 0, 0, 1, 0},
  262. {0, 0, 0, 0, 0, 1, 0, 0},
  263. {0, 0, 0, 0, 0, 1, 0, 0},
  264. {0, 0, 0, 0, 1, 0, 0, 0},
  265. {0, 0, 0, 0, 1, 0, 0, 0},
  266. {0, 0, 0, 1, 0, 0, 0, 0},
  267. {0, 0, 0, 1, 0, 0, 0, 0},
  268. {0, 0, 1, 0, 0, 0, 0, 0},
  269. {0, 0, 1, 0, 0, 0, 0, 0},
  270. {0, 1, 0, 0, 0, 0, 0, 0}
  271. };
  272. uint8_t eight[11][8] = {
  273. {0, 0, 1, 1, 1, 1, 0, 0},
  274. {0, 1, 0, 0, 0, 0, 1, 0},
  275. {0, 1, 0, 0, 0, 0, 1, 0},
  276. {0, 1, 0, 0, 0, 0, 1, 0},
  277. {0, 1, 0, 0, 0, 0, 1, 0},
  278. {0, 0, 1, 1, 1, 1, 0, 0},
  279. {0, 1, 0, 0, 0, 0, 1, 0},
  280. {0, 1, 0, 0, 0, 0, 1, 0},
  281. {0, 1, 0, 0, 0, 0, 1, 0},
  282. {0, 1, 0, 0, 0, 0, 1, 0},
  283. {0, 0, 1, 1, 1, 1, 0, 0}
  284. };
  285. uint8_t nine[11][8] = {
  286. {0, 0, 1, 1, 1, 1, 0, 0},
  287. {0, 1, 0, 0, 0, 0, 1, 0},
  288. {0, 1, 0, 0, 0, 0, 1, 0},
  289. {0, 1, 0, 0, 0, 0, 1, 0},
  290. {0, 1, 0, 0, 0, 0, 1, 0},
  291. {0, 0, 1, 1, 1, 1, 1, 0},
  292. {0, 0, 0, 0, 0, 0, 1, 0},
  293. {0, 0, 0, 0, 0, 0, 1, 0},
  294. {0, 1, 0, 0, 0, 0, 1, 0},
  295. {0, 1, 0, 0, 0, 0, 1, 0},
  296. {0, 0, 1, 1, 1, 1, 0, 0}
  297. };
  298. uint8_t (*digitOne)[8];
  299. uint8_t (*digitTwo)[8];
  300. void drawFrame(uint8_t arr[][16], String identifier)
  301. {
  302. FastLED.clear();
  303. FastLED.show();
  304. for (int i = 0; i < 16; i++) {
  305. for (int j = 0; j < 16; j++) {
  306. uint8_t elem = arr[i][j];
  307. if (elem == 1) {
  308. if (identifier == "Rain") {
  309. if (i >= 12) {
  310. leds[ XYsafe(i, j)] = CRGB::Blue;
  311. } else {
  312. leds[ XYsafe(i, j)] = CRGB::WhiteSmoke;
  313. }
  314. } else if (identifier == "Clouds") {
  315. leds[ XYsafe(i, j)] = CRGB::WhiteSmoke;
  316. } else if (identifier == "Sun") {
  317. leds[ XYsafe(i, j)] = CRGB::Yellow;
  318. } else if (identifier == "CloudSun") {
  319. if ((((i >= 0) && (i <= 8))
  320. && ((j >= 0) && (j <= 2)))
  321. ||
  322. (((i >= 0) && (i <= 4))
  323. && ((j >= 0) && (j <= 15)))) {
  324. leds[ XYsafe(i, j)] = CRGB::Yellow;
  325. Serial.println("Yellow!");
  326. } else {
  327. leds[ XYsafe(i, j)] = CRGB::WhiteSmoke;
  328. Serial.println("White!");
  329. }
  330. }
  331. }
  332. }
  333. }
  334. FastLED.show();
  335. }
  336. void drawTemp()
  337. {
  338. FastLED.clear();
  339. FastLED.show();
  340. for (int i = 0; i < 5; i++) {
  341. for (int j = 0; j < 16; j++) {
  342. // Celsius Symbol
  343. uint8_t elemCelsius = celsius[i][j];
  344. if (elemCelsius == 1) {
  345. leds[ XYsafe(i, j)] = CRGB::WhiteSmoke;
  346. }
  347. }
  348. }
  349. }
  350. void drawFirstDigit(int num) {
  351. for (int i = 0; i < 11; i++) {
  352. for (int j = 0; j < 8; j++) {
  353. uint8_t elem;
  354. switch (num) {
  355. case 1:
  356. elem = one[i][j];
  357. break;
  358. case 2:
  359. elem = two[i][j];
  360. break;
  361. case 3:
  362. elem = three[i][j];
  363. break;
  364. case 4:
  365. elem = four[i][j];
  366. break;
  367. case 5:
  368. elem = five[i][j];
  369. break;
  370. case 6:
  371. elem = six[i][j];
  372. break;
  373. case 7:
  374. elem = seven[i][j];
  375. break;
  376. case 8:
  377. elem = eight[i][j];
  378. break;
  379. case 9:
  380. elem = nine[i][j];
  381. break;
  382. default:
  383. elem = 0;
  384. break;
  385. }
  386. if (elem == 1) {
  387. leds[ XYsafe(i + 5, j)] = CRGB::WhiteSmoke;
  388. }
  389. }
  390. }
  391. }
  392. void drawSecondDigit(int num) {
  393. for (int i = 0; i < 11; i++) {
  394. for (int j = 0; j < 8; j++) {
  395. uint8_t elem;
  396. switch (num) {
  397. case 1:
  398. elem = one[i][j];
  399. break;
  400. case 2:
  401. elem = two[i][j];
  402. break;
  403. case 3:
  404. elem = three[i][j];
  405. break;
  406. case 4:
  407. elem = four[i][j];
  408. break;
  409. case 5:
  410. elem = five[i][j];
  411. break;
  412. case 6:
  413. elem = six[i][j];
  414. break;
  415. case 7:
  416. elem = seven[i][j];
  417. break;
  418. case 8:
  419. elem = eight[i][j];
  420. break;
  421. case 9:
  422. elem = nine[i][j];
  423. break;
  424. default:
  425. elem = zero[i][j];
  426. break;
  427. }
  428. if (elem == 1) {
  429. leds[ XYsafe(i + 5, j + 8)] = CRGB::WhiteSmoke;
  430. }
  431. }
  432. }
  433. FastLED.show();
  434. }
  435. int extractDigit(int num, int i)
  436. {
  437. return num / i % 10 ;
  438. }
  439. void loop()
  440. {
  441. currentMillis1 = millis();
  442. currentMillis2 = millis();
  443. if (firstIteration || (currentMillis1 - startMillis1 >= 180000)) {
  444. respCode = http.GET();
  445. firstIteration = false;
  446. startMillis1 = currentMillis1;
  447. }
  448. if (respCode > 0) {
  449. JsonDocument payload;
  450. const String response = http.getString();
  451. deserializeJson(payload, response);
  452. if (currentMillis2 - startMillis2 >= 3000) {
  453. if (tempView) {
  454. Serial.print("Response: ");
  455. Serial.println(response);
  456. int temp = (int) payload["main"]["temp"];
  457. Serial.print("Temp: ");
  458. Serial.println(temp);
  459. int firstDigit = extractDigit(temp, 10);
  460. int secondDigit = extractDigit(temp, 1);
  461. Serial.print("First digit: ");
  462. Serial.println(firstDigit);
  463. Serial.print("Second digit: ");
  464. Serial.println(secondDigit);
  465. drawTemp();
  466. drawFirstDigit(firstDigit);
  467. drawSecondDigit(secondDigit);
  468. } else {
  469. if (String("Clouds") == String(payload["weather"][0]["main"])) {
  470. if (payload["weather"][0]["id"] >= 803) {
  471. drawFrame(cloud_yx, "Clouds");
  472. } else {
  473. drawFrame(cloudSun_yx, "CloudSun");
  474. }
  475. } else if (String("Rain") == String(payload["weather"][0]["main"]) || String("Drizzle") == String(payload["weather"][0]["main"])) {
  476. drawFrame(rain_yx, "Rain");
  477. } else if (String("Clear") == String(payload["weather"][0]["main"])) {
  478. drawFrame(sun_yx, "Sun");
  479. } else {
  480. drawFrame(cloudSun_yx, "CloudSun");
  481. }
  482. }
  483. tempView = !tempView;
  484. startMillis2 = currentMillis2;
  485. }
  486. } else {
  487. Serial.print("error ");
  488. Serial.println(respCode);
  489. }
  490. }
  491. void setup() {
  492. startMillis1 = millis();
  493. startMillis2 = millis();
  494. Serial.begin(115200);
  495. // weather api
  496. const float lat = 48.9517;
  497. const float lon = 10.1703;
  498. const String apiKey = "e471293b25fdb6697c0a862f2695df5f";
  499. apiUrl = buildAPIUrl(lat, lon, apiKey);
  500. connectWiFi();
  501. if (WiFi.status() != WL_CONNECTED) {
  502. Serial.println(WiFi.status());
  503. }
  504. http.begin(client, apiUrl);
  505. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  506. FastLED.setBrightness( BRIGHTNESS );
  507. }