2016年10月24日月曜日

Arduino Web Server

Ethernet に接続してみましょう。有線による接続ならば Ethernet Sheld があります。Aruduino UNOの上に(子亀として)接続します。Arduino には照度センサーを接続し、ブラザーからの呼び出しがあると照度を測定してその結果を返します。ブラウザーには現在の照度が表示されます。

Arduino UNO は R2 を利用しています。

サンプルはメニューのファイル → スケッチの例 → Ethernet → WebServer とたどれまば見つかります。照度センサーTSL2561 はメーカーよりライブラリが提供されていますので利用します。
 Arduino IDE へのライブラリの追加は(メーカーのホームページ)より、左の Using the TSL2561 sensor をクリックし、Download Adafruit_TSL2561 V2 Library と Download Adafruit_Sensor Library
をクリックしてダウンロードします。ウンロードした *.zip ファイルをそれぞれ展開し、Arduino IDE を展開した下にlibraries がありますのでその配下にコピーしてください。
 MAC アドレスは Ethernet Sheld の裏にアドレスを書いた貼り紙がありますのでプログラムに登録してください。IP アドレスは環境に合わせて設定してください。DHCP クライアントの機能はありません。固定アドレスを設定してください。今回は自宅の環境に合わせて 192.168.100.77 としています。クライアント(ウエブブラウザー)は5秒ごとにリフレッシュする様になっています。サーバーは呼び出しがある都度、照度を測定するルーチンを呼び出し結果を送り返します。
 多数のクライアントから同時にアクセスするとオーバーフローとなります。今回はサーバーとしての応答の確認のためのスケッチです。

サーバーで計測した照度がウエブブラウザーで表示しているところ(Microsoft Edge)。

ソースコード
01: /*
02:   Web Server
03: 
04:  A simple web server that shows the value of the analog input pins.
05:  using an Arduino Wiznet Ethernet shield.
06: 
07:  Circuit:
08:  * Ethernet shield attached to pins 10, 11, 12, 13
09:  * Analog inputs attached to pins A0 through A5 (optional)
10: 
11:  created 18 Dec 2009
12:  by David A. Mellis
13:  modified 9 Apr 2012
14:  by Tom Igoe
15:  modified 02 Sept 2015
16:  by Arturo Guadalupi
17: 
18:  */
19: 
20: #include <SPI.h>
21: #include <Ethernet.h>
22: #include <Wire.h>
23: #include <Adafruit_Sensor.h>
24: #include <Adafruit_TSL2561_U.h>
25: 
26: // Enter a MAC address and IP address for your controller below.
27: // The IP address will be dependent on your local network:
28:  byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0xEB };   // 1.MAC Address
29: IPAddress ip(192, 168, 100, 77);                      // 2.IP  Address
30: 
31: // Initialize the Ethernet server library
32: // with the IP address and port you want to use
33: // (port 80 is default for HTTP):
34: EthernetServer server(80);                            // 3.Port 指定
35: 
36: Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
37: 
38: void GetLux( float &weblight ) ;
39: 
40: /**************************************************************************/
41: /*
42:     Displays some basic information on this sensor from the unified
43:     sensor API sensor_t type (see Adafruit_Sensor for more information)
44: */
45: /**************************************************************************/
46: void displaySensorDetails(void)
47: {
48:   sensor_t sensor;
49:   tsl.getSensor(&sensor);
50:   Serial.println("------------------------------------");
51:   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
52:   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
53:   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
54:   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
55:   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
56:   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
57:   Serial.println("------------------------------------");
58:   Serial.println("");
59:   delay(500);
60: }
61: 
62: /**************************************************************************/
63: /*
64:     Configures the gain and integration time for the TSL2561
65: */
66: /**************************************************************************/
67: void configureSensor(void)
68: {
69:   /* You can also manually set the gain or enable auto-gain support */
70:   // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
71:   // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
72:   tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
73:   
74:   /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
75:   tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
76:   // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
77:   // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
78: 
79:   /* Update these values depending on what you've set above! */  
80:   Serial.println("------------------------------------");
81:   Serial.print  ("Gain:         "); Serial.println("Auto");
82:   Serial.print  ("Timing:       "); Serial.println("13 ms");
83:   Serial.println("------------------------------------");
84: }
85: 
86: void setup() {
87:   // Open serial communications and wait for port to open:
88:   Serial.begin(9600);
89:   while (!Serial) {
90:     ; // wait for serial port to connect. Needed for native USB port only
91:   }
92:   /* Initialise the sensor */
93:   if(!tsl.begin())
94:   {
95:     /* There was a problem detecting the TSL2561 ... check your connections */
96:     Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
97:     while(1);
98:   }
99:   
100:   /* Display some basic information on this sensor */
101:   displaySensorDetails();
102:   
103:   /* Setup the sensor gain and integration time */
104:   configureSensor();
105:   
106:   /* We're ready to go! */
107:   Serial.println("");
108: 
109:   // start the Ethernet connection and the server:
110:   Ethernet.begin(mac, ip);                            // 4.Ethernet 開始
111:   server.begin();                                     // 5.サーバー開始
112:   Serial.print("server is at ");
113:   Serial.println(Ethernet.localIP());
114: }
115: 
116: 
117: void loop() {
118:   float weblight ;
119: 
120:   // listen for incoming clients
121:   EthernetClient client = server.available();
122:   if (client) {
123:     Serial.println("new client");
124:     // an http request ends with a blank line
125:     boolean currentLineIsBlank = true;
126:     while (client.connected()) {                // 接続要求があったか?
127:       if (client.available()) {
128:         char c = client.read();
129:         Serial.write(c);
130:         // if you've gotten to the end of the line (received a newline
131:         // character) and the line is blank, the http request has ended,
132:         // so you can send a reply
133:         if (c == '\n' && currentLineIsBlank) {
134:           // send a standard http response header
135:           client.println("HTTP/1.1 200 OK");
136:           client.println("Content-Type: text/html");
137:           client.println("Connection: close");  // the connection will be closed after completion of the response
138:           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
139:           client.println();
140:           client.println("<!DOCTYPE HTML>");
141:           client.println("<html>");
142:           client.println("<head>");
143:           client.println("<title>Lux</title>");
144:           client.println("</head>");
145:         
146:           GetLux( &weblight ) ;       // 照度測定を呼び出す。
147:           client.println("Light :");          
148:           client.println(weblight);          
149:           client.println(" lx");          
150: 
151:           client.println("<br />");
152:           
153:           client.println("</html>");
154:           break;
155:         }
156:         if (c == '\n') {
157:           // you're starting a new line
158:           currentLineIsBlank = true;
159:         } else if (c != '\r') {
160:           // you've gotten a character on the current line
161:           currentLineIsBlank = false;
162:         }
163:       }
164:     }
165:     // give the web browser time to receive the data
166:     delay(2);
167:     // close the connection:
168:     client.stop();
169:     Serial.println("client disconnected");
170:   }
171: }
172: 
173: void GetLux( float* weblight )
174: {
175:   sensors_event_t event;
176:   tsl.getEvent(&event);
177:  
178:   // Display the results (light is measured in lux)
179:   if (event.light)
180:   {
181:     Serial.print(event.light); Serial.println(" lux");
182:     *weblight = event.light ;
183:   }
184:   else
185:   {
186:     // If event.light = 0 lux the sensor is probably saturated
187:     //   and no reliable data could be generated!
188:     Serial.println("Sensor overload");
189:   }
190: }

0 件のコメント:

コメントを投稿