Простая метеостанция на ESP8266
Представляю вашему вниманию проект простой метеостанции на ESP8266. Погодные данные она получает от общеизвестного сервиса OpenWeatherMap. Данный проект очень прост и дешев изготовлении, требует минимум компонентов и легко повторяем.
Необходимые компоненты
Схема соединений
Соединяем все компоненты по схеме
Получение ключа API OpenWeatherMap
Зарегистрируйтесь на сайте https://openweathermap.org, После подтверждения регистрации, авторизуйтесь на сайте. Затем в правом верхнем углу кликните мышкой по имени своего профиля. Должно открыться выпадающее меню.
Перейдите по ссылке My API keys
Откроется новое окно
Введите в поле API key name произвольное название вашего ключа API и нажмите кнопку Generate. В левой части окна появится ваш ключ. Скопируйте и сохраните его. Он пригодится вам в будущем
Прошивка устройства
После сборки устройства по схеме переходим к прошивке ESP8266. Для начала вам необходимо установить в Arduino IDE ядро ESP8266 версии 3.0 и выше. Как это сделать можно узнать в статье Ядро ESP8266 для ArduinoIDE. Установка и обзор библиотек.
Также вам необходимо скачать и установить дополнительные библиотеки, необходимые для работы программы.
Установка дополнительных библиотек
Для работы устройства нам необходимо скачать и установить в Arduino IDE ряд дополнительных библиотек. Таких как:
- Adafruit_GFX - 1.1.13 и выше
- Adafruit_ST7735 - 1.9.3 и выше
- Adafruit_ST7789 - 1.9.3 и выше
- UnixTime - 1.1.0 и выше
- ArduinoJson - 6.1 и выше
- SPI.h - входит в стандартную поставку Arduino IDE
Следующие библиотеки будут установлены автоматически вместе с ядром ESP8266 для Arduino IDE.
- ESP8266WiFi
- ESP8266HTTPClient
- WiFiClient
Сверьтесь со списком и установите недостающие библиотеки или обновите имеющиеся через Менеджер библиотек Arduino IDE (Инструменты > Управлять библиотеками...)
Код программы
После установки всех необходимых библиотек, откройте файл из вложения в Arduino IDE, либо создайте новый проект и скопируйте в него все содержимое из блока ниже
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <UnixTime.h>
//Инициализация дисплея
#define TFT_CS D8
#define TFT_RST D6
#define TFT_DC D1
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
uint16_t color;
String degree = degree.substring(degree.length()) + (char)247 + "C";
String percentage = percentage.substring(percentage.length()) + (char)37;
//Настройки WiFi
const char* ssid = "ssid";
const char* password = "pass";
//настройки Open Weather Map
const String api_1 = "http://api.openweathermap.org/data/2.5/weather?q=";
const String qLocation = "Gubkin,ru"; //ваш город
const String api_2 = "&units=metric"; //температура в градусах цельсия
const String api_3 = "&APPID=";
const String api_key = "xxxxxxxxxxxxxxxxxxxxxxx"; //ваш api ключ
//графика дисплея для погоды
const unsigned char thunder[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00111110,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000001,B11100011,B11000000,B00000000,B00000000,
B00000000,B00000000,B00000011,B00000000,B01100000,B00000000,B00000000,
B00000000,B00000000,B00000100,B00000000,B00110000,B00000000,B00000000,
B00000000,B00000000,B00001000,B00000000,B00011000,B00000000,B00000000,
B00000000,B00000000,B00010000,B00000000,B00001100,B00000000,B00000000,
B00000000,B00000000,B00110000,B00000000,B00000111,B11000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,B01100000,B00000000,
B00000000,B00001111,B11100000,B00000000,B00000000,B00010000,B00000000,
B00000000,B00011000,B00000000,B00000000,B00000000,B00011000,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B00001000,B00000000,
B00000000,B01100000,B00000000,B00000000,B00000000,B00001100,B00000000,
B00000000,B01000000,B00000000,B00000000,B00000000,B00001100,B00000000,
B00000000,B11000000,B00000000,B00000000,B00000000,B00001100,B00000000,
B00001111,B10000000,B00000000,B00000100,B00000000,B00001000,B00000000,
B00011000,B00000000,B00000000,B00000100,B00000000,B00001100,B00000000,
B00110000,B00000000,B00000000,B00001100,B00000000,B00000110,B00000000,
B00100000,B00000000,B00000000,B00001000,B00000000,B00000010,B00000000,
B01100000,B00000000,B00000000,B00011000,B00000000,B00000011,B00000000,
B01000000,B00000000,B00000000,B00010000,B00000000,B00000001,B00000000,
B01000000,B00000000,B00010000,B00110000,B00000000,B00000001,B00000000,
B01000000,B00000000,B00110000,B00100000,B00000000,B00000001,B00000000,
B01100000,B00000000,B00100000,B00100000,B00000000,B00000011,B00000000,
B00100000,B00000000,B01100000,B01111111,B10000000,B00000010,B00000000,
B00010000,B00000000,B01000000,B01111111,B10000000,B00000110,B00000000,
B00011100,B00000000,B01111000,B00000001,B00000000,B00011100,B00000000,
B00000111,B11111100,B00001000,B00000011,B00111111,B11110000,B00000000,
B00000000,B00000000,B00011000,B00000010,B00000000,B00000000,B00000000,
B00000000,B00000000,B00010000,B00000010,B00000000,B00000000,B00000000,
B00000000,B00000000,B00110000,B00000100,B00000000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000100,B00000000,B00000000,B00000000,
B00000000,B00000000,B01100000,B00001100,B00000000,B00000000,B00000000,
B00000000,B00000000,B01000000,B00001000,B00000000,B00000000,B00000000,
B00010000,B00000110,B00000000,B10000000,B00010000,B00000100,B00000000,
B00110000,B00000100,B00000001,B10000000,B00100000,B00000100,B00000000,
B00100000,B00001100,B00000001,B00000000,B00100000,B00001100,B00000000,
B01100000,B00001000,B00000001,B00000000,B01100000,B00001000,B00000000,
B01000000,B00001000,B00000011,B00000000,B01000000,B00011000,B00000000,
B01000000,B00000000,B00000010,B00000000,B01000000,B00010000,B00000000
};
const unsigned char drizzle[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01111111,B11000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B11100000,B11100000,B00000000,B00000000,
B00000000,B00000000,B00000011,B10000000,B00111000,B00000000,B00000000,
B00000000,B00000000,B00000110,B00000000,B00001100,B00000000,B00000000,
B00000000,B00000000,B00000100,B00000000,B00000110,B00000000,B00000000,
B00000000,B00000000,B00001100,B00000000,B00000010,B00000000,B00000000,
B00000000,B00000000,B00011000,B00000000,B00000011,B11100000,B00000000,
B00000000,B00000000,B00010000,B00000000,B00000001,B11111000,B00000000,
B00000000,B00000011,B11110000,B00000000,B00000000,B00001100,B00000000,
B00000000,B00001110,B01110000,B00000000,B00000000,B00000110,B00000000,
B00000000,B00011000,B00000000,B00000000,B00000000,B00000110,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B00000010,B00000000,
B00000000,B01100000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00000000,B01000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00000000,B11000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00000111,B11000000,B00000000,B00000000,B00000000,B00000010,B00000000,
B00011100,B00000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00010000,B00000000,B00000000,B00000000,B00000000,B00000001,B00000000,
B00100000,B00000000,B00000000,B00000000,B00000000,B00000001,B10000000,
B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01000000,
B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01000000,
B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01000000,
B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B00100000,B00000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00110000,B00000000,B00000000,B00000000,B00000000,B00000001,B10000000,
B00011000,B00000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00001110,B00000000,B00000000,B00000000,B00000000,B00001110,B00000000,
B00000111,B11111111,B11111111,B11111111,B11111111,B11111100,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B11000000,B00110000,B00001100,B00000011,B00000000,B00000000,
B00000000,B11000000,B00100000,B00001000,B00000010,B00000000,B00000000,
B00000000,B10000000,B00100000,B00011000,B00000110,B00000000,B00000000,
B00000001,B10000000,B01100000,B00010000,B00000100,B00000000,B00000000,
B00000001,B00000000,B01000000,B00110000,B00000100,B00000000,B00000000,
B00000001,B00000000,B01000000,B00110000,B00001100,B00000000,B00000000
};
const unsigned char rain[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B11111110,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000011,B00000001,B10001111,B11110000,B00000000,B00000000,
B00000000,B00000110,B00000000,B11110000,B00001100,B00000000,B00000000,
B00000000,B00001100,B00000000,B01000000,B00000110,B00000000,B00000000,
B00000000,B01111000,B00000000,B10000000,B00000011,B00000000,B00000000,
B00000001,B10000000,B00000001,B10000000,B00000001,B11100000,B00000000,
B00000001,B00000000,B00000001,B00000000,B00000000,B00011000,B00000000,
B00000001,B00000000,B01111111,B00000000,B00000000,B00001100,B00000000,
B00000001,B00000000,B11000000,B00000000,B00000000,B00000110,B00000000,
B00000001,B00000001,B10000000,B00000000,B00000000,B00000010,B00000000,
B00000011,B00000001,B00000000,B00000000,B00000000,B00000010,B00000000,
B00000110,B00000111,B00000000,B00000000,B00000000,B00000010,B00000000,
B00000100,B00111000,B00000000,B00000000,B00000000,B00000110,B00000000,
B00000100,B01100000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00000100,B01000000,B00000000,B00000000,B00000000,B00000001,B00000000,
B00000110,B11000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00000011,B10000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00000001,B10000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00000000,B01000000,B00000000,B00000000,B00000000,B00000001,B10000000,
B00000000,B01000000,B00000000,B00000000,B00000000,B00000001,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B00000010,B00000000,
B00000000,B00011111,B11111111,B11111111,B11111111,B11111100,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000010,B00000001,B00000000,B10000000,B01000000,B00110000,B00000000,
B00000100,B00000010,B00000001,B10000000,B01000000,B00100000,B00000000,
B00000100,B00000010,B00000001,B00000000,B10000000,B01100000,B00000000,
B00001000,B00000100,B00000011,B00000000,B10000000,B01000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000001,B00000000,B00000000,
B00100000,B00010000,B00001000,B00000110,B00000001,B00000000,B00000000,
B01100000,B00110000,B00001000,B00000100,B00000010,B00000000,B00000000,
B01000000,B00100000,B00010000,B00001100,B00000010,B00000000,B00000000,
B11000000,B01100000,B00010000,B00001000,B00000100,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000
};
const unsigned char snow[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00100000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00100000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00100000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00010000,B00100000,B01000000,B00000000,B00000000,
B00000000,B00000000,B00011000,B00100000,B11000000,B00000000,B00000000,
B00000000,B00000000,B00000110,B00100001,B10000000,B00000000,B00000000,
B00000000,B00000000,B00000011,B00100110,B00000000,B00000000,B00000000,
B00000000,B00100000,B00000000,B10101100,B00000000,B00100000,B00000000,
B00000000,B00010000,B00000000,B01110000,B00000000,B01100000,B00000000,
B00000000,B00010000,B00000000,B00100000,B00000000,B01000000,B00000000,
B00011100,B00011000,B00000000,B00100000,B00000000,B11000001,B11000000,
B00000011,B00001000,B00000000,B01110000,B00000000,B10000111,B00000000,
B00000000,B11101100,B00000000,B11111000,B00000000,B10111000,B00000000,
B00000000,B00011100,B00000011,B00100110,B00000001,B11000000,B00000000,
B00000000,B11111111,B11111100,B00100001,B11111111,B11111000,B00000000,
B00011111,B00000001,B11100000,B00100000,B00111100,B00000111,B11000000,
B00000000,B00000000,B11011000,B00100000,B11111000,B00000000,B00000000,
B00000000,B00000000,B01000111,B00100011,B00011000,B00000000,B00000000,
B00000000,B00000000,B01000001,B11111100,B00010000,B00000000,B00000000,
B00000000,B00000000,B01000000,B01111000,B00010000,B00000000,B00000000,
B00000000,B00000000,B01000011,B10100110,B00010000,B00000000,B00000000,
B00000000,B00000000,B11011100,B00100001,B11011000,B00000000,B00000000,
B00011100,B00000000,B11100000,B00100000,B00111100,B00000001,B11000000,
B00000011,B11110011,B11111000,B00100000,B11111110,B01111110,B00000000,
B00000000,B00011110,B00000111,B00100011,B10000001,B11000000,B00000000,
B00000000,B01111100,B00000001,B10101100,B00000001,B10110000,B00000000,
B00000001,B10001000,B00000000,B01111000,B00000000,B10001110,B00000000,
B00001110,B00001000,B00000000,B00110000,B00000000,B11000011,B10000000,
B00010000,B00011000,B00000000,B00100000,B00000000,B01000000,B00000000,
B00000000,B00010000,B00000000,B01110000,B00000000,B01100000,B00000000,
B00000000,B00110000,B00000000,B11111000,B00000000,B00100000,B00000000,
B00000000,B00100000,B00000001,B10100110,B00000000,B00100000,B00000000,
B00000000,B00000000,B00000110,B00100011,B00000000,B00000000,B00000000,
B00000000,B00000000,B00001100,B00100001,B10000000,B00000000,B00000000,
B00000000,B00000000,B00010000,B00100000,B01100000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00100000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00100000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000
};
const unsigned char mist[] PROGMEM = {
B00000000,B00000000,B00000011,B11111000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00001111,B11111111,B10000000,B00000000,B00000000,
B00000000,B00000000,B00111000,B00000011,B11000000,B00000000,B00000000,
B00000000,B00000000,B01110000,B00000000,B01100000,B00000000,B00000000,
B00000000,B00000000,B11000000,B00000000,B00111000,B00000000,B00000000,
B00000000,B00000011,B10000000,B00000000,B00011111,B11110000,B00000000,
B00000000,B00000011,B00000000,B00000000,B00011111,B11111110,B00000000,
B00000000,B00000111,B00000000,B00000000,B00000000,B00001111,B00000000,
B00000000,B11111111,B11000000,B00000000,B00000000,B00000001,B11000000,
B00000111,B11000000,B11111000,B00000000,B00000000,B00000001,B11000000,
B00001111,B00000000,B00111100,B00000000,B00000000,B00000000,B11100000,
B00011000,B00000000,B00001111,B00000000,B00000000,B00000000,B11100000,
B01110000,B00000000,B00000111,B10111100,B00000000,B00000000,B11100000,
B01110000,B00000000,B00000011,B11111111,B11000000,B00000000,B11100000,
B01100000,B00000000,B00000000,B10000001,B11100000,B00000000,B11100000,
B11100000,B00000000,B00000000,B00000000,B00111000,B00000000,B11100000,
B11100000,B00000000,B00000000,B00000000,B00011000,B00000000,B11000000,
B11100000,B00000000,B00000000,B00000000,B00011100,B00000001,B11000000,
B11100000,B00000000,B00000000,B00000000,B00011100,B00000011,B00000000,
B11100000,B00000000,B00000000,B00000000,B00000100,B00011110,B00000000,
B11100000,B00000000,B00000000,B00000000,B00000111,B11111100,B00000000,
B01100000,B00000000,B00000000,B00000000,B00000111,B11000000,B00000000,
B01110000,B00000000,B00000000,B00000000,B00011100,B00000000,B00000000,
B00011000,B00000000,B00000000,B00000000,B00011100,B00000000,B00000000,
B00011110,B00000000,B00000000,B00000000,B00111000,B00000000,B00000000,
B00001111,B00000000,B00000000,B00000000,B01100000,B00000000,B00000000,
B00000001,B11000000,B00000000,B00000011,B11000000,B00000000,B00000000,
B00000000,B11111111,B11111111,B11111111,B10000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B11000011,B11110000,B11111001,B11111000,B01100000,B00000000,
B00000000,B11111111,B11111111,B10111111,B11111111,B11100000,B00000000,
B00000000,B00111100,B00001111,B00011110,B00000111,B10000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B11000011,B11110000,B11111001,B11111000,B01100000,B00000000,
B00000000,B11111111,B11111111,B10111111,B11111111,B11100000,B00000000,
B00000000,B00111100,B00001111,B00011110,B00000111,B10000000,B00000000
};
const unsigned char clearS[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000110,B00000000,B01000000,B00001100,B00000000,B00000000,
B00000000,B00000011,B00000000,B01000000,B00011000,B00000000,B00000000,
B00000000,B00000001,B00000000,B01000000,B00010000,B00000000,B00000000,
B00000000,B00000000,B10000000,B10100000,B00100000,B00000000,B00000000,
B00000000,B00000000,B01011111,B00011111,B01000000,B00000000,B00000000,
B00111000,B00000000,B01100000,B00000000,B11100000,B00000001,B10000000,
B00000110,B00000001,B10000000,B00000000,B00011000,B00001100,B00000000,
B00000001,B11000110,B00000000,B00000000,B00001100,B01110000,B00000000,
B00000000,B00101100,B00000000,B00000000,B00000110,B11000000,B00000000,
B00000000,B00001000,B00000000,B00000000,B00000011,B00000000,B00000000,
B00000000,B00010000,B00000000,B00000000,B00000001,B00000000,B00000000,
B00000000,B00110000,B00000000,B00000000,B00000001,B10000000,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B10000000,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B10000000,B00000000,
B00111111,B11100000,B00000000,B00000000,B00000000,B11111111,B11000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B10000000,B00000000,
B00000000,B00100000,B00000000,B00000000,B00000000,B10000000,B00000000,
B00000000,B00110000,B00000000,B00000000,B00000001,B10000000,B00000000,
B00000000,B00010000,B00000000,B00000000,B00000001,B00000000,B00000000,
B00000000,B00001000,B00000000,B00000000,B00000011,B00000000,B00000000,
B00000000,B01101100,B00000000,B00000000,B00000110,B11000000,B00000000,
B00000001,B11000110,B00000000,B00000000,B00001100,B01110000,B00000000,
B00001110,B00000001,B10000000,B00000000,B00110000,B00001110,B00000000,
B00110000,B00000000,B01100000,B00000000,B11000000,B00000001,B10000000,
B00000000,B00000000,B01011111,B00011111,B01100000,B00000000,B00000000,
B00000000,B00000000,B10000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000001,B00000000,B01000000,B00010000,B00000000,B00000000,
B00000000,B00000011,B00000000,B01000000,B00001000,B00000000,B00000000,
B00000000,B00000110,B00000000,B01000000,B00001100,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B01000000,B00000000,B00000000,B00000000
};
const unsigned char clearS1[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00100000,B00100000,B00100000,B00000000,
B00000000,B00100000,B00100000,B01000000,B00000000,
B00000000,B00010000,B00100000,B01000000,B00000000,
B00000000,B00001000,B01110000,B10000000,B00000000,
B00000000,B00001011,B10001111,B10000000,B00000000,
B00110000,B00001100,B00000001,B10000000,B01100000,
B00001100,B00010000,B00000000,B11000001,B10000000,
B00000110,B00100000,B00000000,B00100110,B00000000,
B00000001,B01000000,B00000000,B00111100,B00000000,
B00000000,B01000000,B00000000,B00010000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000001,B00000000,B00000000,B00001000,B00000000,
B00111111,B00000000,B00000000,B00001111,B11100000,
B00000001,B00000000,B00000000,B00001000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000000,B10000000,B00000000,B00001000,B00000000,
B00000000,B01000000,B00000000,B00010000,B00000000,
B00000001,B01000000,B00000000,B00111100,B00000000,
B00000110,B00100000,B00000000,B00100010,B00000000,
B00001100,B00010000,B00000000,B11000001,B10000000,
B00110000,B00001100,B00000001,B10000000,B01100000,
B00000000,B00001011,B10001111,B10000000,B00000000,
B00000000,B00011000,B01000000,B10000000,B00000000,
B00000000,B00010000,B00100000,B01000000,B00000000,
B00000000,B00100000,B00100000,B01000000,B00000000,
B00000000,B00100000,B00100000,B00100000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000,
B00000000,B00000000,B00100000,B00000000,B00000000
};
const unsigned char clouds[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B01100000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000011,B11111100,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000111,B00001110,B00001111,B11000000,B00000000,B00000000,
B00000000,B00001100,B00000011,B00011110,B11110000,B00000000,B00000000,
B00000000,B00011000,B00000001,B11110000,B00011000,B00000000,B00000000,
B00000000,B00010000,B00000000,B11000000,B00001100,B00000000,B00000000,
B00000000,B01110000,B00000000,B10000000,B00000110,B00000000,B00000000,
B00000001,B11100000,B00000001,B10000000,B00000011,B00000000,B00000000,
B00000011,B00000000,B00000001,B00000000,B00000001,B00000000,B00000000,
B00000110,B00000000,B00000011,B00000000,B00000001,B11110000,B00000000,
B00000100,B00000000,B00000010,B00000000,B00000000,B10111000,B00000000,
B00001100,B00000000,B01111110,B00000000,B00000000,B00001100,B00000000,
B00001000,B00000000,B11100110,B00000000,B00000000,B00000100,B00000000,
B00001000,B00000001,B10000000,B00000000,B00000000,B00000010,B00000000,
B00001000,B00000011,B00000000,B00000000,B00000000,B00000010,B00000000,
B00000100,B00000010,B00000000,B00000000,B00000000,B00000010,B00000000,
B00001100,B00000100,B00000000,B00000000,B00000000,B00000011,B00000000,
B00011000,B00000100,B00000000,B00000000,B00000000,B00000011,B00000000,
B00010000,B00011100,B00000000,B00000000,B00000000,B00000010,B00000000,
B00010000,B01111100,B00000000,B00000000,B00000000,B00000010,B00000000,
B00110000,B11000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00110001,B10000000,B00000000,B00000000,B00000000,B00000001,B00000000,
B00110001,B00000000,B00000000,B00000000,B00000000,B00000001,B10000000,
B00010011,B00000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00010010,B00000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00011010,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B00001110,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B00000110,B00000000,B00000000,B00000000,B00000000,B00000000,B11000000,
B00000010,B00000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00000011,B00000000,B00000000,B00000000,B00000000,B00000000,B10000000,
B00000001,B00000000,B00000000,B00000000,B00000000,B00000001,B10000000,
B00000001,B10000000,B00000000,B00000000,B00000000,B00000001,B00000000,
B00000000,B11000000,B00000000,B00000000,B00000000,B00000011,B00000000,
B00000000,B01111111,B11111111,B11111111,B11111111,B11111110,B00000000,
B00000000,B00011111,B11111111,B11111111,B11111111,B11111000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000
};
const unsigned char weather1[] PROGMEM = {
B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00011111,B11000000,B00000000,
B00000000,B00000000,B01100000,B01100000,B00000000,
B00000000,B00000000,B10000000,B00010000,B00000000,
B00000000,B00000001,B00000000,B00001000,B00000000,
B00000000,B00000011,B00000000,B00000111,B00000000,
B00000000,B00000010,B00000000,B00000000,B11000000,
B00000000,B01111110,B00000000,B00000000,B01100000,
B00000000,B10000000,B00000000,B00000000,B00110000,
B00000001,B00000000,B00000000,B00000000,B00010000,
B00000011,B00000000,B00000000,B00000000,B00010000,
B00000110,B00000000,B00000000,B00000000,B00010000,
B00011100,B00000000,B00000000,B00000000,B00010000,
B00100000,B00000000,B00000000,B00000000,B00011000,
B01000000,B00000000,B00000000,B00000000,B00001000,
B01000000,B00000000,B00000000,B00000000,B00000100,
B11000000,B00000000,B00000000,B00000000,B00000100,
B01000000,B00000000,B00000000,B00000000,B00000100,
B01000000,B00000000,B00000000,B00000000,B00001100,
B01000000,B00000000,B00000000,B00000000,B00001000,
B00100000,B00000000,B00000000,B00000000,B00010000,
B00011111,B11111111,B11111111,B11111111,B11100000,
B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000
};
const unsigned char pressure[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x30, 0x1f, 0x38, 0x38, 0x1f, 0x38, 0x78, 0x0f, 0x38,
0x70, 0x00, 0x38, 0x70, 0x00, 0x38, 0x70, 0x00, 0x38, 0x70, 0xff, 0x38, 0x70, 0xff, 0x38, 0x38,
0x7f, 0x1c, 0x38, 0x00, 0x1c, 0x1c, 0x00, 0x0e, 0x1c, 0x00, 0x0e, 0x0e, 0x1f, 0x0e, 0x0e, 0x1f,
0x0e, 0x0e, 0x0f, 0x0e, 0x0e, 0x00, 0x0e, 0x0e, 0x00, 0x3e, 0x7e, 0x00, 0x3f, 0x3e, 0x1f, 0x1f,
0x3f, 0x1f, 0x1e, 0x1c, 0x0f, 0x08, 0x10, 0x00
};
const unsigned char humidity[] PROGMEM = {
// 'humidity, 16x16px
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x1f, 0x03, 0xe0, 0x1f, 0x07, 0xe0, 0x0f, 0x07,
0xf0, 0x00, 0x0f, 0x70, 0x00, 0x0e, 0x78, 0x00, 0x1c, 0x3c, 0xff, 0x3c, 0x1c, 0xff, 0x38, 0x1e,
0x7f, 0x38, 0x0e, 0x00, 0x70, 0x0e, 0x00, 0x70, 0x0f, 0x00, 0xf0, 0xff, 0x1f, 0xff, 0xff, 0x1f,
0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, 0x7f, 0xff, 0x00, 0x7f, 0xfe, 0x00, 0x3f, 0xfe, 0x1f, 0x3f,
0xfc, 0x1f, 0x0f, 0xf8, 0x0f, 0x03, 0xe0, 0x00
};
const unsigned char windicon [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x0f, 0xc0, 0x60, 0x1c, 0xe1, 0xf8, 0x00,
0x63, 0x9c, 0x00, 0x62, 0x0c, 0x00, 0xe0, 0x04, 0x3f, 0xc0, 0x06, 0x3f, 0x80, 0x0c, 0x00, 0x00,
0x1c, 0x3f, 0xff, 0xf8, 0x3f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x3f, 0xfe, 0x00,
0x00, 0x07, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0xc7, 0x00, 0x00, 0x7e, 0x00, 0x00,
0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char temp [] PROGMEM = {
0x00, 0x3c, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x7e, 0x00, 0x00,
0x7e, 0x00, 0x00, 0x70, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x78, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x70,
0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0xff, 0x00, 0x01, 0xff, 0x80,
0x01, 0xff, 0x80, 0x03, 0xff, 0xc0, 0x03, 0xff, 0xc0, 0x03, 0xff, 0xc0, 0x01, 0xff, 0x80, 0x01,
0xff, 0x80, 0x00, 0xff, 0x00, 0x00, 0x3c, 0x00
};
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 30000;
String jsonBuffer;
void setup() {
Serial.begin(115200);
// Инициализация дисплея
tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
tft.setRotation (3);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0,0);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.useHTTP10(true);
http.begin(client,api_1 + qLocation + api_2 + api_3 + api_key);
http.GET();
DynamicJsonDocument doc(1200);
DeserializationError error = deserializeJson(doc, http.getStream());
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
http.end();
float coord_lon = doc["coord"]["lon"]; // 37.5458
float coord_lat = doc["coord"]["lat"]; // 51.2817
JsonObject weather_0 = doc["weather"][0];
int weather_0_id = weather_0["id"]; // 801
const char* weather_0_main = weather_0["main"]; // "Clouds"
const char* weather_0_description = weather_0["description"]; // "few clouds"
const char* weather_0_icon = weather_0["icon"]; // "02d"
const char* base = doc["base"]; // "stations"
JsonObject main = doc["main"];
int main_temp = main["temp"]; // -8.9
int main_feels_like = main["feels_like"]; // -15.9
int main_temp_min = main["temp_min"]; // -8.9
int main_temp_max = main["temp_max"]; // -8.9
int main_pressure = main["pressure"]; // 1036
int main_humidity = main["humidity"];
int main_pressure_mm = main_pressure/1.333;
int main_sea_level = main["sea_level"]; // 1036
int main_grnd_level = main["grnd_level"]; // 1014
int visibility = doc["visibility"]; // 10000
JsonObject wind = doc["wind"];
float wind_speed = wind["speed"]; // 4.98
int wind_deg = wind["deg"]; // 47
float wind_gust = wind["gust"]; // 8.75
int clouds_all = doc["clouds"]["all"]; // 20
long dt = doc["dt"]; // 1675757728
JsonObject sys = doc["sys"];
const char* sys_country = sys["country"]; // "RU"
long sys_sunrise = sys["sunrise"]; // 1675745921
long sys_sunset = sys["sunset"]; // 1675780153
int timezone = doc["timezone"]; // 10800
timezone=timezone/360;
UnixTime stamp(timezone);
long id = doc["id"]; // 558146
const char* name = doc["name"]; // "Gubkin"
int cod = doc["cod"]; // 200
//вывод погоды на экран
Serial.println("main_temp_min: " + String(main_temp_min));
Serial.println("main_temp_max: " + String(main_temp_max));
tft.setTextColor(ST7735_WHITE,ST7735_BLACK);
tft.setTextSize(1,1);
tft.setCursor(60,2);
tft.print(String(name)+", "+String(sys_country));
if(weather_0_main=="Clouds")
{
tft.drawBitmap(4, 2, clouds, 51, 38,0xFFE0,ST7735_BLACK);
}
else if(weather_0_main=="Rain")
{
tft.drawBitmap(4, 2, rain, 51, 40,0x07FF,ST7735_BLACK);
}
else if(weather_0_main=="Thunderstorm")
{
tft.drawBitmap(4, 2, thunder, 51, 40,0x03DF,ST7735_BLACK);
}
else if(weather_0_main=="Clear")
{
tft.drawBitmap(4, 2, clearS1, 37, 40,0xFFE0,ST7735_BLACK);
}
else if(weather_0_main=="Drizzle")
{
tft.drawBitmap(4, 2, drizzle, 51, 40,0x07FF,ST7735_BLACK);
}
else if(weather_0_main=="Snow")
{
tft.drawBitmap(4, 2, snow, 51, 40,ST7735_WHITE,ST7735_BLACK);
}
else
{
tft.drawBitmap(4, 2, mist, 51, 40,0xD6D5,ST7735_BLACK);
}
if (main_temp>=5) color=0xF705;
if (main_temp>20) color=0xF005;
if (main_temp<5) color=0x0E3F;
if (main_temp<=-10) color=0x001D;
tft.drawBitmap(60, 12, temp, 24, 24, color);
tft.setCursor(88,14);
tft.setTextSize(3,3);
tft.setTextColor(color,ST7735_BLACK);
tft.print (String(main_temp)+degree);
tft.setCursor(4,40);
tft.setTextSize(1,1);
tft.setTextColor(ST7735_WHITE,ST7735_BLACK);
tft.print (weather_0_description);
tft.println(" ");
tft.setCursor(4,52);
tft.setTextColor(color,ST7735_BLACK);
tft.println("Min: "+String(main_temp_min)+degree+" Max: "+String(main_temp_max)+degree);
stamp.getDateTime(sys_sunrise);
tft.setCursor(0,64);
tft.setTextColor(ST7735_YELLOW,ST7735_BLACK);
tft.println ("Sunrise: "+String(stamp.hour)+":"+String(stamp.minute));
stamp.getDateTime(sys_sunset);
tft.println ("Sunset: "+String(stamp.hour)+":"+String(stamp.minute));
tft.setTextColor(ST7735_WHITE,ST7735_BLACK);
tft.print ("Visiblity: "+String(visibility)+" meters");
tft.setTextSize(1,1);
tft.drawBitmap(8, 90, pressure, 24, 24,0x6515);
tft.setCursor(8,120);
tft.setTextColor(ST7735_WHITE,ST7735_BLACK);
tft.println (String(main_pressure_mm));
tft.drawBitmap(44, 90, humidity, 24, 24,0xAF1F);
tft.setCursor(44,120);
tft.println(String(main_humidity));
tft.drawBitmap(80, 90, windicon, 24, 24,ST7735_WHITE);
tft.setCursor(80,120);
tft.println (String(wind_speed));
int x2=(134+(sin(wind_deg/57.29577951)*(10)));
int y2=(102-(cos(wind_deg/57.29577951)*(10)));
tft.drawCircle(134,102,12,ST7735_WHITE);
tft.fillCircle(134,102,11,ST7735_BLACK);
tft.drawLine(134,102,x2,y2,ST7735_CYAN);
tft.fillCircle(x2,y2,2,ST7735_WHITE);
tft.setCursor(124,120);
tft.println (String(wind_deg));
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
Настройки
Перед компиляцией и прошивкой скетча в ESP8266 необходимо провести некоторые настройки
//Настройки WiFi
const char* ssid = "ssid";
const char* password = "pass";
//настройки Open Weather Map
const String api_1 = "http://api.openweathermap.org/data/2.5/weather?q=";
const String qLocation = "Gubkin,ru"; //ваш город
const String api_2 = "&units=metric"; //температура в градусах цельсия
const String api_3 = "&APPID=";
const String api_key = "xxxxxxxxxxxxxxxxxxxxxxx"; //ваш api ключ
ssid и pass - учетные данные для подключения к вашей сети Wifi. Замените на свои
const String qLocation = "Gubkin,ru"; - укажите ваш город и регион через запятую. Строго по английски. Например "Moskow,ru" или "Minsk,by"
const String api_key = "xxxxxxxxxxxxxxxxxxxxxxx"; //ваш api ключ - здесь вам необходимо указать ваш API ключ OpenWeatherMap, полученный ранее
На этом настройки программы завершены и можно компилировать и прошивать ее в ESP8266
Разбор кода программы
Напоследок, хотелось бы дать несколько комментариев по некоторым участкам кода программы, если вы вдруг захотите модифицировать ее под себя и разработать на ее основе что-то более глобальное.
Сначала остановимся на участках кода такого вида
const unsigned char pressure[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x30, 0x1f, 0x38, 0x38, 0x1f, 0x38, 0x78, 0x0f, 0x38,
0x70, 0x00, 0x38, 0x70, 0x00, 0x38, 0x70, 0x00, 0x38, 0x70, 0xff, 0x38, 0x70, 0xff, 0x38, 0x38,
0x7f, 0x1c, 0x38, 0x00, 0x1c, 0x1c, 0x00, 0x0e, 0x1c, 0x00, 0x0e, 0x0e, 0x1f, 0x0e, 0x0e, 0x1f,
0x0e, 0x0e, 0x0f, 0x0e, 0x0e, 0x00, 0x0e, 0x0e, 0x00, 0x3e, 0x7e, 0x00, 0x3f, 0x3e, 0x1f, 0x1f,
0x3f, 0x1f, 0x1e, 0x1c, 0x0f, 0x08, 0x10, 0x00
};
Это не что иное как картинки, конвертированные в массив строк. Это позволяет экономить ресурсы системы и благоприятно сказывается на скорости работы и отрисовки изображений. Каждый элемент массива - это отдельный пиксель. Его содержимое - HEX значение цвета пикселя. Перевести картинки с в этот формат можно с помощью онлайн утилиты
Далее хотелось бы остановится, на этом небольшом, но важном участке кода
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
Здесь timerDelay - это интервал обновления данных о погоде в миллисекундах. Бесплатный тарифный план OpenWeatherMap позволяет не более 60 обращений к APi за минуту с одного аккаунта. При превышении этого числа аккаунт пользователя может быть забанен. Так что внимательно следим за этим параметром и интервал ставим как минимум 30 секунд или еще выше если быстрое обновление данных о погоде не критично.
Далее идет разбор Json файла и запись значений из него в локальные переменные
float coord_lon = doc["coord"]["lon"]; // 37.5458
float coord_lat = doc["coord"]["lat"]; // 51.2817
JsonObject weather_0 = doc["weather"][0];
int weather_0_id = weather_0["id"]; // 801
const char* weather_0_main = weather_0["main"]; // "Clouds"
const char* weather_0_description = weather_0["description"]; // "few clouds"
const char* weather_0_icon = weather_0["icon"]; // "02d"
const char* base = doc["base"]; // "stations"
JsonObject main = doc["main"];
int main_temp = main["temp"]; // -8.9
int main_feels_like = main["feels_like"]; // -15.9
int main_temp_min = main["temp_min"]; // -8.9
int main_temp_max = main["temp_max"]; // -8.9
int main_pressure = main["pressure"]; // 1036
int main_humidity = main["humidity"];
int main_pressure_mm = main_pressure/1.333;
int main_sea_level = main["sea_level"]; // 1036
int main_grnd_level = main["grnd_level"]; // 1014
int visibility = doc["visibility"]; // 10000
JsonObject wind = doc["wind"];
float wind_speed = wind["speed"]; // 4.98
int wind_deg = wind["deg"]; // 47
float wind_gust = wind["gust"]; // 8.75
int clouds_all = doc["clouds"]["all"]; // 20
long dt = doc["dt"]; // 1675757728
JsonObject sys = doc["sys"];
const char* sys_country = sys["country"]; // "RU"
long sys_sunrise = sys["sunrise"]; // 1675745921
long sys_sunset = sys["sunset"]; // 1675780153
int timezone = doc["timezone"]; // 10800
timezone=timezone/360;
UnixTime stamp(timezone);
long id = doc["id"]; // 558146
const char* name = doc["name"]; // "Gubkin"
int cod = doc["cod"]; // 200
В проекте практически все они отображаются на экране, но если вам надо что-нибудь экзотичное, типа отображения высоты над уровнем моря, то можете найти соответствующую переменную и вставить её в вывод программы
Это всё, о чем я хотел написать и напоследок прилагаю фото прототипа устройства.