Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------- Servo Motor -------------------------------------
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Servo.h>
- Servo microservo;
- int pos = 0;
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- byte ip[] = { 192, 168, 1, 188 };
- byte gateway[] = { 192, 168, 1, 1 };
- byte subnet[] = { 255, 255, 255, 0 };
- EthernetServer server(8089);
- String readString;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- microservo.attach(7);
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- 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 (c == '\n') {
- Serial.println(readString);
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<HTML>");
- client.println("<HEAD>");
- client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">");
- client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
- client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
- //<!-- Optional theme -->
- client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css\">");
- //<!-- Latest compiled and minified JavaScript -->
- client.println("<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js\"></script>");
- client.println("</HEAD>");
- client.println("<BODY>");
- client.println("<center>");
- client.println("<H1>Arduino Servo Motor</H1>");
- client.println("<hr />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<a href=\"/?button2on\" class=\"btn btn-primary\">SERVO</a>");
- client.println("<a href=\"/?button2off\" class=\"btn btn-primary\">SERVO</a>");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("</center>");
- client.println("</BODY>");
- client.println("</HTML>");
- delay(1);
- client.stop();
- //controls the Arduino if you press the buttons
- if (readString.indexOf("?button2on") > 0) {
- for (pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- microservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 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
- {
- microservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- //clearing string for next read
- readString = "";
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement