SimpleWiFiServerをアレンジしてゆく
WiFiコントロール・カーを遠隔操作
Just real WordPress site
SimpleWiFiServerをアレンジしてゆく
WiFiコントロール・カーを遠隔操作
LEDチカチカまできたので次はWIFIでスマホからLEDを点けるに挑戦。
おいらのHTTPなどの知識が古いかも。まあ見てゆけば分かるだろう。
まずは例題ライブラリをダウンロードせよということで以下を実行。
問題なくうまく行った。pythonの版(2.7/3.5.3.6混在なmac)で何か起こるかもと思ったが問題なかった。
$>mkdir -p ~/Documents/Arduino/hardware/espressif $>cd ~/Documents/Arduino/hardware/espressif $>git clone https://github.com/espressif/arduino-esp32.git esp32 $>cd esp32 $>git submodule update --init --recursive $>cd tools $>python get.py
Arduinoを立ち上げて以下のサンプルファイルを開く。
~/Documents/Arduino/hardware/espressif/esp32/libraries/WiFi/examples/SimpleWiFiServer/SimpleWiFiServer.ino
ソース30, 31行目を家のWiFiルータのSSIDとパスワードに書きかえ。
/* WiFi Web Server LED Blink */ #include <WiFi.h> const char* ssid = "wx03-xxxxx"; const char* password = "xxxxxxxxx"; WiFiServer server(80); void setup() { Serial.begin(115200); pinMode(5, OUTPUT); // set the LED pin mode delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } int value = 0; void loop(){ WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("New Client."); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); // the content of the HTTP response follows the header: client.print("<h1>Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.</h1>"); client.print("<h1>Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.</h1><br>"); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } // Check to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(5, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(5, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } }
●シリアルモニタに表示
ブラウザにESPのIP(以下赤文字)を入れると以下表示されてくる。
※ESP32側は5ピンにLEDを付けておく
Click here to turn the LED on pin 5 on.
Click here to turn the LED on pin 5 off.
●シリアルモニタ表示(リセットして点灯をクリックを実施)
畳んでいるモニタ表示をオープン
rst:0x1 (POWERON_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
Connecting to wx03-bc7caa
………….
WiFi connected.
IP address:
192.168.179.7
New Client.
GET /H HTTP/1.1
Host: 192.168.179.7
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.179.7/L
Accept-Encoding: gzip, deflate
Accept-Language: ja,en-US;q=0.9,en;q=0.8
Client Disconnected.
New Client.
GET /favicon.ico HTTP/1.1
Host: 192.168.179.7
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.179.7/H
Accept-Encoding: gzip, deflate
Accept-Language: ja,en-US;q=0.9,en;q=0.8
Client Disconnected.
これは楽だわ。便利を使わない手はないな。小生組み込み屋だったから意地を見せてIDEネーティブ環境でと思ったけど最初はこれでいい。
マイコンが電子屋以外で活用できると電子系の輩でない人達の感性が反映されていいものできるチャンスは沢山ありかなと期待。久々にmac mini の出番があっていい。だいぶご無沙汰なのでいい機会だ。
int led = 14;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(led, OUTPUT);
Serial.println("LED Test Start");
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
ピン配置
●考察
これをやるともうPCのWIFIにESP32というWIFI名がが出てこなくなる。初期のファームでコマンドラインのときには出ていた。そういうことだね。プログラムで指定しない限りはということだ。ライブラリのインクルードが必要だ。
環境構築でだいぶ手間取った。まだ不足はあると思うけど。まずはESP-IDFで開発することにしよう。一通り動作させたら micro Pythonもやってみる。気になっていたキットに入っていたファームのバイナリも吸い上げた。まだ再書き込みやってないけど。大丈夫ということで。GitHubにもおいてあるだろし。
ここが参考になった(多謝)
まず、
$>cd hello_world
$>make menuconfig でシリアルポートを設定(tty.SLAB_USBtoUART)しておくとのこと。save しないで抜けるとエラーでる。再度やって確認して save で抜けるとOKだった。
$>make
エラーがでた。pythonの版が云々だ。
Mac-mini:hello_world m_kurosaki$ make
Toolchain path: /Users/m_kurosaki/esp/xtensa-esp32-elf/bin/xtensa-esp32-elf-gccToolchain version: crosstool-ng-1.22.0-80-g6c4433a
Compiler version: 5.2.0
The following Python requirements are not satisfied:
future>=0.15.2
Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required packages. Alternatively, you can run “/Users/m_kurosaki/anaconda/bin/python -m pip install –user -r /Users/m_kurosaki/esp/esp-idf/requirements.txt” for resolving the issue.
make: *** [check_python_dependencies] Error 1
ということで素直にanaconda/bin/で以下実行
$>python -m pip install –user -r /Users/m_kurosaki/esp/esp-idf/requirements.txt
successとでたので再度
$>make
buildできたということなので書き込み。
※ここまで長いことかかった(youtubeのフラメンコギターがいい旋律だな)。
BGMないと眠くなってしまうのでアグレシブな曲で!
$>make flash
書き込まれた。make flash でmake と flash 書き込みまでやってくれる。
$>make flash monitor
これだと一気に確認表示まで行ける。CTRL+] 停止できる。
$>screen /dev/tty.SLAB_USBtoUART 115200
で表示させる。これはCTRL+] では止められない。
本日はここまでとしよう。いい終わり方というのも大事だからね。
参考にしたサイトの解説にあったけどプロトタイプはAuduinoで楽しろとある。なるほどな話だな。pythonもやってみる。対話型ってのが安心できいていい。