おはようございます。
早速真似て、作ってみました。
左:USB-シリアル経由のTera Termでローカル接続、右:ChromeからHTTP接続
こちらがMicroPythonのコードになります。Wi-Fiルーターの接続に必要となるSSIDとPASSWORDは、個人の環境に依存します。
ターミナルプログラム(例:Tera Term)から、CTRL-Aコマンドでraw REPL モードに入ってダウンロード後に、CTRL-D コマンドを実行すると
HTTPサーバーが起動します。
サーバーのポート#は、80 です。
ESP32のGPIOは、
- ピン 1 と 3 は、それぞれ REPL UART の TX と RX
- ピン 6, 7, 8, 11, 16, 17 は内蔵フラッシュの接続に使っているので、他の目的で使うのは推奨しません
とのことなので表示する対象から除いてあります。
更新しました。2020/10/15
#simple_http_server.py
#https://micropython-docs-ja.readthedocs.io/ja/latest/esp8266/tutorial/network_tcp.html#simple-http-server
import network
import socket
import machine
def do_connect(id, pw):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(id, pw)
while not wlan.isconnected():
pass
print('\nIP/netmask/gw/DNS addresses:', wlan.ifconfig())
print('MAC address:', wlan.config('mac'))
html = """<!DOCTYPE html>
<html>
<head> <title>waves ESP32 ESPDuino-32 (ESP-WROOM-32) Pins</title> </head>
<body> <h1>waves ESP32 ESPDuino-32 (ESP-WROOM-32) Pins</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""
#https://micropython-docs-ja.readthedocs.io/ja/latest/esp32/quickref.html#pins-and-gpio
pins = [machine.Pin(i, machine.Pin.IN) for i in (0,
2, 4, 35, 34, 36, 39,
26, 25, 17, 16, 27, 14, 12, 13,
5, 23, 19, 18)]
do_connect('SSID', 'PASSWORD')
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
response = html % '\n'.join(rows)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
【参照記事】
https://ameblo.jp/kissam59/entry-12630056916.html
https://ameblo.jp/kissam59/entry-12630452888.html
【参考記事】
http://docs.micropython.org/en/latest/esp32/quickref.html#pins-and-gpio
https://micropython-docs-ja.readthedocs.io/ja/latest/esp32/quickref.html#pins-and-gpio