본문 바로가기
ESP8266

esp8266 - lcd 16x2 and ntp

by space father 2023. 1. 2.
반응형

연결 방법

 

ESP8266의  I2C는 다음과 같이 변경 가능함.

수정 전 :Wire.begin() - SDA(D2), SCL(D1))

 

 

 

 

 

 

 

 

 

 

 

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//==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_16by2==START
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x3F,16,2);
//==LCD_16by2==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_16by2==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");
}
 
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(00, text_str_day, true);
  lcd_text(01, text_str_time, false);
}
//==LCD_16by2==END
 
 
void setup(void) {
  
  Serial.begin(115200);
  Serial.println();
  delay(3000);
 
//==LCD_16by2==START
  lcd.begin(16,2);
  start_text();
  delay(2000);  
//==LCD_16by2==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 - multicast with python pc  (0) 2023.01.02
ESP8266 - lcd 20x4 with ntp  (0) 2023.01.02
esp8266 - i2c 주소 찾기 ( find i2c address)  (0) 2023.01.02
esp8266 - led 깜빡이기 (blink)  (0) 2023.01.02
ESP8266-OLED(128x64) and NTP  (0) 2023.01.01