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