คือผมต้องการใช้ Time Input 2 ตัว ครับ เพื่อตั้งเวลารดน้ำ 2 เวลา คือช่วงเช้าและเย็น
1 ตัวสามารถทำได้ครับ แต่พอทำตัวที่ 2 มีปัญหาคือ พอเวลาของ TimeInput ตัวแรกถึงเวลาที่ตั้ง มันทำให้เอาต์พุตกระพริบครับเหมือนมันรวนๆ แต่พอตัวที่ 2 ถึงเวลาที่ตั้งไว้กลับไม่กระพริบครับ
ปล.TimeInput ทั้งสองผมตั้ง 6 วินาทีให้หยุดครับ
/*
GPIO5 D1 LED
GPIO14 D5 TMP
*/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <EEPROM.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
#define EEPROM_STATE_ADDRESS_1 128
#define EEPROM_STATE_ADDRESS_2 144
#define EEPROM_STATE_ADDRESS_3 232
#define EEPROM_STATE_ADDRESS_4 320
#define DHTPIN 0 //D3
#define DHTTYPE DHT22
#define relayLed 14 //D5
#define relayp2 12 //D6
SimpleTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V3);
WidgetLED led1(V4);
WidgetLED led2(V5);
WidgetLED led3(V13);
WidgetLED led4(V14);
WidgetLED led5(V15);
DHT dht(DHTPIN, DHTTYPE);
char auth[] = "54eddd921a1b4068a1766cf1c5a2a33d";
char Date[16];
char Time[16];
long startsecondswd;
long stopsecondswd;
long nowseconds;
long startsecondswd2;
long stopsecondswd2;
long nowseconds2;
bool isFirstConnect = true;
int manual = 0;
int oldstatus;
int automode;
char ledstate = 0;
char schestate = 0;
bool check = true;
bool check2 = true;
bool check3 = true;
int state = 0;
int manual2 = 0;
int oldstatus2;
int automode2;
char ledstate2 = 0;
char schestate2 = 0;
bool check4 = true;
bool check5 = true;
bool check6 = true;
int state2 = 0;
int tab;
int value;
int value2;
void clockDisplay()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " / " + month() + " / " + year();
Serial.print("Current time: ");
Serial.print(currentTime);
Serial.print(" ");
Serial.print(currentDate);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V7, currentTime);
// Send date to the App
Blynk.virtualWrite(V6, currentDate);
}
void setup(){
Wire.begin(D2,D1);
rtc.begin();
dht.begin();
ads.begin();
ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
Serial.begin(115200);
Serial.println("Starting");
pinMode(relayLed, OUTPUT);
pinMode(relayp2, OUTPUT);
digitalWrite(relayLed, HIGH); // set LED OFF
digitalWrite(relayp2,HIGH);
EEPROM.begin(512);
ledstate = EEPROM.read(EEPROM_STATE_ADDRESS_1) == 1 ? 1 : 0;
schestate = EEPROM.read(EEPROM_STATE_ADDRESS_2) == 1 ? 1 : 0;
ledstate2 = EEPROM.read(EEPROM_STATE_ADDRESS_3) == 1 ? 1 : 0;
schestate2 = EEPROM.read(EEPROM_STATE_ADDRESS_4) == 1 ? 1 : 0;
ledonstart();
ledonstart2();
WiFiManager wifiManager;
//wifiManager.resetSettings(); //Uncomment this to wipe WiFi settings from EEPROM on boot. Comment out and recompile/upload after 1 boot cycle.
wifiManager.autoConnect("NodeMCU4.0");
Serial.println("connected...yeey :)");
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output, I like blinkies.
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
setSyncInterval(1 * 60);
timer.setInterval(1000L, activetoday); // check every 10 SECONDS if schedule should run today
timer.setInterval(1000L, check_sche);
timer.setInterval(1000L, check_sche2);
timer.setInterval(5000L, sendUptimedht);
timer.setInterval(1000L, clockDisplay);
timer.setInterval(5000L, sendUptime);
timer.setInterval(2000L, checktab);
//timer.setInterval(1000L, checkvalue);
}
void loop() {
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
}
void sendUptime() {
int Moisture;
int16_t Mois;
float voltm = 0.0;
Mois = ads.readADC_SingleEnded(0);
Moisture = map(Mois, 0, 20000, 0, 100);
Serial.println("-------------------------------------------------------------");
Serial.print("Moisture = ");
Serial.print(Moisture);
Serial.print("%");
Serial.print(" ");
Serial.println(Mois);
voltm = (Mois * 0.125)/1000;
Serial.print("volt = ");
Serial.println(voltm);
Blynk.virtualWrite(V9,Moisture);
Serial.println("-------------------------------------------------------------");
int16_t PH;
int ph;
float voltp = 0.0;
PH = ads.readADC_SingleEnded(1);
ph = map(PH, 0, 10000, 8, 3.5);
Serial.print("PH = ");
Serial.print(ph);
Serial.print(" ");
Serial.println(PH);
voltp = (PH * 0.125)/1000;
Serial.print("volt = ");
Serial.println(voltp);
Blynk.virtualWrite(V8,ph);
Serial.println("-------------------------------------------------------------");
int Moisture2;
int16_t Mois2;
Mois2 = ads.readADC_SingleEnded(2);
Moisture2 = map(Mois2, 0, 23000, 100, 0);
Serial.println("-------------------------------------------------------------");
Serial.print("Moisture2 = ");
Serial.print(Moisture2);
Serial.print("%");
Serial.print(" ");
Serial.println(Mois2);
Blynk.virtualWrite(V18,Moisture2);
Serial.println("-------------------------------------------------------------");
int Moisture3;
int16_t Mois3;
Mois3 = ads.readADC_SingleEnded(3);
Moisture3 = map(Mois3, 0, 27000, 100, 0);
Serial.println("-------------------------------------------------------------");
Serial.print("Moisture3 = ");
Serial.print(Moisture3);
Serial.print("%");
Serial.print(" ");
Serial.println(Mois3);
Blynk.virtualWrite(V19,Moisture3);
Serial.println("-------------------------------------------------------------");
float moisave;
moisave = (Moisture + Moisture2 + Moisture3)/3;
Serial.print("MoistureAve = ");
Serial.print(moisave);
Serial.print("%");
Serial.println(" ");
Blynk.virtualWrite(V20,moisave);
}
void sendUptimedht() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)){
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" Humidity: ");
Serial.println(h);
Blynk.virtualWrite(V0,t);
Blynk.virtualWrite(V1,h);
}
void checktab(){
float t = dht.readTemperature();
if(tab == 0){
digitalWrite(relayp2,1);
led3.off();
}
else if(tab <= t){
digitalWrite(relayp2,0);
led3.on();
}
else{
digitalWrite(relayp2,1);
led3.off();
}
Serial.print("tabcheck = ");
Serial.println(tab);
}
BLYNK_WRITE(V12) { //text input from blynk
tab = param.asInt();
checktab();
Serial.println(tab);
}
void ledonstart() {
if (ledstate == 1) {
digitalWrite(relayLed, 0);
Blynk.virtualWrite(V2, 1);
led4.on();
}
else {
digitalWrite(relayLed, 1);
Blynk.virtualWrite(V2, 0);
led4.off();
}
if (schestate == 1) {
Blynk.virtualWrite(V11, 1);
led1.on();
led2.off();
led4.on();
if ((schestate == 1) && (check3 == true)) {
timer.setTimeout(50, resetTerminal);
timer.setTimeout(50, checklastbuttonpressed);
automode = 1;
schestate = 1;
check3 = false;
}
}
else {
Blynk.virtualWrite(V11, 0);
led1.off();
led2.on();
led4.off();
}
}
void ledonstart2() {
if (ledstate2 == 1) {
digitalWrite(relayLed, 0);
Blynk.virtualWrite(V2, 1);
led4.on();
}
else {
digitalWrite(relayLed, 1);
Blynk.virtualWrite(V2, 0);
led4.off();
}
if (schestate2 == 1) {
Blynk.virtualWrite(V17, 1);
led5.on();
if ((schestate2 == 1) && (check6 == true)) {
timer.setTimeout(50, checklastbuttonpressed2);
automode2 = 1;
schestate2 = 1;
check6 = false;
}
}
else {
Blynk.virtualWrite(V17, 0);
led4.off();
led5.off();
}
}
void check_status() {
if (digitalRead(relayLed) == LOW) {
ledstate = 1;
if (check == true) {
EEPROM.write(EEPROM_STATE_ADDRESS_1, ledstate);
EEPROM.commit();
Serial.println("EEPROM Write ledstate ON 1");
}
check = false;
}
else {
ledstate = 0;
if (check == false) {
EEPROM.write(EEPROM_STATE_ADDRESS_1, ledstate);
EEPROM.commit();
Serial.println("EEPROM Write ledstate OFF 1");
}
check = true;
}
}
void check_status2() {
if (digitalRead(relayLed) == LOW) {
ledstate2 = 1;
if (check4 == true) {
EEPROM.write(EEPROM_STATE_ADDRESS_3, ledstate2);
EEPROM.commit();
Serial.println("EEPROM Write ledstate ON 2");
}
check4 = false;
}
else {
ledstate2 = 0;
if (check4 == false) {
EEPROM.write(EEPROM_STATE_ADDRESS_3, ledstate2);
EEPROM.commit();
Serial.println("EEPROM Write ledstate OFF 2");
}
check4 = true;
}
}
void check_sche() {
if (automode == 1) {
schestate = 1;
if (check2 == true) {
EEPROM.write(EEPROM_STATE_ADDRESS_2, schestate);
EEPROM.commit();
Serial.println("EEPROM Write schestate ON 1");
led1.on();
led2.off();
}
check2 = false;
}
else {
schestate = 0;
if (check2 == false) {
EEPROM.write(EEPROM_STATE_ADDRESS_2, schestate);
EEPROM.commit();
Serial.println("EEPROM Write schestate OFF 1");
led1.off();
led2.on();
}
check2 = true;
}
}
void check_sche2() {
if (automode2 == 1) {
schestate2 = 1;
if (check5 == true) {
EEPROM.write(EEPROM_STATE_ADDRESS_4, schestate2);
EEPROM.commit();
Serial.println("EEPROM Write schestate ON 2");
led2.off();
led5.on();
}
check5 = false;
}
else {
schestate2= 0;
if (check5 == false) {
EEPROM.write(EEPROM_STATE_ADDRESS_4, schestate2);
EEPROM.commit();
Serial.println("EEPROM Write schestate OFF 2");
led2.on();
led5.off();
}
check5 = true;
}
}
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
Blynk.notify("TIMER STARTING!!!!");
isFirstConnect = false;
}
}
void activetoday() { // check if schedule should run today
if (year() != 1970) {
if (automode == 1) {
Blynk.syncVirtual(V10); // sync timeinput widget
}
if (automode2 == 1){
Blynk.syncVirtual(V16);
}
}
}
void checklastbuttonpressed () {
if (automode == 1) {
oldstatus = 1; //5
} else {
oldstatus = 2; //6
}
}
void checklastbuttonpressed2 () {
if (automode2 == 1) {
oldstatus = 3; //7
} else {
oldstatus = 4; //8
}
}
void restorelastbuttonpressed () {
if (oldstatus == 1) {
automode = 1;
Blynk.virtualWrite(V11, 1);
} else {
automode = 0;
Blynk.virtualWrite(V11, 0);
}
}
void restorelastbuttonpressed2 () {
if (oldstatus2 == 3) {
automode2 = 1;
Blynk.virtualWrite(V17, 1);
} else {
automode2 = 0;
Blynk.virtualWrite(V17, 0);
}
}
void resetTerminal()
{
terminal.println();
terminal.println();
terminal.println();
terminal.println("AUTO Mode has been selected");
terminal.println("Wait for update (10 seconds as maximum)");
terminal.println();
terminal.println();
terminal.println();
terminal.flush();
}
BLYNK_WRITE(V2) // ON-OFF Manual
{
if (param.asInt() == 1) { // boton encendido
terminal.println();
terminal.println();
terminal.println();
terminal.println("Manual MODE is ON");
terminal.println("Press ON/OFF button if required");
terminal.println("Device is ON");
terminal.println();
terminal.println();
terminal.flush();
if (manual == 0) { //está en modo automático
checklastbuttonpressed ();
checklastbuttonpressed2 ();
manual = 1;
automode = 0;
automode2 = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V17, 0);
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1); //Turn ON Button Widget
led4.on();
}
else { //está en modo manual
automode = 0;
automode2 = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V17, 0);
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1); //Turn ON Button Widget
led4.on();
}
} else {
terminal.println();
terminal.println();
terminal.println();
terminal.println("Manual MODE is ON");
terminal.println("Press ON/OFF button if required");
terminal.println("Device is OFF");
terminal.println();
terminal.println();
terminal.flush();
if (manual == 0) { //modo automático
checklastbuttonpressed ();
checklastbuttonpressed2 ();
manual = 1;
automode = 0;
automode2 = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V17, 0);
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget
led4.off();
} else {
automode = 0;
automode2 = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V17, 0);
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget
led4.off();
}
}
}
BLYNK_WRITE(V11) // Up to you selected
{
if (param.asInt() == 1) {
timer.setTimeout(50, resetTerminal);
timer.setTimeout(50, checklastbuttonpressed);
automode = 1;
schestate = 1;
} else {
automode = 0;
schestate = 0;
}
}
BLYNK_WRITE(V17) // Up to you selected
{
if (param.asInt() == 1) {
timer.setTimeout(50, resetTerminal);
timer.setTimeout(50, checklastbuttonpressed2);
automode2 = 1;
schestate2 = 1;
} else {
automode2 = 0;
schestate2 = 0;
}
}
BLYNK_WRITE(V10)//Up to you
{
if (automode == 1) {
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
TimeInputParam t(param);
terminal.print("Auto Mode Checked schedule at: ");
terminal.println(Time);
terminal.flush();
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if (t.isWeekdaySelected(weekday() + dayadjustment)) { //Time library starts week on Sunday, Blynk on Monday
terminal.println("Auto Mode ACTIVE today");
terminal.flush();
if (t.hasStartTime()) // Process start time
{
terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
}
if (t.hasStopTime()) // Process stop time
{
terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute()+ ":" + t.getStopSecond());
terminal.flush();
}
// Display timezone details, for information purposes only
terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
terminal.println("At least ONE day MUST be selected");
//terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
terminal.flush();
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
terminal.println(String("Day ") + i + " is selected");
terminal.flush();
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60) + (t.getStartSecond());
Serial.println(startsecondswd); // used for debugging
if (nowseconds >= startsecondswd) {
terminal.print("Auto Mode STARTED at");
terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute() + ":" + t.getStartSecond());
terminal.flush();
/*if (nowseconds <= startsecondswd + 90) { // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1);
led4.on();
// code here to switch the relay ON
}*/
}
else {
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
terminal.println("Auto Mode Device NOT STARTED today");
terminal.flush();
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60) + (t.getStopSecond());
Serial.println(stopsecondswd); // used for debugging
if (nowseconds >= stopsecondswd) {
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
terminal.print("Auto Mode STOPPED at");
terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
/*if (nowseconds <= stopsecondswd + 1) { // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
// code here to switch the relay OFF
}*/
}
else {
if (nowseconds >= startsecondswd) {
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1);
led4.on();
terminal.println("Auto Mode is ON");
terminal.flush();
}
}
}
else {
terminal.println("Auto Mode INACTIVE today");
terminal.flush();
// nothing to do today, check again in 30 SECONDS time
}
terminal.println();
}
}
BLYNK_WRITE(V16)//Up to you
{
if (automode2 == 1) {
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
TimeInputParam t(param);
terminal.print("Auto Mode Checked schedule at: ");
terminal.println(Time);
terminal.flush();
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if (t.isWeekdaySelected(weekday() + dayadjustment)) { //Time library starts week on Sunday, Blynk on Monday
terminal.println("Auto Mode ACTIVE today");
terminal.flush();
if (t.hasStartTime()) // Process start time
{
terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
}
if (t.hasStopTime()) // Process stop time
{
terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute()+ ":" + t.getStopSecond());
terminal.flush();
}
// Display timezone details, for information purposes only
terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
terminal.println("At least ONE day MUST be selected");
//terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
terminal.flush();
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
terminal.println(String("Day ") + i + " is selected");
terminal.flush();
}
}
nowseconds2 = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd2 = (t.getStartHour() * 3600) + (t.getStartMinute() * 60) + (t.getStartSecond());
Serial.println(startsecondswd2); // used for debugging
if (nowseconds2 >= startsecondswd2) {
terminal.print("Auto Mode STARTED at");
terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute() + ":" + t.getStartSecond());
terminal.flush();
/*if (nowseconds2 <= startsecondswd2 + 90) { // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1);
led4.on();
// code here to switch the relay ON
}*/
}
else {
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
terminal.println("Auto Mode Device NOT STARTED today");
terminal.flush();
}
stopsecondswd2 = (t.getStopHour() * 3600) + (t.getStopMinute() * 60) + (t.getStopSecond());
Serial.println(stopsecondswd); // used for debugging
if (nowseconds2 >= stopsecondswd2) {
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
terminal.print("Auto Mode STOPPED at");
terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
/*if (nowseconds2 <= stopsecondswd2 + 1) { // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(relayLed, HIGH); // set LED OFF
Blynk.virtualWrite(V2, 0);
led4.off();
// code here to switch the relay OFF
}*/
}
else {
if (nowseconds2 >= startsecondswd2) {
digitalWrite(relayLed, LOW); // set LED ON
Blynk.virtualWrite(V2, 1);
terminal.println("Auto Mode is ON");
terminal.flush();
led4.on();
}
}
}
else {
terminal.println("Auto Mode INACTIVE today");
terminal.flush();
// nothing to do today, check again in 30 SECONDS time
}
terminal.println();
}
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if (Blynk.connect()) {
BLYNK_LOG("Reconnected");
} else {
BLYNK_LOG("Not reconnected");
}
}
}