#include "ThingSpeak.h"
#include <esp_now.h>
#include <WiFi.h>
#include "DHT.h"
#include "Adafruit_Sensor.h"
#define DHTPIN 27
#define DHTTYPE DHT11
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
float Humidity ;
float Temperature;
float Heat_Index;
unsigned long myChannelNumber = 997460;
const char* myWriteAPIKey = "ENQRFRN7I1Z2LNUW";
// Structure example to receive data
// Must match the sender structure
// Create a struct_message called myData
// callback function that will be executed when data is received
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
dht.begin();
ThingSpeak.begin(client);
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);// minimum 5 seconds required for connection
}
Serial.println("\nConnected.");
}
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
ThingSpeak.setField(1, h);
ThingSpeak.setField(2, t);
ThingSpeak.setField(3, f);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
Serial.print("Humidity = ");
Serial.println(h);
Serial.print("Temperature = ");
Serial.println(t);
Serial.print("Heat Index = ");
Serial.println(f);
//delay(1000);
}
else{
//Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(2000);
}