arduinoを用いてwebサーバーを作ろうと考えております。
サンプルプログラム(webserver)は動いたため、自作したHTMLファイルをSDカードに保存し、arduinoとイーサネットシールド2を用いてPCのwebブラウザに表示させようとしています。
しかし、下記コードを記載してもブラウザ上に何も表示がされません。
打開策等ご教授いただけると幸いです。
なお、HTMLファイル、JSSファイル、JavascriptファイルはPC上に保存し、ブラウザで開いた際は正常に動作します。
〇arduinoプログラム
#include <SPI.h> #include <Ethernet2.h> #include <SD.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xBF, 0x98 }; IPAddress ip(192, 168, 100, 222); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); File file; file = SD.open("system_terminal.html"); if(file){ while(file.available()){ client.write(file.read()); } file.close(); } client.write(file); break; } if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; } } } delay(1); client.stop(); Serial.println("client disconnected"); } }
〇シリアルモニタ出力
server is at 192.168.100.222 new client GET / HTTP/1.1 Host: 192.168.100.222 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: ja,en-US;q=0.8,en;q=0.6 client disconnected new client GET /favicon.ico HTTP/1.1 Host: 192.168.100.222 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 Accept: image/webp,image/apng,image/*,*/*;q=0.8 Referer: http://192.168.100.222/ Accept-Encoding: gzip, deflate Accept-Language: ja,en-US;q=0.8,en;q=0.6 client disconnected
SDカード内ファイル構造
system_terminal.html
program-can.js
|
-point.css
〇system_terminal.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <meta name="GENERATOR" content="HomuPage Writer Ver1.20"> <title>西城駅 連動画面</title> <script src="program/can.js" type="text/javascript" charset="UTF-8"></script> <LINK REL="stylesheet" HREF="program/point.css" TYPE="text/css"> </head> <body> <h2></h2> <canvas id="terminal"width="1900" height="900"></canvas> <br/> <script> var M10='N'; var M11='N'; var sinro_setting=[0,0,0,0]; rendo_gamen(sinro_setting); document.addEventListener('click', dosa); function dosa(pix){ dosa_10M(pix); dosa_11M(pix); dosa_1RA(pix); } </script> </body> </html>
〇JavaScriptファイルとCSSファイルは文字数制限に引っかかったため省略します。(必要でしたら別質問にて提示します。)
→原因追及結果
シリアルモニタを用いて原因追及を行っています。
#include <SPI.h> #include <Ethernet2.h> #include <SD.h> char *FILENAME="system_terminal.html"; // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xBF, 0x98 }; IPAddress ip(192, 168, 100, 222); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); //Serial.println('111'); Serial.write(c); //Serial.println('222'); if (c == '\n' && currentLineIsBlank) { //Serial.println('333'); client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); Serial.print('4'); File file; file = SD.open(FILENAME,FILE_READ); Serial.print('5'); if(file){ while(file.available()){ Serial.print(file.position()); Serial.print('6'); char buff=file.read(); Serial.print(buff); client.write(buff); } }else{ Serial.println("Error!!"); } file.close(); //client.write(file); //break; } if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; } } } delay(1); client.stop(); Serial.println("client disconnected"); } }
server is at 192.168.100.222 new client GET / HTTP/1.1 Host: 192.168.100.222 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: ja,en-US;q=0.8,en;q=0.6 45Error!!
SDカード内のファイルを開封できていない可能性があります。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/23 07:12 編集