/*
* code for LAB_3
* 17/11/2023
* by Farhan
* 21DDT22F1235
*/
//libarys
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <ThingSpeak.h>
//setting for dht 22 pin
#define DHTPIN 26
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//setting lcd i2c 16*22
LiquidCrystal_I2C lcd(0x27, 16, 2);
//ntp setting
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 28800, 60000);
//wifi setting wokwi only for real device please change your ssid and password
WiFiClient client;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
//thingspeak config
String apiWritekey= "MABLDIAICZM22502"; // Api thingspeek write key
const char* server = "api.thingspeak.com";//server api thingspeak
const int myChannelNumber = 2347791;// channel number
//time to updated thingspeak
const int Second = 30;
const int Second_2 = 10;
const int Second_3 = 40;
const int Second_4 = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//wifi enable connection
WiFi.disconnect();
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//WiFi.begin(ssid, password);
//check wifi status/connection
while (WiFi.status() != WL_CONNECTED) {
delay(5);
Serial.print(".");
}
Serial.println("");
Serial.print(" connected to wifi...");
Serial.println(ssid);
Serial.println();
//declare wifi static mode
WiFi.mode(WIFI_STA);
//start client mode thingspeak
ThingSpeak.begin(client);
//configure lcd
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("lab 3 v1");
lcd.setCursor(0,1);
lcd.print("By Nasr_2023");
delay(1);
lcd.clear();
}
void loop() {
//read dht22
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
//ntp update reach server
timeClient.update();
Serial.println(timeClient.getFormattedTime());
//display time and values dht 22 on lcd
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(timeClient.getFormattedTime());
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
lcd.setCursor(0,1);
lcd.print(F("Hum:"));
lcd.print(humidity);
lcd.print(F("% Temp:"));
lcd.print(temperature);
lcd.print((char)223);
lcd.print(F("C"));
delay(10); // this speeds up the simulation
//read time for update
if(timeClient.isTimeSet()) {
if (Second == timeClient.getSeconds()) {
if (client.connect(server,80))
{ //wirte data on cloud
String tsData = apiWritekey;
tsData +="&field1=";
tsData += String(temperature);
tsData +="&field2=";
tsData += String(humidity);
tsData += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n"); // the 2 carriage returns indicate closing of Header fields & starting of data
client.print(tsData);
delay (1);
Serial.println(" Serial Write ");
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)humidity);
Serial.println(" H");
}
}
}
}