//THINGSPEAK ----------
// channel iD:2394318
//channel APIKEY: A0UA3XKB4L1I0B7G
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const char * WIFI_NAME = "Wokwi-GUEST";
const char * WIFI_PASSWORD = "";
const int myChannelNumber = 2394318;
const char * myApiKey = "A0UA3XKB4L1I0B7G";
const char * server = "api.thingspeak.com";
//Flame Sensor
const int flamePin = 33; //LDR analog output connected to analog pin 33
int flameValue = 0;
// INPUT: Potentiometer
//Barometer BMP180
const int potPin = 34; // Potentiometer output connected to analog pin 34
int barometer = 0; // Variable to store the input from the potentiometer
//MQ-135 Air Quality
const int potPin1 = 35; // Potentiometer output connected to analog pin 35
int airQuality = 0; // Variable to store the input from the potentiometer
const int DHT_PIN = 15;
const int RED_PIN = 13;
const int GREEN_PIN = 12;
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(1152000);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float humidity = data.humidity;
float temperature = data.temperature;
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F(" Temperature: "));
Serial.print(temperature);
Serial.print(F("°C "));
barometer = analogRead(potPin); // read the potentiometer value at the input pin
Serial.print(F("Atmospheric Pressure: "));
barometer = map(barometer, 0, 4095, 870, 1013);
Serial.println(barometer); //milibars , 980 is already sign of typhoon/cyclone
airQuality = analogRead(potPin1); // read the potentiometer value at the input pin
Serial.print(F("Air Quality: "));
airQuality = map(airQuality, 0, 4095, 150, 1200);
Serial.println(airQuality);
// Flame Sensor
// read the value from the sensor
flameValue = analogRead(flamePin);
// map the sensor reading to a range for the LED
flameValue = map(flameValue, 4058, 32, 0, 100);
if (flameValue < 60 || airQuality < 1000) {
Serial.print(F("Safe - Flame Sensor "));
digitalWrite(RED_PIN, LOW);
Serial.println(flameValue);
} else {
Serial.print(F("FIRE DANGER - Flame Sensor: "));
digitalWrite(RED_PIN, HIGH);
Serial.println(flameValue);
}
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, barometer);
ThingSpeak.setField(4, flameValue);
ThingSpeak.setField(5, airQuality);
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
if (x == 200) {
Serial.println("Data pushed successfully");
digitalWrite(GREEN_PIN, HIGH);
} else {
Serial.println("Push error" + String(x));
digitalWrite(GREEN_PIN, LOW);
}
Serial.println("---");
delay(5000);
}