#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <random>
const int DHT_PIN = 15; // DHT22 sensor GPIO Pin
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFI Password
const int myChannelNumber = 2485437; // ThingSpeak channel number
const char* myApiKey = "Y1AW649A4WULA9NU"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
// NTC sensor and LED setup
const int NTC_PIN = 32; // ADC pin connected to the NTC sensor
const int LED_PIN = 2; // GPIO pin for the LED
const float beta = 3950.0; // Beta coefficient
const float seriesResistor = 10000.0; // Series resistor value
const float thermistorNominal = 10000.0; // Resistance at 25 degrees C
const float temperatureNominal = 25.0; // Nominal temperature
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// 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();
// Read from the NTC sensor
int analogValue = analogRead(NTC_PIN);
float resistance = (4095.0 / analogValue) - 1;
resistance = seriesResistor / resistance;
float steinhart = resistance / thermistorNominal; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= beta; // 1/B * ln(R/Ro)
steinhart += 1.0 / (temperatureNominal + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert to Celsiu
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
ThingSpeak.setField(3, steinhart); // NTC sensor temperature
int status = ThingSpeak.writeFields(myChannelNumber, myApiKey);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("NTC Temp: " + String(steinhart, 2) + "°C");
Serial.println(steinhart);
if (steinhart > 20) {
digitalWrite(LED_PIN, HIGH);
Serial.println("High temperature detected! LED on.");
} else {
digitalWrite(LED_PIN, LOW);
}
if(status == 200){
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error" + String(status));
}
Serial.println("---");
delay(1000);
}