Skip to content

Instantly share code, notes, and snippets.

@NaotoKumagai
Last active November 19, 2016 10:29
Show Gist options
  • Save NaotoKumagai/61155ddf08ea846970228c22bff83bb6 to your computer and use it in GitHub Desktop.
Save NaotoKumagai/61155ddf08ea846970228c22bff83bb6 to your computer and use it in GitHub Desktop.
ESP-WROOM-02 でローカルのWebサーバ
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define WIFI_SSID "接続したいSSID"
#define WIFI_PWD "接続したいSSIDのパスワード"
// HTML
#define HTML_HEADER "<!doctype html>"\
"<html><head><meta charset=\"UTF-8\"/>"\
"<meta name=\"viewport\" content=\"width=device-width\"/>"\
"</head><body>"
#define HTML_FOOTER "</body></html>"
ESP8266WebServer server(80); // 接続に用いるポート(今回はWebサーバなのでデフォルトの80番を指定)
// 最初に1回だけ行われる処理
void setup() {
Serial.begin(9600); // シリアル通信の速度設定。シリアルモニタの値と同じなら多分オーケー。
WiFi.begin(WIFI_SSID, WIFI_PWD);
Serial.println("");
while(WiFi.status() != WL_CONNECTED){ // Wifi接続待ち。回数制限掛けてタイムアウト処理入れたほうがベター
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println(WiFi.localIP()); //ブラウザでアクセスするローカルIPを出力
setupServer();
}
// setup()の後に延々とループして呼ばれる処理
void loop() {
server.handleClient();
}
void setupServer() {
server.on("/", [](){ // ルートにアクセスされた時の処理を記述。
String html = HTML_HEADER "<h1>しゃけなべいびー</h1>" HTML_FOOTER;
server.send(200, "text/html", html);
});
server.begin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment