この週末にESP32で2つの実験をしたので、簡単にまとめます。プログラムは記事の後ろのほうにつけておきます。
1つ目はスタンドアロンーンのWeb.サーバー(つまり既存のWiFi LANを使わないアクセスポイント)。2つ目は勝手な「リモート・サーボ」をESPとステッパーで作ってみたので紹介。
この記事は、まず1つ目の独立WiFiサーバーです。ありきたりのようにも見えますが、これが便利だと思う勝手な理由は次の3つ。
①ふつうは立ち上げ時にメインのWiFi-LANにつないで、その中のIPアドレスとして動かすのですが、場合によりいちいちMACアドレスフィルタリングを通さないといけません。その手間がたいへん(MACアドレスフィルタリングを使ってない場合は手間はないですがSecurity心配ですね)。
②AlexaやGoogleHomeなどがたくさん加わり、WiFiルーターによっては数の限界に近づいています。そして電子工作を楽しむ人にとっては、とにかくESPマイコンの最近の低価格化はありがたい事でESP8266Development Boardに至っては、海外ネットで今や200円台で買えますから、何にでも使うわけでどんどん増えるわけ^^;
③既存のWiFi-LANの中で動かすと、場所を変える場合には、最初の接続SSIDの変更または追加を要する。スタンドアローンのアクセスポイントにすれば、その手間がいらない、つまりプログラムをいじらなくてよい。
ここで使うESP32DevelopmentBoardは次の38ピンのもので、次のように結線。
電源はUSBからの5Vでよいですが、自由に持ち回るときは上のように電池をつなぎます。最近のESP development Boardは比較的消費電流が小さくなりました(とはいえ実行中は100mA程度)。
ブレッドボードは、普通の1列5ピンのは使いにくいので6ピン配置のもの。これは秋月でも売っています。
とはいっても、実はどんなブレッドボードでも工夫すれば自由に使えます。そのことは次回の記事で書くことにします。
なお、Arduino-IDEでESP32ボードを開発するためには、ESP32用のボードマネジャーをArduino IDEへインストールする必要があります。導入法はネットの色々なところに出ていますが、Windowsでの現時点の入れ方を簡単に書いておきます。
====================
・ESP32をArduino-IDEで開発するためのソフト導入
Arduino IDE自体は次のアドレスから導入します。https://www.arduino.cc/
そしてIDEを開いたら、上のメニューバーにある「ファイル」→「環境設定」をクリックします。
環境設定画面の下のほうにある「追加のボードマネジャのURL欄」にボードマネジャーのあるURLアドレスを入力しますが、ESP32の場合は次を入力。
https://dl.espressif.com/dl/package_esp32_index.json
次に、IDE本体のメニューバーで「ツール」→「ボード」→「ボードマネジャ」と進むと使えるボードマネジャーのメニューが表示されるので、その中から
esp32by Espressif Systemsと表示されている欄内の右のほうにある「インストール」をクリックすれば、導入が始まり、しばらくすると終わります。
====================
IDEで後述するプログラムを作って、「ツール」で「ESP32 Dev Module」を選んで書き込む際に、画面の下の欄に赤色で書き込み開始の合図「・・・___」が始まったらESP32開発ボード上でUSB端子の隣にあるBootボタンを押すと書き込まれます。Arduinoと違って書き込み量が多いので少し時間がかかります。
そうすると「勝手なWiFi-LANサーバー」が立ち上がり、勝手に決めたアドレスが独立のアクセスポイントとなります。PCやスマホなどでこれを選んで(PWはもちろん入れて)接続します。もちろんPCやスマホでなく、工作用には他のESPからでもよいわけですが、、(あたりまえか)。
ここでのプログラム(この記事の後ろのほうに載せます)ではIPアドレス192.168.32.2を指定しています。PCやスマホならインターネットブラウザーでそこをアクセスすると次の画面。PCでも同じです。簡単な実験のためにテキストだけのページですが。
そしてリンクをタッチすれば指定LEDが点きます。
この実験装置全体の様子は次です。下の写真ではサーバーの電源をUSBで供給しています。
このサーバーの電源としては、前述のように電池(単三電池を3本直列)をつかっても問題ありません。そうすれば動くものやロボカーなどにも便利です。
さて、こうやって工作室に勝手なWiFi-LANをどんどん作るとどうなるか?
PCやスマホにSSIDがじゃんじゃん表示されますし、運用場所によっては近所からも丸見えですからあまり変なSSID名を使わないのがいいですね^^;
最後にこの実験用に作ったプログラムをつけておきます。
/********************************************************
* Independent Access-Point - ESP32 simple example
* Initial version V.00 Sept.26 2020
* by Akira Tominaga
********************************************************/
#include "WiFi.h"
const char ssid[] = "HOGEHOGE-00X"; // *** set any ssid ***
const char pass[] = "hoge00xpw"; // *** set any pw ***
const IPAddress ip(192, 168, 32, 2); // *** set any addr ***
const IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
// ESP32 GPIO pin definitions (should not be #define)
gpio_num_t Red = GPIO_NUM_21; // Red LED
gpio_num_t Green = GPIO_NUM_22; // Green LED
gpio_num_t Blue = GPIO_NUM_23; // Blue LED
void setup() { // ***** Arduino (ESP32) Setup *****
Serial.begin(9600);
pinMode(Red, OUTPUT); // Red pin as output
digitalWrite(Red, LOW);
pinMode(Green, OUTPUT); // Green pin as output
digitalWrite(Green, LOW);
pinMode(Blue, OUTPUT); // Blue pin as output
digitalWrite(Blue, LOW);
delay(100);
WiFi.softAP(ssid, pass);
delay(100);
WiFi.softAPConfig(ip, ip, subnet);
IPAddress myIP = WiFi.softAPIP();
server.begin();
Serial.print("SSID= ");
Serial.println(ssid);
Serial.print("Fixed IP addr= ");
Serial.println(myIP);
Serial.println("Server starting!");
}
void loop() { // ***** Arduino (ESP32) Loop *****
WiFiClient client = server.available();
if (client) { // if accessed
Serial.println("Accessed");
String inMsg = "";
while (client.connected()) { // loop while client connected
if (client.available()) { // if a message,
char c = client.read(); // read each byte, and
Serial.write(c); // write to Serial
if (c == '\n') { // if LF and
if (inMsg.length() == 0) { // if new, send response
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println(); // send NewLine
client.print("
- Touch to switch LED on/off -
");
client.print("
- Switch Red-on
- Switch Green-on
- "); client.print("
- Switch Blue-on
- "); client.print("
- "); client.print("
- Switch Off
");
client.println(); // send another NewLine
break; // go out of while loop
} else { // if not new
inMsg = ""; // initialize inMsg
}
} else if (c != '\r') { // when not (c == '\n') ,too
inMsg += c; // add to inMsg
}
if (inMsg.endsWith("GET /R")) {
digitalWrite(Red, HIGH); // Red on
}
if (inMsg.endsWith("GET /G")) {
digitalWrite(Green, HIGH); // Green on
}
if (inMsg.endsWith("GET /B")) {
digitalWrite(Blue, HIGH); // Blue on
}
if (inMsg.endsWith("GET /F")) {
digitalWrite(Red, LOW); // Red off
digitalWrite(Green, LOW); // Green off
digitalWrite(Blue, LOW); // Blue off
}
} // end of (client.available())
} // end of (client.connected())
client.stop();
Serial.println("Client Disconnected.");
} // end of (client)
}
// end of program
上のプログラムはPreとCodeで挟みましたが、肝心のHTMLを送るところはソースが表示されず、結果が表示されてしまいますね。はてなブログでのタグの認識防止方法がまだしらべきれてないので、当面はそこの行以下最後までを次に直接かいておきます^^; 良い方法がわかった時点で直しますが、たいへんすみません<(_ _)>
if (c == '\n') { // if LF and
if (inMsg.length() == 0) { // if new, send response
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println(); // send NewLine
client.print("<font size=15 clolor=black><br>- Touch to switch LED on/off -<br>");
client.print("<ul><li>Switch <a href=\"/R\"><font color=red>Red-on</font></a></li><br>");
client.print("<li>Switch <a href=\"/G\"><font color=green>Green-on</font></a></li><br>");
client.print("<li>Switch <a href=\"/B\"><font color=blue>Blue-on</font></a></li><br>");
client.print("<li>Switch <a href=\"/F\"><font color=black>Off</font></a></li></ul></font>");
client.println(); // send another NewLine
break; // go out of while loop
} else { // if not new
inMsg = ""; // initialize inMsg
}
} else if (c != '\r') { // when not (c == '\n') ,too
inMsg += c; // add to inMsg
}
if (inMsg.endsWith("GET /R")) {
digitalWrite(Red, HIGH); // Red on
}
if (inMsg.endsWith("GET /G")) {
digitalWrite(Green, HIGH); // Green on
}
if (inMsg.endsWith("GET /B")) {
digitalWrite(Blue, HIGH); // Blue on
}
if (inMsg.endsWith("GET /F")) {
digitalWrite(Red, LOW); // Red off
digitalWrite(Green, LOW); // Green off
digitalWrite(Blue, LOW); // Blue off
}
} // end of (client.available())
} // end of (client.connected())
client.stop();
Serial.println("Client Disconnected.");
} // end of (client)
}
// end of program
この記事は、自分のまとめのために書きましたが、もしもお役に立つことがあれば幸いです。
次回はESPとステッパーで作った「勝手なリモートサーボ」について、近日中に書きたいと思います。
©2020 Akira Tominaga, All rights reserved.