Arduino: Ethernet TCP Web Control LED
Jump to navigation
Jump to search
Sumber: http://embeddedlifehelp.blogspot.co.id/2014/10/turn-on-led-onoff-from-internet-using.html
Code
/*
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
* Pin 13 as LED output
created 16 Oct 2014
by Macjan Camilo Fernandes
*/
#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 led_state;
byte mac[] = {
0xC2, 0x83, 0xA1, 0x88, 0xFC, 0xB7 };
IPAddress ip(192,168,0,3);
String readString;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(8080);
void setup() {
pinMode(13, OUTPUT); //pin selected to control
led_state=0;
// 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:
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();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(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) {
///////////////
Serial.println(readString); //print to serial monitor for debuging
///////////////////// control arduino pin
if(readString.indexOf("?lighton") >0)//checks for on
{
digitalWrite(13, HIGH); // set pin 13 high
led_state=1;
Serial.println("Led On");
}
else{
if(readString.indexOf("?lightoff") >0)//checks for off
{
digitalWrite(13, LOW); // set pin 13 low
led_state=0;
Serial.println("Led Off");
}
}
//clearing string for next read
readString="";
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='MobileOptimized' content='320' />");
client.println("<meta name='viewport' content='width=device-width, initial-scale=1' />");
client.println("<meta name='HandheldFriendly' content='true' />");
// client.println("<meta http-equiv='refresh' content='5'>");
client.println("<TITLE>Embedded Life</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("
Embedded Life Server
"); client.println("
");
client.println("
");
if(led_state==1)
{
client.println("
LED State (Live) : ON
");
}
else
{
client.println("
LED State (Live) : OFF
");
}
client.println("<a href=\"/?lighton\"\" target='_parent'><button>LED On</button></a>");
client.println("<a href=\"/?lightoff\"\" target='_parent'><button>LED Off</button></a>");
client.println("<a href=\'/'\" target='_parent'><button>Status Check</button></a>");
client.println("<footer>
Server: Arduino Uno R3 (W5100 Ethernet Shield)
Copyright: <a href='http://embeddedlifehelp.blogspot.com'>Embedded Life</a>
</footer>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
}
}
}
}
}