반응형
Code
//==NodeMCU==START
#define D0 (16)
#define D1 (5) //SCL //LiquidCrystal_I2C.cpp //Wire.begin();
#define D2 (4) //SDA //LiquidCrystal_I2C.cpp //Wire.begin();
#define D3 (0) //SDA(D3) //LiquidCrystal_I2C.cpp//Wire.begin(0,2);
#define D4 (2) //SCL(D4) ////LiquidCrystal_I2C.cpp//Wire.begin(0,2);
#define D5 (14)
#define D6 (12)
#define D7 (13)
#define D8 (15)
#define SD0 (7)
#define SD1 (8)
#define SD2 (9)
#define SD3 (10)
//==NodeMCU==END
//==WIFI==START
#include <ESP8266WiFi.h>
const char* ssid = "xxxx-xxx";
const char* password = "xxxxxxxxxx";
//==WIFI==END
//==LCD_20by4==START
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//==LCD_20by4==END
//==LED BLINK==START
#define BUILTIN_LED D4
volatile bool statusLed = false;
//==LED BLINK==END
//==NTP==START
#include <time.h>
#include <sys/time.h>
#include <coredecls.h>
#define TZ 8
#define DST_MN 60
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)
timeval cbtime;
bool cbtime_set = false;
timeval tv;
timespec tp;
time_t now;
void time_is_set (void)
{
gettimeofday(&cbtime, NULL);
cbtime_set = true;
}
int now_ss;
void loop_NTP()
{
gettimeofday(&tv, nullptr);
static time_t lastv = 0;
if (lastv != tv.tv_sec) {
lastv = tv.tv_sec;
now_ss = (lastv % 60);
static bool started = false;
if (!started)
{
started = true;
pinMode(BUILTIN_LED, OUTPUT);
}
digitalWrite(BUILTIN_LED, statusLed);
statusLed = !statusLed;
}
}
//==NTP==END
//==Time Sync CHECK==START
unsigned int g_hour = 0;
unsigned int g_minute = 0;
unsigned int g_second = 0;
unsigned int g_mday = 0;
unsigned int g_mon = 0;
unsigned int g_year = 0;
int old_ss;
void time_sync_check_func(void)
{
if(old_ss != now_ss)
{
time_t rawtime;
struct tm* local_timeinfo;
time(&rawtime);
local_timeinfo = localtime(&rawtime);
if((local_timeinfo->tm_year+1900) >= 2022)
{
g_hour = local_timeinfo->tm_hour;
g_minute = local_timeinfo->tm_min;
g_second = local_timeinfo->tm_sec;
g_mday = local_timeinfo->tm_mday;
g_mon = local_timeinfo->tm_mon+1;
g_year = local_timeinfo->tm_year+1900;
}
else
{
Serial.println("Error Time Sync=========!!");
}
time_show();
old_ss = now_ss;
}
}
//==Time Sync CHECK==END
//==LCD_20by4==START
void start_text(void) {
Serial.println("\nlcd.init..");
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("LCD TEST!");
lcd.setCursor(0,1);
lcd.print("0xABCDEF");
lcd.setCursor(0,2);
lcd.print("1234567890");
lcd.setCursor(0,3);
lcd.print("0987654321");
}
void lcd_text(int x, int y, String text,bool reset_flag) {
if(reset_flag)
{
lcd.clear();
lcd.setCursor(x,y);
lcd.print(text);
}
else
{
lcd.setCursor(x,y);
lcd.print(text);
}
}
void time_show(void)
{
static bool colon_view = true;
String g_year_str = String(g_year);
String g_mon_str = String(g_mon);
String g_mday_str = String(g_mday);
if(g_mon < 10)
g_mon_str = " " + String(g_mon);
if(g_mday < 10)
g_mday_str = " " + String(g_mday);
String text_str_day = g_year_str + "/" + g_mon_str + "/" + g_mday_str;
String text_str_time;
String g_hour_str = String(g_hour);
if(g_hour < 10)
g_hour_str = " " + String(g_hour);
String g_minute_str = String(g_minute);
if(g_minute < 10)
g_minute_str = "0" + String(g_minute);
String g_second_str = String(g_second);
if(g_second < 10)
g_second_str = "0" + String(g_second);
if(colon_view)
{
text_str_time = g_hour_str + ":" + g_minute_str + ":" + g_second_str;
colon_view = false;
}
else
{
text_str_time = g_hour_str + " " + g_minute_str+ " " + g_second_str;
colon_view = true;
}
lcd_text(0, 1, text_str_day, true);
lcd_text(0, 2, text_str_time, false);
}
//==LCD_20by4==END
void setup(void) {
Serial.begin(115200);
Serial.println();
delay(3000);
//==LCD_20by4==START
lcd.begin(20,4);
start_text();
delay(2000);
//==LCD_20by4==END
//==WIFI==START
Serial.println("WIFI !");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("\nConnecting to "); Serial.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 600) delay(500);
if(i == 601){
Serial.print("Could not connect to"); Serial.println(ssid);
while(1) delay(500);
}
else
{
Serial.print("\nConnected: "); Serial.println(ssid);
}
//==WIFI==END
//==NTP==START
settimeofday_cb(time_is_set);
configTime(TZ_SEC, DST_SEC, "pool.ntp.org");
//==NTP==END
} //setup()
void loop(void) {
//==Time Sync CHECK==START
time_sync_check_func();
//==Time Sync CHECK==END
//==NTP==START
loop_NTP();
//==NTP==END
} //loop()
|
cs |
Result
lcd.init.. WIFI ! Connecting to xxxx-xxx Connected: xxxx-xxx Error Time Sync=========!! |
'ESP8266' 카테고리의 다른 글
esp8266 - udp (send, receive) (0) | 2023.01.05 |
---|---|
esp8266 - multicast with python pc (0) | 2023.01.02 |
esp8266 - lcd 16x2 and ntp (0) | 2023.01.02 |
esp8266 - i2c 주소 찾기 ( find i2c address) (0) | 2023.01.02 |
esp8266 - led 깜빡이기 (blink) (0) | 2023.01.02 |