Easy data logging with Grove sensors using the MKR connector bracket and MKR cards
Devices and components
Arduino MKR connector holder (Grove compatible)
Arduino MKR ZERO (I2S and SD bus for sound, music and digital audio data)
Arduino MKR WiFi 1010
Grove - Pro Temperature and Humidity Sensor (DHT22/AM2302)
Software and tools
Arduino IoT Cloud
Arduino IDE
Project description
Introduction
Circuit
IoT solution
Inspecting the IoT Sketch
#include "thingProperties.h"
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
DHT dht(0, DHT22);
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
dht.begin();
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
temperature = dht.readTemperature();
}
DHT dht(0, DHT22);
DHT22
DHT11
dht.begin();
temperature = dht.readTemperature();
IoT result
Offline solution
Offline sketch inspection
#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#include <DHT_U.h>
DHT dht(0, DHT22);
const int chipSelect = SDCARD_SS_PIN;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
dht.begin();
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// Saves the temperature in a variable
float temperature = dht.readTemperature();
dataString = String(temperature);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1000);
}
DHT dht(0, DHT22);
DHT22
DHT11
dht.begin();
SD.begin(chipSelect)
dataString = String(temperature);
File dataFile = SD.open("datalog.txt", FILE_WRITE)
dataFile
datalog.txt
dataFile.print()
datalog.txt
dataFile.close()
Test the sketch offline
while(!Serial)
.txt
1. Turn off power to the MKR Zero board.
2. Remove the micro SD card from the card.
3. Insert the micro SD card into an adapter suitable for your computer.
4. Insert the card into a computer and open the datalog.txt file.
datalog.txt
.txt
.csv
Conclusion
Offline Solution Sketch
csharp
Use it to save offline data to an SD card
1#include <SPI.h>
2#include <SD.h>
3#include <DHT.h>
4#include <DHT_U.h>
5
6DHT dht(0, DHT22);
7
8const int chipSelect = SDCARD_SS_PIN;
9
10void setup() {
11 // Open serial communications and wait for port to open:
12 Serial.begin(9600);
13 while (!Serial) {
14 ; // wait for serial port to connect. Needed for native USB port only
15 }
16
17
18 Serial.print("Initializing SD card...");
19
20 // see if the card is present and can be initialized:
21 if (!SD.begin(chipSelect)) {
22 Serial.println("Card failed, or not present");
23 // don't do anything more:
24 while (1);
25 }
26 Serial.println("card initialized.");
27 dht.begin();
28}
29
30void loop() {
31 // make a string for assembling the data to log:
32 String dataString = "";
33 // Saves the temperature in a variable
34 float temperature = dht.readTemperature();
35
36 dataString = String(temperature);
37
38 // open the file. note that only one file can be open at a time,
39 // so you have to close this one before opening another.
40 File dataFile = SD.open("datalog.txt", FILE_WRITE);
41
42 // if the file is available, write to it:
43 if (dataFile) {
44 dataFile.println(dataString);
45 dataFile.close();
46 // print to the serial port too:
47 Serial.println(dataString);
48 }
49 // if the file isn't open, pop up an error:
50 else {
51 Serial.println("error opening datalog.txt");
52 }
53 delay(1000);
54}
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.