下手の横好きのスクラップブック!!

下手の横好きの、いろいろな記録です。
電子工作・PIC・Arduino・太陽光発電・写真などetc

Arduino・Ethernetシールドをテストする(2) ネットワークで温度・湿度を見る

2012-03-26 | Arduino

前回使ったスケッチを少し変更して、温度と湿度をネットワークを介して見れるようにしてみます。

温度センサー
温度センサーはLM61を使いAnalog(0)へ接続します。
換算式は 温度 = ((analogRead(0)*0.00489)-0.6)*100を使います。

湿度センサー
湿度センサーは HSM20Gを使いAnalog(1)へ接続します。HSM20Gは温度と湿度の両方が読み取れますが、今回は湿度の方だけ使用しました。
湿度の出力特性と配線は次のようになっています。


湿度への換算式は  湿度(%)= (analogRead(1)*0.00489*30.855)-11.504としました。

配線

スケッチ
スケッチは以下の部分を赤の太字のように書き換えます。

  // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("");


/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 
 */

#include
#include

// 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, 0x00, 0x48, 0xC6 };
IPAddress ip(192,168,1, 90);

// 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()
{
 
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  int temp = 0;//追加
  int hum = 0;//追加
 
  
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // 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();


      //スケッチ変更部分
            temp = ((analogRead(0)*0.00489)-0.6)*100;
            hum = (analogRead(1)*0.00489*30.855)-11.504;
            client.print("Temp = ");
            client.print(temp);
            client.print("deg");
             client.println("");
            client.print("Hum = ");
            client.print(hum);
            client.print("%");       
            client.println("");
            
          
          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();
  }
}


アップロードが完了したらPCのWebブラウザでIPアドレスにアクセスすると、温度と湿度をネットワークを介して見ることが出来ます。

 

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« Arduino・ Ethernetシールド... | トップ | Arduino・Ethernetシールドを... »
最新の画像もっと見る

コメントを投稿

Arduino」カテゴリの最新記事