#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
// --- TFT Setup ---
#define TFT_DC 48
#define TFT_CS 53
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// --- DHT Setup ---
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// --- Analog Sensor Pins ---
#define CLOUD_SENSOR_PIN A0
#define WIND_SENSOR_PIN A1
#define RAIN_SENSOR_PIN A2
// --- Weather Thresholds ---
const float windThreshold = 20.0;
const float rainThreshold = 10.0;
// --- Weather Icons ---
#include "sunny.h"
#include "partlycloudy.h"
#include "cloudy.h"
#include "sunnywindy.h"
#include "partlycloudywindy.h"
#include "cloudywindy.h"
#include "rainy.h"
#include "rainywindy.h"
void setup() {
Serial.begin(9600);
Serial.println("Booting...");
tft.begin();
tft.setRotation(0); // Portrait mode
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
dht.begin();
pinMode(CLOUD_SENSOR_PIN, INPUT);
pinMode(WIND_SENSOR_PIN, INPUT);
pinMode(RAIN_SENSOR_PIN, INPUT);
Serial.println("Setup complete");
}
void loop() {
Serial.println("Loop running...");
int cloudValue = analogRead(CLOUD_SENSOR_PIN);
float windSpeed = analogRead(WIND_SENSOR_PIN) * 0.05;
float rainIntensity = analogRead(RAIN_SENSOR_PIN) * 0.02;
// Simulated values if DHT not used
float temperature = random(20, 35);
float humidity = random(50, 80);
String weatherStatus = "Sunny";
const uint16_t* icon = sunny;
if (cloudValue < 300) {
weatherStatus = "Sunny";
icon = sunny;
} else if (cloudValue < 700) {
weatherStatus = "Partly Cloudy";
icon = partlycloudy;
} else {
weatherStatus = "Cloudy";
icon = cloudy;
}
if (rainIntensity > rainThreshold) {
if (windSpeed > windThreshold) {
weatherStatus = "Rainy & Windy";
icon = rainywindy;
} else {
weatherStatus = "Rainy";
icon = rainy;
}
} else if (windSpeed > windThreshold) {
if (weatherStatus == "Sunny") {
weatherStatus = "Sunny & Windy";
icon = sunnywindy;
} else if (weatherStatus == "Partly Cloudy") {
weatherStatus = "Partly Cloudy & Windy";
icon = partlycloudywindy;
} else if (weatherStatus == "Cloudy") {
weatherStatus = "Cloudy & Windy";
icon = cloudywindy;
}
}
displayWeather(weatherStatus, icon, temperature, humidity);
delay(2000);
}
void displayWeather(String status, const uint16_t* icon, float temperature, float humidity) {
Serial.println("Displaying weather...");
tft.fillScreen(ILI9341_BLACK);
// Load PROGMEM image into RAM buffer
uint16_t buffer[64 * 64];
memcpy_P(buffer, icon, sizeof(buffer));
tft.drawRGBBitmap(48, 30, buffer, 64, 64);
tft.setCursor(10, 110);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print("Temp: ");
tft.print(temperature);
tft.println("C");
tft.setCursor(10, 140);
tft.print("Humidity: ");
tft.print(humidity);
tft.println("%");
tft.setCursor(10, 170);
tft.print("Status: ");
tft.println(status);
}