// Работа со светодиодами
#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <RTClib.h>
#define PIN_WS2812B 14 // контакт ESP32, который подключается к WS2812B
#define NUM_PIXELS 78 // количество светодиодов (пикселей) на WS2812B
Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
RTC_DS3231 rtc; // Создаем объект RTC_DS3231 для работы с RTC модулем
const char* ssid = "Wokwi-GUEST"; // Имя Wi-Fi сети
const char* password = ""; // Пустой пароль
int time_now_arr[5] = {0, 0, 0, 0, 0};
int weather_now_arr[2] = {0, 0};
int weather_now_conditions = 1;
// Цифры на циферблате
int first_digit_time[10] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39};
int second_digit_time[10] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
int third_digit_time[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int fourth_digit_time[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// Цифры погоды
int second_digit_weather[10] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49};
int first_digit_weather[10] = {50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
// Состояние погоды Плюсовая/Минусовая
int weather_conditions[2] = {60, 61};
// Иконки погоды
int weather_conditions_icon[16] = {62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77};
int minut_now = 1;
String weather_conditions_icon_data = "0";
void setup_rtc_module(){
Serial.begin(9600);
if (!rtc.begin()) { // Инициализируем RTC модуль
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) { // Проверяем, была ли потеряна питание RTC
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Устанавливаем текущее время во время компиляции скетча
}
}
void get_time_now(){
DateTime now = rtc.now(); // Получаем текущее время от RTC
String time_now = String(now.hour()) + String(now.minute());
minut_now = now.minute();
for (int i = 0; i <= time_now.length(); i++) {
char char_num = time_now[i]; // ваш символ
int num = atoi(&char_num);
time_now_arr[i] = num;
}
}
// Получение погоды с Яндекс Погода
void get_yandex_weather(){
if (WiFi.status() == WL_CONNECTED) { // Проверка подключения к Wi-Fi
HTTPClient http;
// Плюсовая https://run.mocky.io/v3/3b0845f1-ad49-45bf-8120-6feacdd2ebca
// Минусовая https://run.mocky.io/v3/53445461-c6e6-4f1f-af09-33b867b8770d
http.begin("https://run.mocky.io/v3/3b0845f1-ad49-45bf-8120-6feacdd2ebca");
int httpCode = http.GET();
if (httpCode > 0) { // Проверка кода ответа
String payload = http.getString();
JsonDocument doc;
deserializeJson(doc, payload);
String weather_now = doc["fact"]["temp"]; // feels_like
weather_now.replace("-","");
int weather_now_int = doc["fact"]["temp"];
weather_conditions_icon_data = doc["fact"]["condition"];
if (weather_now_int < 0){
weather_now_conditions = -1;
// Serial.println("Меньше нуля");
} else {
weather_now_conditions = 1;
// Serial.println("Больше нуля");
}
if(weather_now.length() == 1){
char char_num = weather_now[0];
int num = atoi(&char_num);
weather_now_arr[1] = num;
}else{
for (int i = 0; i <= weather_now.length(); i++) {
char char_num = weather_now[i];
int num = atoi(&char_num);
weather_now_arr[i] = num;
}
}
// Serial.println(weather_now);
// Serial.println(weather_now_conditions);
// Serial.println(weather_now_arr[0]);
// Serial.println();
} else {
Serial.println("Error on HTTP request");
}
http.end(); // Завершение HTTP соединения
}
}
void show_time(){
ws2812b.setPixelColor(first_digit_time[time_now_arr[0]], ws2812b.Color(0, 255, 0));
ws2812b.show();
ws2812b.setPixelColor(second_digit_time[time_now_arr[1]], ws2812b.Color(0, 255, 0));
ws2812b.show();
ws2812b.setPixelColor(third_digit_time[time_now_arr[2]], ws2812b.Color(0, 255, 0));
ws2812b.show();
ws2812b.setPixelColor(fourth_digit_time[time_now_arr[3]], ws2812b.Color(0, 255, 0));
ws2812b.show();
}
void show_weather(){
ws2812b.setPixelColor(first_digit_weather[weather_now_arr[0]], ws2812b.Color(0, 255, 0));
ws2812b.show();
ws2812b.setPixelColor(second_digit_weather[weather_now_arr[1]], ws2812b.Color(0, 255, 0));
ws2812b.show();
}
void show_conditions(){
if (weather_now_conditions == 1){
ws2812b.setPixelColor(weather_conditions[0], ws2812b.Color(0, 255, 0));
ws2812b.show();
}else{
ws2812b.setPixelColor(weather_conditions[1], ws2812b.Color(0, 255, 0));
ws2812b.show();
}
}
void show_weather_conditions_icon(){
if (weather_conditions_icon_data == "clear") {
ws2812b.setPixelColor(62, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "partly-cloudy") {
ws2812b.setPixelColor(63, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "cloudy") {
ws2812b.setPixelColor(64, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "overcast") {
ws2812b.setPixelColor(65, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "light-rain") {
ws2812b.setPixelColor(66, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "rain") {
ws2812b.setPixelColor(67, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "heavy-rain") {
ws2812b.setPixelColor(68, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "showers") {
ws2812b.setPixelColor(69, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "wet-snow") {
ws2812b.setPixelColor(70, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "light-snow") {
ws2812b.setPixelColor(71, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "snow") {
ws2812b.setPixelColor(72, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "snow-showers") {
ws2812b.setPixelColor(73, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "hail") {
ws2812b.setPixelColor(74, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "thunderstorm") {
ws2812b.setPixelColor(75, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "thunderstorm-with-rain") {
ws2812b.setPixelColor(76, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else if (weather_conditions_icon_data == "thunderstorm-with-hail") {
ws2812b.setPixelColor(77, ws2812b.Color(0, 255, 0));
ws2812b.show();
}
else {
Serial.print("Нет иконки погоды");
}
}
// Подключение к Wi-Fi сети
void connecting_to_wifi(){
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void setup() {
setup_rtc_module();
connecting_to_wifi();
ws2812b.begin(); // ИНИЦИАЛИЗАЦИЯ объекта полосы WS2812B (ОБЯЗАТЕЛЬНО)
get_yandex_weather();
Serial.println("Setup successfully");
}
uint32_t timer_show_time, timer_get_weather;
void loop() {
if (millis() - timer_show_time >= (1*1000)) {
timer_show_time = millis();
get_time_now();
ws2812b.clear();
show_time();
show_weather();
show_conditions();
show_weather_conditions_icon();
Serial.println("Update time");
}
if (millis() - timer_get_weather >= (1800*1000)) {
timer_get_weather = millis();
get_yandex_weather();
Serial.println("get_yandex_weather");
}
}