質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

シリアルポート

シリアルポートは一度に一ビットごと移行される物理的なインターフェイスです。一般的には、9ピンのd-subコネクタであるRS-232を指します。

ネットワーク

ネットワークとは、複数のコンピューター間を接続する技術です。インターネットが最も主流なネットワークの形態で、TCP/IP・HTTP・DNSなどの様々なプロトコルや、ルータやサーバーなどの様々な機器の上に成り立っています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

マイコン

マイクロコンピュータの略で、CPUにマイクロプロセッサを用いたコンピュータのこと。家電製品、電磁機器などの制御に用いられています。単体でコンピュータとしての機能を一通り備えています。 現代のパーソナルコンピュータに近く、同時期のメインフレームやミニコンピュータと比べ、小さいことが特徴です。

Q&A

解決済

1回答

1668閲覧

Arduino とEthernetシールドを用いて圧力センサからの結果をネット上で閲覧可能にする

yukina01271022

総合スコア1

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

シリアルポート

シリアルポートは一度に一ビットごと移行される物理的なインターフェイスです。一般的には、9ピンのd-subコネクタであるRS-232を指します。

ネットワーク

ネットワークとは、複数のコンピューター間を接続する技術です。インターネットが最も主流なネットワークの形態で、TCP/IP・HTTP・DNSなどの様々なプロトコルや、ルータやサーバーなどの様々な機器の上に成り立っています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

マイコン

マイクロコンピュータの略で、CPUにマイクロプロセッサを用いたコンピュータのこと。家電製品、電磁機器などの制御に用いられています。単体でコンピュータとしての機能を一通り備えています。 現代のパーソナルコンピュータに近く、同時期のメインフレームやミニコンピュータと比べ、小さいことが特徴です。

0グッド

0クリップ

投稿2020/11/27 10:05

編集2020/11/27 10:54

前提・実現したいこと

ArduinoUno、Ethernetシールド、圧力センサ、RTCモジュールを用いて、圧力が加わった時間とネット上に表示したいと考えています。
500以上の圧力が一定時間以上加われば「時間+good morning」、200以下の圧力ならば「時間+good night」と表示する予定です。
「〜年〜月〜日〜:〜:〜
state:Good Morning」
または
「〜年〜月〜日〜:〜:〜
state:Good Night」
とシリアルモニタ&webブラウザ上で確認可能なプログラムを実現したいと思っています。

発生している問題・エラーメッセージ

シリアルモニタでは 「〜年〜月〜日〜:〜:〜  state:Good Morning」 といったように表示されているのですが、指定したipアドレスに接続すると全く何も表示されていません。 一方でEthernetシールドに有線を接続している間は、指定したIPアドレスに接続できます。(文字は一切何も表示されませんが) マイコンボードに書き込む際もエラーが表示されず問題なく書き込みはでき、シリアルモニタで表示したい内容は問題なく確認できるのですが、ネット上では何も表示されないということはLANの処理を誤っているのかとも思うのですが、自分で考えたのですが全くわからないため教えていただけたら幸いです。 ![圧力センサに力を加えるとこのような文字が表示されます。](98b1f553ec72de8ca4e46decc26e0cf0.png)

該当のソースコード

#include <DS3232RTC.h> #include <TimeLib.h> #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1 }; IPAddress ip(0, 0, 0, 0); //この2つは実際にはちゃんと入力しています EthernetServer server(80); const int unknown=0; const int getup=1; const int sleep=2; //状態が変化してから確定するまでに待機する時間 const int timeToWait=1000; //寝てると判断する閾値 const int thresholdH=500; //起きてると判断する閾値 const int thresholdL = 200; int state=unknown; int nextState=unknown; int lastChange=0; int lastpresState=unknown; void setup() { // put your setup code here, to run once: setTime(14, 27, 0, 16, 11, 2020); RTC.set(now()); Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Ethernet WebServer Example"); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } // start the server server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // put your main code here, to run repeatedly: tmElements_t tm; RTC.read(tm); //時間の設定 //センサから明るさを読み取る int pres=analogRead(1); //現在の時刻を取得 unsigned long now=millis(); //現在のセンサの状態を表す変数の初期に前回のセンサの状態を代入 int presState=lastpresState; //読み取った値と閾値を比較して現在のセンサの状態をセット if(pres>thresholdH){ presState=getup; } else if(pres<thresholdL){ presState=sleep; } if(lastpresState != presState){ if(presState==getup){ nextState=getup; lastChange=now; } else if(presState==sleep){ nextState=sleep; lastChange=now; } } lastpresState=presState; if((state != nextState)&&((now - lastChange) >timeToWait)){ if(nextState==getup){ Serial.print(tm.Year + 1970, DEC); Serial.print("年"); Serial.print(tm.Month,DEC); Serial.print("月"); Serial.print(tm.Day, DEC); Serial.print("日"); Serial.print(tm.Hour,DEC); Serial.print("時"); Serial.print(tm.Minute,DEC); Serial.print("分"); Serial.println(tm.Second, DEC); Serial.print("STATE:"); Serial.print("GOOD NIGHT\n"); } else if(nextState==sleep){ Serial.print(tm.Year + 1970, DEC); Serial.print("年"); Serial.print(tm.Month,DEC); Serial.print("月"); Serial.print(tm.Day, DEC); Serial.print("日"); Serial.print(tm.Hour,DEC); Serial.print("時"); Serial.print(tm.Minute,DEC); Serial.print("分"); Serial.println(tm.Second, DEC); Serial.print("STATE:"); Serial.print("GOOD MORNING\n"); } state=nextState; } //以下LANの処理 EthernetClient client = server.available(); //クライアントからの測定データの要求の有無を確認 if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { //クライアントが接続されている限り if (client.available()) { //クライアントからの読み取りデータがある場合 int c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); //センサから圧力を読み取る int pres=analogRead(1); unsigned long now=millis(); int presState=lastpresState; if(pres>thresholdH){ presState=getup; } else if(pres<thresholdL){ presState=sleep; } if(lastpresState != presState){ if(presState==getup){ nextState=getup; lastChange=now; } else if(presState==sleep){ nextState=sleep; lastChange=now; } } lastpresState=presState; if((state != nextState)&&((now - lastChange) >timeToWait)){ if(nextState==getup){ Serial.print(tm.Year + 1970, DEC); Serial.print("年"); Serial.print(tm.Month,DEC); Serial.print("月"); Serial.print(tm.Day, DEC); Serial.print("日"); Serial.print(tm.Hour,DEC); Serial.print("時"); Serial.print(tm.Minute,DEC); Serial.print("分"); Serial.println(tm.Second, DEC); Serial.print("STATE:"); Serial.print("GOOD NIGHT\n"); } else if(nextState==sleep){ Serial.print(tm.Year + 1970, DEC); Serial.print("年"); Serial.print(tm.Month,DEC); Serial.print("月"); Serial.print(tm.Day, DEC); Serial.print("日"); Serial.print(tm.Hour,DEC); Serial.print("時"); Serial.print(tm.Minute,DEC); Serial.print("分"); Serial.println(tm.Second, DEC); Serial.print("STATE:"); Serial.print("GOOD MORNING\n"); } state=nextState; } client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(100); // close the connection: client.stop(); Serial.println("client disconnected"); } }```

