//BLYNK
//#define BLYNK_TEMPLATE_ID "TMPLs-LMQQxq"
//#define BLYNK_DEVICE_NAME "ESP32 TB"
//#define BLYNK_AUTH_TOKEN "sU1Qec8xJ1YMgppksf_wPtslMUtIL1JI"
//#define BLYNK_PRINT Serial
//#define APP_DEBUG
//#include <BlynkSimpleEsp32.h>
//char auth[] = BLYNK_AUTH_TOKEN;
//char ssid[] = "";
//char pass[] = "";
#include <WiFi.h>
#include <WiFiClient.h>
int blynkswmode,blynkswup,blynkswdown,blynkshowruntime;
int showtime;
//bool state_heater;
//BLYNK_WRITE(V19)
//{
//blynkswmode = param.asInt();
//}
//BLYNK_WRITE(V18)
//{
//blynkswup = param.asInt();
//}
//BLYNK_WRITE(V5)
//{
//blynkswdown = param.asInt();
// }
//BLYNK_WRITE(V13)
//{
// blynkshowruntime = param.asInt();
//}
//เรียกใช้ไลบราลี่
#include <LiquidCrystal_I2C.h>
#include <DHTesp.h>
#define DHTTYPE DHT22
#include <Ticker.h>
#include <EEPROM.h>
#define EEPROM_SIZE 12
const int DHT_PIN = 23; //กำหนดขาเซนเซอร์วัดอุณหภูมิ
const int Heater = 4; //กำหนดขาหลอดไฟ
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27,16,2); //กำหนด Address,จำนวนบรรทัดและอักษร
int value,i;
int potValue = 0; //การกำหนดตัวแปร I แบบจำนวนเต็ม
int swMode,swUp,swDown; //การกำหนดตัวแปรสวิตซ์
int mode;
int showminn;
int x,f,temp;
int tempset,timeset,timer = 0;
int tempRead,outputValue,SetT,t = 0; //การกำหนดตัวแปร แบบจำนวนเต็ม
Ticker count_time1;
float h = 0,minn = 0,sec = 0,y = 0;
void Time(){
y++;
if(y >= 10){
sec++;
timer++;
y = 0;
}
if(sec >= 60){
minn++;
sec = 0;
}
if(minn >= 60){
h++;
minn = 0;
}
}
void setup(){
Serial.begin(115200);
dhtSensor.setup(DHT_PIN,DHTesp::DHT22);
pinMode(5,INPUT_PULLUP); //sw down
pinMode(18,INPUT_PULLUP); //sw up
pinMode(19,INPUT_PULLUP); //sw mode
pinMode(15,OUTPUT); //หลอดไฟ
Wire.begin(21,22);
lcd.init(); //เรียกหน้าจอ
lcd.backlight(); //เปิดแบล็คไลท์
lcd.setCursor(1,0);
lcd.print("Loading.. : %");
for(i=0;i<100;i++){ //หน่วงเวลาแบบนับเลข
lcd.setCursor(13,0); //ตำแหน่งแสดงตัวเลข
lcd.print(i); //แสดงค่าตัวเลข
delay (80);
}
//lcd.clear();
mode = 1;
x = 0;
if(t == 0)
noInterrupts();
EEPROM.begin(EEPROM_SIZE); //ดึงค่าจาก EEPROM
tempset = EEPROM.read(0); //ดึงค่าอุณหภูมิที่Address 0
timeset = EEPROM.read(3); //ดึงค่าเวลาที่Address 3
if(tempset > 80)
tempset = 50;
temp = tempset;
if(timeset > 254)
timeset = 0;
t = timeset;
Serial.print("TEMP SET = ");
Serial.println(tempset);
Serial.print("TIME SET = ");
Serial.println(timeset);
//Blynk.begin(auth,ssid,pass);
}
void loop(){
//Blynk.virtualWrite(V1,tempRead);
//Blynk.virtualWrite(V2,tempset);
//Blynk.virtualWrite(V3,timeset/60);
//Blynk.virtualWrite(V13,minn,sec);
//Blynk.run();
swMode = digitalRead(19);
swUp = digitalRead(18);
swDown = digitalRead(5);
if(swMode ==0 || blynkswmode == 1){
mode ++;
digitalWrite(Heater,LOW);
lcd.clear();
}
if(mode%3 == 1){
settemp();
}
else if(mode%3 == 2){
settime();
}
else if((mode%3 == 0 && timer < t) || (mode%3 == 0 && t == 0)){
runmode();
}
if(timer >= t && timer != 0){
if(x == 0){
//lcd.clear();
x = 1;
lcd.setCursor(0,0);
lcd.print(" END OF TIME ");
lcd.setCursor(0,1);
lcd.print(" TIME = ");
showtime = timer/60;
lcd.print(showtime);
lcd.print(" Min");
}
digitalWrite(Heater,LOW);
noInterrupts();
}
if(y == 0){
Serial.print(h); Serial.print(":");
Serial.print(minn); Serial.print(":");
Serial.print(sec); Serial.print(":");
Serial.print(i); Serial.print(":");
Serial.println();
tempset = EEPROM.read(0);
timeset = EEPROM.read(3);
Serial.print("TEMP SET = ");
Serial.println(tempset);
Serial.print(" C");
Serial.print("TIME SET = ");
Serial.println(timeset);
Serial.print(" Min");
}
}
void settime(){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("SET TIMEMODE");
lcd.setCursor(0,1);
lcd.print("TIMESET = "+String(t/60)+" Min");
//Blynk.run();
swUp = digitalRead(18);
swDown = digitalRead(5);
if(swUp == 0 || blynkswup == 1){
t++;
t = t+59;
showminn = t/60;
}
if(swDown == 0 || blynkswdown == 1){
t--;
t = t-59;
showminn = t/60;
if(t < 0)
t = 0;
}
timer = 0;
x = 0;
digitalWrite(Heater,LOW);
timeset = t;
EEPROM.write(3,timeset);
EEPROM.commit();
}
void settemp(){
lcd.setCursor(0,0);
lcd.print("SET TEMPMODE");
lcd.setCursor(0,1);
lcd.print("TEMPSET = "+String(temp)+" C");
//Blynk.run();
swUp = digitalRead(18);
swDown = digitalRead(5);
if(swUp == 0){
temp++;
if(temp > 80)
temp = 80;
}
if(swDown == 0){
temp--;
if(temp < 30)
t = 30;
}
f = 0;
tempset = temp;
EEPROM.write(0,timeset);
EEPROM.commit();
}
void runmode(){
//lcd.clear();
//Blynk.run();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
tempRead = data.temperature;
lcd.setCursor(0, 0);
lcd.print("READ="+ String(data.temperature,0)+" Set="+String(outputValue,0));
lcd.setCursor(0, 1);
lcd.print("TIME="+ String(h,0)+":"+ String(minn,0)+":"+ String(sec,0));
if(t == 0){
noInterrupts();
}
else{
if(f == 0){
h = 0;
minn = 0;
sec = 0;
y = 0;
timer = 0;
interrupts();
count_time1.attach(0.1,Time); //interrupts every 0.1 sec
f = 1;
}
}
if(outputValue > tempRead){
digitalWrite(Heater,HIGH);
}
else{
digitalWrite(Heater,LOW);
}
}