Introducción
El objetivo de este tutorial es medir durante aproximadamente 9 horas la temperatura, humedad y presión atmosférica de dos habitaciones (la de matrimonio y la de mi hijo) para ver las variaciones que hay durante las noche de estos tres parámetros en cada habitación y posteriormente comparar los valores obtenidos entre ambas habitaciones.
Toda esta información la almacenaremos en una tarjeta microSD para posteriormente analizar los datos.
NOTA: Si quereis saber como enviar la información al Cloud podéis ver mi otro tutorial en el siguiente enlace: https://mecatronicalab.es/internet-of-things-arduino-adafruit-iot-cloud/
Listado de componentes
-
- Arduino MKR WiFi 1010.
- Arduino MKR Zero.
- Arduino MKR ENV SHIELD.
- Arduino MKR CONNECTOR CARRIER.
- Sensor GROVE – Barometer Sensor (BMP280)
- Sensor GROVE -Temperature&Humidity Sensor Pro (DHT22, AM2302)
- 3 pilas Panasonic NCR18650B de Li-Ion recargables ( 3400 mAh, 3.7V).
- Portapilas para pilas 18650 con cable de alimentación.
- 2 tarjetas micro SD.
Librerías RTC y SD
Tanto la placa Arduino MKR Zero como la placa Arduino MKR WiFi 1010 están basadas en arquitecturas SAMD y podremos usar el RTC interno (reloj de tiempo real) para poder hacer un seguimiento de la hora en que hacemos las mediciones. La librería RTC nos permitirá usar y controlar este reloj interno en nuestras placas.
Para más información sobre la librería RTC:
https://www.arduino.cc/en/Reference/RTC
NOTA: Cada vez que la placa se enciende el RTC se reinicia y comenzará desde la fecha y hora que hayamos indicado en el sketch. Para mantener el tiempo y el RTC en funcionamiento, es necesario mantener la placa alimentada.
Tanto la MKR Zero como la MKR ENV dispone de una slot para poder poner una microSD. La librería SD nos permitirá gestionar la escritura en un archivo de texto plano de toda la información que vayamos leyendo de los sensores.
Para más información sobre la librería de SD:
https://www.arduino.cc/en/Reference/SD
Circuitos
Habitación 1
En la siguiente imagen podemos ver el circuito que montaremos en la habitación 1 para realizar las mediciones:
El circuito estará formado por los siguientes elementos:
-
- Arduino MKR Zero.
- Arduino MKR CONNECTOR CARRIER.
- Sensor GROVE – Barometer Sensor (BMP280)
- Sensor GROVE -Temperature&Humidity Sensor Pro (DHT22, AM2302)
- 2 pilas Panasonic NCR18650B de Li-Ion recargables ( 3400 mAh, 3.7V).
- Portapilas para pilas 18650 con cable de alimentación.
El sensor Sensor GROVE – Barometer Sensor (BMP280) irá conectado al conector etiquetado en la placa MKR CONNECTOR CARRIER como TWI y el Sensor GROVE -Temperature&Humidity Sensor Pro (DHT22, AM2302) en el conector etiquetado como D0.
Habitación 2
En la siguiente imagen podemos ver el circuito que montaremos en la habitación 2 para realizar las mediciones:
El circuito estará formado por los siguientes elementos:
-
- Arduino MKR WiFi 1010.
- Arduino MKR ENV SHIELD.
- 1 pila Panasonic NCR18650B de Li-Ion recargables ( 3400 mAh, 3.7V).
- Portapilas para pilas 18650 con cable de alimentación.
Programación
Ahora solo faltará cargar en cada placa el sketch correspondiente teniendo en cuenta que debemos poner los valores de fecha y hora correctos para que el RTC (reloj interno) se inicialice con los valores de fecha y hora en los que queramos empezar a adquirir los datos. Recordad que para mantener el tiempo y el RTC en funcionamiento, es necesario mantener la placa alimentada. Por tanto cuando conectemos la batería a los distintos circuitos debemos tener presente que el reloj interno se iniciará con el valor que hayamos especificado en el sketch.
En nuestro caso concreto debemos especificar la fecha y hora en las siguientes constantes dentro de nuestro sketch:
/* Change these values to set the current initial time !!! */ const byte seconds = 0; const byte minutes = 17; const byte hours = 22; /* Change these values to set the current initial date !!!*/ const byte day = 4; const byte month = 5; const byte year = 19;
Con los valores de las constantes anteriores el reloj interno se iniciaría con el día 4/5/2019 a las 22:17:00.
Por último comentar que se grabarán los datos en la tarjeta microSD cada 10 minutos, de esta manera tendremos por cada hora 6 registros con la información ambiental que para nuestro propósito será más que suficiente. Si queréis variar este intervalo bastará con modificar la siguiente constante dentro del código:
const unsigned long postingInterval = 1000 * 60 * 10;
→ 1000 milisegundos = 1 segundo, si lo multiplicamos por 60 nos dará 1 minuto y si lo multiplicamos por 10 tendremos los 10 minutos.
Sketch 1
Cargar el siguiente sketch para el circuito con la MKR Zero + MKR CONNECTOR CARRIER:
#include <SPI.h> #include <SD.h> #include <RTCZero.h> #include <Wire.h> #include "DHT.h" #define DHTPIN 0 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); #include "Seeed_BMP280.h" BMP280 bmp280; float _temperature = 0; float _humidity = 0; float _pressure = 0; unsigned long lastTime = 0; // last time you write to the SD, in milliseconds const unsigned long postingInterval = 1000 * 60 * 10; // delay between updates, in milliseconds /* Change these values to set the current initial time !!! */ const byte seconds = 0; const byte minutes = 0; const byte hours = 22; /* Change these values to set the current initial date !!!*/ const byte day = 15; const byte month = 5; const byte year = 19; const int chipSelect = SDCARD_SS_PIN; File myFile; RTCZero rtc; void setup() { Serial.begin(9600); // Descomentar la siguiente linea solo para ver las trazas con el monitor serie. Si no dejarla comentada //while (!Serial); // initialize RTC rtc.begin(); // Set date and time rtc.setTime(hours, minutes, seconds); rtc.setDate(day, month, year); Serial.println("Initializing DHT sensor..."); dht.begin(); Serial.println("DHT sensor initialized."); Serial.println("Initializing BMP280 sensor..."); if(!bmp280.init()) { Serial.println("initialization failed!"); while (1); } Serial.println("BMP280 sensor initialized."); Serial.print("Initializing SD card..."); if (!SD.begin(chipSelect)) { Serial.println("SD card failed, or not present!"); while (1); } Serial.println("SD card initialized."); // open file Serial.println("Opening file..."); myFile = SD.open("data.txt", FILE_WRITE); // if the file opened okay, write to it: if (myFile) { Serial.println("file opened"); myFile.println("DateTime Temperature(ºC) Humidity(%) Pressure(hPa)"); myFile.println("----------------- --------------- ----------- -------------"); Serial.println("DateTime Temperature(ºC) Humidity(%) Pressure(hPa)"); Serial.println("----------------- --------------- ----------- -------------"); // close the file: myFile.close(); } } void loop() { // if 10 minutes have passed since your last connection, // then connect again and send data: if (millis() - lastTime > postingInterval) { readSensors(); saveInfoToSD(); lastTime = millis(); } } // Read sensors value: Temperature, Humidity, Atmospheric Pressure void readSensors() { // DHT Sensor _temperature = dht.readTemperature(); _humidity = dht.readHumidity(); // BMP280 Sensor _pressure = bmp280.getPressure(); // Pa // 1 bar = 100.000 Pa = 1000 hPa = 100 kPa _pressure = (_pressure/100); // hPa } // Write info to SD card void saveInfoToSD() { // open file myFile = SD.open("data.txt", FILE_WRITE); // if the file opened ok, write to it: if (myFile) { printDateTime(); myFile.print(_temperature); Serial.print(_temperature); myFile.print(" "); Serial.print(" "); myFile.print(_humidity); Serial.print(_humidity); myFile.print(" "); Serial.print(" "); myFile.print(_pressure); Serial.print(_pressure); myFile.println(""); Serial.println(""); // close the file: myFile.close(); } } void printDateTime() { // Print date... print2digits(rtc.getDay()); myFile.print("/"); Serial.print("/"); print2digits(rtc.getMonth()); myFile.print("/"); Serial.print("/"); print2digits(rtc.getYear()); myFile.print(" "); Serial.print(" "); // ...and time print2digits(rtc.getHours()); myFile.print(":"); Serial.print(":"); print2digits(rtc.getMinutes()); myFile.print(":"); Serial.print(":"); print2digits(rtc.getSeconds()); myFile.print(" "); Serial.print(" "); } void print2digits(int number) { // print a 0 before if the number is < than 10 if (number < 10) { myFile.print("0"); Serial.print("0"); } myFile.print(number); Serial.print(number); }
Sketch 2
Cargar el siguiente sketch para el circuito con la MKR WiFi 1010 + MKR ENV Shield:
#include <SPI.h> #include <SD.h> #include <Arduino_MKRENV.h> #include <RTCZero.h> float _temperature = 0; float _humidity = 0; float _pressure = 0; unsigned long lastTime = 0; // last time you write to the SD, in milliseconds const unsigned long postingInterval = 1000 * 60 * 10; // delay between updates, in milliseconds /* Change these values to set the current initial time !!! */ const byte seconds = 0; const byte minutes = 0; const byte hours = 22; /* Change these values to set the current initial date !!!*/ const byte day = 15; const byte month = 5; const byte year = 19; const int chipSelect = 4; File myFile; RTCZero rtc; void setup() { Serial.begin(9600); // Descomentar la siguiente linea solo para ver las trazas con el monitor serie. Si no dejarla comentada //while (!Serial); // initialize RTC rtc.begin(); // Set date and time rtc.setTime(hours, minutes, seconds); rtc.setDate(day, month, year); Serial.print("Initializing MKR ENV shield..."); if (!ENV.begin()) { Serial.println("Failed to initialize MKR ENV shield!"); while (1); } Serial.println("MKR ENV shield initialized."); Serial.print("Initializing SD card..."); if (!SD.begin(chipSelect)) { Serial.println("SD card failed, or not present!"); while (1); } Serial.println("SD card initialized."); // open file Serial.println("Opening file..."); myFile = SD.open("data.txt", FILE_WRITE); // if the file opened okay, write to it: if (myFile) { Serial.println("file opened"); myFile.println("DateTime Temperature(ºC) Humidity(%) Pressure(hPa)"); myFile.println("----------------- --------------- ----------- -------------"); Serial.println("DateTime Temperature(ºC) Humidity(%) Pressure(hPa)"); Serial.println("----------------- --------------- ----------- -------------"); // close the file: myFile.close(); } } void loop() { // if 10 minutes have passed since your last connection, // then connect again and send data: if (millis() - lastTime > postingInterval) { readSensors(); saveInfoToSD(); lastTime = millis(); } } // Read sensors value: Temperature, Humidity, Atmospheric Pressure void readSensors() { _temperature = ENV.readTemperature(); _humidity = ENV.readHumidity(); _pressure = ENV.readPressure(); // kPa // 1 bar = 100.000 Pa = 1000 hPa = 100 kPa _pressure = (_pressure * 10); // hPa } // Write info to SD card void saveInfoToSD() { // open file myFile = SD.open("data.txt", FILE_WRITE); // if the file opened ok, write to it: if (myFile) { printDateTime(); myFile.print(_temperature); Serial.print(_temperature); myFile.print(" "); Serial.print(" "); myFile.print(_humidity); Serial.print(_humidity); myFile.print(" "); Serial.print(" "); myFile.print(_pressure); Serial.print(_pressure); myFile.println(""); Serial.println(""); // close the file: myFile.close(); } } void printDateTime() { // Print date... print2digits(rtc.getDay()); myFile.print("/"); Serial.print("/"); print2digits(rtc.getMonth()); myFile.print("/"); Serial.print("/"); print2digits(rtc.getYear()); myFile.print(" "); Serial.print(" "); // ...and time print2digits(rtc.getHours()); myFile.print(":"); Serial.print(":"); print2digits(rtc.getMinutes()); myFile.print(":"); Serial.print(":"); print2digits(rtc.getSeconds()); myFile.print(" "); Serial.print(" "); } void print2digits(int number) { // print a 0 before if the number is < than 10 if (number < 10) { myFile.print("0"); Serial.print("0"); } myFile.print(number); Serial.print(number); }
Resultados
Como se ve en los sketchs anteriores la información se ha grabado en un archivo de texto plano que hemos llamado: DATA.txt cuya muestra de datos recogidos se muestra a continuación:
HABITACIÓN 1 ( HABITACIÓN HIJO)
DateTime Temperature(ºC) Humidity(%) Pressure(hPa) ----------------- --------------- ----------- ------------- 15/05/19 22:09:59 19.96 68.57 759.48 15/05/19 22:19:59 20.58 65.14 1007.08 15/05/19 22:29:59 20.77 65.20 1007.10 15/05/19 22:39:59 20.77 64.39 1007.20 15/05/19 22:49:59 20.75 63.89 1007.38 15/05/19 22:59:59 20.80 63.89 1007.38 15/05/19 23:09:59 20.82 63.58 1007.42 15/05/19 23:19:59 20.84 63.55 1007.44 15/05/19 23:29:59 20.78 63.66 1007.46 15/05/19 23:39:59 20.82 63.58 1007.39 15/05/19 23:49:59 20.82 63.25 1007.43 15/05/19 23:59:59 20.82 63.25 1007.31 16/05/19 00:09:59 20.80 63.02 1007.23 16/05/19 00:19:59 20.82 63.06 1007.24 16/05/19 00:29:59 20.78 62.95 1007.10 16/05/19 00:39:59 20.69 62.74 1007.13 16/05/19 00:49:59 20.75 62.61 1007.13 16/05/19 00:59:59 20.75 62.61 1007.31 16/05/19 01:09:59 20.71 62.62 1007.27 16/05/19 01:19:59 20.69 62.47 1007.18 16/05/19 01:29:59 20.71 62.42 1007.18 16/05/19 01:39:59 20.69 62.30 1007.09 16/05/19 01:49:59 20.73 62.26 1006.97 16/05/19 01:59:59 20.66 62.31 1006.83 16/05/19 02:09:59 20.66 62.13 1006.62 16/05/19 02:19:59 20.69 62.05 1006.43 16/05/19 02:29:59 20.69 62.14 1006.24 16/05/19 02:39:59 20.69 61.99 1006.25 16/05/19 02:49:59 20.66 61.89 1006.23 16/05/19 02:59:59 20.62 61.99 1006.19 16/05/19 03:09:59 20.64 61.70 1006.18 16/05/19 03:19:59 20.62 61.48 1006.07 16/05/19 03:29:59 20.58 61.32 1006.01 16/05/19 03:39:59 20.62 61.30 1005.87 16/05/19 03:49:59 20.49 61.15 1005.75 16/05/19 03:59:59 20.55 61.19 1005.72 16/05/19 04:09:59 20.56 61.06 1005.71 16/05/19 04:19:59 20.49 60.96 1005.76 16/05/19 04:29:59 20.53 60.73 1005.73 16/05/19 04:39:59 20.49 60.68 1005.63 16/05/19 04:49:59 20.53 60.67 1005.55 16/05/19 04:59:59 20.47 60.53 1005.43 16/05/19 05:09:59 20.51 60.34 1005.43 16/05/19 05:19:59 20.45 60.24 1005.41 16/05/19 05:29:59 20.44 60.22 1005.22 16/05/19 05:39:59 20.44 60.07 1005.11 16/05/19 05:49:59 20.36 60.50 1005.05 16/05/19 05:59:59 20.38 60.26 1004.96 16/05/19 06:09:59 20.38 60.07 1004.91 16/05/19 06:19:59 20.36 60.06 1004.98 16/05/19 06:29:59 20.34 60.10 1004.91 16/05/19 06:39:59 20.33 60.02 1004.93 16/05/19 06:49:59 20.33 60.22 1004.95 16/05/19 06:59:59 20.33 60.01 1004.97
HABITACIÓN 2 ( HABITACIÓN MATRIMONIO)
DateTime Temperature(ºC) Humidity(%) Pressure(hPa) ----------------- --------------- ----------- ------------- 15/05/19 22:10:00 22.20 59.70 1005.80 15/05/19 22:20:01 22.10 60.00 1005.78 15/05/19 22:30:01 22.00 59.50 1005.94 15/05/19 22:40:02 22.10 59.30 1006.06 15/05/19 22:50:03 22.00 59.10 1006.05 15/05/19 23:00:03 22.00 59.30 1006.08 15/05/19 23:10:04 22.00 59.00 1006.14 15/05/19 23:20:05 22.10 59.60 1006.17 15/05/19 23:30:05 22.10 59.20 1006.12 15/05/19 23:40:06 22.10 59.10 1006.09 15/05/19 23:50:07 22.10 59.60 1006.02 16/05/19 00:00:07 22.00 59.70 1005.95 16/05/19 00:10:08 22.00 59.90 1005.93 16/05/19 00:20:09 22.00 59.70 1005.79 16/05/19 00:30:09 22.00 59.80 1005.84 16/05/19 00:40:10 22.00 60.00 1005.83 16/05/19 00:50:11 22.00 59.80 1005.97 16/05/19 01:00:11 22.00 60.00 1005.96 16/05/19 01:10:12 22.00 60.00 1005.91 16/05/19 01:20:13 22.00 60.10 1005.89 16/05/19 01:30:13 22.00 60.10 1005.77 16/05/19 01:40:14 21.90 60.20 1005.66 16/05/19 01:50:15 21.90 60.20 1005.50 16/05/19 02:00:15 22.00 60.00 1005.30 16/05/19 02:10:16 21.90 60.00 1005.13 16/05/19 02:20:17 21.90 60.00 1004.98 16/05/19 02:30:17 21.90 60.00 1004.98 16/05/19 02:40:18 21.90 60.30 1004.92 16/05/19 02:50:19 21.90 60.00 1004.93 16/05/19 03:00:19 21.90 60.00 1004.92 16/05/19 03:10:20 21.90 60.00 1004.79 16/05/19 03:20:21 21.90 60.00 1004.68 16/05/19 03:30:21 21.90 59.80 1004.56 16/05/19 03:40:22 21.80 59.80 1004.42 16/05/19 03:50:23 21.80 59.50 1004.42 16/05/19 04:00:23 21.80 59.40 1004.43 16/05/19 04:10:24 21.80 59.40 1004.42 16/05/19 04:20:25 21.80 59.30 1004.42 16/05/19 04:30:25 21.80 59.20 1004.31 16/05/19 04:40:26 21.80 59.10 1004.20 16/05/19 04:50:27 21.80 59.10 1004.10 16/05/19 05:00:27 21.80 58.80 1004.12 16/05/19 05:10:28 21.80 58.80 1004.05 16/05/19 05:20:29 21.80 58.70 1003.94 16/05/19 05:30:29 21.70 58.90 1003.84 16/05/19 05:40:30 21.80 58.90 1003.77 16/05/19 05:50:31 21.70 58.50 1003.62 16/05/19 06:00:31 21.70 58.40 1003.61 16/05/19 06:10:32 21.70 58.20 1003.68 16/05/19 06:20:33 21.60 57.90 1003.62 16/05/19 06:30:33 21.70 58.00 1003.65 16/05/19 06:40:34 21.70 57.80 1003.65 16/05/19 06:50:35 21.70 57.70 1003.71