Created
December 29, 2015 05:20
-
-
Save hatsunea/31c8f29c203e68589677 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 <SoftwareSerial.h> | |
SoftwareSerial ESP8266_Serial(2, 3); | |
Servo CameraServo; | |
Servo WaterServo; | |
long LastTime = 0; | |
boolean WROOM_boot = false;//WROOMが起動したかどうかの判定 | |
boolean WiFi_setup = false;//WIFIセットアップが済んだかどうかの判定 | |
void setup() | |
{ | |
Serial.begin(115200); | |
ESP8266_Serial.begin(115200); | |
Serial.println("Wait."); | |
delay(5000); | |
ESP8266_Serial.print("AT+RST\r\n");//ソフトウェアリセット | |
// | |
CameraServo.attach(9); | |
WaterServo.attach(10); | |
CameraServo.write(90); | |
WaterServo.write(180); | |
} | |
void loop() | |
{ | |
String str; | |
char c; | |
int pos = 180; // variable to store the servo position | |
int cameraPos = 90; | |
if (Serial.available()) { //WROOM購入時はreadyのみ返って来る。 | |
if (WROOM_boot == false) { //シリアルが有効であれば、WROOMが起動していると判定 | |
Serial.println("ESP-WROOM-02 Serial Boot OK!"); | |
WROOM_boot = true; | |
} | |
Serial.print("."); | |
switch (WiFi_setup) { | |
case false: | |
if (Serial.find("ready")) { | |
//セットアップ済みならばWIFI CONNECTが返ってきてWIFI GOT ITという文字が返るまで約4秒 | |
delay(4000); | |
Serial.println(); | |
Serial.println("-----WIFI setup IN"); | |
//ステーションモード有効コマンド | |
ESP8266_Serial.print("AT+CWMODE=1\r\n"); | |
Data_Read_Display("OK");//WROOMからのOKレスポンスを受けたら次へ進む | |
//アクセスポイントのSSID、パスワード設定コマンド | |
ESP8266_Serial.print("AT+CWJAP=\"GL06P-F801137BF437\",\"10416488\"");//ご自分のルーターのSSIDとパスワード | |
ESP8266_Serial.print("\r\n"); | |
Data_Read_Display("OK"); //WROOMからWIFI GOT IPというレスポンスを受けたら次へ進む | |
//APから割り当てられたIPアドレスとWROOMのMACアドレス確認 | |
delay(1000); | |
ESP8266_Serial.print("AT+CIFSR\r\n"); | |
Data_Read_Display("OK");//WROOMからのOKレスポンスを受けたら次へ進む | |
//マルチプルコネクション有効コマンド | |
ESP8266_Serial.print("AT+CIPMUX=1\r\n"); | |
Data_Read_Display("OK");//WROOMからのOKレスポンスを受けたら次へ進む | |
//サーバーモード有効、ポート番号設定 | |
ESP8266_Serial.print("AT+CIPSERVER=1,80\r\n"); | |
Data_Read_Display("OK");//WROOMからのOKレスポンスを受けたら次へ進む | |
Serial.println("Arduino WROOM Server OK!");//この表示が出たらWROOMのサーバー設定終了 | |
WiFi_setup = true; | |
} | |
break; | |
case true: | |
byte ipd_no = 0; | |
str = ""; | |
while (1) { | |
if (Serial.available()) { | |
c = Serial.read(); | |
if (c < 0x80 && c >= 0) { // 不要な文字や読み取り不能な文字をカット | |
str += c; | |
} | |
} | |
if (c == '\n') { | |
if (str.indexOf("+IPD") >= 0) { | |
ipd_no = str[str.indexOf("+IPD") + 5] - 0x30; //これが重要。このID番号を取得しておかねばならない | |
if (str.indexOf("GET / HTTP") >= 0) { | |
HTTP_Request_Display("Accept-Encoding", ipd_no); | |
delay(1500);//------各自のネットワーク環境に合わせて変えてください | |
HTTP_response("WROOM LED SWITCH", ipd_no); | |
str = ""; | |
break; | |
} | |
if (str.indexOf("?") >= 0) { //HTTP GETリクエストで?という文字を検知したらLED表示 | |
char sw = str.charAt(str.indexOf("?") + 1); | |
switch (sw) { | |
case 'L': | |
HTTP_response("Camera Move Left", ipd_no); | |
for (pos = cameraPos; pos >= 45; pos -= 1) { // goes from 0 degrees to 180 degrees | |
cameraPos = pos; | |
// in steps of 1 degree | |
CameraServo.write(cameraPos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
break; | |
case 'R': | |
HTTP_response("Camera Move Right", ipd_no); | |
for (pos = cameraPos; pos <= 135; pos += 1) { // goes from 0 degrees to 180 degrees | |
cameraPos = pos; | |
// in steps of 1 degree | |
CameraServo.write(cameraPos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
break; | |
case 'C': | |
HTTP_response("Camera Move Center", ipd_no); | |
if (cameraPos > 90) | |
{ | |
for (pos = cameraPos; pos >= 90; pos -= 1) { // goes from 0 degrees to 180 degrees | |
cameraPos = pos; | |
CameraServo.write(cameraPos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} | |
else | |
{ | |
for (pos = cameraPos; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees | |
cameraPos = pos; | |
CameraServo.write(cameraPos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} | |
break; | |
case 'H': | |
HTTP_response("Water HomePosition", ipd_no); | |
WaterServo.write(180); | |
break; | |
case 'D': | |
HTTP_response("Drop Water", ipd_no); | |
for (pos = 180; pos >= 10; pos -= 1) { // goes from 0 degrees to 180 degrees | |
// in steps of 1 degree | |
WaterServo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
break; | |
case '0': | |
HTTP_response("TEST", ipd_no); | |
break; | |
} | |
str = ""; | |
} | |
else | |
{ //Google ChromeではGET送信の後すぐにfaviconを投げかけてくるところの対処 | |
HTTP_Request_Display("\r\n", ipd_no); | |
delay(1000); | |
ESP8266_Serial.print("AT+CIPCLOSE="); | |
ESP8266_Serial.print(ipd_no); //------Client IDをかならず付ける | |
ESP8266_Serial.print("\r\n"); | |
Data_Read_Display("OK"); | |
Serial.println("Connection Close"); | |
str = ""; | |
break; | |
} | |
} | |
str = ""; | |
} | |
} | |
break; | |
} | |
} | |
if ((WiFi_setup == false) && (millis() - LastTime > 40000)) { //40秒たってもセットアップできない場合、リセット | |
Serial.println("Software Reset Again"); | |
ESP8266_Serial.print("AT+RST"); | |
ESP8266_Serial.print("\r\n"); | |
WROOM_boot = false; | |
LastTime = millis(); | |
} | |
} | |
//コマンドモニター用関数*********************************** | |
void Data_Read_Display(String response) | |
{ | |
char c; | |
String str; | |
while (1) { | |
if (Serial.available()) { | |
c = Serial.read(); | |
if (c < 0x80 && c >= 0) { // 不要な文字や読み取り不能な文字をカット | |
str += c; | |
} | |
} | |
if (c == '\n') { | |
if (str.indexOf(response) == 0) { | |
Serial.println(str); | |
str = ""; | |
break; | |
} else if (str.indexOf("ERROR") >= 0) { | |
break; | |
} | |
Serial.print(str); | |
str = ""; | |
} | |
} | |
} | |
//HTTPリクエストを表示する関数********************************** | |
void HTTP_Request_Display(String response, byte ipd_no) | |
{ | |
char c; | |
String str; | |
while (1) { | |
if (Serial.available()) { | |
c = Serial.read(); | |
if (c < 0x80 && c >= 0) { // 不要な文字や読み取り不能な文字をカット | |
str += c; | |
} | |
} | |
if (c == '\n') { | |
if (str.indexOf(response) >= 0) { | |
Serial.println(str); | |
str = ""; | |
break; | |
} else if (str.indexOf("GET") >= 0) { //ブラウザがフリーズした時にGET送信すれば離脱できるようにした | |
ESP8266_Serial.print("AT+CIPCLOSE="); | |
ESP8266_Serial.print(ipd_no); //-----Client IDをかならず付ける | |
ESP8266_Serial.print("\r\n"); | |
Serial.println("Connection Close"); | |
str = ""; | |
Data_Read_Display("\r\n"); | |
break; | |
} | |
Serial.print(str); | |
str = ""; | |
} | |
} | |
} | |
//HTTPレスポンスをブラウザに送信する関数****************************************** | |
void HTTP_response(String message, byte ipd_no) | |
{ | |
String http_res; | |
String html_str; | |
//Stringが長すぎると受け付けないので、2回に分けて送信 | |
http_res = "HTTP/1.1 200 OK\r\n"; | |
http_res += "Content-Type: text/html\r\n"; | |
http_res += "Connection: close\r\n"; | |
http_res += "\r\n"; | |
http_res += "<!DOCTYPE html>\r\n"; | |
http_res += "<html>\r\n"; | |
http_res += "<head>\r\n"; | |
http_res += "<meta name=\"viewport\" content=\"initial-scale=1.5\">\r\n"; | |
http_res += "</head>\r\n"; | |
html_str = "<body>\r\n"; | |
//ご自分のWROOMローカルアドレスとポート番号を指定 | |
html_str += "<form action=\"http://192.168.13.103/\" method=\"get\">\r\n"; | |
html_str += message; | |
html_str += "</form>\r\n"; | |
html_str += "</body>\r\n"; | |
html_str += "</html>\r\n"; | |
html_str += "\0"; | |
ESP8266_Serial.print("AT+CIPSEND="); //文字送信コマンド。 | |
ESP8266_Serial.print(ipd_no); //--------Client IDをかならず付ける | |
ESP8266_Serial.print(','); | |
ESP8266_Serial.print(http_res.length() + html_str.length()); //HTMLのバイト数も送る | |
ESP8266_Serial.print("\r\n"); | |
Serial.readStringUntil('>'); //この記号がWROOMから返ってきたら送信スタンバイOKとなる。 | |
ESP8266_Serial.print(http_res); //HTML1回目送信 | |
ESP8266_Serial.print("\r\n"); | |
ESP8266_Serial.print(html_str); //HTML2回目送信 | |
ESP8266_Serial.print("\r\n"); | |
HTTP_Request_Display("SEND OK", ipd_no); //SEND OKという文字が返ってきたら次へ進む | |
http_res = ""; | |
html_str = ""; | |
ESP8266_Serial.print("AT+CIPCLOSE=");//ここでクローズしないと、ブラウザがフリーズする | |
ESP8266_Serial.print(ipd_no); //--------Client IDをかならず付ける | |
ESP8266_Serial.print("\r\n"); | |
Serial.println("Connection Close"); | |
HTTP_Request_Display("\r\n", ipd_no); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment