#include <WiFi.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT_U.h>
// Define the number of sensors
#define NUM_SENSORS 2
// Define the pins where the sensors are connected
const int sensorPins[NUM_SENSORS] = {15, 4};
// Create an array of DHT objects
DHT dht[NUM_SENSORS] = {
DHT(sensorPins[0], DHT22),
DHT(sensorPins[1], DHT22)
};
// #define DHTPIN 15
// #define DHTTYPE DHT22
// DHT dht(DHTPIN, DHTTYPE);
#define BUZZER_PIN 13
#define SDA_PIN 21
#define SCL_PIN 22
#define BARGRAPH_NUM_LEDS 10
#define BASE_PIN_RED 23
#define BASE_PIN_GREEN 22
#define BASE_PIN_BLUE 21
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* thingSpeakServer = "api.thingspeak.com";
const String apiKey = "V84JE66O008V9UFP"; // Replace with your ThingSpeak API Key
float tempData[2];
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
Serial.println("System starting up...");
for (int i = 0; i < NUM_SENSORS; i++) {
dht[i].begin();
Serial.println("DHT22 sensor initialized.");
}
Wire.begin(SDA_PIN, SCL_PIN);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("LCD initialized.");
Serial.println("LCD initialized.");
pinMode(BUZZER_PIN, OUTPUT);
Serial.println("Buzzer initialized.");
for (int i = 0; i < BARGRAPH_NUM_LEDS; i++) {
pinMode(BASE_PIN_RED + i, OUTPUT);
pinMode(BASE_PIN_GREEN + i, OUTPUT);
pinMode(BASE_PIN_BLUE + i, OUTPUT);
}
// Connect to Wi-Fi
Serial.print("Attempting to connect to Wi-Fi network: ");
Serial.print(ssid);
WiFi.begin(ssid, password);
Serial.print("Connecting");
int attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
attempt++;
if (attempt % 10 == 0) {
Serial.print("Still trying to connect...");
}
}
Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void activateBuzzer() {
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
}
void setColor(int led, int red, int green, int blue) {
ledcWrite(led * 3, red);
ledcWrite(led * 3 + 1, green);
ledcWrite(led * 3 + 2, blue);
}
void updateLEDBarGraph(float value, float maxValue) {
int numLEDs = BARGRAPH_NUM_LEDS;
int ledsToLight = map(value, 0, maxValue, 0, numLEDs);
for (int i = 0; i < numLEDs; i++) {
if (i < ledsToLight) {
if (value < maxValue / 3) {
setColor(i, 0, 255, 0);
} else if (value < 2 * maxValue / 3) {
setColor(i, 255, 255, 0);
} else {
setColor(i, 255, 0, 0);
}
} else {
setColor(i, 0, 0, 0);
}
}
}
void loop() {
// float temperature = dht.readTemperature();
// float humidity = dht.readHumidity();
// if (isnan(temperature) || isnan(humidity)) {
// Serial.println("Error: Failed to read from DHT sensor.");
// return;
// }
for (int i = 0; i < NUM_SENSORS; i++) {
float temperature = dht[i].readTemperature();
if (isnan(temperature)) {
Serial.print("Failed to read from DHT sensor ");
Serial.println(i);
} else {
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
// Serial.print(temperature);
tempData[i] = temperature;
Serial.print(tempData[i]);
Serial.println(" *C");
}
}
delay(2000); // Wait a few seconds between readings
// Serial.print("Temperature reading: ");
// Serial.print(temperature);
// Serial.println(" °C");
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("T:");
// lcd.print(temperature);
// lcd.print("C");
// Serial.print("Humidity reading: ");
// Serial.print(humidity);
// Serial.println(" %");
// lcd.setCursor(0, 1);
// lcd.print("H:");
// lcd.print(humidity);
// lcd.print("%");
// Serial.println("Updating LED bar graph for temperature...");
// updateLEDBarGraph(temperature, 40.0);
// if (temperature > 30.0) {
// Serial.println("Temperature Alert: Exceeded 30°C");
// activateBuzzer();
// }
// if (humidity > 80.0) {
// Serial.println("Humidity Alert: Exceeded 80%");
// activateBuzzer();
// }
Serial.print("Sending data to ThingSpeak server: ");
Serial.println(thingSpeakServer);
WiFiClient client;
if (client.connect(thingSpeakServer, 80)) {
Serial.println("Connected to ThingSpeak server.");
String postStr = "api_key=" + apiKey +
"&field1=" + String(tempData[0]) +
"&field2=" + String(tempData[1]);
client.print("POST /update HTTP/1.1\r\n");
client.print("Host: " + String(thingSpeakServer) + "\r\n");
client.print("Connection: close\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: " + String(postStr.length()) + "\r\n\r\n");
client.print(postStr);
Serial.println("Data sent:");
Serial.println(postStr);
delay(2000);
Serial.println("Data transmission completed.");
} else {
Serial.println("Failed to connect to ThingSpeak server.");
}
Serial.println("Waiting for 10 seconds before next reading...");
delay(10000);
}