#include "DHTesp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ThingSpeak.h"
const int DHT_PIN = 15;
const int LDR_PIN = 32;
const int LED_PIN = 27;
const int BUZZER_PIN = 26;
const float GAMMA = 0.7;
const float RL10 = 50;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
String getLight(float lux) {
if(lux < 1) {
return "Completely Dark";
} else if (lux < 1000) {
return "Normal";
} else if (lux < 5000) {
return "May Be Fire";
} else if (lux < 10000) {
return "Fire!!!";
} else if (lux < 100000) {
return "Massive Fire!!!";
}
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temprature = data.temperature;
float humidity = data.humidity;
float analogValue = analogRead(LDR_PIN) / 4;
Serial.println(analogValue);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("lux: " + String(lux,0) + "LM");
Serial.println(getLight(lux));
Serial.println("---");
if(temprature > 60 && humidity > 55 && lux > 1500) {
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 100, 1000);
delay(2000);
digitalWrite(LED_PIN, LOW);
} else {
tone(BUZZER_PIN, 0);
digitalWrite(LED_PIN, LOW);
}
ThingSpeak.setField(1, humidity);
ThingSpeak.setField(2, temprature);
ThingSpeak.setField(3, lux);
delay(1000);
}