#include <WiFi.h>
#include <Wire.h>
#include <DHT.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Define pin numbers
#define DHTPIN 4
#define DHTTYPE DHT22
#define FAN_IN_PIN 12
#define FAN_OUT_PIN 13
#define RELAY_PIN 14
#define MQ135_PIN 34
#define DSM501A_PIN 35
#define DS18B20_PIN 5
#define SOLAR_PANEL_PIN 36
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize DS18B20 sensor
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// WiFi settings
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Server URL
const char* serverName = "http://your-server-ip/save_data.php";
// Initialize WiFi client
WiFiClient client;
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize DS18B20 sensor
sensors.begin();
// Initialize pins
pinMode(FAN_IN_PIN, OUTPUT);
pinMode(FAN_OUT_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(MQ135_PIN, INPUT);
pinMode(DSM501A_PIN, INPUT);
pinMode(SOLAR_PANEL_PIN, INPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
// Read sensor values
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
sensors.requestTemperatures();
float outside_temp = sensors.getTempCByIndex(0); // DS18B20 sensor
float co2 = analogRead(MQ135_PIN); // Raw value, consider calibration
float pm25 = getPM25(); // Implement this function based on DSM501A sensor
float solar_voltage = analogRead(SOLAR_PANEL_PIN); // Read solar panel voltage
String sunlight_condition = mapSolarVoltageToCondition(solar_voltage);
// Print values to Serial Monitor
Serial.print("Temperature: "); Serial.println(temperature);
Serial.print("Humidity: "); Serial.println(humidity);
Serial.print("Outside Temp: "); Serial.println(outside_temp);
Serial.print("CO2: "); Serial.println(co2);
Serial.print("PM2.5: "); Serial.println(pm25);
Serial.print("Solar Voltage: "); Serial.println(solar_voltage);
Serial.print("Sunlight Condition: "); Serial.println(sunlight_condition);
// Send data to server
if (client.connect(serverName, 80)) {
String postData = "temperature=" + String(temperature) +
"&humidity=" + String(humidity) +
"&outside_temp=" + String(outside_temp) +
"&co2=" + String(co2) +
"&pm25=" + String(pm25) +
"&solar_voltage=" + String(solar_voltage) +
"&sunlight_condition=" + sunlight_condition;
client.println("POST /save_data.php HTTP/1.1");
client.println("Host: your-server-ip");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: " + String(postData.length()));
client.println();
client.println(postData);
delay(1000);
}
// Control fans and relay based on conditions
controlFans(temperature, humidity, co2, pm25);
// Delay between readings
delay(60000); // 1 minute
}
void controlFans(float temperature, float humidity, float co2, float pm25) {
// Example control logic
if (temperature > 29) {
analogWrite(FAN_IN_PIN, map(temperature, 29, 31, 50, 255));
analogWrite(FAN_OUT_PIN, map(temperature, 29, 31, 50, 255));
if (temperature > 31) {
digitalWrite(RELAY_PIN, HIGH); // Turn on exhaust fan
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off exhaust fan
}
} else {
analogWrite(FAN_IN_PIN, 0);
analogWrite(FAN_OUT_PIN, 0);
digitalWrite(RELAY_PIN, LOW);
}
}
float getPM25() {
// Implement DSM501A sensor reading code here
return 10.0; // Placeholder value
}
String mapSolarVoltageToCondition(float voltage) {
// Map voltage values to sunlight conditions
if (voltage < 1.0) {
return "Night";
} else if (voltage < 2.0) {
return "Dawn/Dusk";
} else if (voltage < 3.0) {
return "Rainy";
} else if (voltage < 4.0) {
return "Cloudy";
} else {
return "Full Sun";
}
}