อ้างอิง : https://github.com/esp8266/Arduino#using-git-version
1.Download IDE (https://www.arduino.cc/en/Main/Software)
2.แตกไฟล์แล้วเข้าไปใน folder
3.ติดตั้งสิ่งที่จำเป็นเพิ่มเติมจาก terminal
4.restart Arduino
5.เพิ่มบอร์ดจากเมนู File > Preferences แล้วเพิ่ม http://arduino.esp8266.com/stable/packa ... index.json ตรงช่อง Additional Boards Manager URLs
6.เข้าเมนู Tools > Board... > Boards Manager แล้วค้นหา board ที่ต้องการ เช่น esp8266 แล้วคลิก install
7.เพิ่มสิทธิให้ user อ่าน/เขียน USB ได้ (http://playground.arduino.cc/Linux/All#Permission)
*re-login
All Code Language Reference
https://www.arduino.cc/en/Reference/HomePage
Code : ต่อ WIFI
Code : ต่อ WIFI + NTP + Check Open Led
Code : ต่อ WIFI + NTP แบบ Loop Function
ต้องใช้ Timer.h ติดตั้งเพิ่มเติม
http://playground.arduino.cc/Code/Timer
Install :
You can download the library from here:
https://github.com/JChristensen/Timer
As with all libraries, unzip the file into the 'libraries' folder in your Arduino directory, which will be in something like 'My Documents\Arduino' on Windows, 'Documents/Arduino' on Mac etc. If this is the first library you have installed, you will need to create a directory there called 'libraries'.
Code : WIFI+NTP+SonOff เปิดปิดตามเวลา
1.Download IDE (https://www.arduino.cc/en/Main/Software)
2.แตกไฟล์แล้วเข้าไปใน folder
3.ติดตั้งสิ่งที่จำเป็นเพิ่มเติมจาก terminal
- cd hardware
- mkdir esp8266com
- cd esp8266com
- git clone https://github.com/esp8266/Arduino.git esp8266
- cd esp8266/tools
- python get.py
4.restart Arduino
5.เพิ่มบอร์ดจากเมนู File > Preferences แล้วเพิ่ม http://arduino.esp8266.com/stable/packa ... index.json ตรงช่อง Additional Boards Manager URLs
6.เข้าเมนู Tools > Board... > Boards Manager แล้วค้นหา board ที่ต้องการ เช่น esp8266 แล้วคลิก install
7.เพิ่มสิทธิให้ user อ่าน/เขียน USB ได้ (http://playground.arduino.cc/Linux/All#Permission)
- usermod -a -G dialout username
*re-login
All Code Language Reference
https://www.arduino.cc/en/Reference/HomePage
Code : ต่อ WIFI
- #include <ESP8266WiFi.h>
- const char* ssid = "SCI_EMP";
- const char* password = "619513b917";
- void setup() {
- Serial.begin(115200);
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
- would try to act as both a client and an access-point and could cause
- network-issues with your other WiFi-devices on your WiFi-network. */
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- int value = 0;
- void loop() {
- delay(5000);
- ++value;
- }
Code : ต่อ WIFI + NTP + Check Open Led
- #include <ESP8266WiFi.h>
- #include <time.h>
- const char* ssid = "SCI_EMP";
- const char* password = "619513b917";
- int timezone = 7 * 3600;
- int dst = 0;
- //public variable.
- struct tm* p_tm ;
- int CountSec;
- int ContMin;
- int ContHour;
- void setup()
- {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- pinMode(LED_BUILTIN, OUTPUT);
- 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;
- ContMin = p_tm->tm_min;
- ContHour = p_tm->tm_hour;
- }
- void loop()
- {
- /*configTime(timezone, dst, "pool.ntp.org", "time.nist.gov");
- time_t now = time(nullptr);
- struct tm* p_tm = localtime(&now);
- delay(1000);*/
- //time_t now = time(nullptr);
- //struct tm* p_tm = localtime(&now);
- if(CountSec == 60){
- CountSec = 0;
- ContMin = ContMin + 1;
- }
- if(ContMin == 60){
- ContMin = 0;
- ContHour = ContHour + 1;
- }
- if(ContHour == 24){
- ContHour = 0;
- }
- if(ContHour == 0 && ContMin == 30){
- time_t now = time(nullptr);
- p_tm = localtime(&now);
- CountSec = p_tm->tm_sec;
- ContMin = p_tm->tm_min;
- ContHour = p_tm->tm_hour;
- }
- Serial.print("Hour = ");
- Serial.print(ContHour);
- Serial.println("");
- Serial.print("Min = ");
- Serial.print(ContMin);
- Serial.println("");
- Serial.print("Sec = ");
- Serial.print(CountSec);
- Serial.println("");
- /*Serial.print("Hour = ");
- Serial.print(p_tm->tm_hour);
- Serial.println("");
- Serial.print("Min = ");
- Serial.print(p_tm->tm_min);
- Serial.println("");
- Serial.print("Sec = ");
- Serial.print(p_tm->tm_sec);
- Serial.println("");*/
- //### Test ###//
- if((ContHour == 13) && (ContMin == 53) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- if((ContHour == 13) && (ContMin == 55) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //### Test ###//
- //0:00
- if((ContHour == 0) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //0:30
- if((ContHour == 0) && (ContMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //5:30
- if((ContHour == 5) && (ContMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //6:00
- if((ContHour == 6) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //7:50
- if((ContHour == 7) && (ContMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //8:00
- if((ContHour == 8) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //10:20
- if((ContHour == 10) && (ContMin == 20) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:00
- if((ContHour == 12) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:50
- if((ContHour == 12) && (ContMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //13:00
- if((ContHour == 13) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //17:40
- if((ContHour == 17) && (ContMin == 40) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //18:00
- if((ContHour == 18) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //21:00
- if((ContHour == 21) && (ContMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
- CountSec = CountSec + 1;
- delay(1000);
- }
Code : ต่อ WIFI + NTP แบบ Loop Function
ต้องใช้ Timer.h ติดตั้งเพิ่มเติม
http://playground.arduino.cc/Code/Timer
Install :
You can download the library from here:
https://github.com/JChristensen/Timer
As with all libraries, unzip the file into the 'libraries' folder in your Arduino directory, which will be in something like 'My Documents\Arduino' on Windows, 'Documents/Arduino' on Mac etc. If this is the first library you have installed, you will need to create a directory there called 'libraries'.
- #include <ESP8266WiFi.h>
- #include <time.h>
- #include "Timer.h"
- Timer t;
- const char* ssid = "SCI_EMP";
- const char* password = "619513b917";
- int timezone = 7 * 3600;
- int dst = 0;
- //public variable.
- struct tm* p_tm ;
- int CountSec;
- int CountMin;
- int CountHour;
- void setup()
- {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- pinMode(LED_BUILTIN, OUTPUT);
- 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;
- int tickEvent = t.every(1000, loopTime);
- }
- void loop()
- {
- t.update();
- }
- 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;
- }
- 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;
- }
- 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((CountHour == 8) && (CountMin == 45) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- if((CountHour == 8) && (CountMin == 46) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //### Test ###//
- //0:00
- if((CountHour == 0) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //0:30
- if((CountHour == 0) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //5:30
- if((CountHour == 5) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //6:00
- if((CountHour == 6) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //7:50
- if((CountHour == 7) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //8:00
- if((CountHour == 8) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //10:20
- if((CountHour == 10) && (CountMin == 20) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:00
- if((CountHour == 12) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:50
- if((CountHour == 12) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //13:00
- if((CountHour == 13) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //17:40
- if((CountHour == 17) && (CountMin == 40) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //18:00
- if((CountHour == 18) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //21:00
- if((CountHour == 21) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(LED_BUILTIN, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
- }
Code : WIFI+NTP+SonOff เปิดปิดตามเวลา
- #include <ESP8266WiFi.h>
- #include <time.h>
- #include "Timer.h"
- Timer t;
- int pin = 12;
- const char* ssid = "SCI_EMP";
- const char* password = "619513b917";
- int timezone = 7 * 3600;
- int dst = 0;
- //public variable.
- struct tm* p_tm ;
- int CountSec;
- int CountMin;
- int CountHour;
- void setup()
- {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- //pinMode(LED_BUILTIN, OUTPUT);
- pinMode(pin, OUTPUT);
- 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;
- int tickEvent = t.every(1000, loopTime);
- }
- void loop()
- {
- t.update();
- }
- 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;
- }
- 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){
- digitalWrite(pin, HIGH);
- delay(8000);
- }
- if((CountHour == 8) && (CountMin == 56) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- if((CountHour == 8) && (CountMin == 57) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- */
- //### Test ###//
- //0:00
- if((CountHour == 0) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //0:30
- if((CountHour == 0) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //5:30
- if((CountHour == 5) && (CountMin == 30) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //6:00
- if((CountHour == 6) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //7:50
- if((CountHour == 7) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //8:00
- if((CountHour == 8) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //10:20
- if((CountHour == 10) && (CountMin == 20) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:00
- if((CountHour == 12) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //12:50
- if((CountHour == 12) && (CountMin == 50) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //13:00
- if((CountHour == 13) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //17:40
- if((CountHour == 17) && (CountMin == 40) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //18:00
- if((CountHour == 18) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- //21:00
- if((CountHour == 21) && (CountMin == 0) && (CountSec == 0)){
- Serial.print("Open");
- digitalWrite(pin, HIGH);
- delay(8000);
- CountSec = CountSec + 8;
- }
- digitalWrite(pin, LOW);
- }
No comments:
Post a Comment