Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //simple button GET server code to control servo and arduino pin 5
- //for use with IDE 1.0
- //open serial monitor to see what the arduino receives
- //use the \ slash to escape the " in the html
- //for use with W5100 based ethernet shields
- //Powering a servo from the arduino usually DOES NOT WORK.
- //note that the below bug fix may be required
- // http://code.google.com/p/arduino/issues/detail?id=605
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x94 }; //this is the mac address on the sticker on your Ethernet shield
- byte ip[] = { 192, 168, 1, 177 }; //here you are setting the ip address of the Ethernet shield yourself. The last three digits can be whatever you like. But they must be inside the pool of allowed ip's which is usually 200. So you can use any number from 192.168.1.1 to 192. 168.1.200 as long as they are not being used my any other device on you network. Use this ip when setting up port forwarding in the router for NAT-> port forwarding.
- byte gateway[] = { 192, 168, 1, 1 }; //this is the ip you get when you run the example sketch DhcpAddressPrinter to find this ip remember to look at the serial monitor.
- byte subnet[] = { 255, 255, 255, 0 }; //this can be found in cmd->ipconfig, but usually it is as it is here and so shouldn't need to be changed.
- EthernetServer server(80); //you will have to set this to the same as the start and end ports of the router configuration. Both the start and end ports are the same.
- String readString;
- //////////////////////
- void setup(){
- pinMode(6, OUTPUT); //pin selected to control
- //start Ethernet
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- //the pin for the servo co
- //enable serial data print
- Serial.begin(9600);
- Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
- }
- void loop(){
- // Create a client connection
- EthernetClient client = server.available();
- if (client) {
- 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 HTTP request has ended
- if (c == '\n') {
- ///////////////
- Serial.println(readString); //print to serial monitor for debuging
- 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='apple-mobile-web-app-capable' content='yes' />");
- client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
- client.println("<link rel='stylesheet' type='text/css' href='http://chriscosma.co.cc/a.css' />");
- client.println("<TITLE>Home Automation</TITLE>");
- client.println("</HEAD>");
- client.println("<BODY>");
- client.println("<H1>Home Automation</H1>");
- client.println("<hr />");
- client.println("<br />");
- client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
- client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");
- client.println("</BODY>");
- client.println("</HTML>");
- delay(1);
- //stopping client
- client.stop();
- ///////////////////// control arduino pin
- if(readString.indexOf("?lighton") >0)//checks for on
- {
- digitalWrite(6, HIGH); // set pin 4 high
- Serial.println("Led On");
- client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
- }
- else{
- if(readString.indexOf("?lightoff") >0)//checks for off
- {
- digitalWrite(6, LOW); // set pin 4 low
- Serial.println("Led Off");
- client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
- }
- }
- //clearing string for next read
- readString="";
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement