Blynk ตั้ง Timer แล้วไม่ทำงาน

จะทำสมาร์ทฟาร์มครับ คือ ตัวที่ส่งไปยัง blynk ทำได้ค่อนข้างจะสมบูรณ์แล้ว

ติด 2 ที่ คือ (1).ตั้ง Timer แล้วไม่ทำงาน (2).ปุ่ม manual หน้าตู้ ผมทำให้มันลิ้งกันไม่ได้

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11

#define wifiLed    LED_BUILTIN

int sensorPin = A0;
int sensorValue = 0;
int led1 = D0;
int led2 = D1;
int Relay = D3;
int botton1 = D5;
int botton2 = D6;
bool auto_on = false;
//SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);

WidgetLCD lcd(V6);
//#include "Timer.h"

#define VPIN_BUTTON_1    V3
#define VPIN_BUTTON_2    V4

int toggleState_1 = 1;
int wifiFlag = 0;
int SetMin;
int SetMax;

BLYNK_WRITE(V10) //set soil Moisture sensor slider in app on pin V10
{
  SetMin = param.asInt();
}

BLYNK_WRITE(V11) //set soil Moisture sensor slider in app on pin V11
{
  SetMax = param.asInt();
}

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
BlynkTimer timer;

//#include "BottonManual.h"
//#include "RelayOnOff.h"

BLYNK_CONNECTED() {
  Blynk.syncAll();
}


BLYNK_WRITE(V4) {
  if (param.asInt() == 1)
  { auto_on = false;
    digitalWrite(Relay, HIGH);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: ON");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
    Blynk.virtualWrite(V3, LOW);
  }
  if (param.asInt() == 0)
  { //auto_on = true;
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
    //Blynk.virtualWrite(V4, LOW);
  }
}

void without_internet2() {
  //Manual Switch Control
  if (digitalRead(botton2) == HIGH) {
    delay(200);
    auto_on = false;
    digitalWrite(Relay, HIGH);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: ON");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
    Blynk.virtualWrite(V3, LOW);
  }
  if (digitalRead(botton2) == LOW) {
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
  }

}

BLYNK_WRITE(V7)  // Timer 1
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: ON");
    lcd.print(0, 1, "STATS: TIMER 1");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
  }
}

BLYNK_WRITE(V8) // Timer 2
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: ON");
    lcd.print(0, 1, "STATS: TIMER 2");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
  }
}

BLYNK_WRITE(V9) // Timer 3
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: ON");
    lcd.print(0, 1, "STATS: TIMER 3");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
  }
}

void sendTemp() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
}

void sendSoilValue() {
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, 0, 1023, 0, 100);
  Blynk.virtualWrite(V5, sensorValue);
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, LOW); //Turn on WiFi LED
  }
}

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(DHTPIN, INPUT);
  pinMode(botton1, INPUT);
  pinMode(botton2, INPUT);
  pinMode(led1, OUTPUT); // กำหนดขาทำหน้าที่ให้ขา D0 เป็น OUTPUT
  pinMode(led2, OUTPUT); // กำหนดขาทำหน้าที่ให้ขา D1 เป็น OUTPUT
  pinMode(Relay, OUTPUT);

  digitalWrite(botton1, LOW);
  digitalWrite(botton2, LOW);
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(Relay, LOW);

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk.iot-cm.com", 8080);

  dht.begin();
  lcd.clear();
  delay(1000);
  lcd.print(5, 0, "SMART");
  lcd.print(0, 1, "Watering System");
  delay(1000);

  lcd.clear();
  lcd.print(0, 0, "Please Wait");
  delay(300);
  lcd.print(0, 0, "Please Wait.");
  delay(300);
  lcd.print(0, 0, "Please Wait..");
  delay(300);
  lcd.print(0, 0, "Please Wait...");
  delay(300);
  lcd.print(0, 0, "Please Wait....");
  lcd.clear();
  delay(800);
  lcd.clear();
  lcd.print(0, 0, "PUMP: OFF");
  lcd.print(0, 1, "STATS: MANUAL");

  timer.setInterval(1000L, sendTemp);
  timer.setInterval(1000L, sendSoilValue);
}

