Created
January 12, 2018 05:28
-
-
Save Tomokatsu-Sakamoto/321a90143636f78d94f0ec3bc2098316 to your computer and use it in GitHub Desktop.
ESP-WROOM-02 を使ったネットワーク時計 ※LCDに表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ESP-WROOM-02 + LCD で現在時刻を表示(NTPで現在時刻を取得) | |
https://qiita.com/exabugs/items/b1b7430d185b268a1abf | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <NTP.h> | |
#include <ST7032.h> | |
#include "MyWiFi.h" // このファイルの中では ssid と password を定義 | |
#ifndef ssid // 他の場所で SSID が設定されていなかった時は、ここで... | |
#define ssid "SSID" | |
#endif | |
#ifndef password // 他の場所で Password が設定されていなかった時は、ここで... | |
#define password "password" | |
#endif | |
#define RETRY_MAX 40 | |
#define ntp_server "xx.xx.xx.xx" // 使用する NTP サーバーを記述する | |
ST7032 lcd; | |
char lineBuff[ 16 ]; // LCDの1行分バッファ | |
void setup() { | |
int iCount = 0; | |
int iStatus; | |
ESP.eraseConfig(); | |
delay( 500 ); | |
ESP8266_Information( ); | |
Serial.begin( 115200 ); | |
Serial.println( ); | |
Serial.printf("Serial is %d bps", Serial.baudRate( ) ); | |
ESP8266_Information(); | |
lcd.begin(8, 2); | |
lcd.setContrast(30); // コントラスト設定 | |
lcd.clear( ); | |
lcd.print("Wi-Fi "); | |
lcd.setCursor( 0, 1 ); | |
lcd.print("connect."); | |
lcd.blink( ); | |
// 自分のネットワークに合わせてください | |
Serial.print( "Connect to [" ); | |
Serial.print( ssid ); | |
Serial.println( "]." ); | |
WiFi.mode( WIFI_STA ); | |
WiFi.begin( ssid, password ); | |
WiFi.printDiag(Serial); | |
while ( ( iStatus = WiFi.status( ) ) != WL_CONNECTED ) { | |
delay( 500 ); | |
Serial.printf( "%d", iStatus ); | |
iCount++; | |
if ( iCount >= RETRY_MAX ) { | |
iCount = 0; | |
Serial.print( " " ); | |
WiFi_Status( ); | |
WiFi.printDiag( Serial ); | |
WiFi.reconnect(); | |
} | |
} | |
Serial.println( "" ); | |
Serial.print( "WiFi connected." ); | |
Serial.print( "Connected as " ); | |
Serial.println( WiFi.localIP( ) ); | |
lcd.setCursor( 0, 1 ); | |
lcd.print(" OK! "); | |
lcd.blink( ); | |
// 初期化 | |
// 2390 はローカルのUDPポート。空いている番号なら何番でもいいです。 | |
ntp_begin( 2390 ); | |
// NTPサーバを変更 (デフォルト: ntp.nict.jp) | |
setTimeServer( ntp_server ); | |
// NTP同期間隔を変更 (デフォルト: 300秒) | |
setSyncInterval(10); | |
} | |
void display() { | |
time_t n = now(); | |
time_t t; | |
char s[20]; | |
const char* format = "%04d-%02d-%02d %02d:%02d:%02d"; | |
int iDY, iDM, iDD, iTH, iTM, iTS; | |
// JST | |
t = localtime( n, 9 ); | |
iDY = year( t ); | |
iDM = month( t ); | |
iDD = day( t ); | |
iTH = hour( t ); | |
iTM = minute( t ); | |
iTS = second( t ); | |
sprintf( s, format, iDY, iDM, iDD, iTH, iTM, iTS ); | |
Serial.print( "JST : " ); | |
Serial.print( s ); | |
lcd.setCursor( 0, 0 ); | |
sprintf( lineBuff, "%02d/%2d/%2d", ( iDY % 100 ), iDM, iDD ); | |
lcd.print( lineBuff ); | |
lcd.setCursor( 0, 1 ); | |
sprintf( lineBuff, "%2d:%02d:%02d", iTH, iTM, iTS ); | |
lcd.print( lineBuff ); | |
// UTC | |
t = localtime( n, 0 ); | |
sprintf( s, format, year( t ), month( t ), day( t ), hour( t ), minute( t ), second( t ) ); | |
Serial.print( " UTC : " ); | |
Serial.println( s ); | |
} | |
void loop( ) { | |
display( ); | |
delay( 1000 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ESP8266_Information( void ) | |
{ | |
// https://www.mgo-tec.com/blog-entry-chip-info-esp-wroom-02-esp8266.html | |
// 上記 URL にある情報をもとに、ESP-WROOM-02 のボード情報を表示する | |
Serial.println( "" ); | |
Serial.println( "" ); | |
Serial.println( "" ); | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.println( | |
"--- ESP-WROOM-02 ( ESP8266 ) Chip Infomation ---" ); | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.print( | |
"Core Version = " ); | |
Serial.println( | |
ESP.getCoreVersion( ) ); | |
Serial.print( | |
"CPU Frequency = " ); | |
Serial.print( ESP.getCpuFreqMHz( ) ); | |
Serial.println( | |
" MHz" ); | |
Serial.print( | |
"ChipID = " ); | |
Serial.println( ESP.getChipId( ), HEX ); // MACアドレスの下位3バイト | |
Serial.print( | |
"Flash ID = " ); | |
Serial.println( ESP.getFlashChipId( ), HEX ); | |
Serial.print( | |
"SDK version = " ); | |
Serial.println( ESP.getSdkVersion( ) ); | |
Serial.print( | |
"Boot version = " ); | |
Serial.println( ESP.getBootVersion( ) ); | |
Serial.print( | |
"Boot Mode = " ); | |
Serial.println( ESP.getBootMode( ) ); | |
Serial.print( | |
"Flash Chip IDE Size = " ); | |
Serial.print( ESP.getFlashChipSize( ) ); // Arduino IDE 設定の Flash Size になる | |
Serial.println( | |
" byte" ); | |
Serial.print( | |
"Flash Chip Real Size = " ); | |
Serial.print( | |
ESP.getFlashChipRealSize( ) ); // ESP-WROOM-32 内蔵最大 Flash Size になる | |
Serial.println( | |
" byte" ); | |
Serial.print( | |
"Flash Frequency = " ); | |
Serial.print( | |
ESP.getFlashChipSpeed( ) ); | |
Serial.println( | |
" Hz" ); | |
String mode_str; | |
switch ( ESP.getFlashChipMode() ) { | |
case 0: mode_str = "QIO"; break; | |
case 1: mode_str = "QOUT"; break; | |
case 2: mode_str = "DIO"; break; | |
case 3: mode_str = "DOUT"; break; | |
default: mode_str = "Unknown"; break; | |
} | |
Serial.println( | |
"Flash Chip Mode = " + mode_str ); | |
Serial.print( | |
"Free Heap Size = "); | |
Serial.println( ESP.getFreeHeap( ) ); | |
Serial.print( | |
"Free Sketch Size = " ); | |
Serial.println( ESP.getFreeSketchSpace( ) ); | |
Serial.print( | |
"Sketch Size = " ); | |
Serial.println( ESP.getSketchSize( ) ); | |
Serial.println( | |
"------------------------------------------------" ); | |
uint8_t mac0[ 6 ]; | |
WiFi.macAddress( mac0 ); | |
Serial.printf( | |
"WiFi StationAP Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", | |
mac0[ 0 ], mac0[ 1 ], mac0[ 2 ], mac0[ 3 ], mac0[ 4 ], mac0[ 5 ] ); | |
uint8_t mac1[ 6 ]; | |
WiFi.softAPmacAddress( mac1 ); | |
Serial.printf( | |
"WiFi SoftAP Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", | |
mac1[ 0 ], mac1[ 1 ], mac1[ 2 ], mac1[ 3 ], mac1[ 4 ], mac1[ 5 ] ); | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.println( ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////////////////////// | |
// 現在の Wi-Fi 状態を表示 | |
int WiFi_Status( void ) | |
{ | |
int status = WL_IDLE_STATUS; // the Wifi radio's status | |
Serial.print( | |
"WiFi.status( ) = " ); | |
switch ( status = WiFi.status( ) ) { | |
case WL_CONNECTED: | |
// assigned when connected to a WiFi network; | |
Serial.println( "WL_CONNECTED" ); | |
break; | |
case WL_NO_SHIELD: | |
// assigned when no WiFi shield is present; | |
Serial.println( "WL_NO_SHIELD" ); | |
break; | |
case WL_IDLE_STATUS: | |
// it is a temporary status assigned when WiFi.begin() is called and | |
// remains active until the number of attempts expires (resulting in | |
// WL_CONNECT_FAILED) or a connection is established (resulting in | |
// WL_CONNECTED); | |
Serial.println( "WL_IDLE_STATUS" ); | |
break; | |
case WL_NO_SSID_AVAIL: | |
// assigned when no SSID are available; | |
Serial.println( "WL_NO_SSID_AVAIL" ); | |
break; | |
case WL_SCAN_COMPLETED: | |
// assigned when the scan networks is completed; | |
Serial.println( "WL_SCAN_COMPLETED" ); | |
break; | |
case WL_CONNECT_FAILED: | |
// assigned when the connection fails for all the attempts; | |
Serial.println( "WL_CONNECT_FAILED" ); | |
break; | |
case WL_CONNECTION_LOST: | |
// assigned when the connection is lost; | |
Serial.println( "WL_CONNECTION_LOST" ); | |
break; | |
case WL_DISCONNECTED: | |
// assigned when disconnected from a network; | |
Serial.println( "WL_DISCONNECTED" ); | |
break; | |
default: | |
Serial.println( "? ? ? ? ? ? ? ? ? ?" ); | |
break; | |
} | |
return status; | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// 現在のネットワーク情報を表示 | |
void WiFi_Information( void ) | |
{ | |
int status = WL_IDLE_STATUS; // the Wifi radio's status | |
byte mac[6]; // the MAC address of your Wifi shield | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.println( | |
"--- WiFi Infomation ---" ); | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.print( | |
"WiFi.localIP( ) = " ); | |
Serial.println( WiFi.localIP( ) ); | |
Serial.print( | |
"WiFi.subnetMask( ) = " ); | |
Serial.println( WiFi.subnetMask( ) ); | |
Serial.print( | |
"WiFi.gatewayIP( ) = " ); | |
Serial.println( WiFi.gatewayIP( ) ); | |
status = WiFi_Status( ); | |
if ( status != WL_CONNECTED ) { | |
Serial.println("Couldn't get a wifi connection"); | |
} | |
else { | |
// if you are connected, print your MAC address: | |
WiFi.macAddress( mac ); | |
Serial.print( | |
"MAC ( Physical Address ) = " ); | |
Serial.print( mac[ 5 ], HEX ); | |
Serial.print( ":" ); | |
Serial.print( mac[ 4 ], HEX ); | |
Serial.print( ":" ); | |
Serial.print( mac[ 3 ], HEX ); | |
Serial.print( ":" ); | |
Serial.print( mac[ 2 ], HEX ); | |
Serial.print( ":" ); | |
Serial.print( mac[ 1 ], HEX ); | |
Serial.print( ":" ); | |
Serial.println( mac[ 0 ], HEX ); | |
} | |
Serial.println( | |
"------------------------------------------------" ); | |
Serial.println( ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment