Progetto

Generale

Profilo

Wiki » Insettoteca_v1_4.ino

Sorgente arduino - Francesco Menghini, 17-09-2014 00:24

 
1
/*
2
 rev 1.3 04/04/2014
3
 Insettoteca 
4
 http://frameyun.local/sd/index.php
5
 */
6

    
7
#include <Bridge.h>
8
#include <YunServer.h>
9
#include <YunClient.h>
10
#include <Process.h>
11
#include <dht.h>
12
#include <Servo.h>
13

    
14
#define DHT22_PIN 13
15

    
16
// Listen on default port 5555, the webserver on the Yun
17
// will forward there all the HTTP requests for us.
18
YunServer server;
19
String readString; 
20
dht DHT;
21
Servo motore;
22

    
23
// Variabili
24
int cicli = 0;           //Contatore del numero di loop
25
int timePumpOn = 400;      //Contatore per l'autospegnimento della pompa
26

    
27
// Costanti
28
int oLight = 3;
29
int oVent  = 5;          //Porta del ventilatore
30
int oPump  = 4;          //Porta della pompa
31
int oHeat  = 2;          //Porta del riscaldatore
32
int maxPumpOn = 400;     //Tempo massimo di accensione della pompa 200 cicli
33
int posWebcam  = 90;     //Posizione della webcam centrale
34

    
35

    
36
// ---- SETUP ----
37
void setup() {
38
  
39
//  Serial.begin(9600);
40
//  Serial.println("--INSETTOTECA V1.1--");
41
  
42
  motore.attach(10);       // attaches the servo on pin 9 to the servo object 
43
  
44
  // Bridge startup
45
  pinMode(oLight, OUTPUT);
46
  pinMode(oVent, OUTPUT);
47
  pinMode(oPump, OUTPUT);  
48
  pinMode(oHeat, OUTPUT);
49
  
50
  //Modalità di avvio
51
  digitalWrite(oPump, HIGH);
52
  digitalWrite(oHeat, HIGH);
53
  
54
  Bridge.begin();
55
  //digitalWrite(13, HIGH);
56
 
57
  //Acquisizione stato porte
58
  //Process linuxBootUp;
59
 
60
  // Listen for incoming connection only from localhost (Linino)
61
  // (no one from the external network could connect)
62
  server.listenOnLocalhost();
63
  server.begin();
64
  
65
}
66

    
67
// ------ LOOP ----- 
68
void loop() {
69
  // Get clients coming from server
70
  YunClient client = server.accept();
71
  Process linux;
72
  String testo;
73
  
74
  //-- GESTIONE AUTOMATICA --
75
  if(timePumpOn <= 0){
76
    //Spegniamo la pompa dopo un massimo di tempo
77
    digitalWrite(oPump, HIGH);
78
    linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,0,3,datetime('now','localtime') );\" ");
79
    delay(10);
80
    timePumpOn = maxPumpOn;
81
  }
82
  
83
  
84
  //-- GESTIONE RICHIESTE DA PAGINA WEB --
85
  if (client) {
86
        
87
    digitalWrite(13, HIGH);
88
    // read the command
89
    String command = client.readString();
90
    command.trim();        //kill whitespace
91
//    Serial.println(command); //Visualizza sul monitor seriale i comandi inviati
92

    
93
    if (command == "ledon") {
94
       digitalWrite(13, HIGH);
95
    }
96
    else if (command == "ledoff") {
97
       digitalWrite(13, LOW);
98
    }
99
    
100
    //Gestione Luce
101
    if (command == "lightON") {
102
       digitalWrite(oLight, LOW);
103
       client.print(" <button width=50px height=20px style='background:green'> ACCESA </button>");
104
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,1,1,datetime('now','localtime') );\" ");  
105
    }
106
    else if (command == "lightOFF") {
107
       digitalWrite(oLight, HIGH);
108
       client.print(" <button width=50px height=20px style='background:red'> SPENTA </button> ");
109
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,0,1,datetime('now','localtime') );\" ");  
110
    }
111
    
112
        //Gestione Ventola
113
    if (command == "ventON") {
114
       digitalWrite(oVent, LOW);
115
       client.print(" <button width=50px height=20px style='background:green'> ACCESA </button>");
116
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,1,2,datetime('now','localtime') );\" ");
117
    }
