2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Arduino IDE + Raspberry Pi Pico + ESP-01で時計

Last updated at Posted at 2025-02-02

概要

Raspberry Pi Pico と ESP-01 で時計を作成します。
時刻はSNTPで取得します。
下記のWiFiライブラリーを利用します。
https://github.com/JAndrassy/WiFiEspAT

IMG_20250129_190410508_S.jpg
写真の赤線の枠内が ESP-01 です。

開発環境

Arduino IDE for Raspberry Pi Pico on Windows 11

材料

  • [Cytron] Maker Pi Pico Base (開発用基板)
  • Raspberry Pi Pico
  • ESP-01(WiFi module)
  • SSD1306(OLED)
  • GROVE - 4ピン-ジャンパメスケーブル

詳細

サンプルプログラム SNTPTime が動くように修正します。
AT+CIPSNTPTIME? コマンドの返値の文字列を time_t 型に変換します。

Arduino\libraries\WiFiEspAT\src\utility\EspAtDrv.cpp を下記のように修正します。

sntpTime関数の上にparseTimeString関数を追加します。

parseTimeString()
unsigned long parseTimeString(const char* timeString) {
  struct tm tm;
  static const char *md[] = {"Jan", "Feb", "Mar", "Apr",
  	"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  char dayName[4], monthName[4];
  int month, year;

  sscanf(timeString, "%s %s %d %d:%d:%d %d",
  	dayName, monthName, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &year);
  for(month=0; month<12; month++) if (strcmp(md[month], monthName) == 0) break;
  tm.tm_year = year - 1900;
  tm.tm_mon = month;
  return mktime(&tm);
}

sntpTime関数を下記の通り修正します。

sntpTime()
unsigned long EspAtDrvClass::sntpTime() {
  maintain();

  LOG_INFO_PRINT_PREFIX();
  LOG_INFO_PRINTLN(F("SNTP time"));

#ifdef WIFIESPAT1
-  cmd->print(F("AT+SNTPTIME?")); // AT LoBo firmware command
+  cmd->print(F("AT+CIPSNTPTIME?"));
-   if (!sendCommand(PSTR("+SNTPTIME")))
+   if (!sendCommand(PSTR("+CIPSNTPTIME")))
    return 0;
-  char* tok = strtok(buffer + strlen("+SNTPTIME:"), ",");
+  char* tok = buffer + strlen("+CIPSNTPTIME:");
-  unsigned long res = strtoul(tok, NULL, 10);
+  unsigned long res = parseTimeString(tok);
#else
  cmd->print(F("AT+SYSTIMESTAMP?"));
  if (!sendCommand(PSTR("+SYSTIMESTAMP")))
    return 0;
  unsigned long res = strtoul(buffer + strlen("+SYSTIMESTAMP:"), NULL, 10);
#endif
  readOK();
  return res;
}

まとめ

WiFiEspATライブラリーは ESP8266_AT_LoBo Firmware に対応しています。
AT+SNTPTIME?
+SNTPTIME:1556721502,2019-05-01 14:38:22
https://github.com/loboris/ESP8266_AT_LoBo

私が入手したESP-01は下記のコマンドになっています。
AT+CIPSNTPTIME?
+CIPSNTPTIME:Sun Feb 02 10:07:55 2025
Firmwareの更新は難しそうなので WiFiEspAT ライブラリーの修正で対応しました。

この投稿にはOLEDへの表示処理は含めていません。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?