source: trunk/Arduino/testing/WebServerTesting/WebServerTesting.ino @ 27

Last change on this file since 27 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 4.0 KB
Line 
1/*
2  Web Server
3
4 A simple web server that shows the value of the analog input pins.
5 using an Arduino Wiznet Ethernet shield.
6
7 Circuit:
8 * Ethernet shield attached to pins 10, 11, 12, 13
9 * 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
23// Enter a MAC address and IP address for your controller below.
24// The IP address will be dependent on your local network:
25byte mac[] = {
26  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
27};
28IPAddress ip(192, 168, 16, 70);
29
30// Initialize the Ethernet server library
31// with the IP address and port you want to use
32// (port 80 is default for HTTP):
33EthernetServer server(80);
34
35const int chipSelectPin = 4;
36int pinData = LOW;
37
38void setup() {
39  // You can use Ethernet.init(pin) to configure the CS pin
40  //Ethernet.init(10);  // Most Arduino shields
41  //Ethernet.init(5);   // MKR ETH shield
42  //Ethernet.init(0);   // Teensy 2.0
43  //Ethernet.init(20);  // Teensy++ 2.0
44  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
45  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet
46
47  // Open serial communications and wait for port to open:
48  Serial.begin(9600);
49  while (!Serial) {
50    ; // wait for serial port to connect. Needed for native USB port only
51  }
52  Serial.println("Ethernet WebServer Example");
53
54  // start the Ethernet connection and the server:
55  Ethernet.begin(mac, ip);
56
57  // Check for Ethernet hardware present
58  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
59    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
60    while (true) {
61      delay(1); // do nothing, no point running without Ethernet hardware
62    }
63  }
64  if (Ethernet.linkStatus() == LinkOFF) {
65    Serial.println("Ethernet cable is not connected.");
66  }
67
68  // start the server
69  server.begin();
70  Serial.print("server is at ");
71  Serial.println(Ethernet.localIP());
72
73  // simulate usage of D4
74  pinMode(chipSelectPin, OUTPUT);
75  pinData ^= HIGH;
76}
77
78
79void loop() {
80  // listen for incoming clients
81  EthernetClient client = server.available();
82  if (client) {
83    Serial.println("new client");
84    // an http request ends with a blank line
85    boolean currentLineIsBlank = true;
86    while (client.connected()) {
87      if (client.available()) {
88        char c = client.read();
89        Serial.write(c);
90        // if you've gotten to the end of the line (received a newline
91        // character) and the line is blank, the http request has ended,
92        // so you can send a reply
93        if (c == '\n' && currentLineIsBlank) {
94          // send a standard http response header
95          client.println("HTTP/1.1 200 OK");
96          client.println("Content-Type: text/html");
97          client.println("Connection: close");  // the connection will be closed after completion of the response
98          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
99          client.println();
100          client.println("<!DOCTYPE HTML>");
101          client.println("<html>");
102          // output the value of each analog input pin
103          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
104            int sensorReading = analogRead(analogChannel);
105            client.print("analog input ");
106            client.print(analogChannel);
107            client.print(" is ");
108            client.print(sensorReading);
109            client.println("<br />");
110          }
111          client.println("</html>");
112          break;
113        }
114        if (c == '\n') {
115          // you're starting a new line
116          currentLineIsBlank = true;
117        } else if (c != '\r') {
118          // you've gotten a character on the current line
119          currentLineIsBlank = false;
120        }
121      }
122    }
123    // give the web browser time to receive the data
124    delay(1);
125    // close the connection:
126    client.stop();
127    pinData ^= HIGH;
128    digitalWrite(chipSelectPin, pinData);
129    Serial.println("client disconnected");
130  }
131}
Note: See TracBrowser for help on using the repository browser.