-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiot.ino
295 lines (251 loc) · 6.45 KB
/
biot.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
284
285
286
287
288
289
290
291
292
293
294
295
#include <FileIO.h>
#include <UbidotsYUN.h>
#include <LiquidCrystal.h>
#include <Process.h>
#include "DHT.h"
//Token único de Ubidots
#define TOKEN "YOUR_TOKEN_HERE"
//Definiendo pin y tipo de sensor DHTxx
#define DHTPIN 2
#define DHTTYPE DHT22
#define LDR_PIN A0 //LDR PIN
#define MAX_ADC_READING 5023 //Valor máximo de la conversión ADC
#define ADC_REF_VOLTAGE 5.0 //Voltaje referencial Vcc del circuito
#define REF_RESISTANCE 600 //Resistencia referencial del circuito
#define LUX_CALC_SCALAR 12518931 //Escalar
#define LUX_CALC_EXPONENT -1.405 //Exponente
#define MOISTURE_PIN A1
// Inicia la librería con los números de pines de la interfaz
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
DHT dht(DHTPIN, DHTTYPE);
byte termo[8] = //icon for termometer
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110,
};
byte drop[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
byte light[8] = //icon for water droplet
{
0b00000,
0b10101,
0b01110,
0b11111,
0b11111,
0b01110,
0b10101,
0b00000,
};
byte celsius[8] = {
0b11000,
0b11000,
0b00111,
0b01000,
0b01000,
0b01000,
0b01000,
0b00111,
};
byte cloud[8] = {
0b00000,
0b00000,
0b01000,
0b11110,
0b11111,
0b00111,
0b01000,
0b00101,
};
byte wifiSearch[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00100,
0b00000,
};
byte wifiOk[8] = {
0b00000,
0b01110,
0b10001,
0b00100,
0b01010,
0b00000,
0b00100,
0b00000,
};
Ubidots client(TOKEN);
int moistureIndicator;
int LED = 3;
void setup() {
client.init(); //?
Bridge.begin(); //?
Serial.begin(9600);
FileSystem.begin();
lcd.begin(16, 2);
dht.begin();
// while (!SerialUSB); // wait for Serial port to connect.
SerialUSB.println("BIoT v1.0 - github.com/fideliocc/biot\n");
pinMode(moistureIndicator,INPUT);
pinMode(LED,OUTPUT);
// set up the LCD's number of columns and rows:
lcd.createChar(0, termo);
lcd.createChar(1, drop);
lcd.createChar(2, light);
lcd.createChar(3, celsius);
lcd.createChar(4, cloud);
lcd.createChar(5, wifiSearch);
lcd.createChar(6, wifiOk);
lcd.setCursor(0,0);
lcd.write(byte(0));
lcd.setCursor(5,1);
lcd.write(byte(1));
lcd.setCursor(5,0);
lcd.write(byte(2));
lcd.setCursor(3,0);
lcd.write(byte(3));
lcd.setCursor(0,1);
lcd.write(byte(4));
// Print a message to the LCD.
lcd.setCursor(3, 1);
lcd.print("%");
delay(2000);
}
void loop() {
int ldrRawData;
int sensor3int = dht.readTemperature();
int sensor4int = dht.readHumidity();
float moistureValue;
float resistorVoltage, ldrVoltage;
float ldrResistance;
float ldrLux;
float sensor3;
float sensor4;
float sensor5;
Process wifiCheck; // inicia nuevo Process
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua"); // command you want to run
// while there's any characters coming back from the
// process, print them to the serial monitor:
if (wifiCheck.available() > 0) {
lcd.setCursor(15,1);
lcd.write(byte(6));
} else { if (wifiCheck.available() == 0) {
lcd.write(byte(5));
}
}
//LDR a Lux
ldrRawData = analogRead(LDR_PIN);
resistorVoltage = (float)ldrRawData / MAX_ADC_READING * ADC_REF_VOLTAGE;
ldrVoltage = ADC_REF_VOLTAGE - resistorVoltage;
ldrResistance = ldrVoltage/resistorVoltage * REF_RESISTANCE;
ldrLux = LUX_CALC_SCALAR * pow(ldrResistance, LUX_CALC_EXPONENT);
//Humedad del suelo y alerta por sequía
moistureValue = analogRead(MOISTURE_PIN);
if (moistureValue <= 300)
{
digitalWrite(LED, HIGH);
} else { digitalWrite(LED, LOW); };
//Enviando datalog a microSD
String dataString;
dataString += getTimeStamp();
dataString += " ";
// Sensores de luz y humedad del suelo
for (int analogPin = 0; analogPin < 1; analogPin++) {
dataString += "Nivel de luz = ";
dataString += String(ldrLux);
dataString += " lux";
if (analogPin < 2) {
dataString += " | "; // separa los valores
}
dataString += "Humedad del suelo = ";
dataString += String(moistureValue);
if (analogPin < 2) {
dataString += " | ";
}
}
//Sensores de temperatura, humedad e indice de calor
for (int digitalPin = 1; digitalPin < 2; digitalPin++) {
sensor3 = dht.readTemperature();
dataString += "Temperatura = ";
dataString += String(sensor3);
dataString += "ºC ";
if (digitalPin < 2) {
dataString += " | ";
}
sensor4 = dht.readHumidity();
dataString += "Humedad = ";
dataString += String(sensor4);
dataString += " % ";
if (digitalPin < 2) {
dataString += " | ";
}
sensor5 = dht.computeHeatIndex(sensor3, sensor4, false);
dataString += "Índice de calor = ";
dataString += String(sensor5);
dataString += "ºC";
}
//Enviando datos a Ubidots
client.add("Temperatura", dht.readTemperature());
client.add("Humedad", dht.readHumidity());
client.add("Luz" , ldrLux);
client.add("Humedad del suelo" , moistureValue);
client.add("Indice de calor" , dht.computeHeatIndex(sensor3, sensor4, false));
client.sendAll();
lcd.setCursor(1,0);
lcd.print(sensor3int);
lcd.setCursor(1,1);
lcd.print(sensor4int);
lcd.setCursor(6,1);
lcd.print(moistureValue);
lcd.setCursor(6,0);
lcd.print(ldrLux);
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
SerialUSB.println(dataString);
}
// if the file isn't open, pop up an error:
else {
SerialUSB.println("Eror al abrir datalog.txt");
};
delay (2000);
}
// This function return a string with the time stamp
String getTimeStamp() {
String result;
Process time;
// date is a command line utility to get the date and the time
// in different formats depending on the additional parameter
time.begin("date");
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command
// read the output of the command
while (time.available() > 0) {
char c = time.read();
if (c != '\n') {
result += c;
}
}
return result;
}