Arduino + 网络扩展版 通过Web显示IO口状态

发布日期:分类:智能硬件 & IOT Arduino + 网络扩展版 通过Web显示IO口状态无评论
时效性提醒:本文首次编写发布于8 年前。

自带的例子修改而来,加了点简单的样式

代码:

/*
    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");
    }
}

作者:WuSiYu

学生,Web开发者,智能硬件&IOT爱好者

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注