-
Notifications
You must be signed in to change notification settings - Fork 0
/
invernabot.ino
199 lines (161 loc) · 4.42 KB
/
invernabot.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
// Inverna Bot. An Arduino based greenhouse controller for sensing and controlled environment.
// Written by Luchux
/********************************************************************/
// libraries
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
/********************************************************************/
/********************************************************************/
// defining some pins
#define TIPO_SENSOR_THA DHT22 // DHT22 (AM2302), AM2321
#define SENSOR_THA 12 // pin for DHT22 Temperatura-humedad Aire
#define SENSOR_TIERRA 2 // pin for ground sensor
#define SENSOR_LUZ 0 // fotovoltaic sensor
#define SONAR_DISPARADOR 8 //sonar (measuring plants growing height)
#define SONAR_RECEPTOR 9
/********************************************************************/
class Ventana {
private:
Servo _servo;
bool _state;
int _pin;
public:
Ventana(int pin);
bool close();
bool open();
bool isOpen();
bool isClosed();
void showStatus();
void begin();
};
Ventana::Ventana(int pin) {
pinMode(pin, OUTPUT);
_pin = pin;
_state = false;
}
void Ventana::begin() {
this->_servo.attach(this->_pin);
this->_servo.write(90);
}
bool Ventana::open() {
this->_state = true;
this->_servo.write(180);
return this->_state;
Serial.print("Abriendo ventana \n");
}
bool Ventana::close() {
this->_state = false;
this->_servo.write(90);
return this->_state;
Serial.print("Cerrando ventana \n");
}
bool Ventana::isOpen() {
return (this->_state);
}
bool Ventana::isClosed() {
if(this->_state){
return false;
}
else {
return true;
}
}
void Ventana::showStatus() {
Serial.print("ventana: ");
if (this->_state) {
Serial.println("abierta");
}
else {
Serial.println("cerrada");
}
}
/********************************************************************/
// Initializing DHT sensor
DHT dht(SENSOR_THA, TIPO_SENSOR_THA);
// Intializing ground sensor
OneWire oneWire(SENSOR_TIERRA);
DallasTemperature sensors(&oneWire);
/********************************************************************/
float distancia;
float humedad;
float temperatura;
float temperatura_suelo;
int luz;
long tiempo;
Ventana ventana(7);
void setup() {
Serial.begin(9600);
//pinModes for sonar
pinMode(SONAR_DISPARADOR, OUTPUT);
pinMode(SONAR_RECEPTOR, INPUT);
// start dht sensor
dht.begin();
// start ground sensor
sensors.begin();
//initialize ventana, start servo
ventana.begin();
}
void loop() {
// showing in serial
Serial.println("=========================================");
Serial.println("Nueva medición de datos...");
delay(1000);
//reading humidity
humedad = dht.readHumidity();
//reading temperature
temperatura = dht.readTemperature();
//showing values
Serial.print("Humedad: ");
Serial.print(humedad);
Serial.println(" %\t");
Serial.print("Temperatura: ");
Serial.print(temperatura);
Serial.println(" *C ");
//Reading underground temperature
sensors.requestTemperatures(); // leyendo la temperatura del sensor
temperatura_suelo = sensors.getTempCByIndex(0);
Serial.print("Temperatura Suelo: ");
Serial.println(temperatura_suelo);
Serial.println();
//reading light
int nivelDeLuz;
int nivelDeLuzAjustado;
nivelDeLuz=analogRead(SENSOR_LUZ);
nivelDeLuzAjustado = map(nivelDeLuz, 990, 1030, 0, 100); //el sensor con esta resistencia va entre 990, y 1030.
//showing values
Serial.print("Luz: ");
Serial.print(nivelDeLuzAjustado);
Serial.println(" [0-100]");
/********************************************************************/
// Sonar activity
// sending pulse de 10useg
digitalWrite(SONAR_DISPARADOR, HIGH);
delayMicroseconds(10);
digitalWrite(SONAR_DISPARADOR, LOW);
// reading pulse (divided by 2, distance goes + back)
tiempo = (pulseIn(SONAR_RECEPTOR, HIGH)/2);
//transforming time into distance by sound speed
distancia = float(tiempo * 0.0343);
//showing
Serial.print("Distancia: ");
Serial.println(distancia);
Serial.println();
delay(1000);
//Accion ventana
if ( ventana.isClosed() ) {
if (temperatura_suelo > 28 ) {
Serial.println("deberia abrir ventana");
ventana.open();
}
}
else {
if (temperatura_suelo < 25) {
Serial.println("deberia cerrar ventana");
ventana.close();
}
}
//ventana, abierta o cerrada?
ventana.showStatus();
}