diff options
Diffstat (limited to 'ethernet-servomotore.ino')
-rw-r--r-- | ethernet-servomotore.ino | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/ethernet-servomotore.ino b/ethernet-servomotore.ino new file mode 100644 index 0000000..a4045e3 --- /dev/null +++ b/ethernet-servomotore.ino @@ -0,0 +1,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 + +} + |