#include <WiFi.h>
#include <time.h>
#define TIMEZONE_JST (3600 * 9)
#define DAYLIGHTOFFSET_JST (0)
const char* ntp_server1 = "ntp.nict.jp";
const char* ntp_server2 = "pool.ntp.org";
static char *ssid[] = {"ssid_1","ssid_2"};
static char *password[] = {"password_1","password_2"};
int wifi_cnt = 2;
static time_t t = 0;
static char clocktime[25]; // 時刻表示用文字列
static char filename[7]; // 撮影用文字列
static char dirname[9]; // ディレクトリ用文字列
static char jpegname[20];
void setup() {
Serial.begin(115200);
delay(100);
for (int i=0;i<wifi_cnt;++i) {
get_ntp(i);
if (t>TIMEZONE_JST) break;
}
}
void get_ntp(int num) {
int cnt = 0;
WiFi.begin(ssid[num], password[num]);
while (WiFi.status() != WL_CONNECTED) {
if ((++cnt)==20) return;
delay(500);
}
configTime(TIMEZONE_JST, DAYLIGHTOFFSET_JST, ntp_server1, ntp_server2);
for (cnt=0;cnt<10;++cnt) {
t = time(NULL);
if (t>TIMEZONE_JST) break;
delay(500);
}
WiFi.disconnect();
}
void getdate() {
static const char *pszWDay[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t now = t + millis()/1000;
struct tm* tm = localtime(&now);
sprintf(clocktime, "%04d/%02d/%02d(%s) %02d:%02d:%02d",
tm->tm_year+1900,
tm->tm_mon+1,
tm->tm_mday,
pszWDay[tm->tm_wday],
tm->tm_hour,
tm->tm_min,
tm->tm_sec );
sprintf(filename,"%02d%02d%02d",tm->tm_hour,tm->tm_min,tm->tm_sec);
sprintf(dirname, "%04d%02d%02d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday);
sprintf(jpegname,"%s/%s.jpg",dirname,filename);
}
void loop() {
getdate();
Serial.println(clocktime);
Serial.print("filename=");
Serial.println(jpegname);
delay(1000);
}