luishenriique

protectedhouse_arduino

Jun 3rd, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Servo.h>
  4.  
  5. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  6. IPAddress server(192,168,1,10);
  7. IPAddress ip(192,168,1,5);
  8.  
  9. EthernetClient client;
  10. Servo myServo;
  11. int pinBotao = 7;
  12. int pinServo = 9;
  13. int pos = 0;
  14. int valorBotao = 0;
  15.  
  16. int led = 6;
  17. int led2 = 3;
  18.  
  19. void servo(String cmd){
  20.   if(cmd == "Open"){
  21.     for(pos; pos < 140; pos++){
  22.       myServo.write(pos);
  23.       digitalWrite(led, HIGH);
  24.       delay(20);
  25.     }
  26.   }else if(cmd == "Close"){
  27.     for(pos; pos > 92; pos--){
  28.       myServo.write(pos);
  29.       digitalWrite(led, LOW);
  30.       delay(20);
  31.     }    
  32.   }
  33. }
  34.  
  35. void alarme(){
  36.   if (Ethernet.begin(mac) == 0) {
  37.     Serial.println("Failed to configure Ethernet using DHCP");
  38.     Ethernet.begin(mac, ip);
  39.   }
  40.   char a;
  41.   do{
  42.     Serial.println("connecting...");
  43.     if(client.connect(server, 80)) {
  44.       Serial.println("connected");
  45.       client.println("GET /android/alarme.php");
  46.     }else{
  47.       Serial.println("connection failed");
  48.     }
  49.     if (client.available()) {
  50.       a = client.read();
  51.     }
  52.   }while(a != '1');
  53.  
  54.   if (!client.connected()) {
  55.     Serial.println();
  56.     Serial.println("disconnecting.");
  57.     client.stop();
  58.   }
  59. }
  60.  
  61. void janela(){
  62.   if (Ethernet.begin(mac) == 0) {
  63.     Serial.println("Failed to configure Ethernet using DHCP");
  64.     Ethernet.begin(mac, ip);
  65.   }
  66.   Serial.println("connecting...");
  67.   if(client.connect(server, 80)) {
  68.     Serial.println("connected");
  69.     client.println("GET /android/janela.txt");
  70.     Serial.println("Funcionando");
  71.   }else{
  72.     Serial.println("connection failed");
  73.   }
  74. }
  75.  
  76. void setup() {
  77.   myServo.attach(9);
  78.   pinMode(led, OUTPUT);
  79.   Serial.begin(9600);
  80.  
  81.   janela();
  82. }
  83.  
  84. void loop(){  
  85.   while(client.connected()){
  86.     if (client.available()) {
  87.     char j = client.read();
  88.    
  89.       if(j == '0'){
  90.         valorBotao = digitalRead(pinBotao);
  91.         if(valorBotao == HIGH){
  92.           client.stop();
  93.           digitalWrite(led2, HIGH);
  94.           Serial.println("Alarme acionado");
  95.           alarme();
  96.         }else{
  97.           digitalWrite(led2, LOW);
  98.         }
  99.         digitalWrite(led, LOW);
  100.         servo("Close");
  101.       }else if (j == '1'){
  102.         digitalWrite(led, HIGH);
  103.         servo("Open");
  104.       }
  105.      Serial.println(j);
  106.     }
  107.   }
  108.   client.stop();
  109.  
  110.   if (!client.connected()) {
  111.     Serial.println();
  112.     Serial.println("disconnecting.");
  113.     janela();
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment