#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
String apiKey = "RIKAROA6N3I1MLC1";
#define DHT_PIN 4
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;
bool wifiConnected = false;
bool thingSpeakConnected = false;
float temperature = 0;
float humidity = 0;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Starting System");
lcd.setCursor(0, 1);
lcd.print("Please Wait...");
dht.begin();
delay(2000);
connectToWiFi();
readSensors();
Serial.println("System initialized");
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
wifiConnected = true;
readSensors();
if ((millis() - lastTime) > timerDelay)
{
sendToThingSpeak();
lastTime = millis();
}
}
else
{
wifiConnected = false;
thingSpeakConnected = false;
Serial.println("WiFi Disconnected");
connectToWiFi();
}
updateLCD();
delay(2000);
}
void connectToWiFi()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connecting WiFi");
lcd.setCursor(0, 1);
lcd.print("Please wait...");
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20)
{
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED)
{
wifiConnected = true;
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
delay(3000);
}
else
{
wifiConnected = false;
Serial.println();
Serial.println("Failed to connect to WiFi");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Failed!");
lcd.setCursor(0, 1);
lcd.print("Check Settings");
delay(3000);
}
}
void readSensors()
{
temperature = dht.readTemperature();
humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity))
{
Serial.println("Failed to read from DHT sensor!");
temperature = 0;
humidity = 0;
}
else
{
Serial.printf("Temperature: %.1f°C, Humidity: %.1f%%\n", temperature, humidity);
}
}
void sendToThingSpeak()
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + apiKey;
url += "&field1=" + String(temperature);
url += "&field2=" + String(humidity);
Serial.println("Sending to ThingSpeak: " + url);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
String response = http.getString();
Serial.println("ThingSpeak Response: " + response);
if (response.toInt() > 0)
{
thingSpeakConnected = true;
Serial.println("Data sent successfully to ThingSpeak!");
}
else
{
thingSpeakConnected = false;
Serial.println("Failed to send data to ThingSpeak");
}
}
else
{
thingSpeakConnected = false;
Serial.printf("Error sending to ThingSpeak: %d\n", httpResponseCode);
}
http.end();
}
}
void updateLCD()
{
lcd.clear();
lcd.setCursor(0, 0);
if (wifiConnected)
{
if (thingSpeakConnected)
{
lcd.print("Net:OK TS:OK");
}
else
{
lcd.print("Net:OK TS:FAIL");
}
}
else
{
lcd.print("Net:DISCONNECTED");
}
lcd.setCursor(0, 1);
if (!isnan(temperature) && !isnan(humidity))
{
lcd.printf("%.1fC %.1f%%", temperature, humidity);
}
else
{
lcd.print("Sensor Error");
}
static unsigned long statusTime = 0;
if (millis() - statusTime > 10000)
{
showDetailedStatus();
statusTime = millis();
}
}
void showDetailedStatus()
{
if (wifiConnected)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IP Address:");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
delay(2000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ThingSpeak:");
lcd.setCursor(0, 1);
if (thingSpeakConnected)
{
lcd.print("Data Sent OK");
}
else
{
lcd.print("Connection Failed");
}
delay(2000);
}