#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h> //lcd
#include <WiFi.h>
#include <Wire.h>
#include <DHTesp.h>
#define dht22_pin 27
#define trigPin 26
#define echoPin 25
#define Rled 13
#define Yled 12
#define Gled 14
#define buzzer 15
#define LDR_PIN 36
DHTesp dht;
String tem = "", hum = "";
TempAndHumidity dhtData;
LiquidCrystal_I2C lcd(0x27,16,2); //set the LCD address to 0x3F for a 16 chars and 2-line display
const char* server = "api.thingspeak.com";
WiFiClient client;
//========= Your WiFi Information =========
char ssid[] = "Wokwi-GUEST";// Wokwi wifi SSID
char pass[] = ""; // Open access point. No WiFi password is needed
//----------------------------------------------------
//========= Your Thingspeak Channel Information =========
unsigned long myChannelNumber = 2163594; //Paste your Channel ID here
const char * myWriteAPIKey ="QMRIFXJJPOVRNFG1"; //Paste your Channel Write API Key here
//--------------------------------------------------
void setup() {
// ===============DHT22 start =============
dht.setup(dht22_pin, DHTesp::DHT22);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0); // column#4 and Row #1
lcd.print("==IoT Project==");
Serial.begin(9600);
delay(10);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.println("Contest project");
Serial.println("Name: Dr> Omar "); //replace xxxxx with your name
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Rled, OUTPUT);
pinMode(Yled, OUTPUT);
pinMode(Gled, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(LDR_PIN, INPUT);
}
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.
Serial.print(".");
delay(3000);
}
Serial.println("\nConnected.");
}
// ==================LDR start ===========
int value=analogRead(LDR_PIN);
Serial.print("value= ");
Serial.println(value);
if (value >1300 ){
digitalWrite(Yled,HIGH);
} else
{ digitalWrite(Yled,LOW);}
//====================== connected to Wifi ===
dhtData = dht.getTempAndHumidity();
tem = String(dhtData.temperature, 2);
hum = String(dhtData.humidity, 2);
float temperature = dhtData.temperature;
float humidity = dhtData.humidity;
Serial.print("temp = ");
Serial.print(temperature);
Serial.print(" Hum = ");
Serial.println(humidity);
//===========================
long duration, distance, inches;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo signal
duration = pulseIn(echoPin, HIGH); // Read duration for roundtrip distance
distance = (duration /2) * 0.0135 ; //Convert duration to one way distance in units of inches
distance = 2.54 * distance; // distance in cm
//=============
lcd.setCursor(0,1); // set the cursor to column 1, line 2
// lcd.clear(); // clears the display to print new message
lcd.print(" ");
lcd.setCursor(0,1); // set the cursor to column 1, line 2
lcd.print("dist = ");
lcd.print(distance); // Walk characters to the LCD.
lcd.print(" cm");
if (distance <=40) { // Outer IF statement units of inches
if (distance <=20){ // Alert range condition
digitalWrite(Rled, HIGH); // Alert green LED on
// digitalWrite(Yled, LOW);
digitalWrite(Gled, LOW);
}
if (distance <40 and distance > 20){ // Warning range condition
digitalWrite(Rled, LOW);
// digitalWrite(Yled, HIGH); // Warning yellow LED on
digitalWrite(Gled, LOW);
}
//==================== Beeping Rate Code Start ======
tone(buzzer,500);
for (int i= distance; i>0; i--)
delay(10);
noTone(buzzer);
for (int i= distance; i>0; i--)
delay(10);
//==================== Beeping Rate Code End =======
}
else{ //Safe range condition
digitalWrite(Rled, LOW);
// digitalWrite(Yled, LOW);
digitalWrite(Gled, HIGH); // Safe distance green LED on
noTone(buzzer);
}// end of outer IF statement
// ===========send data to ThingSpeak ======
// 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, distance); // Distance
ThingSpeak.setField(2, temperature); // Send Temperature in Fahrenheit to Field 3 of Thingspeak
ThingSpeak.setField(3, humidity); // 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
/****if (distance < 156) // Filter noise to show readings only less than the sensor range of 13 ft = 156 inches
Serial.println(distance); // print distance to show in Serial Monitor
delay(100); //pause program to stabilize ultrasonic sensor readings
***********/
} //end of loop