せっかくのIoT機器なので、Wi-Fi接続を試してみます。
Network Overview
https://wiki.seeedstudio.com/Wio-Terminal-Network-Overview/
Wi-Fi 接続する前に、Wi-FiチップRTL8720のファームウェアのアップデートをする必要があります。
Wio Terminal に、ファームウェア書き換え用のプログラムを書き込み。 PCからシリアルでデータを受け取り、それをそのままチップのファームウェアに書き込むプログラムです。(PC側のプログラムは、現状はWindows用しか用意されてないみたいです。)
- Chip Select で AmebaD(8721D)を選択
- 接続しているシリアルポートを選択
- Address に 0x08000000 , Size に 2048 KB を設定して Erase 実行
- ファームウェアの3つのファイルを選択。
- それらのチェックボックスをチェックしてDownlaod実行!
Successfullyが出れば完了です!チェックボックス忘れずに。
僕は最初忘れてて、Wi-Fi動作せずでした…。再度ファームウェアアップデートで解決!
ファームウェアアップデートが完了したら、ドライバーをインストールします。
- Seeed_Arduino_atWiFi
- Seeed-Arduino-FreeRTOS
- Seeed_Arduino_atUnified
- Seeed-Studio/esp-at-lib
- Seeed_Arduino_mbedtls
- Seeed_Arduino_atWiFiClientSecure
6つ。多いですねー。Wi-Fi使うためにこれだけ必要とは。全部zipで取得して手動インストールする必要あります。このあたり、もうちょっとすっきり改善していくといいですね。
インストール終わった後の実際の使い方はこちらに。
Wi-Fi Connectivity
https://wiki.seeedstudio.com/Wio-Terminal-Wi-Fi/
Scanning Wi-Fi Network Example Code で、アクセスポイントのスキャン。
Connecting to Specified Network Example Code で、指定したアクセスポイントへの接続ですね。M5Stackでは非対応だった5GHz帯にも対応してるのが良いですね。
WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(2000); WiFi.begin(ssid, password);
いったんdisconnect呼ぶのがコツなのでしょうか。M5Stackもそうでしたが…。
Webサイト接続して、データ取得してみます。とはいえ、最近のWebサイトはほぼ全部 https 対応してて、http だと接続させてくれないんですよね。どうせやらないといけないので、最初から WiFiClientSecure.h 使って、https で接続してみます。
M5Atomでニュース速報出力した時に使った、Googleトレンド取得を行ってみました。
ほぼ、サンプル通りです。CAは設定してないです。
#include <WiFiClientSecure.h>;
const char* ssid = "yourNetworkName"; // your network SSID
const char* password = "yourNetworkPassword"; // your network password
const char* server = "trends.google.co.jp"; // Server URL
// You can use x.509 client certificates if you want
//const char* test_client_key = ""; //to verify the client
//const char* test_client_cert = ""; //to verify the client
WiFiClientSecure client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while(!Serial); // Wait for Serial to be ready
delay(1000);
Serial.print("Attempting to connect to SSID: ");
//Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}
Serial.print("Connected");
//Serial.println(ssid);
//client.setCACert(test_root_ca);
//client.setCertificate(test_client_key); // for client verification
//client.setPrivateKey(test_client_cert); // for client verification
Serial.println("\nStarting connection to server...");
if (!client.connect(server, 443)) {
Serial.println("Connection failed!");
} else {
Serial.println("Connected to server!");
// Make a HTTP request:
client.println("GET /trends/trendingsearches/daily/rss?geo=JP HTTP/1.0");
client.println("Host: trends.google.co.jp");
client.println("Connection: close");
client.println();
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
if (c == '\n') {
Serial.write('\r');
}
Serial.write(c);
}
client.stop();
}
}
void loop() {
// do nothing
}
実行してみると…
あれ?名前解決に失敗してつながらない…。プログラム変更なしに、何度か再起動すると…
つながった!データ取得もできてる!
が、エラーで途中までしか読んでくれてない…。上の記事、まだ2つめの記事ですね。全然読めてない。うーん、これは厳しい。
というか、こんな大量のデータ(巨大HTML全文とか)を読ませるのは想定外かもしれないですね。短いデータのやり取りならうまくいきそうな気がします。とはいえ、実際に試したわけではないので、気がするだけです…w