118
    else if (command == "ventOFF") {
119
       digitalWrite(oVent, HIGH);
120
       client.print(" <button width=50px height=20px style='background:red'> SPENTA </button> ");
121
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,0,2,datetime('now','localtime') );\" "); 
122
    }
123
    
124
    //Gestione pompa
125
    if (command == "pumpON") {
126
       digitalWrite(oPump, LOW);
127
       timePumpOn = maxPumpOn - 1;
128
       client.print(" <button width=50px height=20px style='background:green'> ACCESA </button>");
129
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,1,3,datetime('now','localtime') );\" ");
130
    }
131
    else if (command == "pumpOFF") {
132
       digitalWrite(oPump, HIGH);
133
       timePumpOn = maxPumpOn;
134
       client.print(" <button width=50px height=20px style='background:red'> SPENTA </button> ");
135
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,0,3,datetime('now','localtime') );\" "); 
136
    }
137
    
138
    //Gestione riscaldamento
139
    if (command == "heatON") {
140
       digitalWrite(oHeat, LOW);
141
       client.print(" <button width=50px height=20px style='background:green'> ACCESA </button>");
142
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,1,4,datetime('now','localtime') );\" ");
143
    }
144
    else if (command == "heatOFF") {
145
       digitalWrite(oHeat, HIGH);
146
       client.print(" <button width=50px height=20px style='background:red'> SPENTA </button> ");
147
       linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO output VALUES (null,0,4,datetime('now','localtime') );\" "); 
148
    }
149
    
150
    //Comando della rotazione  della webcam
151
    if (command == "webcamDx") {
152
      if (posWebcam <= 160 ){
153
          posWebcam += 15;
154
      }
155
      String testo = String(posWebcam);
156
       client.print(" <button width=50px height=20px style='background:blue'> "+testo+"</button> ");
157
       //Fai spostare la webcam a Destra
158
       motore.write(posWebcam);
159
    }else if (command == "webcamSx") {
160
       if (posWebcam >= 15){
161
         posWebcam -= 15;
162
       }
163
       String testo = String(posWebcam);
164
       client.print(" <button width=50px height=20px style='background:blue'> "+testo+" </button> ");
165
       motore.write(posWebcam);
166
    }
167
  
168
    // Close connection and free resources.
169
    client.stop();
170
  }
171
  
172
  // -- GESTIONE STATO --
173

    
174
  //Sonda temperatura ed umidita  
175
  if(cicli > 5000){ 
176

    
177
    // READ DATA
178
    //Serial.print("DHT22, \t");
179
    int chk = DHT.read22(DHT22_PIN);
180
    //switch (chk)
181
    //{
182
    //  case DHTLIB_OK:  
183
 // 		Serial.print("OK,\t"); 
184
 // 		break;
185
 //     case DHTLIB_ERROR_CHECKSUM: 
186
 // 		Serial.print("Checksum error,\t"); 
187
 // 		break;
188
 //     case DHTLIB_ERROR_TIMEOUT: 
189
 // 		Serial.print("Time out error,\t"); 
190
 // 		break;
191
 //     default: 
192
 // 		Serial.print("Unknown error,\t"); 
193
 // 		break;
194
 //   }
195
    
196
    // Display data
197
    String temp = String(DHT.temperature);
198
    String umid = String(DHT.humidity);
199
    
200
    linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO input VALUES (null,\""+temp+"\",1,datetime('now','localtime') );\" ");
201
    delay(10);
202
    linux.runShellCommand("sqlite3 /mnt/sd/arduino/db/data.db \"INSERT INTO input VALUES (null,\""+umid+"\",2,datetime('now','localtime') );\" ");
203
    delay(10);
204
    
205
    cicli = 0;
206
  }
207
  
208

    
209
  //-- CHISURA ED AVANZAMENTO --
210
  
211
  //Avanzamento timer disattivazione pompa
212
  if ( timePumpOn < maxPumpOn){
213
    timePumpOn -= 1;
214
  }
215
  
216
  
217
  //Chiusura e avanzamento del ciclo  
218
  cicli += 1;
219
  delay(5); // Poll every 10ms
220
}
    (1-1/1)