Hello everyone! I am attempting to use an Arduino MKR1010 and ENV shield to capture data on an SD card. The sketch in this post verifies and uploads onto the board however, I do not get a file created on the SD card that stores the sensor data. Also, this sketch makes the board constantly connect and disconnect from the computer. What am I doing wrong?
/*
Sketch generated by the Arduino IoT Cloud Thing "GreenDuino"
https://create.arduino.cc/cloud/things/c9febcef-b462-4371-a588-28531c050e36
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float uvb;
float uvi;
float pressure;
float uva;
float temperature;
float lux;
float humidity;
float soilMoisture;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Arduino_MKRENV.h>
#include <ArduinoOTA.h>
#include <SPI.h>
#include <SD.h>
// variables for the hygrometer
const int AirValue = 570;
const int WaterValue = 0;
const int SD_CS_PIN = 4;
int intervals = (AirValue - WaterValue)/3;
int soilMoistureValue = 0;
// PIN SETUPS
int pinOutRelay = 13;
//file object
File dataFile;
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);
// PIN SETUPS
pinMode(pinOutRelay, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
//
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while(1);
}
//init SPI
SPI.begin();
delay(100);
//init SD card
if(!SD.begin(SD_CS_PIN)) {
Serial.println("Failed to initialize SD card!");
while (1);
}
//init the logfile
File datafile = SD.open("log-0000.csv", FILE_WRITE);
delay(1000);
//init the CSV file with headers
dataFile.println("temperature,humidity,pressure,UVA,UVB,UVindex");
//close the file
datafile.close();
delay(100);
}
//
void loop() {
//init cloud
ArduinoCloud.update();
//init the logfile
dataFile = SD.open("log-0000.csv", FILE_WRITE);
humidity = ENV.readHumidity();
lux = ENV.readIlluminance();
pressure = ENV.readPressure();
temperature = ENV.readTemperature();
uva = ENV.readUVA();
uvb = ENV.readUVB();
uvi = ENV.readUVIndex();
// print each of the sensor values
dataFile.print(temperature);
dataFile.print(",");
dataFile.print(humidity);
dataFile.print(",");
dataFile.print(pressure);
dataFile.print(",");
dataFile.print(uva);
dataFile.print(",");
dataFile.print(uvb);
dataFile.print(",");
dataFile.println(uvi);
//close the file
dataFile.close();
//wait 1 sec and print again
delay(1000);
//delay(1000);
soilMoistureValue = analogRead(A0);
if(soilMoistureValue < 190)
{
Serial.println("Very Wet");
digitalWrite(pinOutRelay, LOW);
}
else if(soilMoistureValue < 380)
{
Serial.println("Wet");
}
else if(soilMoistureValue < 890)
{
Serial.println("Dry");
digitalWrite(pinOutRelay, HIGH);
}
delay(10000);
Serial.println(soilMoistureValue);
soilMoisture = soilMoistureValue;
}
void onHumidityChange() {
// Do something
}
void onLuxChange() {
// Do something
}
void onPressureChange() {
// Do something
}
void onTemperatureChange() {
// Do something
}
void onUvaChange() {
// Do something
}
void onUvbChange() {
// Do something
}
void onUviChange() {
// Do something
}
void onMoistureChange() {
// Do something
}
void onmoistureChange() {
// Do something
}
void onSoilMoistureChange() {
// Do something
}