void loop() {
  Blynk.run();
  timer.run();
  if (auto_on == true) {
    if (sensorValue > SetMin && sensorValue < SetMax) {
      digitalWrite(Relay, HIGH);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: ON");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
    if (sensorValue < SetMin) {
      digitalWrite(Relay, LOW);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: ON");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
    else if (sensorValue > SetMax) {
      digitalWrite(Relay, LOW);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: OFF");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
  }
}

BLYNK_WRITE(V3) {
  //sensorValue = analogRead(sensorPin);
  //sensorValue = map(sensorValue, 0, 1023, 0, 100);

  if (param.asInt() == 1)
  { auto_on = true;
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: AUTO");
    delay(500);
    Blynk.virtualWrite(V4, LOW);
  }
  if (param.asInt() == 0)
  { auto_on = false;
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
    //Blynk.virtualWrite(V3, LOW);
  }
}

void without_internet1() {
  //Manual Switch Control
  if (digitalRead(botton1) == HIGH) {
    delay(200);
    auto_on = true;
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: AUTO");
    delay(500);
    Blynk.virtualWrite(V4, LOW);
  }
  if (digitalRead(botton1) == LOW) {
    auto_on = false;
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
  }
}

ส่งเป็นตัว Clone มาครับ

ยังไงหรอครับ clone ยังไง ผมมือใหม่ เพิ่งหัดทำครับ

ผมว่าน่าจะคล้ายกับกระทู้นี้เรื่อง manual หน้าตู้ ลองศึกษาดูสิ

อยากรู้ว่า ถึงเวลาแล้วมันขึ้นว่าอะไรบ้าง ของ V7

ภาพ

ภาพ

ถึงเวลา มันไม่ทำงาน แต่มันจะทำงานและปิด หลังจากเวลาที่ตั้งไว้ ประมาณ 5-6 นาที

ลองใส่ real time clock เข้าไปแล้วตั้งเป็น โซนไทย

ใส่ Real time clock แล้วครับ ผลก็ปรากฏตามรูป ตั้งไว้ 14.33 แต่ไปเริ่มทำงานตอน 14.39

ตั้งเป็น Bangkok หรือยัง

ตั้งแล้วครับ

ใส่โค้ต RTC ของมันลงไปด้วย

#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

BlynkTimer timer;

WidgetRTC rtc;

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  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(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, clockDisplay);
}

void loop()
{
  Blynk.run();
  timer.run();
}

ที่ต้องเพิ่มคือ

#include <WidgetRTC.h>
WidgetRTC rtc;
rtc.begin();

ใส่เพิ่มแล้วครับ สรุป ก็ยังช้ากว่าเวลา ประมาณ 6 นาทีครับ

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();

ดึงเวลามาดูเลยครับว่ามันตรงกับเวลาเราหรือไม่

น่าจะไม่ตรงนะครับ ต่างกัน 6 นาทีพอดี ^^

วางโค้ตล่าสุดมาดูหน่อย

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN D1
#define DHTTYPE DHT11

#define wifiLed    LED_BUILTIN

int sensorPin = D0;
int sensor = 0;
int sensorValue = 0;
int sensorState = 0;
int lastState = 0;
int led1 = D2;
int led2 = D3;
int Relay = D4;
int botton1 = D5;
int botton2 = D6;

bool auto_on = false;
//SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);

WidgetLCD lcd(V6);
//#include "Timer.h"

#define VPIN_BUTTON_1    V3
#define VPIN_BUTTON_2    V4

int toggleState_1 = 1;
int wifiFlag = 0;
int SetMin;
int SetMax;

BLYNK_WRITE(V10) //set soil Moisture sensor slider in app on pin V10
{
  SetMin = param.asInt();
}

BLYNK_WRITE(V11) //set soil Moisture sensor slider in app on pin V11
{
  SetMax = param.asInt();
}

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
BlynkTimer timer;

//#include "BottonManual.h"
//#include "RelayOnOff.h"

BLYNK_CONNECTED() {
  Blynk.syncAll();
}


BLYNK_WRITE(V4) {
  if (param.asInt() == 1)
  { auto_on = false;
    digitalWrite(Relay, HIGH);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP : ON");
    lcd.print(0, 1, "STATUS : MANUAL");
    delay(500);
    Blynk.virtualWrite(V3, LOW);
  }
  if (param.asInt() == 0)
  { auto_on = false;
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP : OFF");
    lcd.print(0, 1, "STATUS : MANUAL");
    delay(500);
    //Blynk.virtualWrite(V4, LOW);
  }
}

BLYNK_WRITE(V7)  // Timer 1
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP : ON");
    lcd.print(0, 1, "STATUS : TIMER 1");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP : OFF");
    lcd.print(0, 1, "STATUS : MANUAL");
    delay(500);
  }
}

BLYNK_WRITE(V8) // Timer 2
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP : ON");
    lcd.print(0, 1, "STATUS : TIMER 2");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP : OFF");
    lcd.print(0, 1, "STATUS : MANUAL");
    delay(500);
  }
}

BLYNK_WRITE(V9) // Timer 3
{
  if (param.asInt() == 1)
  { digitalWrite(Relay, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP : ON");
    lcd.print(0, 1, "STATUS : TIMER 3");
    delay(500);
  }
  if (param.asInt() == 0)
  { digitalWrite(Relay, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP : OFF");
    lcd.print(0, 1, "STATUS : MANUAL");
    delay(500);
  }
}

void sendTemp() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
}

void sendSoilValue() {
  sensorValue = analogRead(A0);
  sensorValue = map(sensorValue, 0, 1023, 0, 100);
  Blynk.virtualWrite(V5, sensorValue);
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, LOW); //Turn on WiFi LED
  }
}

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(DHTPIN, INPUT);
  pinMode(botton1, INPUT);
  pinMode(botton2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(Relay, OUTPUT);

  digitalWrite(botton1, LOW);
  digitalWrite(botton2, LOW);
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(Relay, LOW);

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk.iot-cm.com", 8080);
  dht.begin();
  
  lcd.clear();
  delay(1000);
  lcd.print(5, 0, "SMART");
  lcd.print(0, 1, "Watering System");
  delay(1000);

  lcd.clear();
  lcd.print(0, 0, "Please Wait");
  delay(300);
  lcd.print(0, 0, "Please Wait.");
  delay(300);
  lcd.print(0, 0, "Please Wait..");
  delay(300);
  lcd.print(0, 0, "Please Wait...");
  delay(300);
  lcd.print(0, 0, "Please Wait....");
  delay(300);
  lcd.clear();
  delay(500);
  lcd.clear();
  lcd.print(0, 0, "PUMP : OFF");
  lcd.print(0, 1, "STATUS : MANUAL");

  timer.setInterval(1000L, sendTemp);
  timer.setInterval(1000L, sendSoilValue);
}

void loop() {
  Blynk.run();
  timer.run();

  //send notification
  sensorState = digitalRead(sensorPin);
  if (sensorState == 1 && lastState == 0) {
    Blynk.notify("Water your plants");
    lastState = 1;
    delay(1000);
  }

  //do nothing, has not been watered yet
  else if (sensorState == 1 && lastState == 1) {
    delay(1000);
  }

  //st
  else {
    lastState = 0;
    delay(1000);
  }

  if (auto_on == true) {
    if (sensorValue > SetMin && sensorValue < SetMax) {
      digitalWrite(Relay, HIGH);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: ON");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
    if (sensorValue < SetMin) {
      digitalWrite(Relay, LOW);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: ON");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
    else if (sensorValue > SetMax) {
      digitalWrite(Relay, LOW);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lcd.clear();
      lcd.print(0, 0, "PUMP: OFF");
      lcd.print(0, 1, "STATS: AUTO");
      delay(500);
    }
  }
}

BLYNK_WRITE(V3) {
  //sensorValue = analogRead(sensorPin);
  //sensorValue = map(sensorValue, 0, 1023, 0, 100);

  if (param.asInt() == 1)
  { auto_on = true;
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: AUTO");
    delay(500);
    Blynk.virtualWrite(V4, LOW);
  }
  if (param.asInt() == 0)
  { auto_on = false;
    digitalWrite(Relay, LOW);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    lcd.clear();
    lcd.print(0, 0, "PUMP: OFF");
    lcd.print(0, 1, "STATS: MANUAL");
    delay(500);
    //Blynk.virtualWrite(V3, LOW);
  }
}

จุดผิดอยู่ตรงนี้ หาดูว่าลืมใส่อะไรรึป่าว

ปัญหาใหม่มาอีกแล้วครับ บอร์ดมันรวน รีสตาร์ทเองตลอดเลย