試したこと

void loop内の、「EthernetClient client = server.available();」以下が間違っていると考えたのですが、どこを書き直せばいいのかわかりませんでした。

補足情報(FW/ツールのバージョンなど)

Arduino 1.8.13
DS3232 RTCモジュール

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fumu7

2020/11/27 10:25

「指定したIPアドレスに接続できます」というのは、ArduinoあるいはEtherシールドに接続されているどのような機器から、どのような事が出来ることを意味していますか? まず、ハードウェア(機器)には何を使っていて、それらが何でどのように接続されているのかを説明してください。そして、そこでどんなソフトウェアがどう動作させたいのかを説明してください。
yukina01271022

2020/11/27 10:45

ArduinoおよびEthernetシールドはPCとルーターに接続した状態で、ネット環境の整ったスマートフォンからコード内で指定した「IPAddress ip(0, 0, 0, 0);」の0,0,0,0をGoogle等で入力すると、白紙のページに飛びます。 一方でルーターから切り離した状態で0,0,0,0をGoogle等で入力すると、ページが見つからないとの文字が表示されるので、「指定したIPアドレスに接続できます」といった表現をいたしました。 使用しているのは、MacBook Pro,Arduino Uno,Ethernetシールド2,RTCモジュール DS3232,ブレッドボード、四角形の感圧センサです。 500以上の圧力が一定時間以上加わったら、 「〜年〜月〜日00:00:00 state:Good Morning」 200以下の圧力が一定時間以上加わったら 「〜年〜月〜日00:00:00 state:Good Morning」 とwebブラウザ上に表示させたいと思っています。
guest

回答1

0

ベストアンサー

そもそものはなしになりますが、インターネット上から見るようにするためには、それに対応した回線で、なおかつ、ルータの設定を、インターネットから閲覧できるような設定を行わなければなりません。
ということで、
まずは同じLAN内から、そいつのローカルIPを指定して閲覧できるのを目指してください。
現状では、インターネット上からの閲覧は不可能です

投稿2020/11/27 13:39

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukina01271022

2020/11/27 14:01

お返事ありがとうございます。 私が質問で書かせていただいたコードですが、Arduino Genuinoに入っているスケッチ例を参考にしたものです。 そのスケッチ例を実行した結果、問題なくインターネット上から閲覧することができました。 私のコードはスケッチ例に必要なコードを追加したものですので、ルータの設定等は関係ないと思っているのですが違うのでしょうか? 私のコードとスケッチ例で書かれていたIPアドレスは全く同じものを使用しているので、ルータなどの設定は問題ないと思っておりました。 以下スケッチ例です。 #include <SPI.h> #include <Ethernet.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); // 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() { // You can use Ethernet.init(pin) to configure the CS pin //Ethernet.init(10); // Most Arduino shields //Ethernet.init(5); // MKR ETH shield //Ethernet.init(0); // Teensy 2.0 //Ethernet.init(20); // Teensy++ 2.0 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Ethernet WebServer Example"); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } // start the server 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"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println("<br />"); } client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }
y_waiwai

2020/11/27 14:08

ああ、 > IPAddress ip(192, 168, 1, 177); でアクセスできるってことですね。それなら問題はありません。 ローカルIPで、同じLAN内からの閲覧はそれで問題はないです。 ましかし、インターネット上からの閲覧はそれではできないので注意しましょう
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問