===== Эксперимент 71. Метеостанция =====
В прошлых экспериментах мы научились подключаться к беспроводной сети Wi-Fi, запрашивать данные о текущей погоде с веб-сервиса weatherbit.io и отображать красивый пользовательский интерфейс. Теперь соединим воедино эти достижения, чтобы получить полноценную погодную станцию.
==== Схема эксперимента ====
Схема эксперимента не изменилась по сравнению с предыдущей
{{ :products:esp-iot:exp39.1_mont.png?direct&600 |}}
//Рисунок 1. Монтажная схема эксперимента с 8 выводами//
{{ :products:esp-iot:exp39.1_mont_11pin.png?direct&600 |}}
//Рисунок 2. Монтажная схема эксперимента с 11 выводами//
==== Программный код эксперимента ====
#include
#include
#include "LittleFS_ImageReader.h"
#include
#include
#include
#include
#define PIN_CS 2
#define PIN_DC 4
#define PIN_RST 5
#define WIFI_NAME "WiFi_name"
#define WIFI_PASSWORD "WiFi_pass"
Adafruit_ST7735 tft = Adafruit_ST7735(PIN_CS, PIN_DC, PIN_RST);
LittleFS_ImageReader reader;
String httpGet(String url) {
WiFiClient client;
HTTPClient http;
String data = "";
if (http.begin(client, url)) {
Serial.println("[HTTP] GET");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
data = http.getString();
} else {
Serial.printf("[HTTP] GET failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("[HTTP] Unable to connect");
}
}
return data;
}
void setup() {
Serial.begin(9600);
Serial.println();
LittleFS.begin();
tft.initR(INITR_BLACKTAB);
tft.setRotation(2);
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
String API_key = "YourApiKey";
String city_name = "Moscow";
String json_string = httpGet("http://api.weatherbit.io/v2.0/current?city=" + city_name + "&key=" + API_key);
JsonDocument json_doc;
deserializeJson(json_doc, json_string);
String w_city = json_doc["data"][0]["city_name"];
int w_temp = json_doc["data"][0]["temp"];
int w_temp_feels = json_doc["data"][0]["app_temp"];
int w_humidity = json_doc["data"][0]["rh"];
float w_wind = json_doc["data"][0]["wind_spd"];
String w_pod = json_doc["data"][0]["pod"];
int w_code = int(json_doc["data"][0]["weather"]["code"]);
String w_pic;
if (w_code <= 233) w_pic = "11";
else if ((w_code >= 300 && w_code <= 520) || w_code == 522) w_pic = "09";
else if (w_code >= 521 && w_code <= 600) w_pic = "10";
else if (w_code >= 601 && w_code <= 622) w_pic = "13";
else if (w_code >= 623 && w_code <= 751) w_pic = "50";
else if (w_code == 800) w_pic = "01";
else if (w_code >= 801 && w_code <=802) w_pic = "02";
else if (w_code == 803) w_pic = "03";
else if (w_code == 804) w_pic = "04";
String w_pic_temp;
if (w_temp > 0) w_pic_temp = "tp";
else w_pic_temp = "tn";
unsigned int font_color = tft.color565(33, 149, 82);
unsigned int font_color2 = tft.color565(8, 85, 41);
tft.fillScreen(ST77XX_WHITE);
tft.setTextColor(font_color);
tft.setTextSize(2);
tft.setCursor(5, 1);
tft.print(w_city);
reader.drawBMP("/weather/" + w_pic + w_pod + ".bmp", tft, 34, 16);
reader.drawBMP("/weather/" + w_pic_temp + ".bmp", tft, 5, 70);
tft.setTextSize(4);
tft.setCursor(30, 75);
tft.print(w_temp);
tft.setTextColor(font_color2);
tft.setTextSize(1);
tft.setCursor(5, 120);
tft.print("Feels: " + String(w_temp_feels));
tft.setCursor(5, 130);
tft.print("Humidity: " + String(w_humidity) + "%");
tft.setCursor(5, 140);
tft.print("Wind: " + String(w_wind, 1) + "m/s");
}
void loop() {
}
Не забудь заменить в коде название сети Wi-Fi и ее пароль.
#define WIFI_NAME "WiFi_name"
#define WIFI_PASSWORD "WiFi_pass"
Не забудь заменить в коде ключ доступа к сервису weatherbit.io, который мы получили во время эксперимента 69
String API_key = "YourApiKey";