' command lineでOPTION WIFI "Pikara2-91d8e4","090e219cbdbf5"
' つづいて option tcp server port 80
'再設定のためのリセットはoption wifi off,option tcp server 0
ーーーーーーーーーーー WebMite V6.02.01 最小Webサーバーーーーーーーーー
' HTMLファイルを作成
Open "index.html" For Output As #1
Print #1, "<!DOCTYPE html>"
Print #1, "<html>"
Print #1, "<head>"
Print #1, "<meta charset=""UTF-8"">"
Print #1, "<title>WebMite</title>"
Print #1, "</head>"
Print #1, "<body>"
Print #1, "<h1>Hello WebMite!</h1>"
Print #1, "<p>Pico2W WebMite V6.02.01</p>"
Print #1, "</body>"
Print #1, "</html>"
Close #1
' TCPサーバーからの要求を受け取る
WEB TCP INTERRUPT WebInterrupt
Print "Web server started"
Print "IP address = "; MM.INFO(IP ADDRESS)
Do
Pause 1000
Loop
' HTTPリクエスト処理
Sub WebInterrupt
Local a%, p%, t%
Local buff%(4096/8)
For a% = 1 To MM.INFO(MAX CONNECTIONS)
WEB TCP READ a%, buff%()
p% = LINSTR(buff%(), "GET")
t% = LINSTR(buff%(), "HTTP")
If (p% <> 0) And (t% > p%) Then
WEB TRANSMIT PAGE a%, "index.html"
EndIf
Next a%
End Sub
---------最終的にled-on-off-serverは以下のとうり
' ==========================================
' WebMite V6.02.01
' LED ON/OFF Webサーバー
' LED = GP15
' ==========================================
' --- HTMLファイル作成 ---
Open "index.html" For Output As #1
Print #1, "<!DOCTYPE html>"
Print #1, "<html>"ーーーーーーーー
Print #1, "<head>"
Print #1, "<meta charset=""UTF-8"">"
Print #1, "<meta name=""viewport"" content=""width=device-width,initial-scale=1"">"
Print #1, "<title>WebMite LED Control</title>"
Print #1, "</head>"
Print #1, "<body style=""font-family:sans-serif;text-align:center;padding-top:50px"">"
Print #1, "<h1>WebMite LED Control</h1>"
Print #1, "<p>GP15 LED</p>"
Print #1, "<p>"
Print #1, "<a href=""/?cmd=on"">"
Print #1, "<button style=""font-size:24px;padding:15px 30px"">LED ON</button>"
Print #1, "</a>"
Print #1, "</p>"
Print #1, "<p>"
Print #1, "<a href=""/?cmd=off"">"
Print #1, "<button style=""font-size:24px;padding:15px 30px"">LED OFF</button>"
Print #1, "</a>"
Print #1, "</p>"
Print #1, "</body>"
Print #1, "</html>"
Close #1
' --- LED設定 ---
SetPin GP15, DOUT
Pin(GP15) = 0
' --- TCP要求を割り込みで受け取る ---
WEB TCP INTERRUPT WebInterrupt
Print "Web server started"
Print "IP address = "; MM.INFO(IP ADDRESS)
' --- メインループ は、なにもしない---
Do
Pause 1000
Loop
' ==========================================
' HTTPリクエスト処理
' ==========================================
Sub WebInterrupt
Local a%, p%, t%
Local buff%(4096/8)
For a% = 1 To MM.INFO(MAX CONNECTIONS) // connectionごとの処理
WEB TCP READ a%, buff%()
p% = LINSTR(buff%(), "GET")
// LINSTR(buff%(), "GET") は、MMBasicで 文字列配列 buff%() の中から
// "GET" が出てくる位置を探す
t% = LINSTR(buff%(), "HTTP")
// 以上も同様
If (p% <> 0) And (t% > p%) Then
' LED ON
If LINSTR(buff%(), "?cmd=on") <> 0 Then
Pin(GP15) = 1
Print "LED ON"
EndIf
' LED OFF
If LINSTR(buff%(), "?cmd=off") <> 0 Then
Pin(GP15) = 0
Print "LED OFF"
EndIf
' HTMLを送信
WEB TRANSMIT PAGE a%, "index.html" // connection a%へHTMLを送る
EndIf
Next a%
End Sub