/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <WiFi.h>
#include "ThingSpeak.h"
// set the LCD number of columns and rows
int lcdColumns = 20;
int lcdRows = 4;
const char * ssid = "Samiullah";
const char * password = "suotal1142";
WiFiClient client;
unsigned long myChannelNumber = 1933108;
const char * myWriteAPIKey = "PDGLGF3W0NW5O3JQ";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ldrPin A0 // phototrans pin connected to A1
DHT dht(DHTPIN, DHTTYPE);
int led = 13; // the pin that the LED is atteched to
int sensor = 5; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0;
int threshold = 100; // LDR threshold value
float temp;
float hum;
float ambi_temp = 30;
float lightValue = 0.0; // Store the ldr analog value
int ldrActuator = 36;
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(ldrPin, INPUT);
analogReadResolution(10);
dht.begin();
lcd.begin(16,2);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}
void loop(){
lcd.setCursor(0, 0);
Temp_sensor();
Motion_sensor();
Ldr_sensor();
// set cursor to first column, first row
// print message
delay(1000);
// clears the display to print new message
//lcd.clear();
// set cursor to first column, second row
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
// Get a new temperature reading
// temperatureC = bme.readTemperature();
temperatureC = 35;
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);
//uncomment if you want to get temperature in Fahrenheit
/*temperatureF = 1.8 * bme.readTemperature() + 32;
Serial.print("Temperature (ºC): ");
Serial.println(temperatureF);*/
// 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. Here, we write to field 1.
int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureC, myWriteAPIKey);
//uncomment if you want to get temperature in Fahrenheit
//int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
// delay(1000);
//lcd.clear();
}
void Temp_sensor(void){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
hum = dht.readHumidity();
Serial.print("humidity : ");
Serial.println(hum);
// Read temperature as Celsius (the default)
temp = dht.readTemperature();
Serial.print("Temperature : ");
Serial.println(temp);
lcd.print("temp:");
lcd.print(temp);
lcd.print("hum:");
lcd.print(hum);
// Read temperature as Fahrenheit (isFahrenheit = true)
if (temp > ambi_temp){
// turn on the fan
}
else{
//turn off the fan
}
}
void Motion_sensor(void){
lcd.setCursor(0,1);
val = digitalRead(sensor); // read sensor value
Serial.print("Motion Sensor : ");
Serial.println(val);
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(500); // delay 100 milliseconds
if (state == LOW) {
lcd.print("Motion detected!");
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(500); // delay 200 milliseconds
if (state == HIGH){
lcd.print("Motion stopped!");
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
void Ldr_sensor(void){
int LDR_Reading = analogRead(ldrPin);
Serial.println(LDR_Reading);
float LDR_Voltage = ((float)LDR_Reading*3.3/1023);
Serial.println(LDR_Voltage);
if(LDR_Reading<threshold){
digitalWrite(18, HIGH);}
else
digitalWrite(18, LOW);}