自带的例子修改而来,加了点简单的样式
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
/* 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 9 Apr 2012 by Tom Igoe modified 31 Dec 2015 by SiYu Wu */ #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 static_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() { for(int i=2;i<14;i++){ pinMode(i, INPUT_PULLUP); } // Open serial communications and wait for port to open: Serial.begin(9600); // while (!Serial) { // ; // wait for serial port to connect. Needed for Leonardo only // } // start the Ethernet connection and the server: Serial.print("Trying to configure Ethernet using DHCP..."); if (Ethernet.begin(mac) == 0){ Serial.println("Failed"); Serial.println("Use static IP"); Ethernet.begin(mac, static_ip); }else{ Serial.println("Success"); } server.begin(); Serial.print("Web server is at "); Serial.println(Ethernet.localIP()); pinMode(13, OUTPUT); digitalWrite(13, HIGH); } 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: 2"); // refresh the page automatically every 1 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html style='background:#39ABFF;height:100%;'><body style='height:100%;width:360px;margin:0 auto 0 120px;background:#0072C6;color:#fff;'>"); client.println("<title>Arduino IO Status</title><div style='padding:20px 50px;'><h1 style='font-weight:400;'>Arduino IO</h1>"); client.println("<span style='width:175px;display:inline-block;'><b>Pin</b></span><b>Value</b><br/>"); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("<span style='width:175px;display:inline-block;'>Analog input "); client.print(analogChannel); client.print("</span>"); client.print(sensorReading); client.println("<br />"); } client.println("<br />"); for (int digitalChannel = 2; digitalChannel < 14; digitalChannel++) { int sensorReading = digitalRead(digitalChannel); client.print("<span style='width:175px;display:inline-block;'>Digital input "); client.print(digitalChannel); client.print("</span>"); if(sensorReading){ client.print("HIGH"); }else{ client.print("LOW"); } client.println("<br />"); } client.println("</div></body></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 disonnected"); } } |
0
Leave A Comment