#include <Arduino.h>
#include <AsyncTCP.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <WiFi.h>
//#include <ESP8266WiFi.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHTesp.h"
#include <RTClib.h>
RTC_DS1307 rtc;
const int DHT_PIN = 21;
DHTesp dhtSensor;
String openWeatherMapApiKey = "7e04cc3aaf4976b9ffd56f77d83b1fa4";
String temperature_unit = "metric";
String city = "Kyiv";
String jsonBuffer;
float OutTemperature ;
int OutHumidity;
String Icon;
int Sun_rise;
int Sun_set;
String endpoint;
JSONVar readings;
JSONVar owmreadings;
#define TFT_DC 2
#define TFT_CS 15
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const unsigned char img_thermometer[] PROGMEM = {
0x00, 0xfe, 0x03, 0xfe, 0x50,
0x00, 0xff, 0x00, 0xff, 0x55,
0x60, 0x9f, 0x80, 0x9f, 0x65,
};
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
String timeConverter(long int epoch){
String sun;
int hr=(epoch % 86400L) / 3600;
int min=(epoch % 3600) / 60;
int sec=(epoch % 60);
sun= String(hr)+":"+String(min);
Serial.println(sun);
return sun;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
tft.begin();
tft.setTextSize(2);
tft.setCursor(10,10);
tft.setTextColor(ILI9341_GREEN);
tft.println("----- Indoor -----");
tft.setCursor(10, 40);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.print(""+ String(data.temperature, 1) +" C");
tft.println(" "+ String(data.humidity,0) + "%");
tft.setCursor(10,80);
tft.setTextSize(2);
tft.setTextColor(ILI9341_GREEN);
tft.println("----- Outdoor -----");
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
endpoint = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=" + temperature_unit + "&appid=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(endpoint.c_str());
JSONVar myObject = JSON.parse(jsonBuffer);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
}
//Icon = String(myObject["weather"][0]["icon"]);
OutTemperature = double(myObject["main"]["temp"]);
OutHumidity = int(myObject["main"]["humidity"]);
Sun_rise = int(myObject["sys"]["sunrise"]);
Sun_set = int(myObject["sys"]["sunset"]);
int timeOffset = int(myObject["timezone"]);
Serial.println("Temperature: ");
Serial.println(OutTemperature);
Serial.println("OutHumidity: ");
Serial.println(OutHumidity);
int sensetemp;
sensetemp = OutTemperature;
Serial.println(sensetemp);
tft.setCursor(10, 100);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.print(""+ String(OutTemperature, 1) +" C");
tft.println(" "+ String(OutHumidity) + "%");
Serial.println(Sun_rise);
Serial.println(Sun_set);
tft.setCursor(10, 160);
tft.setTextSize(2);
tft.setTextColor(ILI9341_GREEN);
tft.println("Sun Rise | SunSet");
tft.print(timeConverter(Sun_rise+timeOffset));
tft.println(" "+timeConverter(Sun_set+timeOffset));
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}