├── NODEMCU_OLED_ANALOG_CLOCK └── NODEMCU_OLED_ANALOG_CLOCK.ino ├── NODEMCU_OLED_CLOCK └── NODEMCU_OLED_CLOCK.ino ├── README.md ├── READ_TIME_FROM_INTERNET └── READ_TIME_FROM_INTERNET.ino └── TURNON_OFF_WITH_TIME └── TURNON_OFF_WITH_TIME.ino /NODEMCU_OLED_ANALOG_CLOCK/NODEMCU_OLED_ANALOG_CLOCK.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define OLED_RESET LED_BUILTIN //4 10 | Adafruit_SSD1306 display(OLED_RESET); 11 | 12 | const char* ssid = ""; 13 | const char* password = ""; 14 | 15 | int ledPin = 13; 16 | 17 | int timezone = 7 * 3600; 18 | int dst = 0; 19 | 20 | #if (SSD1306_LCDHEIGHT != 64) 21 | #error("Height incorrect, please fix Adafruit_SSD1306.h!"); 22 | #endif 23 | 24 | 25 | 26 | void setup() { 27 | 28 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 29 | 30 | // Clear the buffer. 31 | display.clearDisplay(); 32 | display.display(); 33 | 34 | pinMode(ledPin,OUTPUT); 35 | digitalWrite(ledPin,LOW); 36 | 37 | Serial.begin(115200); 38 | 39 | display.setTextSize(1); 40 | display.setTextColor(WHITE); 41 | 42 | display.setCursor(0,0); 43 | display.println("Wifi connecting to "); 44 | display.println( ssid ); 45 | 46 | WiFi.begin(ssid,password); 47 | 48 | display.println("\nConnecting"); 49 | 50 | display.display(); 51 | 52 | while( WiFi.status() != WL_CONNECTED ){ 53 | delay(500); 54 | display.print("."); 55 | display.display(); 56 | } 57 | 58 | // Clear the buffer. 59 | display.clearDisplay(); 60 | display.display(); 61 | display.setCursor(0,0); 62 | 63 | display.println("Wifi Connected!"); 64 | display.print("IP:"); 65 | display.println(WiFi.localIP() ); 66 | 67 | display.display(); 68 | 69 | configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); 70 | display.println("\nWaiting for NTP..."); 71 | 72 | while(!time(nullptr)){ 73 | Serial.print("*"); 74 | 75 | delay(1000); 76 | } 77 | display.println("\nTime response....OK"); 78 | display.display(); 79 | delay(1000); 80 | 81 | display.clearDisplay(); 82 | display.display(); 83 | } 84 | 85 | void loop() { 86 | 87 | time_t now = time(nullptr); 88 | struct tm* p_tm = localtime(&now); 89 | int r = 35; 90 | // Now draw the clock face 91 | 92 | display.drawCircle(display.width()/2, display.height()/2, 2, WHITE); 93 | // 94 | //hour ticks 95 | for( int z=0; z < 360;z= z + 30 ){ 96 | //Begin at 0° and stop at 360° 97 | float angle = z ; 98 | 99 | angle=(angle/57.29577951) ; //Convert degrees to radians 100 | int x2=(64+(sin(angle)*r)); 101 | int y2=(32-(cos(angle)*r)); 102 | int x3=(64+(sin(angle)*(r-5))); 103 | int y3=(32-(cos(angle)*(r-5))); 104 | display.drawLine(x2,y2,x3,y3,WHITE); 105 | } 106 | // display second hand 107 | float angle = p_tm->tm_sec*6 ; 108 | angle=(angle/57.29577951) ; //Convert degrees to radians 109 | int x3=(64+(sin(angle)*(r))); 110 | int y3=(32-(cos(angle)*(r))); 111 | display.drawLine(64,32,x3,y3,WHITE); 112 | // 113 | // display minute hand 114 | angle = p_tm->tm_min * 6 ; 115 | angle=(angle/57.29577951) ; //Convert degrees to radians 116 | x3=(64+(sin(angle)*(r-3))); 117 | y3=(32-(cos(angle)*(r-3))); 118 | display.drawLine(64,32,x3,y3,WHITE); 119 | // 120 | // display hour hand 121 | angle = p_tm->tm_hour * 30 + int((p_tm->tm_min / 12) * 6 ); 122 | angle=(angle/57.29577951) ; //Convert degrees to radians 123 | x3=(64+(sin(angle)*(r-11))); 124 | y3=(32-(cos(angle)*(r-11))); 125 | display.drawLine(64,32,x3,y3,WHITE); 126 | 127 | display.setTextSize(1); 128 | display.setCursor((display.width()/2)+10,(display.height()/2) - 3); 129 | display.print(p_tm->tm_mday); 130 | 131 | // update display with all data 132 | display.display(); 133 | delay(100); 134 | display.clearDisplay(); 135 | 136 | } 137 | -------------------------------------------------------------------------------- /NODEMCU_OLED_CLOCK/NODEMCU_OLED_CLOCK.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define OLED_RESET LED_BUILTIN //4 10 | Adafruit_SSD1306 display(OLED_RESET); 11 | 12 | const char* ssid = ""; 13 | const char* password = ""; 14 | 15 | int ledPin = 13; 16 | 17 | int timezone = 7 * 3600; 18 | int dst = 0; 19 | 20 | #if (SSD1306_LCDHEIGHT != 64) 21 | #error("Height incorrect, please fix Adafruit_SSD1306.h!"); 22 | #endif 23 | 24 | 25 | 26 | void setup() { 27 | 28 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 29 | 30 | // Clear the buffer. 31 | display.clearDisplay(); 32 | display.display(); 33 | 34 | pinMode(ledPin,OUTPUT); 35 | digitalWrite(ledPin,LOW); 36 | 37 | Serial.begin(115200); 38 | 39 | display.setTextSize(1); 40 | display.setTextColor(WHITE); 41 | 42 | display.setCursor(0,0); 43 | display.println("Wifi connecting to "); 44 | display.println( ssid ); 45 | 46 | WiFi.begin(ssid,password); 47 | 48 | display.println("\nConnecting"); 49 | 50 | display.display(); 51 | 52 | while( WiFi.status() != WL_CONNECTED ){ 53 | delay(500); 54 | display.print("."); 55 | display.display(); 56 | } 57 | 58 | // Clear the buffer. 59 | display.clearDisplay(); 60 | display.display(); 61 | display.setCursor(0,0); 62 | 63 | display.println("Wifi Connected!"); 64 | display.print("IP:"); 65 | display.println(WiFi.localIP() ); 66 | 67 | display.display(); 68 | 69 | configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); 70 | display.println("\nWaiting for NTP..."); 71 | 72 | while(!time(nullptr)){ 73 | Serial.print("*"); 74 | 75 | delay(1000); 76 | } 77 | display.println("\nTime response....OK"); 78 | display.display(); 79 | delay(1000); 80 | 81 | display.clearDisplay(); 82 | display.display(); 83 | } 84 | 85 | void loop() { 86 | 87 | time_t now = time(nullptr); 88 | struct tm* p_tm = localtime(&now); 89 | 90 | Serial.print(p_tm->tm_mday); 91 | Serial.print("/"); 92 | Serial.print(p_tm->tm_mon + 1); 93 | Serial.print("/"); 94 | Serial.print(p_tm->tm_year + 1900); 95 | 96 | Serial.print(" "); 97 | 98 | Serial.print(p_tm->tm_hour); 99 | Serial.print(":"); 100 | Serial.print(p_tm->tm_min); 101 | Serial.print(":"); 102 | Serial.println(p_tm->tm_sec); 103 | 104 | // Clear the buffer. 105 | display.clearDisplay(); 106 | 107 | display.setTextSize(3); 108 | display.setTextColor(WHITE); 109 | 110 | display.setCursor(0,0); 111 | display.print(p_tm->tm_hour); 112 | display.print(":"); 113 | if( p_tm->tm_min <10) 114 | display.print("0"); 115 | display.print(p_tm->tm_min); 116 | 117 | display.setTextSize(2); 118 | display.setCursor(90,5); 119 | display.print("."); 120 | if( p_tm->tm_sec <10) 121 | display.print("0"); 122 | display.print(p_tm->tm_sec); 123 | 124 | display.setTextSize(1); 125 | display.setCursor(0,40); 126 | display.print(p_tm->tm_mday); 127 | display.print("/"); 128 | display.print(p_tm->tm_mon + 1); 129 | display.print("/"); 130 | display.print(p_tm->tm_year + 1900); 131 | 132 | display.display(); 133 | 134 | delay(1000); // update every 1 sec 135 | 136 | } 137 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NodeMCU_ESP8266 2 | Maker Tutor Channel 3 | 4 | This folder contains some NodeMCU ESP8266 ArduinoIDE code samples. 5 | -------------------------------------------------------------------------------- /READ_TIME_FROM_INTERNET/READ_TIME_FROM_INTERNET.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const char* ssid = ""; 5 | const char* password = ""; 6 | 7 | int ledPin = 13; 8 | 9 | int timezone = 7 * 3600; 10 | int dst = 0; 11 | 12 | void setup() { 13 | 14 | pinMode(ledPin,OUTPUT); 15 | digitalWrite(ledPin,LOW); 16 | 17 | Serial.begin(115200); 18 | Serial.println(); 19 | Serial.print("Wifi connecting to "); 20 | Serial.println( ssid ); 21 | 22 | WiFi.begin(ssid,password); 23 | 24 | Serial.println(); 25 | 26 | Serial.print("Connecting"); 27 | 28 | while( WiFi.status() != WL_CONNECTED ){ 29 | delay(500); 30 | Serial.print("."); 31 | } 32 | 33 | digitalWrite( ledPin , HIGH); 34 | Serial.println(); 35 | 36 | Serial.println("Wifi Connected Success!"); 37 | Serial.print("NodeMCU IP Address : "); 38 | Serial.println(WiFi.localIP() ); 39 | 40 | configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); 41 | Serial.println("\nWaiting for Internet time"); 42 | 43 | while(!time(nullptr)){ 44 | Serial.print("*"); 45 | delay(1000); 46 | } 47 | Serial.println("\nTime response....OK"); 48 | } 49 | 50 | void loop() { 51 | 52 | time_t now = time(nullptr); 53 | struct tm* p_tm = localtime(&now); 54 | Serial.print(p_tm->tm_mday); 55 | Serial.print("/"); 56 | Serial.print(p_tm->tm_mon + 1); 57 | Serial.print("/"); 58 | Serial.print(p_tm->tm_year + 1900); 59 | 60 | Serial.print(" "); 61 | 62 | Serial.print(p_tm->tm_hour); 63 | Serial.print(":"); 64 | Serial.print(p_tm->tm_min); 65 | Serial.print(":"); 66 | Serial.println(p_tm->tm_sec); 67 | 68 | delay(1000); 69 | 70 | } 71 | -------------------------------------------------------------------------------- /TURNON_OFF_WITH_TIME/TURNON_OFF_WITH_TIME.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const char* ssid = ""; 5 | const char* password = ""; 6 | 7 | int ledPin = 13; 8 | 9 | int timezone = 7 * 3600; 10 | int dst = 0; 11 | 12 | void setup() { 13 | 14 | pinMode(ledPin,OUTPUT); 15 | digitalWrite(ledPin,LOW); 16 | 17 | Serial.begin(115200); 18 | Serial.println(); 19 | Serial.print("Wifi connecting to "); 20 | Serial.println( ssid ); 21 | 22 | WiFi.begin(ssid,password); 23 | 24 | Serial.println(); 25 | 26 | Serial.print("Connecting"); 27 | 28 | while( WiFi.status() != WL_CONNECTED ){ 29 | delay(500); 30 | Serial.print("."); 31 | } 32 | 33 | 34 | Serial.println(); 35 | 36 | Serial.println("Wifi Connected Success!"); 37 | Serial.print("NodeMCU IP Address : "); 38 | Serial.println(WiFi.localIP() ); 39 | 40 | configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); 41 | Serial.println("\nWaiting for Internet time"); 42 | 43 | while(!time(nullptr)){ 44 | Serial.print("*"); 45 | delay(1000); 46 | } 47 | Serial.println("\nTime response....OK"); 48 | } 49 | 50 | void loop() { 51 | 52 | time_t now = time(nullptr); 53 | struct tm* p_tm = localtime(&now); 54 | 55 | // TURN LED ON 56 | 57 | if( (p_tm->tm_hour == 0) && (p_tm->tm_min == 37)){ 58 | digitalWrite(ledPin,HIGH); 59 | } 60 | 61 | 62 | // TURN LED OFF 63 | 64 | if( (p_tm->tm_hour == 0) && (p_tm->tm_min == 40)){ 65 | digitalWrite(ledPin,LOW); 66 | } 67 | 68 | delay(1000); 69 | 70 | 71 | 72 | } 73 | --------------------------------------------------------------------------------