-
Notifications
You must be signed in to change notification settings - Fork 1
/
nivel_arduino.ino
185 lines (154 loc) · 5.33 KB
/
nivel_arduino.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
//Authors:Nayana Machado and Marina Ferrarez
//Email: {[email protected];[email protected]}
//Date: 11/06/2020
//Revision: LESS DATA + CALIBRAÇÃO ULTRASONICO _ delay 60000 1 min#
//Project: {Data logger com sensor de temperatura (DHT22) e nível de água (HC-SR04)}
#include <NewPing.h> // NewPing - Version: Latest
//#include <RTClib.h> // RTClib - Version: Latest
#include <RTClib.h>
#include <DHT.h> // DHT sensor library - Version: Latest
#include <DHT_U.h> // DHT sensor library - Version: Latest
//#include <SDConfig.h> // SDConfig - Version: Latest
#include <SD.h>
#include <Adafruit_Sensor.h>
//Pino CS do cartao SD
int Pino_CS = 10;
//Definicoes do sensor de temperatura DHT11
//#define DHTPIN A0 //Pino de conexao
//#define DHTTYPE DHT11 //Tipo de sensor
//Definicoes do sensor de temperatura DHT22 + HC-SR04
#define DHTPIN 6 // DHT-22 Output Pin connection
#define DHTTYPE DHT22 // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN 4 //HC-SR04
#define ECHO_PIN 5 //HC-SR04
#define MAX_DISTANCE 400 //HC-SR04
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //inicialize the sensor - HC-SR04
//Le as informacoes da temperatura
// float t = dht.readTemperature();
float hum; // Stores humidity value in percent
float temp; // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float dist; // Stores calculated distance in cm
float soundsp; // Stores calculated speed of sound in M/S
float soundcm; // Stores calculated speed of sound in cm/ms
float nivel_agua; // stores calculated water level in cm
float nivel_agua_m; // stores calculated water level in cm
int iterations = 5;
DHT dht(DHTPIN, DHTTYPE); //Cria objeto DHT
RTC_DS1307 rtc;
File file;
void setup()
{
Serial.begin(9600); //speed parameter
Serial.println("Data logger com sensor de temperatura DHT22 e sensor Ultrassônico");
Serial.println();
//Inicia o cartao SD
Serial.println("Iniciando cartao SD...");
if (!SD.begin(Pino_CS))
{
Serial.println("Falha na inicializacao do SD!");
return;
}
Serial.println("Cartao SD iniciado. OK");
Serial.println("dia-mes-ano;hora:min:sec;hum%;tempC;distcm");
//Serial.println();
//Verifica as condicoes do RTC
if (! rtc.begin())
{
Serial.println("RTC nao encontrado!");
while (1);
}
if (! rtc.isrunning())
{
Serial.println("RTC nao operante!");
//A linha abaixo ajusta o RTC com a data e hora do momento da compilacao
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//A linha abaixo permite ajustar manualmente a data e hora do RTC
//Formato: DateTime(ano, mes, dia, hora, minuto, segundo)
//rtc.adjust(DateTime(2020, 6, 19, 14, 32, 0));
}
//Inicializa o DHT22
dht.begin();
}
void loop(void)
{
delay(2000); // Delay so DHT-22 sensor can stabalize - 2 segundos é o mín
hum = dht.readHumidity(); // Get Humidity value
temp = dht.readTemperature(); // Get Temperature value
// Calculate the Speed of Sound in M/S
soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
// Convert to cm/ms
soundcm = soundsp / 10000;
duration = sonar.ping_median(iterations);
// Calculate the distance
dist = (duration/2)*soundcm;
//Le as informacoes do RTC
DateTime now = rtc.now();
//Serial monitor informacoes de hora
Serial.print(now.day() < 10 ? "0" : "");
Serial.print(now.day(), DEC);
Serial.print('-');
Serial.print(now.month() < 10 ? "0" : "");
Serial.print(now.month(), DEC);
Serial.print('-');
Serial.print(now.year() < 10 ? "0" : "");
Serial.print(now.year(), DEC);
Serial.print(";");
Serial.print(now.hour() < 10 ? "0" : "");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute() < 10 ? "0" : "");
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second() < 10 ? "0" : "");
Serial.print(now.second(), DEC);
Serial.print(';');
//Serial.print("Sound: ");
//Serial.print(soundsp);
//Serial.print(" m/s, ");
//Serial.print("Hum:");
Serial.print(hum);
Serial.print(";");
Serial.print(temp);
Serial.print(";");
Serial.print(dist);
Serial.println(";");
//Gravacao do cartao
//Abre arquivo no SD para gravacao
file = SD.open("nivel.csv", FILE_WRITE);
//Grava os dados no cartao SD
file.print(now.day() < 10 ? "0" : "");
file.print(now.day(), DEC);
file.print('-');
file.print(now.month() < 10 ? "0" : "");
file.print(now.month(), DEC);
file.print('-');
file.print(now.year() < 10 ? "0" : "");
file.print(now.year(), DEC);
file.print(";");
file.print(now.hour() < 10 ? "0" : "");
file.print(now.hour(), DEC);
file.print(':');
file.print(now.minute() < 10 ? "0" : "");
file.print(now.minute(), DEC);
file.print(':');
file.print(now.second() < 10 ? "0" : "");
file.print(now.second(), DEC);
file.print(";");
//file.print("Sound: ");
//file.print(soundsp);
//file.print(" m/s, ");
//file.print("Temp:");
file.print(hum);
file.print("C,Umid:");
file.print(temp);
file.print(";");
file.print(dist);
file.println("cm");
//Fecha arquivo
file.close();
//Delay ate a proxima leitura
//delay(58000);
delay(898000); //15 min
//delay(1000); //tempo de delay para ver o programa rodas
}