summaryrefslogtreecommitdiff
path: root/ethernet-servomotore.ino
blob: a4045e353c10b84217ee495eb490e7069f752da2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <SPI.h>
#include <Ethernet.h>


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
int servopin = 3;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "codice-sorgente.it";
char serverPage[] = "/UTCTime.php?zone=Europe/Rome";
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  myservo.attach(servopin);
  Serial.println("Init");
}

void loop(){
  delay(3000); 
  sendGET();
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
//    client.println("GET /ioprogrammo/UTCTime.php HTTP/1.0"); //download text
    client.print("GET ");
    client.print(serverPage);
    client.println(" HTTP/1.0");
    client.print("Host: ");
    client.println(serverName);
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
    return;
  }

String response = "";
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    response += c;
  }

  Serial.println(response);
  int da = response.indexOf("<")+1;
  int a = response.indexOf(">");
  String pageValue = response.substring(da,a);

  da = pageValue.indexOf(" ")+1;
  a = pageValue.indexOf(":",da);
  String hour = pageValue.substring(da,a);
  int ora = hour.toInt();
  int rotazione = map(ora, 0, 23, 0, 180); //può essere 90 invece di 180
  Serial.print("Writing this value to servo: ");
  Serial.println(rotazione);
  myservo.write(rotazione);

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}