Last active
October 10, 2018 16:25
-
-
Save ks-t0/9d2a9b6057097b4d0ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <Servo.h> //サーボモーター用ライブラリを読み込み | |
const char *ssid = "XXXXXXXXXXXXXXX"; | |
const char *password = "XXXXXXXXXXXXXXX"; | |
MDNSResponder mdns; | |
ESP8266WebServer server ( 80 ); | |
const int led = 16; | |
//////////////// ipaddress //////////////// | |
// the IP address for the shield: | |
IPAddress ip(192, 168, 11, 86); | |
IPAddress gateway(192, 168, 11, 38); | |
IPAddress subnet(255, 255, 255, 0); | |
////////////////////////////////////////// | |
//////////// サーボモータ設定 ///////////// | |
Servo myservo; //サーボ用のオブジェクトを作成 | |
int val; //サーボの角度を格納するための変数 | |
////////////////////////////////////////// | |
//////////// サーボモータ動作 ///////////// | |
void servo() { | |
myservo.attach(4); //デジタル4番ピンをサーボの角度命令出力ピンとして設定 | |
val = 0; | |
//90度まで動かす | |
for(int i=0; i<=60; i++){ | |
val = i; | |
myservo.write(val); | |
delay(15); | |
} | |
//元の位置に戻す | |
for(int i=60; i>=0; i--){ | |
val = i; | |
myservo.write(val); | |
delay(15); | |
} | |
myservo.detach(); | |
server.send ( 200, "text/plain", "Servo Move!" ); | |
} | |
////////////////////////////////////////// | |
void handleRoot() { | |
//digitalWrite ( led, HIGH ); | |
char temp[400]; | |
int sec = millis() / 1000; | |
int min = sec / 60; | |
int hr = min / 60; | |
snprintf ( temp, 400, | |
"<html>\ | |
<head>\ | |
<meta http-equiv='refresh' content='5'/>\ | |
<title>ESP8266 Demo</title>\ | |
<style>\ | |
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ | |
</style>\ | |
</head>\ | |
<body>\ | |
<h1>Hello from ESP8266!</h1>\ | |
<p>Uptime: %02d:%02d:%02d</p>\ | |
<img src=\"/test.svg\" />\ | |
</body>\ | |
</html>", | |
hr, min % 60, sec % 60 | |
); | |
server.send ( 200, "text/html", temp ); | |
//digitalWrite ( led, LOW ); | |
} | |
void handleNotFound() { | |
//digitalWrite ( led, HIGH ); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for ( uint8_t i = 0; i < server.args(); i++ ) { | |
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; | |
} | |
server.send ( 404, "text/plain", message ); | |
//digitalWrite ( led, LOW ); | |
} | |
void setup ( void ) { | |
//////////////////////////////// | |
WiFi.config( ip, gateway, subnet ); | |
//////////////////////////////// | |
pinMode ( led, OUTPUT ); | |
analogWrite( led , 100 ); | |
Serial.begin ( 115200 ); | |
WiFi.begin ( ssid, password ); | |
Serial.println ( "" ); | |
// Wait for connection | |
while ( WiFi.status() != WL_CONNECTED ) { | |
analogWrite( led , 100 ); | |
delay ( 500 ); | |
Serial.print ( "." ); | |
analogWrite( led , 0 ); | |
} | |
Serial.println ( "" ); | |
Serial.print ( "Connected e3to " ); | |
Serial.println ( ssid ); | |
Serial.print ( "IP address: " ); | |
Serial.println ( WiFi.localIP() ); | |
if ( mdns.begin ( "esp8266", WiFi.localIP() ) ) { | |
Serial.println ( "MDNS responder started" ); | |
} | |
server.on ( "/", handleRoot ); | |
server.on ( "/test.svg", drawGraph ); | |
server.on ( "/inline", []() { | |
server.send ( 200, "text/plain", "this works as well" ); | |
} ); | |
server.onNotFound ( handleNotFound ); | |
////////// サーボモータ動作URL /////////// | |
server.on ( "/servo", servo ); | |
////////////////////////////////////////// | |
server.begin(); | |
Serial.println ( "HTTP server started" ); | |
} | |
void loop ( void ) { | |
mdns.update(); | |
server.handleClient(); | |
//digitalWrite ( led, HIGH ); | |
int n; | |
// iの値を0~100で明るさをゆっくり変化させる(最大は255) | |
for (n = 0; n < 100; n++){ | |
// nの値でLEDを点灯させる | |
analogWrite( led , n ); | |
delay( 30 ); | |
} | |
//元の位置に戻す | |
for(int n = 100; n >= 0; n--){ | |
// nの値でLEDを点灯させる | |
analogWrite( led , n ); | |
delay( 30 ); | |
} | |
// WiFi Lan の監視。 | |
wifi_lan_check(); | |
} | |
void drawGraph() { | |
String out = ""; | |
char temp[100]; | |
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n"; | |
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n"; | |
out += "<g stroke=\"black\">\n"; | |
int y = rand() % 130; | |
for (int x = 10; x < 390; x+= 10) { | |
int y2 = rand() % 130; | |
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2); | |
out += temp; | |
y = y2; | |
} | |
out += "</g>\n</svg>\n"; | |
server.send ( 200, "image/svg+xml", out); | |
} | |
/* | |
* void wifi_lan_check() | |
* WiFi Lan の監視。 | |
* http://www.netosa.com/blog/2017/10/esp-wroom-02-wi-fi-lan.html | |
*/ | |
void wifi_lan_check(){ | |
int i; | |
if(WiFi.status() !=3){ | |
//Serial.print("wifi_lan_check() :#1 st="); | |
//Serial.println(WiFi.status()); | |
} | |
if(WiFi.status() != WL_CONNECTED){ | |
//Serial.println("WiFi disconnected"); | |
WiFi.disconnect(); | |
delay(50); | |
WiFi.begin(ssid, password); | |
i=50; | |
while (WiFi.status() != WL_CONNECTED) { | |
analogWrite( led , 100 ); | |
delay(250); | |
//Serial.print("."); | |
analogWrite( led , 0 ); | |
i--; | |
if(i<0){ | |
//Serial.println("wifi_lan_check() :#2 ESP.restart()"); | |
delay(250); | |
ESP.restart(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment