SooOff ESP8266
SonOff : Program Code ขับสัญญาณกริ่ง + Function
- //Public Variable.
- #include <ESP8266WiFi.h>
- #include <time.h>
- #include "Timer.h"
- Timer t;
- #define pinRelay 12
- #define pinBlink 13
- int buttonState = 0;
- const int buttonPin = 0;
- const char* ssid = "WIFI_NAME";
- const char* password = "PASSWORD WIFI";
- int timezone = 7 * 3600;
- int dst = 0;
- int result;
- struct tm* p_tm ;
- int CountSec;
- int CountMin;
- int CountHour;
- //Close Public Variable.
- void setup()
- {
- // WIFI.
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- pinMode(pinRelay, OUTPUT);
- pinMode(pinBlink, OUTPUT);
- pinMode(buttonPin, INPUT);
- digitalWrite(pinRelay, LOW);
- //digitalWrite(pinBlink, HIGH);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.println("\nConnecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(",");
- delay(1000);
- }
- //configTime(timezone, dst, "time.nist.gov");
- configTime(timezone, dst, "192.168.0.2");
- Serial.println("\nWaiting for time");
- while (!time(nullptr)) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("");
- //Keep time now.
- time_t now = time(nullptr);
- p_tm = localtime(&now);
- CountSec = p_tm->tm_sec;
- CountMin = p_tm->tm_min;
- CountHour = p_tm->tm_hour;
- //Call Function Every 1 second.
- int tickEvent = t.every(1000, loopTime);
- int CheckClickButton = t.every(1000, ChekButton);
- }
- //Manual Click GPIO0
- void ChekButton()
- {
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH) {
- digitalWrite(pinRelay, LOW); //On relay
- digitalWrite(pinBlink, HIGH); //On LED
- result = 0;
- } else {
- // turn LED off:
- digitalWrite(pinRelay, HIGH); //Off relay
- digitalWrite(pinBlink, LOW); //Off LED
- result = 1;
- }
- Serial.print(result);
- }
- //Call Function On.
- void CallOn()
- {
- digitalWrite(pinRelay, HIGH); //On relay
- digitalWrite(pinBlink, LOW); //On LED
- delay(8000);
- CountSec = CountSec + 8;
- }
- //Call Function Off.
- void CallOff()
- {
- digitalWrite(pinRelay, LOW); //Off relay
- digitalWrite(pinBlink, HIGH); //Off LED
- }
- //Loop.
- void loop()
- {
- t.update();
- }
- //Function Loop Time.
- void loopTime()
- {
- CountSec = CountSec + 1;
- if(CountSec == 60){
- CountSec = 0;
- CountMin = CountMin + 1;
- if(CountMin == 60){
- CountMin = 0;
- CountHour = CountHour + 1;
- if(CountHour == 24){
- CountHour = 0;
- }
- }
- }
- // 12:30 Connet NTP New.
- if(CountHour == 0 && CountMin == 30){
- time_t now = time(nullptr);
- p_tm = localtime(&now);
- CountSec = p_tm->tm_sec;
- CountMin = p_tm->tm_min;
- CountHour = p_tm->tm_hour;
- }
- //Print Time.
- Serial.print("Hour = ");
- Serial.print(CountHour);
- Serial.println("");
- Serial.print("Min = ");
- Serial.print(CountMin);
- Serial.println("");
- Serial.print("Sec = ");
- Serial.print(CountSec);
- Serial.println("");
- //### Test ###//
- /*
- if(CountSec == 10){
- Serial.print("Open");
- CallOn();
- }
- */
- //### Test ###//
- //0:00
- if((CountHour == 0) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //0:30
- if((CountHour == 0) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //5:30
- if((CountHour == 5) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //6:00
- if((CountHour == 6) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //7:50
- if((CountHour == 7) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //8:00
- if((CountHour == 8) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //10:20
- if((CountHour == 10) && (CountMin == 20) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //12:00
- if((CountHour == 12) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //12:50
- if((CountHour == 12) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //13:00
- if((CountHour == 13) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //17:40
- if((CountHour == 17) && (CountMin == 40) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //18:00
- if((CountHour == 18) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- //21:00
- if((CountHour == 21) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- CallOn();
- }
- CallOff();
- }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น