WeatherMatrix.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <FastLED.h>
  2. #define LED_PIN 0
  3. #define COLOR_ORDER GRB
  4. #define CHIPSET WS2811
  5. #define BRIGHTNESS 10
  6. // networking and json
  7. String apiUrl;
  8. int status = WL_IDLE_STATUS;
  9. int connectWiFi() {
  10. WiFi.begin("Wokwi-GUEST", "", 6);
  11. while (WiFi.status() != WL_CONNECTED) {
  12. delay(100);
  13. }
  14. return WiFi.status();
  15. }
  16. String buildAPIUrl (double lat, double lon, const String& apiKey) {
  17. const String latStr(lat, 4);
  18. const String lonStr(lon, 4);
  19. return String("https://api.openweathermap.org/data/2.5/weather?lat=" +
  20. latStr + "&lon=" + lonStr + "&appid=" + apiKey);
  21. }
  22. // Params for width and height
  23. const uint8_t kMatrixWidth = 16;
  24. const uint8_t kMatrixHeight = 16;
  25. uint16_t XY( uint8_t x, uint8_t y)
  26. {
  27. uint16_t i;
  28. if ( x & 0x01) {
  29. i = kMatrixHeight * (kMatrixWidth - (x + 1)) + y;
  30. } else {
  31. i = kMatrixHeight * (kMatrixWidth - x) - (y + 1);
  32. }
  33. return i;
  34. }
  35. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  36. CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
  37. CRGB* const leds( leds_plus_safety_pixel + 1);
  38. uint16_t XYsafe( uint8_t x, uint8_t y)
  39. {
  40. if ( x >= kMatrixWidth) return -1;
  41. if ( y >= kMatrixHeight) return -1;
  42. return XY(x, y);
  43. }
  44. uint8_t sun_yx[16][16] = {
  45. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  46. {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
  47. {0, 0, 1, 0, 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1, 0, 0, 0, 0, 1, 0, 0, 0},
  69. {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0},
  70. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  71. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  72. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  73. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
  74. {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
  75. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  76. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  77. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  78. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  79. };
  80. void DrawFrame(uint8_t arr[][16])
  81. {
  82. for (int i = 0; i < 16; i++) {
  83. for (int j = 0; j < 16; j++) {
  84. uint8_t elem = arr[i][j];
  85. if (elem == 1) {
  86. leds[ XYsafe(i, j)] = CRGB::Yellow;
  87. }
  88. }
  89. }
  90. }
  91. void loop()
  92. {
  93. DrawFrame(sun_yx);
  94. FastLED.show();
  95. delay(3000);
  96. FastLED.clear();
  97. DrawFrame(cloud_yx);
  98. FastLED.show();
  99. delay(3000);
  100. FastLED.clear();
  101. }
  102. void setup() {
  103. Serial.begin(9600);
  104. // weather api
  105. const float lat = 48.9517;
  106. const float lon = 10.1703;
  107. const String apiKey = "e471293b25fdb6697c0a862f2695df5f";
  108. apiUrl = buildAPIUrl(lat, lon, apiKey);
  109. connectWiFi();
  110. if (WiFi.status() != WL_CONNECTED) {
  111. Serial.println(WiFi.status());
  112. }
  113. HTTPClient http;
  114. http.begin(apiUrl);
  115. int respCode = http.GET();
  116. JsonDocument payload;
  117. if(respCode > 0){
  118. const String response = http.getString();
  119. deserializeJson(payload, response);
  120. Serial.println(payload.size());
  121. } else {
  122. Serial.print("error ");
  123. Serial.println(respCode);
  124. }
  125. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  126. FastLED.setBrightness( BRIGHTNESS );
  127. }