#include <Adafruit_GFX.h> // include Adafruit graphics library
#include <Adafruit_ILI9341.h> // include Adafruit ILI9341 TFT library
// ILI9341 TFT module connections
#define TFT_RST 4
#define TFT_CS 5
#define TFT_DC 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* host = "api.openweathermap.org";
String cityName = "Caino";
String appKey = "";
int flagScreen = 0;
int flagFirstData = 1;
int state = 0;
unsigned long measureDelayInternalCycle = 10000; // 10s
unsigned long lastTimeRanInternalCycle;
unsigned long measureDelayExternalCycle = 60000; // 60s
unsigned long measureDelayExternalCycleVariable;
unsigned long lastTimeRanExternalCycle;
String line = "";
String tempNow0 = "";
String pressure0 = "";
String humNow0 = "";
String weatherNow0 = "";
String dateNow0 = "";
String tempNow1 = "";
String pressure1 = "";
String humNow1 = "";
String weatherNow1 = "";
String dateNow1 = "";
String tempNow2 = "";
String pressure2 = "";
String humNow2 = "";
String weatherNow2 = "";
String dateNow2 = "";
String tempNow3 = "";
String pressure3 = "";
String humNow3 = "";
String weatherNow3 = "";
String dateNow3 = "";
String tempNow4 = "";
String pressure4 = "";
String humNow4 = "";
String weatherNow4 = "";
String dateNow4 = "";
void testfillrects(uint16_t color1, uint16_t color2) {
tft.fillScreen(ILI9341_BLACK);
for (int16_t x = tft.width() - 1; x > 6; x -= 6) {
tft.fillRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x, color1);
tft.drawRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x, color2);
}
}
void tftConnecting() {
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Connecting...");
delay(1500);
}
void tftConnected() {
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Connected!");
delay(2000);
tft.fillScreen(ILI9341_BLACK);
}
void setup(void) {
Serial.begin(115200);
delay(2000);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
testfillrects(ILI9341_YELLOW, ILI9341_MAGENTA);
WiFi.begin(ssid, password);
tft.fillScreen(ILI9341_BLACK);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
tftConnecting();
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("CONNECTED");
tft.fillScreen(ILI9341_BLACK);
tftConnected();
}
}
void loop() {
WiFiClient client;
if (flagFirstData == 1) {
measureDelayExternalCycleVariable = 2000;
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextWrap(true);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Wait a few seconds.");
tft.println();
tft.println("I'm receiving the first data.");
} else {
measureDelayExternalCycleVariable = measureDelayExternalCycle;
}
if (millis() > lastTimeRanExternalCycle + measureDelayExternalCycleVariable) {
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80)) {
client.println("GET /data/2.5/forecast?q=" + cityName + "&APPID=" + appKey + "&mode=json&units=metric&cnt=7 HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
while (client.connected() || client.available()) {
if (client.available()) {
line = client.readStringUntil('\n');
}
}
client.stop();
} else {
Serial.println("connection failed!]");
client.stop();
}
Serial.println("\n");
Serial.println("-----------------------------------");
Serial.println(line);
Serial.println("-----------------------------------");
int sizeResponse = line.length();
DynamicJsonDocument doc(sizeResponse);
deserializeJson(doc, line);
JsonObject obj = doc.as<JsonObject>();
String responseCode = obj["cod"];
if (responseCode == "200") {
tempNow0 = String(obj["list"][0]["main"]["temp"]);
humNow0 = String(obj["list"][0]["main"]["humidity"]);
pressure0 = String(obj["list"][0]["main"]["pressure"]);
weatherNow0 = String(obj["list"][0]["weather"][0]["description"]);
dateNow0 = String(obj["list"][0]["dt_txt"]);
tempNow1 = String(obj["list"][1]["main"]["temp"]);
humNow1 = String(obj["list"][1]["main"]["humidity"]);
pressure1 = String(obj["list"][1]["main"]["pressure"]);
weatherNow1 = String(obj["list"][1]["weather"][0]["description"]);
dateNow1 = String(obj["list"][1]["dt_txt"]);
tempNow2 = String(obj["list"][2]["main"]["temp"]);
humNow2 = String(obj["list"][2]["main"]["humidity"]);
pressure2 = String(obj["list"][2]["main"]["pressure"]);
weatherNow2 = String(obj["list"][2]["weather"][0]["description"]);
dateNow2 = String(obj["list"][2]["dt_txt"]);
tempNow3 = String(obj["list"][3]["main"]["temp"]);
humNow3 = String(obj["list"][3]["main"]["humidity"]);
pressure3 = String(obj["list"][3]["main"]["pressure"]);
weatherNow3 = String(obj["list"][3]["weather"][0]["description"]);
dateNow3 = String(obj["list"][3]["dt_txt"]);
tempNow4 = String(obj["list"][4]["main"]["temp"]);
humNow4 = String(obj["list"][4]["main"]["humidity"]);
pressure4 = String(obj["list"][4]["main"]["pressure"]);
weatherNow4 = String(obj["list"][4]["weather"][0]["description"]);
dateNow4 = String(obj["list"][4]["dt_txt"]);
} else {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextWrap(true);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Something went wrong!");
return;
}
flagFirstData = 0;
lastTimeRanExternalCycle = millis();
}
String dateMod = "";
if (millis() > lastTimeRanInternalCycle + measureDelayInternalCycle) {
switch (state) {
case 0:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(cityName);
tft.print("T: " + tempNow0);
tft.println(" C");
tft.print("H: " + humNow0);
tft.print(" %");
tft.println();
tft.print("P: " + pressure0);
tft.print(" hP");
tft.println("\nWeather: ");
tft.print(weatherNow0);
tft.println();
tft.println();
for (int i = 5; i < dateNow0.length() - 3; i++) {
dateMod += dateNow0[i];
}
dateMod.replace("-", "/");
tft.println(dateMod);
state = 1;
break;
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(cityName);
tft.print("T: " + tempNow1);
tft.println(" C");
tft.print("H: " + humNow1);
tft.print(" %");
tft.println();
tft.print("P: " + pressure1);
tft.print(" hP");
tft.println("\nWeather: ");
tft.print(weatherNow1);
tft.println();
tft.println();
for (int i = 5; i < dateNow1.length() - 3; i++) {
dateMod += dateNow1[i];
}
dateMod.replace("-", "/");
tft.println(dateMod);
state = 2;
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(cityName);
tft.print("T: " + tempNow2);
tft.println(" C");
tft.print("H: " + humNow2);
tft.print(" %");
tft.println();
tft.print("P: " + pressure2);
tft.print(" hP");
tft.println("\nWeather: ");
tft.print(weatherNow2);
tft.println();
tft.println();
for (int i = 5; i < dateNow2.length() - 3; i++) {
dateMod += dateNow2[i];
}
dateMod.replace("-", "/");
tft.println(dateMod);
state = 3;
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(cityName);
tft.print("T: " + tempNow3);
tft.println(" C");
tft.print("H: " + humNow3);
tft.print(" %");
tft.println();
tft.print("P: " + pressure3);
tft.print(" hP");
tft.println("\nWeather: ");
tft.print(weatherNow3);
tft.println();
tft.println();
for (int i = 5; i < dateNow3.length() - 3; i++) {
dateMod += dateNow3[i];
}
dateMod.replace("-", "/");
tft.println(dateMod);
state = 4;
break;
case 4:
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(cityName);
tft.print("T: " + tempNow4);
tft.println(" C");
tft.print("H: " + humNow4);
tft.print(" %");
tft.println();
tft.print("P: " + pressure4);
tft.print(" hP");
tft.println("\nWeather: ");
tft.print(weatherNow4);
tft.println();
tft.println();
for (int i = 5; i < dateNow4.length() - 3; i++) {
dateMod += dateNow4[i];
}
dateMod.replace("-", "/");
tft.println(dateMod);
state = 0;
break;
default:
tft.setCursor(0, 0);
tft.setRotation(0);
tft.setTextWrap(true);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Undefined option");
break;
}
lastTimeRanInternalCycle = millis();
}
}
// https://www.techrm.com/weather-station-project-with-esp8266-and-tft-display-real-time-monitoring-of-weather-forecasts/