/* Project: Arduino Uno with Arduino W5100 Ethernet Shield Function: Webserver to control LED and SG90 Micro Servo Motor */ #include //include library code #include #include //************************************************************************************* int led = 5; Servo myservo; //creates servo object int pos = 0; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical MAC address byte ip[] = { 192, 168, 1, 178 }; //IP address in your local network (that's what you need to use in your browser.("192.168.1.178") byte gateway[] = { 192, 168, 1, 1 }; //internet access via router byte subnet[] = { 255, 255, 255, 0 }; //subnet mask EthernetServer server(80);//server port String readString; //************************************************************************************** void setup() { //initializes serial communications at 9600 bps and wait for port to open: Serial.begin(9600); while (!Serial) {; //waits for serial port to connect. required for leonardo only } pinMode(led, OUTPUT); myservo.attach(6); //starts the Ethernet connection and the server: Ethernet.begin(mac, ip, gateway, subnet); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available();//creates a client connection 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(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println("

Project 062a Arduino W5100 Ethernet Shield

"); client.println("
"); client.println("
"); client.println("

Webserver

"); client.println("
"); client.println("Turn On LED"); client.println("Turn Off LED
"); client.println("
"); client.println("Rotate Left"); client.println("Rotate Right
"); client.println(""); client.println(""); delay(1); //stopping client client.stop(); //controls the Arduino if you press the buttons if (readString.indexOf("?button1on") >0){ digitalWrite(led, HIGH); } if (readString.indexOf("?button1off") >0){ digitalWrite(led, LOW); } if (readString.indexOf("?button2on") >0){ for(pos = 0; pos < 180; pos += 3) //goes from 0 degrees to 180 degrees in steps of 1 degree { myservo.write(pos); //tell servo to go to position in variable 'pos' delay(15); //sets delay 15ms for the servo to reach the position } } if (readString.indexOf("?button2off") >0){ for(pos = 180; pos>=1; pos-=3) //goes from 180 degrees to 0 degrees { myservo.write(pos); //tell servo to go to position in variable 'pos' delay(15); //sets delay 15ms for the servo to reach the position } } readString=""; //clearing string for next read } } } } }