Devices & Components
Arduino MKR Connector Carrier (Grove compatible)
Arduino MKR ZERO (I2S bus & SD for sound, music & digital audio data)
Arduino MKR WiFi 1010
Grove - Temperature & Humidity Sensor Pro (DHT22/AM2302)
Software & Tools
Arduino IoT Cloud
Arduino IDE
Project description
Code
Offline solution Sketch
csharp
Use this to datalogg offline onto a 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}
Comments
Only logged in users can leave comments