Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karakuri-musha/0b55f0a926f4a249145c5abb380b65f7 to your computer and use it in GitHub Desktop.
Save karakuri-musha/0b55f0a926f4a249145c5abb380b65f7 to your computer and use it in GitHub Desktop.
//--------------------------------------------------------------------------------
// Arduino UNO R4 WiFI WiFi接続 テストプログラム
//
// Arduino UNO R4 WiFI でWiFiルーターへ接続する
//
// Hardware : Arduino UNO R4 WiFI
//
// Author : Genroku@Karakuri-musha.com
//
// ※WiFi.Statusの状態
// WL_IDLE_STATUS = 0
// WL_NO_SSID_AVAIL = 1
// WL_SCAN_COMPLETED = 2
// WL_CONNECTED = 3
// WL_CONNECT_FAILED = 4
// WL_CONNECTION_LOST = 5
// WL_DISCONNECTED = 6
// WL_NO_SHIELD = 256
//--------------------------------------------------------------------------------
// ------------------------------------------------------------
// ライブラリインクルード部 Library include section.
// ------------------------------------------------------------
#include <WiFiS3.h>
// ------------------------------------------------------------
// 定数/変数 定義部 Constant / variable definition section.
// ------------------------------------------------------------
char ssid[] = "Your SSID"; // WiFiルーターに接続する際の SSID (name) を入力してください。
char pass[] = "Your SSID Password"; // WiFiルーターに接続する際の password を入力してください。
int status = WL_IDLE_STATUS; // WiFiの状態格納
// ------------------------------------------------------------
// 関数 定義部 Function definition section.
// ------------------------------------------------------------
//---------------------------
// WiFi接続情報の表示関数
//---------------------------
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
//---------------------------
// 接続情報の表示関数
//---------------------------
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
//---------------------------
// MACアドレスの表示関数
//---------------------------
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
//---------------------------
// SetUp関数
//---------------------------
void setup() {
// シリアル接続の開始
Serial.begin(115200);
delay(1500);
// WiFiモジュールの確認
if (WiFi.status() == WL_NO_MODULE) { // WiFiモジュールがない場合の処理
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion(); // WiFiモジュールのファームバージョンの取得
if (fv < WIFI_FIRMWARE_LATEST_VERSION) { // ファームバージョンの確認(古い場合はアップデートを促すメッセージ)
Serial.println("Please upgrade the firmware");
}
// WiFi接続
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid); // WiFi接続に使うSSIDをシリアルモニタに表示
status = WiFi.begin(ssid, pass); // WiFi接続
while (status != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
//---------------------------
// Loop関数
//---------------------------
void loop() {
if (status == WL_CONNECTED) {
//Wi-Fiからの切断
Serial.println("Disconnect from WiFi");
WiFi.disconnect();
// Wi-Fi接続の状況を監視(WiFi.statusがWL_IDLE_STATUS:(0)になるまで繰り返し
// 動作確認では、WiFi.statusが0になった状態でネットワーク接続が切れていることを確認
// statusがWL_IDLE_STATUS:(0)で判定
while (WiFi.status() != WL_IDLE_STATUS) {
delay(500);
Serial.print(".");}
status = WiFi.status();
// Wi-Fi切断結果をシリアルモニタへ出力
Serial.println("");
Serial.println("WiFi disconnected");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment