// ======= Dr. Omar in [email protected]=======
// Must add library before the DHT22 can work
// To add the library, click on Library Manager then click on + and select: DHT Sensor library
#include "ThingSpeak.h"
#include <WiFi.h>
#include <Wire.h>
#include <DHT.h> // Including library for dht
//========= Your WiFi Information =========
char ssid[] = "Wokwi-GUEST";// replace with your wifi SSID
char pass[] = ""; // replace with your WiFi password
//----------------------------------------------------------------------------------
int keyIndex = 0; // your network key Index number (needed only for WEP)
const char* server = "api.thingspeak.com";
#define DHTPIN 5 //pin where the dht11 is connected
DHT dht(DHTPIN, DHT22);
WiFiClient client;
//========= Your Thingspeak Channel Information =========
unsigned long myChannelNumber = 2595002; //Paste your Channel ID here
const char * myWriteAPIKey = "N7PEUB9ZTOFALVWK"; //Paste your Channel Write API Key here
//----------------------------------------------------------------------------------
float h,t,f;
#define GrnLed 19
#define RedLed 18
#define Threshold 77 // you can change this Fahrenheit temperature
void setup()
{
pinMode(GrnLed,OUTPUT);
pinMode(RedLed,OUTPUT);
Serial.begin(115200); //Initialize serial
delay(10);
dht.begin();
delay(10);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
/*
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
//Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(3000);
}
*/
//========================
WiFi.mode(WIFI_STA); // set WiFi mode to STA
WiFi.begin(ssid, pass); // connect WiFi using SSID and password
while (WiFi.status() != WL_CONNECTED) { // As long as WiFi connection is not established
delay(500); // wait half a second
Serial.print("."); // display period on serial monitor
}
//========================
Serial.println("\nConnected.");
h = dht.readHumidity();
t = dht.readTemperature();
f=(9.0/5.0)*t +32; // This is just for testing
Serial.print("temperature in C =");
Serial.print(t);
Serial.print(" , ");
Serial.print("temperature in F =");
Serial.print(f);
Serial.print(" , ");
Serial.print("Humidity in % =");
Serial.println(h);
if( f > Threshold){
digitalWrite(RedLed,HIGH);
digitalWrite(GrnLed,LOW);
}
else{
digitalWrite(RedLed,LOW);
digitalWrite(GrnLed,HIGH);
}
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8
// different pieces of information in a channel.
ThingSpeak.setField(1, t); // Send temperature in Celsius to Field 1 of Thingspeak
ThingSpeak.setField(2, f); // Send Temperature in Fahrenheit to Field 3 of Thingspeak
ThingSpeak.setField(3, h); // Send Percentage of Humidity to Field 2 of Thingspeak
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000); // Wait 15 seconds to update the channel again
}