Step 3: Programming the Arduino
Code:
Load the above code in arduino
Code:
/* Web Server A simple web server that shows the static webpage. 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) */ #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 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); String HTTP_req; // stores the HTTP request boolean LED_status = 0; // state of LED, off by default void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only pinMode(13, OUTPUT); // LED on pin 2 } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } 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(); HTTP_req += c; //Save the HTTP request 1 char at a time // 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: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<head>"); client.println("<title>Arduino Web Server</title>"); client.println("</head>"); client.println("<style>"); client.println("table {"); client.println(" width:70%;"); client.println("}"); client.println("table, th, td {"); client.println(" border: 1px solid black;"); client.println(" border-collapse: collapse;"); client.println("}"); client.println("th, td {"); client.println(" padding: 5px;"); client.println(" text-align: left;"); client.println("}"); client.println("table#t01 tr:nth-child(even) {"); client.println(" background-color: #eee;"); client.println("}"); client.println("table#t01 tr:nth-child(odd) {"); client.println(" background-color:#fff;"); client.println("}"); client.println("table#t01 th {"); client.println(" background-color: #00A8A9;"); client.println(" color: white;"); client.println("}"); client.println("</style>"); client.println("</head>"); client.println("<body>"); client.println("<br>"); client.println("<H1><center>blog.circuits4you.com Static web page demo</center></h1>"); client.println("<center>"); client.println("<table id=""t01"">"); client.println(" <tr>"); client.println(" <th>First Name</th>"); client.println(" <th>Last Name</th>"); client.println(" <th>Points</th>"); client.println(" </tr>"); client.println(" <tr>"); client.println(" <td>Jill</td>"); client.println(" <td>Smith</td> "); client.println(" <td>50</td>"); client.println(" </tr>"); client.println(" <tr>"); client.println(" <td>Eve</td>"); client.println(" <td>Jackson</td>"); client.println(" <td>94</td>"); client.println(" </tr>"); client.println(" <tr>"); client.println(" <td>John</td>"); client.println(" <td>Doe</td>"); client.println(" <td>80</td>"); client.println(" </tr>"); client.println("</table>"); client.println("</center>"); client.println("</body>"); client.println("</html>"); Serial.print(HTTP_req); HTTP_req = ""; //finishehed with request, empty string 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 disconnected"); } } // switch LED and send back HTML for LED checkbox void ProcessCheckbox(EthernetClient cl) { if (HTTP_req.indexOf("LED2=2") > -1) { // see if checkbox was clicked // the checkbox was clicked, toggle the LED if (LED_status) { LED_status = 0; } else { LED_status = 1; } } if (LED_status) { // switch LED on digitalWrite(13, HIGH); // checkbox is checked cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \ onclick=\"submit();\" checked>LED2"); } else { // switch LED off digitalWrite(13, LOW); // checkbox is unchecked cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \ onclick=\"submit();\">LED2"); } }
Load the above code in arduino
No comments:
Post a Comment