-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodigoFuenteTFG.ino
283 lines (224 loc) · 6.68 KB
/
codigoFuenteTFG.ino
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <dht_nonblocking.h>
#include <Servo.h>
/*
* Defines
*/
#define DHT_SENSOR_TYPE DHT_TYPE_11
/*
* Sensor PIR
*/
static const int ledPin = 52; // Elegimos el pin para el led
static const int inputPin = 53; // Elegimos el pin de entrada para el sensor PIR
String pirState = "false"; // Empezamos asumiendo que el PIR no ha detectado a nadie
int val = 0; // Variable del estado de lectura del sensor
/*
* Sensor DHT
*/
static const int DHT_SENSOR_PIN = 50;
DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
//Valor de las mediciones de temperatura y humedad, se inicializan en valores normales para evitar problemas en los primeros ciclos
static float temperatura;
static float humedad;
float temperaturaTemp = 22;
float humedadTemp = 22;
/*
* Fotoresistor
*/
static const int pResistor = A0; // Se define al pin analogico 0
int valorFotoresistor; // Almacena los valores del fotoresistor (0-1023)
String hayLuz;
/*
* Servo motor
*/
Servo myservo; // Creamos el objeto servo para controlar el servo
static int pos = 0; // Variable que almacena la posicion del servo
static const int SERVO_MOTOR_PIN = 49;
/*
* Relés
*/
// Definimos los pines de los relés
/* static const int rele_termostato = 10;
static const int rele_luz1 = 11;
static const int rele_luz2 = 12;
static const int rele_persiana1 = 13;
static const int rele_persiana2 = 14; */
/*
* Control del json
*/
String jsonAntiguo = "";
/*
* Lectura puerto serie
*/
int option;
/*
* Debug pin
*/
static const int debugPin = 51;
void setup()
{
// Definimos el puerto serie
Serial.begin(9600);
while(!Serial){
}
/*
* PIR sensor
*/
pinMode(ledPin, OUTPUT); // Declaramos el pin del led como salida
pinMode(inputPin, INPUT); // Declaramos el pin del sensor como entrada
/*
* DHT sensor
*/
/*
* Servo motor
*/
myservo.attach(SERVO_MOTOR_PIN); // Une el servo en el pin al objeto servo
myservo.write(0);
delay(500);
pos = myservo.read();
/*
* Relés
*/
int j;
for(j = 2; j<=40; j++){
pinMode(j, OUTPUT);
}
/*
* Fotoresistor
*/
int calibracionFotoresistor;
calibracionFotoresistor = analogRead(pResistor);
if (calibracionFotoresistor > 20)
{
hayLuz = "true";
}
else if (calibracionFotoresistor <= 20)
{
hayLuz = "false";
}
/*
* Debug pin
*/
pinMode(debugPin, OUTPUT);
}
/*
* Definicion de funciones
*/
static bool measure_environment(float *temperature, float *humidity)
{
static unsigned long measurement_timestamp = millis();
/* Mide cada segundo. */
if (millis() - measurement_timestamp > 1000ul)
{
if (dht_sensor.measure(temperature, humidity) == true)
{
measurement_timestamp = millis();
return (true);
}
}
return (false);
}
void loop()
{
// Primero leemos los datos de los sensores y los escribimos en el puerto serie
/*
* PIR sensor
*/
val = digitalRead(inputPin); // Lee el input
if (val == HIGH)
{
// Si el input esta en HIGH significa que hemos detectado movimiento
digitalWrite(ledPin, HIGH); // Encendemos el led
// Se considera que ha habido movimiento
pirState = "true";
}
else
{
// Si el input esta en LOW significa que NO hemos detectado movimiento
digitalWrite(ledPin, LOW); // Apagamos el led
// Se considera que no ha habido movimiento
pirState = "false";
}
/*
* Fotoresistor
*/
valorFotoresistor = analogRead(pResistor); //Con el valor lo tratamos despues en el servicio que recibe los datos.
if (valorFotoresistor > 20)
hayLuz = "true";
else if (valorFotoresistor < 10)
hayLuz = "false";
//Posteriormente leemos los datos que hay en el puerto serie para leer las ordenes que lleguen
if (Serial.available() > 0)
{
//leemos la opcion enviada
//option = Serial.read();
String data;
data = Serial.readString();
Serial.println(data);
Serial.flush();
if (data.equals("enviame"))
{
unsigned long tiempoinicial, tiempoIntermedio;
tiempoinicial = tiempoIntermedio = millis();
while (tiempoIntermedio - tiempoinicial < 10000ul)
{
/*
* Sensor DHT
*/
bool medision = measure_environment(&temperatura, &humedad);
if (medision)
{
temperaturaTemp = temperatura;
humedadTemp = humedad;
break;
}
tiempoIntermedio = millis();
}
String json = "{\"temperatura\":" + String(temperatura) + ", \"humedad\":" + String(humedad) + ", \"luz\":" + hayLuz + ", \"movimiento\":" + pirState + "}";
Serial.println(json);
Serial.flush();
}
else if (data.equals("abrirVentana"))
{
//Abre la ventana
if (pos != 180)
{
for (pos = myservo.read(); pos <= 180; pos += 1)
{ // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Serial.println("OK");
}
else if (data.equals("cerrarVentana"))
{
//Cierra la ventana
if (pos != 0)
{
for (pos = myservo.read(); pos >= 0; pos -= 1)
{ // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Serial.println("OK");
}
else if (data.toInt() != 0)
{
long pin = data.toInt();
if (pin >= 2 && pin <= 40)
{
digitalWrite(pin, !digitalRead(pin));
}
else
Serial.println("No");
//Conectar Reles
Serial.println("OK");
}
else
{ //Ninguna orden coincide
Serial.println("No");
}
}
}