หน้าเว็บ

วันศุกร์ที่ 9 ธันวาคม พ.ศ. 2559

SonOff : Program Code ขับสัญญาณกริ่ง + Function


 SooOff ESP8266

SonOff : Program Code ขับสัญญาณกริ่ง + Function
  1. //Public Variable.
  2. #include <ESP8266WiFi.h>
  3. #include <time.h>
  4. #include "Timer.h"
  5.  
  6. Timer t;
  7.  
  8. #define pinRelay 12
  9. #define pinBlink 13
  10.  
  11. int buttonState = 0;
  12. const int buttonPin = 0;
  13.  
  14. const char* ssid = "WIFI_NAME";                
  15. const char* password = "PASSWORD WIFI";          
  16.  
  17. int timezone = 7 * 3600;                  
  18. int dst = 0;  
  19. int result;
  20.  
  21. struct tm* p_tm ;
  22. int CountSec;
  23. int CountMin;
  24. int CountHour;      
  25.  
  26. //Close Public Variable.
  27.  
  28. void setup()
  29. {
  30. // WIFI.  
  31.   Serial.begin(115200);
  32.   Serial.setDebugOutput(true);
  33.  
  34.   pinMode(pinRelay, OUTPUT);
  35.   pinMode(pinBlink, OUTPUT);
  36.   pinMode(buttonPin, INPUT);
  37.  
  38.   digitalWrite(pinRelay, LOW);
  39.   //digitalWrite(pinBlink, HIGH);
  40.  
  41.   WiFi.mode(WIFI_STA);                                        
  42.   WiFi.begin(ssid, password);
  43.   Serial.println("\nConnecting to WiFi");
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.      Serial.print(",");
  46.      delay(1000);
  47.    }
  48.     //configTime(timezone, dst, "time.nist.gov");  
  49.     configTime(timezone, dst, "192.168.0.2");  
  50.     Serial.println("\nWaiting for time");
  51.     while (!time(nullptr)) {
  52.       Serial.print(".");
  53.       delay(1000);
  54.     }
  55.     Serial.println("");
  56.    
  57.  
  58. //Keep time now.
  59.   time_t now = time(nullptr);
  60.   p_tm = localtime(&now);
  61.  
  62.   CountSec = p_tm->tm_sec;
  63.   CountMin = p_tm->tm_min;
  64.   CountHour = p_tm->tm_hour;
  65.  
  66. //Call Function Every 1 second.
  67.   int tickEvent = t.every(1000, loopTime);
  68.   int CheckClickButton = t.every(1000, ChekButton);
  69.  
  70. }
  71.  
  72. //Manual Click GPIO0
  73. void ChekButton()
  74. {
  75.   buttonState = digitalRead(buttonPin);
  76.   if (buttonState == HIGH) {
  77.     digitalWrite(pinRelay, LOW); //On relay
  78.     digitalWrite(pinBlink, HIGH); //On  LED
  79.     result = 0;
  80.   } else {
  81.     // turn LED off:
  82.     digitalWrite(pinRelay, HIGH); //Off relay
  83.     digitalWrite(pinBlink, LOW); //Off  LED
  84.     result = 1;
  85.   }
  86.   Serial.print(result);
  87. }  
  88.  
  89. //Call Function On.
  90. void CallOn()
  91. {
  92.   digitalWrite(pinRelay, HIGH); //On relay
  93.   digitalWrite(pinBlink, LOW); //On  LED
  94.   delay(8000);
  95.   CountSec = CountSec + 8;
  96. }  
  97.  
  98. //Call Function Off.
  99. void CallOff()
  100. {
  101.   digitalWrite(pinRelay, LOW); //Off relay
  102.   digitalWrite(pinBlink, HIGH); //Off  LED
  103. }  
  104.  
  105.  
  106. //Loop.        
  107. void loop()
  108. {
  109.   t.update();
  110. }
  111.  
  112. //Function Loop Time.
  113. void loopTime()
  114. {
  115.  
  116.   CountSec = CountSec + 1;
  117.  
  118.   if(CountSec == 60){
  119.     CountSec = 0;
  120.     CountMin = CountMin + 1;  
  121.  
  122.     if(CountMin == 60){
  123.       CountMin = 0;
  124.       CountHour = CountHour + 1
  125.  
  126.       if(CountHour == 24){
  127.         CountHour = 0;    
  128.      }
  129.  
  130.     }  
  131.  
  132.   }
  133.  
  134. // 12:30 Connet NTP New.
  135.   if(CountHour == 0 && CountMin == 30){
  136.     time_t now = time(nullptr);
  137.     p_tm = localtime(&now);
  138.    
  139.     CountSec = p_tm->tm_sec;
  140.     CountMin = p_tm->tm_min;
  141.     CountHour = p_tm->tm_hour;  
  142.   }
  143.  
  144. //Print Time.
  145.   Serial.print("Hour = ");
  146.   Serial.print(CountHour);
  147.   Serial.println("");
  148.  
  149.   Serial.print("Min = ");
  150.   Serial.print(CountMin);
  151.   Serial.println("");
  152.  
  153.   Serial.print("Sec = ");
  154.   Serial.print(CountSec);
  155.   Serial.println("");
  156.  
  157.  
  158.   //### Test ###//
  159.   /*
  160.   if(CountSec == 10){
  161.     Serial.print("Open");  
  162.     CallOn();
  163.   }
  164.   */
  165.   //### Test ###//
  166.  
  167.   //0:00
  168.   if((CountHour == 0) && (CountMin == 0) && (CountSec == 0)){
  169.     Serial.print("Open");
  170.     CallOn();
  171.   }
  172.   //0:30
  173.   if((CountHour == 0) && (CountMin == 30) && (CountSec == 0)){
  174.     Serial.print("Open");
  175.     CallOn();
  176.   }
  177.   //5:30
  178.   if((CountHour == 5) && (CountMin == 30) && (CountSec == 0)){
  179.     Serial.print("Open");
  180.     CallOn();
  181.   }
  182.   //6:00
  183.   if((CountHour == 6) && (CountMin == 0) && (CountSec == 0)){
  184.     Serial.print("Open");
  185.     CallOn();
  186.   }
  187.   //7:50
  188.   if((CountHour == 7) && (CountMin == 50) && (CountSec == 0)){
  189.     Serial.print("Open");
  190.     CallOn();
  191.   }
  192.   //8:00
  193.   if((CountHour == 8) && (CountMin == 0) && (CountSec == 0)){
  194.     Serial.print("Open");
  195.     CallOn();
  196.   }
  197.  //10:20
  198.   if((CountHour == 10) && (CountMin == 20) && (CountSec == 0)){
  199.     Serial.print("Open");
  200.     CallOn();
  201.   }
  202.   //12:00
  203.   if((CountHour == 12) && (CountMin == 0) && (CountSec == 0)){
  204.     Serial.print("Open");
  205.     CallOn();
  206.   }
  207.   //12:50
  208.   if((CountHour == 12) && (CountMin == 50) && (CountSec == 0)){
  209.     Serial.print("Open");
  210.     CallOn();
  211.   }
  212.   //13:00
  213.   if((CountHour == 13) && (CountMin == 0) && (CountSec == 0)){
  214.     Serial.print("Open");
  215.     CallOn();
  216.   }
  217.   //17:40
  218.   if((CountHour == 17) && (CountMin == 40) && (CountSec == 0)){
  219.     Serial.print("Open");
  220.     CallOn();
  221.   }
  222.   //18:00
  223.   if((CountHour == 18) && (CountMin == 0) && (CountSec == 0)){
  224.     Serial.print("Open");
  225.     CallOn();
  226.   }
  227.   //21:00
  228.   if((CountHour == 21) && (CountMin == 0) && (CountSec == 0)){
  229.     Serial.print("Open");
  230.     CallOn();
  231.   }  
  232.  
  233.  
  234. CallOff();  
  235.  
  236. }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น