WiFi Web Server on BBC micro:bit and ESP-01 (ESP8266) - Hackster.io でけた!
192.168.4.1でLEDオンオフできそうだ ATバシバシコード
// for wifi connection
function wait_for_response (str: string) {
time = input.runningTime()
while (true) {
serial_str = "" + serial_str + serial.readString()
if (serial_str.length > 200) {
serial_str = serial_str.substr(serial_str.length - 200, 0)
}
if (serial_str.includes(str)) {
result2 = true
break;
}
if (input.runningTime() - time > 300000) {
break;
}
}
return result2
}
// generate HTML
function getHTML (normal: boolean) {
web_title = "ESP8266 (ESP-01) Wifi on BBC micro:bit"
// HTTP response
html = "" + html + "HTTP/1.1 200 OK\r\n"
html = "" + html + "Content-Type: text/html\r\n"
html = "" + html + "Connection: close\r\n\r\n"
html = "" + html + "<!DOCTYPE html>"
html = "" + html + "<html>"
html = "" + html + "<head>"
html = "" + html + "<link rel=\"icon\" href=\"data:,\">"
html = "" + html + "<title>" + web_title + "</title>"
// mobile view
html = "" + html + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
html = "" + html + "</head>"
html = "" + html + "<body>"
html = "" + html + "<div style=\"text-align:center\">"
html = "" + html + "<h1>" + web_title + "</h1>"
html = "" + html + "<br>"
// generate status text
if (normal) {
if (LED_status) {
LED_statusString = "ON"
LED_buttonString = "TURN IT OFF"
} else {
LED_statusString = "OFF"
LED_buttonString = "TURN IT ON"
}
html = "" + html + "<h3>LED STATUS: " + LED_statusString + "</h3>"
html = "" + html + "<br>"
// generate buttons
html = "" + html + "<input type=\"button\" onClick=\"window.location.href='LED'\" value=\"" + LED_buttonString + "\">"
html = "" + html + "<br>"
} else {
html = "" + html + "<h3>ERROR: REQUEST NOT FOUND</h3>"
}
html = "" + html + "<br>"
html = "" + html + "<input type=\"button\" onClick=\"window.location.href='/'\" value=\"Home\">"
html = "" + html + "</div>"
html = "" + html + "</body>"
html = "" + html + "</html>"
return html
}
let LED_buttonString = ""
let LED_statusString = ""
let html = ""
let web_title = ""
let result2 = false
let time = 0
let GET_command = ""
let HTTP_pos = 0
let GET_pos = 0
let client_ID = ""
let serial_str = ""
let result = false
let HTML_str: string = ""
let GET_success: boolean = false
let LED_status: number = 0
// user settings
// 1 = STA (station, connect to wifi router); 2 = AP (make itself an access point)
let WIFI_MODE = 2
// To Rx pin of ESP-01
const Tx_pin: SerialPin = SerialPin.P0
// To Tx pin of ESP-01
const Rx_pin: SerialPin = SerialPin.P1
// pin for LED control
const LED_pin: DigitalPin = DigitalPin.P2
// wifi router ssid for station mode
let SSID_1 = "your_wifi_ssid"
// wifi router password for station mode
let PASSWORD_1 = "your_wifi_password"
// AP server ssid for AP mode
let SSID_2 = "ESP8266"
// AP password for AP mode (at least 8 characters)
let PASSWORD_2 = "microbit"
pins.digitalWritePin(LED_pin, 0)
// older ESP8266s may use 51200 or 9600...
serial.redirect(Tx_pin, Rx_pin, 115200)
sendAT("AT+RESTORE", 1000)
sendAT("AT+RST", 1000)
sendAT("AT+CWMODE=" + WIFI_MODE)
if (WIFI_MODE == 1) {
sendAT("AT+CWJAP=\"" + SSID_1 + "\",\"" + PASSWORD_1 + "\"")
result = wait_for_response("OK")
if (!(result)) {
control.reset()
}
} else if (WIFI_MODE == 2) {
sendAT("AT+CWSAP=\"" + SSID_2 + "\",\"" + PASSWORD_2 + "\",1,4", 1000)
}
sendAT("AT+CIPMUX=1")
sendAT("AT+CIPSERVER=1,80")
sendAT("AT+CIFSR")
// startup completed
basic.showIcon(IconNames.Yes)
// process HTTP request
while (true) {
// read and store 200 characters from serial port
serial_str = "" + serial_str + serial.readString()
if (serial_str.length > 200) {
serial_str = serial_str.substr(serial_str.length - 200, 0)
}
// output HTML
if (serial_str.includes("+IPD") && serial_str.includes("HTTP")) {
// got a HTTP request
client_ID = serial_str.substr(serial_str.indexOf("IPD") + 4, 1)
GET_pos = serial_str.indexOf("GET")
HTTP_pos = serial_str.indexOf("HTTP")
GET_command = serial_str.substr(GET_pos + 5, HTTP_pos - 1 - (GET_pos + 5))
switch (GET_command) {
case "": // request 192.168.x.x/
GET_success = true
break
case "LED": // request 192.168.x.x/LED
GET_success = true
LED_status = 1 - LED_status
pins.digitalWritePin(LED_pin, LED_status)
break
}
if (GET_success) {
// normal HTML
HTML_str = getHTML(true)
} else {
// HTML with error message
HTML_str = getHTML(false)
}
sendAT("AT+CIPSEND=" + client_ID + "," + (HTML_str.length + 2))
sendAT(HTML_str, 1000)
sendAT("AT+CIPCLOSE=" + client_ID)
serial_str = ""
}
}
function sendAT(command: string, waitTime: number = 100) {
serial.writeString(command + "\u000D\u000A")
basic.pause(waitTime)
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
-------------microbit and raspi uart com -----------------------------------------------
Rapberry PiとBBC Micro:bitでUARTを試す - saitodev.co あっさり成功
ーーーーーーthingspeakーーーーーーーーーーーーーーーーーーーーーーーーーーー
データを簡単に保存&グラフ化できるThingSpeakが便利! - iwathiの/var/log (hatenablog.com) これはブラウザのget機能をもちいた方法であった
ThingSpeakに登録してcurlでデータを送ってグラフを表示するまで #curl - Qiita このcurlはウィンドウズのではだめでリナックスでのでOKだった
センサーの計測値をクラウドにためて見てみる | SG Labs これのcurlはpost仕様だった
例題として以下を試行。。。。宿題
ESP32 Publish Sensor Readings to ThingSpeak (easiest way) | Random Nerd Tutorials
ちなみにmicrobitではgroveという拡張機能のUartWiFiをもちいて成功!
basic.showNumber(0)
basic.clearScreen()
basic.pause(100)
grove.setupWifi(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate115200,
"184F32CF8BF3-2G",
"2215085363556"
)
basic.pause(5000)
if (grove.wifiOK()) {
basic.showNumber(0)
}
basic.forever(function () {
basic.pause(5000)
basic.pause(5000)
basic.pause(5000)
grove.sendToThinkSpeak(
"N9RHY2H4F2K7BNCQ",
45,
0,
0,
0,
0,
0,
0,
0
)
})
ーーーーesp01 2kindーーーーーーーuno,pico ok ーーーーーーーーーーーーーーーー
amazonから購入した4pinアダプタ方式esp01は通常とちがい、Unoにブランクコードをいれ
rx->rx,tx->txでつなぐとATがうごいた AC電源から電圧変換して5vでうごいた!
もうひとつのesp01sは3vでうごく!
---------------------for arduinoUNO -------------------------------------------
以下のコードはEsp01のRxをUnoの3pin,TxをUnoの2pinにして動いた!
ifttt line連携もばっちりだった
#include "ArduinoESPAT.h" // rx 2 tx 3であったので辻褄はあう
ESPAT espat("184F32CF8BF3-2G","2215085363556");
void setup(){
Serial.begin(115200);
if(espat.begin()){
Serial.println("Initialize Ok");
}else{
Serial.println("Initialize Fail");
}
if(espat.changeMode(1)){
Serial.println("Mode OK");
}else{
Serial.println("Mode not OK");
}
if(espat.tryConnectAP()){
Serial.println("Connected");
}else{
Serial.println("Connect Failed");
}
Serial.println(espat.clientIP());
Serial.println(espat.get("www.yahoo.co.jp", "/", 80));
//ここはエラーになるのでhttpなサイトをさがそう
Serial.println("Finished");
}
void loop(){
}
------------------for pico ----------------------------------------------
https://microcontrollerslab.com/esp8266-wifi-module-raspberry-pi-pico-web-server/
上記のパチモンつかうので5VとGNDをACアダプタからとり、あとはGNDを共通化して
picoの1番がTX、2番がRXなのでesp01のRx、esp01のTxとつなぐ Unoと一緒でクロス
SSID,PWDを自分のネットにする! コレハバッチリ!
0 件のコメント:
コメントを投稿