Hello there
I wanted to create a little weather station lately, it works pretty fine for the moment except one thing.
The setup is Arduino UNO, Ethernet Shield with SD slot, SD card, Barometer, Humiture Sensor, and Photosensor (wires and breadboard :))), I wanted to take readings every minute.
The problem is that Arduino only writes first 150-180 entries to the card and stops for some reason.
I cannot find the reson, so I ask your help, my friends
Here is the code:
//for humiture sensor
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
//for barometer
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
//for SD card
#include <SD.h>
#define DHTPINÂ Â Â Â Â Â 2Â Â Â Â // Pin which is connected to the DHT sensor (humiture).
#define BMP_SCK 13Â Â Â Â Â Â Â Â Â //barometer
#define BMP_MISO 12Â Â Â Â Â Â Â Â //barometer
#define BMP_MOSI 11Â Â Â Â Â Â Â Â //barometer
#define BMP_CS 10Â Â Â Â Â Â Â Â Â //barometer
File myFile;Â Â Â Â Â Â Â Â Â Â Â Â //SD card
const int photocellPin = A0;Â Â Â Â //photosensor
int outputValue = 0;Â Â Â Â Â Â Â Â //photosensor
//for humiture sensor
#define DHTTYPEÂ Â Â Â Â DHT11Â Â // DHT 11
//for barometer
Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setup() {
 Serial.begin(9600);
 // Initialize HUMITURE device.
 dht.begin();
 sensor_t sensor;
 dht.temperature().getSensor(&sensor);
 dht.humidity().getSensor(&sensor);
 // Set delay between sensor readings based on sensor details.
 //delayMS = sensor.min_delay / 1000;
 delayMS = 60000; //in miliseconds
 //initialize BAROMETER
 if (!bme.begin()) {
  Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  while (1);
 }
 //SD SETUP
 // Open serial communications and wait for port to open:
 while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.print("Initializing SD card...");
 if (!SD.begin(4)) {
  Serial.println("initialization failed!");
  while (1);
 }
 Serial.println("initialization done.");
}
void loop() {
 Serial.print("Humiture sensor values\n");
 Serial.print("************************************\n");
 // Delay between measurements.
 //delay(delayMS);
 // Get temperature event and print its value.
 sensors_event_t event;
 dht.temperature().getEvent(&event);
 if (isnan(event.temperature)) {
  Serial.println("Error reading temperature!");
 }
 else {
  Serial.print("Temperature: ");
  Serial.print(event.temperature - 1);
  Serial.println(" *C");
 }
 // Get humidity event and print its value.
 dht.humidity().getEvent(&event);
 if (isnan(event.relative_humidity)) {
  Serial.println("Error reading humidity!");
 }
 else {
  Serial.print("Humidity: ");
  Serial.print(event.relative_humidity + 20);
  Serial.println("%");
 }
 // BAROMETER
 Serial.print("Barometer values\n");
 Serial.print("************************************\n");
 Serial.print("Temperature = ");
 Serial.print(bme.readTemperature() - 4.22);
 Serial.println(" *C");
 Serial.print("Pressure = ");
 Serial.print(bme.readPressure()+4339.15);
 Serial.println(" Pa");
 Serial.print("Approx altitude = ");
 Serial.print(bme.readAltitude(1018.00)); // this should be adjusted to your local forcast
 Serial.println(" m");
 Serial.println();
 //delay(delayMS);
 //PHOTOSENSOR
 Serial.print("Photosensor values\n");
 Serial.print("************************************\n");
 outputValue = analogRead(photocellPin);
 Serial.print("Brightness = ");
 Serial.println(outputValue);
 Serial.print("_______________________________________\n");
 //SD card
 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 myFile = SD.open("test.txt", FILE_WRITE);
 // if the file opened okay, write to it:
 if (myFile) {
  Serial.print("Writing to test.txt...");
  myFile.print("H_temp = ");
  myFile.print(event.temperature - 1);
  myFile.print(" ");
  myFile.print("H_humi = ");
  myFile.print(event.relative_humidity +22);
  myFile.print(" ");
  myFile.print("B_temp = ");
  myFile.print(bme.readTemperature() - 3);
  myFile.print(" ");
  myFile.print("B_pres = ");
  myFile.print(bme.readPressure() + 4339.15);
  myFile.print(" ");
  myFile.print("B_elev= ");
  myFile.print(bme.readAltitude(1018.00));
  myFile.print(" ");
  myFile.print("P_valu= ");
  myFile.print(outputValue);
  myFile.println(" ");
 Â
  // close the file:
  myFile.close();
  Serial.println("done.");
 } else {
  // if the file didn't open, print an error:
  Serial.println("error opening test.txt");
 }
 delay(delayMS);
}