Go Down

Topic: BME280 values won't show up in logfile (Read 342 times) previous topic - next topic

phytopia

Hi,

I included a BME280 sensor into a sketch with several other sensors and a data logger which worked fine. Now, after including the BME280 I get the same measurements in the logfile, but without the data of the BME280, no error messages. The BME280 (marked with //////////D in the sketch) is connected via I2C together with a BH1750 sensor (marked with //////////C in the sketch), so are both connected at GND, 5V, SDA and SCL on the arduino uno.

So, how do I make the data logger include the values of the BME280?

Code: [Select]
//NJarpa
//Arduino Data Logger with 2 DS18B20 and sample rate control(every 1 min)
//You must have a datalogger shield(RTC and SD card) mounted
// DS18B20 Pinout (Wires)
//Black   = Ground
//Blue    = Signal (Pin 2):  (Normal power mode with 3.3K to 4.7K resistor to +5 or 3.3 )
//Red     = +5 or +3.3 V

#include <Wire.h>                //Libraries we need
#include "RTClib.h"
#include <OneWire.h>           
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>

#include <dht.h> //////////A
dht DHT; //////////A

#include <math.h>  //////////C

#include <Adafruit_Sensor.h> //////////D
#include <Adafruit_BME280.h> //////////D



#define ONE_WIRE_BUS_PIN 2              // The pin that we are using for sensors

#define DHT21A_PIN 3 //////////A
#define DHT21B_PIN 5 //////////B

int BH1750address = 0x23; //setting i2c address //////////C

byte buff[2]; //////////C

Adafruit_BME280 bme; //////////D
unsigned long delayTime; //////////D


//Variables //////////A
float humA;  //Stores humidity value //////////A
float tempA; //Stores temperature value //////////A

//Variables //////////B
float humB;  //Stores humidity value //////////B
float tempB; //Stores temperature value //////////B

OneWire oneWire(ONE_WIRE_BUS_PIN);     // Setup oneWire
DallasTemperature sensors(&oneWire);   //  oneWire to Dallas Temperature.
RTC_DS1307 RTC;                        // define the Real Time Clock object

const int chipSelect = 4;              //CS pin of your data logger shield.Maybe not yours!!
File Logfile;                          //Name of the file

// Assign the addresses of your  temp sensors.
//Every sensor has it own address.You must use 1 wire adress finder.

DeviceAddress Probe01 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //inox2
DeviceAddress Probe02 = { 0x28, 0xAA, 0x21, 0x9E, 0x13, 0x13, 0x02, 0xE7 }; //Inox1
DeviceAddress Probe03 = { 0x28, 0xAA, 0x5E, 0xA8, 0x13, 0x13, 0x02, 0xA4 };
DeviceAddress Probe04 = { 0x28, 0xAA, 0xA9, 0xA5, 0x13, 0x13, 0x02, 0xCE };
//DeviceAddress Probe05 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //uncomment for further DS18B20 sensor

int BH1750_Read(int address) { //////////C
        int i=0; //////////C
        Wire.beginTransmission(address); //////////C
        Wire.requestFrom(address, 2); //////////C
        while(Wire.available()) { //////////C
                buff[i] = Wire.read();  // receive one byte //////////C
                i++; //////////C
        } //////////C
        Wire.endTransmission();  //////////C
        return i; //////////C
} //////////C

void BH1750_Init(int address) { //////////C
        Wire.beginTransmission(address); //////////C
        Wire.write(0x10);//1lx reolution 120ms //////////C
        Wire.endTransmission(); //////////C
} //////////C


void setup()
{
  //pinMode(10 , OUTPUT);           //For some data logger shields.Uncomment if you need
 
SD.begin(chipSelect);            //Initialize the libraries
Wire.begin();
RTC.begin();
sensors.begin();   
 
  // set the resolution to 9 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 9);
  sensors.setResolution(Probe02, 9);
  sensors.setResolution(Probe03, 9);
  sensors.setResolution(Probe04, 9);
  //sensors.setResolution(Probe05, 9); //uncomment for further DS18B20 sensor
 
Logfile=SD.open("Logfile.csv",FILE_WRITE);               //Open and write once, just for headers
//Logfile.println(" Date/Time         Temp1   Temp2   Temp3    Temp4    Temp5   Hum5");    //Print headers(not saved yet) //////////
Logfile.close();                                        //Print saved

bool status; //////////D
status = bme.begin(0x76); //////////D
delayTime = 1000; //////////D

}

//----------Temp Variable------------//
void printTemperature(DeviceAddress deviceAddress)
{
   float tempC = sensors.getTempC(deviceAddress);
 
   Logfile.print(tempC);
   Logfile.print(" C");
   Logfile.print(" ");
}

void loop()
{

 DateTime now = RTC.now();        // Clock call
 now = RTC.now();                 

if(now.second()==00){          //Sample every minute
 
 Logfile=SD.open("Logfile.csv",FILE_WRITE);  // Print date and time   

Logfile.println();
Logfile.print(now.year(), DEC);
Logfile.print("/");
Logfile.print(now.month(), DEC);
Logfile.print("/");
Logfile.print(now.day(), DEC);
Logfile.print(" ");
Logfile.print(now.hour(), DEC);
Logfile.print(":");
Logfile.print(now.minute(), DEC);
Logfile.print(":");
Logfile.print(now.second(), DEC);
Logfile.print("  ");              //Space beween date/time and temp1
Logfile.close();                  //Save date and time

sensors.requestTemperatures(); // Command all devices on bus to read temperature
Logfile=SD.open("Logfile.csv",FILE_WRITE);
 
 Logfile.print("Temp1: ");             //Print temp1
 printTemperature(Probe01);
 Logfile.print("Temp2: ");             //Print temp2
 printTemperature(Probe02);
 Logfile.print("Temp3: ");             //Print temp3
 printTemperature(Probe03);
 Logfile.print("Temp4: ");             //Print temp4
 printTemperature(Probe04);
 //Logfile.print("Temp5:");             //uncomment for further DS18B20 sensor //Print temp5
 //printTemperature(Probe05);

  int chkA = DHT.read21(DHT21A_PIN); //////////A
    //Read data and store it to variables hum and temp //////////A
    humA = DHT.humidity; //////////A
    tempA= DHT.temperature; //////////A
    //Print temp and humidity values to serial monitor //////////A
    Logfile.print("Hum6: "); //////////A
    Logfile.print(humA); //////////A
    Logfile.print(" %, Temp6: "); //////////A
    Logfile.print(tempA); //////////A
    Logfile.print(" C"); //////////A

    int chkB = DHT.read21(DHT21B_PIN); //////////B
    //Read data and store it to variables hum and temp //////////B
    humB = DHT.humidity; //////////B
    tempB= DHT.temperature; //////////B
    //Print temp and humidity values to serial monitor //////////B
    Logfile.print("Hum7: "); //////////B
    Logfile.print(humB); //////////B
    Logfile.print(" %, Temp7: "); //////////B
    Logfile.print(tempB); //////////B
    Logfile.print(" C"); //////////B

int i;  //////////C
        uint16_t val=0; //////////C
        BH1750_Init(BH1750address); //////////C
        delay(200);//////////C

        if(2==BH1750_Read(BH1750address)) { //////////C
                val=((buff[0]<<8)|buff[1])/1.2; //////////C
                Logfile.print("Light: "); ////////C
                Logfile.print(val,DEC);  //////////C
                Logfile.println("lx"); //////////C
        } //////////C
        delay(150); //////////C

    Logfile.print("Temperature = ");//////////D
    Logfile.print(bme.readTemperature());//////////D
    Logfile.println(" *C");//////////D
    Logfile.print("Pressure = ");//////////D
    Logfile.print(bme.readPressure() / 100.0F);//////////D
    Logfile.println(" hPa");//////////D
    Logfile.print("Humidity = ");//////////D
    Logfile.print(bme.readHumidity());//////////D
    Logfile.println(" %");//////////D
   
 Logfile.close();                //Print saved
 }
 
delay(1000);                        //One data per second
}

pylon

Are the BME280 values just all 0 or is the whole section (Temperature = ??, Pressure = ??, Humidity = ??) not in the log file?

Do you use an original BME280 sensor from Adafruit or any other breakout board? The I2C address is hardware selectable and the Adafruit library default to their selection (which is legitimate in my opinion).

phytopia

Are the BME280 values just all 0 or is the whole section (Temperature = ??, Pressure = ??, Humidity = ??) not in the log file?
The whole section is missing.

Do you use an original BME280 sensor from Adafruit or any other breakout board? The I2C address is hardware selectable and the Adafruit library default to their selection (which is legitimate in my opinion).
It's this one: https://de.aliexpress.com/item/BME280-Digital-Sensor-Temperature-Humidity-Barometric-Pressure-Sensor-New/32659765502.html

And it worked with the sketch below already, so I just copy pasted it into the big sketch I posted in the first post and adjusted it a little bit.


Code: [Select]
Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    Serial.println(F("BME280 test"));

    bool status;
    
    // default settings
    // (you can also pass in a Wire library object like &Wire2)
    status = bme.begin(0x76);  
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();
}


void loop() {
    printValues();
    delay(delayTime);
}


void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

pylon

Quote
The whole section is missing.
In this case the sketch wasn't installed on the Arduino, you're still running an outdated version of it. Upload again and check for upload errors. Clear the SD card to be sure you don't get old data.

phytopia

In this case the sketch wasn't installed on the Arduino, you're still running an outdated version of it. Upload again and check for upload errors. Clear the SD card to be sure you don't get old data.
I uploaded it again, still get the same result, all values from the BME280 are not shown. This is what gets shown in the logfile:

2018/10/23 15:34:0  Temp1: -127.00 C Temp2: 18.00 C Temp3: 18.50 C Temp4: 18.00 C Hum6: 81.80 %, Temp6: 18.50 CHum7: 81.80 %, Temp7: 18.50 CLight: 14lx

2018/10/23 15:35:0  Temp1: -127.00 C Temp2: 18.50 C Temp3: 18.50 C Temp4: 18.00 C Hum6: 81.40 %, Temp6: 18.60 CHum7: 81.40 %, Temp7: 18.60 CLight: 13lx

2018/10/23 15:36:0  Temp1: -127.00 C Temp2: 18.50 C Temp3: 18.50 C Temp4: 18.00 C Hum6: 80.80 %, Temp6: 18.80 CHum7: 80.80 %, Temp7: 18.80 CLight: 14lx

phytopia


wvmarle

As test: replace all the logfile.print() statements by Serial.print() statements to see what you really try to print to your log file. Make sure that matches your expectations. Makes debugging a lot easier.

The easiest way to do this is not by editing all those lines in your code but by adding a #define statement and uncommenting some other lines...

Code: [Select]

#define Logfile Serial


It also tells you whether you're running the latest version of your sketch which, based on earlier comments, is also anything but certain.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

phytopia

#7
Oct 30, 2018, 09:57 am Last Edit: Oct 30, 2018, 01:00 pm by phytopia
As test: replace all the logfile.print() statements by Serial.print() statements to see what you really try to print to your log file. Make sure that matches your expectations. Makes debugging a lot easier.

The easiest way to do this is not by editing all those lines in your code but by adding a #define statement and uncommenting some other lines...

Code: [Select]

#define Logfile Serial


It also tells you whether you're running the latest version of your sketch which, based on earlier comments, is also anything but certain.
I just did that. I don't get any errors, but the serial monitor shows nothing at all. Why is that?

Here is the modified code to display the values on the serial monitor instead of the logfile:

Code: [Select]
//NJarpa
//Arduino Data Logger with 2 DS18B20 and sample rate control(every 1 min)
//You must have a datalogger shield(RTC and SD card) mounted
// DS18B20 Pinout (Wires)
//Black   = Ground
//Blue    = Signal (Pin 2):  (Normal power mode with 3.3K to 4.7K resistor to +5 or 3.3 )
//Red     = +5 or +3.3 V

#include <Wire.h>                //Libraries we need
#include "RTClib.h"
#include <OneWire.h>          
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>

#include <dht.h> //////////A
dht DHT; //////////A

#include <math.h>  //////////C

#include <Adafruit_Sensor.h> //////////D
#include <Adafruit_BME280.h> //////////D

#define Logfile Serial

#define ONE_WIRE_BUS_PIN 2              // The pin that we are using for sensors

#define DHT21A_PIN 3 //////////A
#define DHT21B_PIN 5 //////////B

int BH1750address = 0x23; //setting i2c address //////////C

byte buff[2]; //////////C

Adafruit_BME280 bme; //////////D
unsigned long delayTime; //////////D


//Variables //////////A
float humA;  //Stores humidity value //////////A
float tempA; //Stores temperature value //////////A

//Variables //////////B
float humB;  //Stores humidity value //////////B
float tempB; //Stores temperature value //////////B

OneWire oneWire(ONE_WIRE_BUS_PIN);     // Setup oneWire
DallasTemperature sensors(&oneWire);   //  oneWire to Dallas Temperature.
RTC_DS1307 RTC;                        // define the Real Time Clock object

const int chipSelect = 4;              //CS pin of your data logger shield.Maybe not yours!!
///File Logfile;                          //Name of the file

// Assign the addresses of your  temp sensors.
//Every sensor has it own address.You must use 1 wire adress finder.

DeviceAddress Probe01 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //inox2
DeviceAddress Probe02 = { 0x28, 0xAA, 0x21, 0x9E, 0x13, 0x13, 0x02, 0xE7 }; //Inox1
DeviceAddress Probe03 = { 0x28, 0xAA, 0x5E, 0xA8, 0x13, 0x13, 0x02, 0xA4 };
DeviceAddress Probe04 = { 0x28, 0xAA, 0xA9, 0xA5, 0x13, 0x13, 0x02, 0xCE };
//DeviceAddress Probe05 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //uncomment for further DS18B20 sensor

int BH1750_Read(int address) { //////////C
        int i=0; //////////C
        Wire.beginTransmission(address); //////////C
        Wire.requestFrom(address, 2); //////////C
        while(Wire.available()) { //////////C
                buff[i] = Wire.read();  // receive one byte //////////C
                i++; //////////C
        } //////////C
        Wire.endTransmission();  //////////C
        return i; //////////C
} //////////C

void BH1750_Init(int address) { //////////C
        Wire.beginTransmission(address); //////////C
        Wire.write(0x10);//1lx reolution 120ms //////////C
        Wire.endTransmission(); //////////C
} //////////C


void setup()
{
Serial.begin(9600); /////serial monitor
  //pinMode(10 , OUTPUT);           //For some data logger shields.Uncomment if you need
  
SD.begin(chipSelect);            //Initialize the libraries
Wire.begin();
RTC.begin();
sensors.begin();    
  
  // set the resolution to 9 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 9);
  sensors.setResolution(Probe02, 9);
  sensors.setResolution(Probe03, 9);
  sensors.setResolution(Probe04, 9);
  //sensors.setResolution(Probe05, 9); //uncomment for further DS18B20 sensor
  
///Logfile=SD.open("Logfile.csv",FILE_WRITE);               //Open and write once, just for headers
//Logfile.println(" Date/Time         Temp1   Temp2   Temp3    Temp4    Temp5   Hum5");    //Print headers(not saved yet) //////////
///Logfile.close();                                        //Print saved

bool status; //////////D
status = bme.begin(0x76); //////////D
delayTime = 1000; //////////D

}

//----------Temp Variable------------//
void printTemperature(DeviceAddress deviceAddress)
{
   float tempC = sensors.getTempC(deviceAddress);
 
   Logfile.print(tempC);
   Logfile.print(" C");
   Logfile.print(" ");
}

void loop()
{

 DateTime now = RTC.now();        // Clock call
 now = RTC.now();                

if(now.second()==00){          //Sample every minute
 
 ///Logfile=SD.open("Logfile.csv",FILE_WRITE);  // Print date and time    
Logfile.println();
Logfile.print(now.year(), DEC);
Logfile.print("/");
Logfile.print(now.month(), DEC);
Logfile.print("/");
Logfile.print(now.day(), DEC);
Logfile.print(" ");
Logfile.print(now.hour(), DEC);
Logfile.print(":");
Logfile.print(now.minute(), DEC);
Logfile.print(":");
Logfile.print(now.second(), DEC);
Logfile.print("  ");              //Space beween date/time and temp1
///Logfile.close();                  //Save date and time

sensors.requestTemperatures(); // Command all devices on bus to read temperature
///Logfile=SD.open("Logfile.csv",FILE_WRITE);
  
 Logfile.print("Temp1: ");             //Print temp1
 printTemperature(Probe01);
 Logfile.print("Temp2: ");             //Print temp2
 printTemperature(Probe02);
 Logfile.print("Temp3: ");             //Print temp3
 printTemperature(Probe03);
 Logfile.print("Temp4: ");             //Print temp4
 printTemperature(Probe04);
 //Logfile.print("Temp5:");             //uncomment for further DS18B20 sensor //Print temp5
 //printTemperature(Probe05);

  int chkA = DHT.read21(DHT21A_PIN); //////////A
    //Read data and store it to variables hum and temp //////////A
    humA = DHT.humidity; //////////A
    tempA= DHT.temperature; //////////A
    //Print temp and humidity values to serial monitor //////////A
    Logfile.print("Hum6: "); //////////A
    Logfile.print(humA); //////////A
    Logfile.print(" %, Temp6: "); //////////A
    Logfile.print(tempA); //////////A
    Logfile.print(" C"); //////////A

    int chkB = DHT.read21(DHT21B_PIN); //////////B
    //Read data and store it to variables hum and temp //////////B
    humB = DHT.humidity; //////////B
    tempB= DHT.temperature; //////////B
    //Print temp and humidity values to serial monitor //////////B
    Logfile.print("Hum7: "); //////////B
    Logfile.print(humB); //////////B
    Logfile.print(" %, Temp7: "); //////////B
    Logfile.print(tempB); //////////B
    Logfile.print(" C"); //////////B

int i;  //////////C
        uint16_t val=0; //////////C
        BH1750_Init(BH1750address); //////////C
        delay(200);//////////C

        if(2==BH1750_Read(BH1750address)) { //////////C
                val=((buff[0]<<8)|buff[1])/1.2; //////////C
                Logfile.print("Light: "); ////////C
                Logfile.print(val,DEC);  //////////C
                Logfile.println("lx"); //////////C
        } //////////C
        delay(150); //////////C

    Logfile.print("Temperature = ");//////////D
    Logfile.print(bme.readTemperature());//////////D
    Logfile.println(" *C");//////////D
    Logfile.print("Pressure = ");//////////D
    Logfile.print(bme.readPressure() / 100.0F);//////////D
    Logfile.println(" hPa");//////////D
    Logfile.print("Humidity = ");//////////D
    Logfile.print(bme.readHumidity());//////////D
    Logfile.println(" %");//////////D
    
/// Logfile.close();                //Print saved
 }
 
delay(1000);                        //One data per second
}

wvmarle

Please post your code using code [code][/code] tags (use "copy for forum" in the IDE to make this even easier) after formatting it properly (CTRL-T in the IDE), or it gets mangled.

To make sure you're actually in those functions add a Serial.print() statement at the start. The macro replacement of Logfile to Serial should do the job - that may not work, or you simply never end up in the functions.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

phytopia

To make sure you're actually in those functions add a Serial.print() statement at the start. The macro replacement of Logfile to Serial should do the job - that may not work, or you simply never end up in the functions.
I just changed all of the Logfile.print() to Serial.print() one by one, but it still doesn't show anything in the serial monitor.

wvmarle

Then it's time to dig deeper.
Add more print statements to see what your code is doing.
What is the actual value of now.second() that you get? What time does the RTC provide? That kind of things. Make sure ALL those variables contain what you expect they contain.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Danois90

Please change the first 3 lines in "setup()" to:

Code: [Select]
void setup()
{
  Serial.begin(9600);
  while (!Serial) ;
  Serial.println(F("Sketch V1.0"));

  //Rest of your setup code here

}


If the serial monitor does not print "Sketch V1.0" as the first line, there is a problem which is unrelated to any sensors. Make sure that you have the correct baud rate selected in the serial monitor.
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

phytopia

Thanks for your help! I did everything from start again and now it works. I don't know what was wrong. Just in case, here is the working sketch:

Code: [Select]
//NJarpa
//Arduino Data Logger with 2 DS18B20 and sample rate control(every 1 min)
//You must have a datalogger shield(RTC and SD card) mounted
// DS18B20 Pinout (Wires)
//Black   = Ground
//Blue    = Signal (Pin 2):  (Normal power mode with 3.3K to 4.7K resistor to +5 or 3.3 )
//Red     = +5 or +3.3 V

#include <Wire.h>                //Libraries we need
#include "RTClib.h"
#include <OneWire.h>          
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_Sensor.h> ///BME280
#include <Adafruit_BME280.h> ///BME280
Adafruit_BME280 bme; ///BME280
#include <BH1750.h> ///BH1750
BH1750 lightMeter; ///BH1750
#include <dht.h> ///DHT21AA
dht DHT; ///DHT21A

#define ONE_WIRE_BUS_PIN 2              // The pin that we are using for sensors

#define DHT21A_PIN 3 ///DHT21A
float humA;  //Stores humidity value
float tempA; //Stores temperature value ///DHT21A

#define DHT21B_PIN 5 ///DHT21B
float humB;  //Stores humidity value
float tempB; //Stores temperature value ///DHT21B


OneWire oneWire(ONE_WIRE_BUS_PIN);     // Setup oneWire
DallasTemperature sensors(&oneWire);   //  oneWire to Dallas Temperature.
RTC_DS1307 RTC;                        // define the Real Time Clock object

const int chipSelect = 4;              //CS pin of your data logger shield.Maybe not yours!!
File Logfile;                          //Name of the file

// Assign the addresses of your  temp sensors.
//Every sensor has it own address.You must use 1 wire adress finder.

DeviceAddress Probe01 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //inox2
DeviceAddress Probe02 = { 0x28, 0xAA, 0x21, 0x9E, 0x13, 0x13, 0x02, 0xE7 }; //Inox1
DeviceAddress Probe03 = { 0x28, 0xAA, 0x5E, 0xA8, 0x13, 0x13, 0x02, 0xA4 };
DeviceAddress Probe04 = { 0x28, 0xAA, 0xA9, 0xA5, 0x13, 0x13, 0x02, 0xCE };
//DeviceAddress Probe05 = { 0x28, 0x7E, 0xA5, 0x77, 0x91, 0x08, 0x02, 0x13 }; //uncomment for further DS18B20 sensor


void setup()
{
  //pinMode(10 , OUTPUT);           //For some data logger shields.Uncomment if you need
  
SD.begin(chipSelect);            //Initialize the libraries
Wire.begin();
RTC.begin();
sensors.begin();
lightMeter.begin(); ///BH1750
bme.begin(0x76); ///BME280
  
  // set the resolution to 9 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 9);
  sensors.setResolution(Probe02, 9);
  sensors.setResolution(Probe03, 9);
  sensors.setResolution(Probe04, 9);
  //sensors.setResolution(Probe05, 9); //uncomment for further DS18B20 sensor
  
Logfile=SD.open("Logfile.csv",FILE_WRITE);               //Open and write once, just for headers
//Logfile.println(" Date/Time         Temp1   Temp2   Temp3    Temp4    Temp5   Hum5");    //Print headers(not saved yet) //////////
Logfile.close();                                        //Print saved

}

//----------Temp Variable------------//
void printTemperature(DeviceAddress deviceAddress)
{
   float tempC = sensors.getTempC(deviceAddress);
 
   Logfile.print(tempC);
   Logfile.print(" C;");
   Logfile.print(" ");
   Serial.print(tempC);
   Serial.print(" C;");
   Serial.print(" ");
  
}

void loop()
{
Serial.begin(9600); ///SERIAL MONITOR

 DateTime now = RTC.now();        // Clock call
 now = RTC.now();                

if(now.second()==00){          //Sample every minute
 
 Logfile=SD.open("Logfile.csv",FILE_WRITE);  // Print date and time    

Logfile.println();
Logfile.print(now.year(), DEC);
Logfile.print("/");
Logfile.print(now.month(), DEC);
Logfile.print("/");
Logfile.print(now.day(), DEC);
Logfile.print(" ");
Logfile.print(now.hour(), DEC);
Logfile.print(":");
Logfile.print(now.minute(), DEC);
Logfile.print(":");
Logfile.print(now.second(), DEC);
Logfile.print(";  ");              //Space beween date/time and temp1
Logfile.close();                  //Save date and time
Serial.println();
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print(";  ");              //Space beween date/time and temp1


sensors.requestTemperatures(); // Command all devices on bus to read temperature
Logfile=SD.open("Logfile.csv",FILE_WRITE);
  
 Logfile.print(" Temp1: ");             //Print temp1
 Serial.print(" Temp1: ");
 printTemperature(Probe01);
 Logfile.print(" Temp2: ");             //Print temp2
 Serial.print(" Temp2: ");
 printTemperature(Probe02);
 Logfile.print(" Temp3: ");             //Print temp3
 Serial.print(" Temp3: ");
 printTemperature(Probe03);
 Logfile.print(" Temp4: ");             //Print temp4
 Serial.print(" Temp4: ");
 printTemperature(Probe04);
 //Logfile.print(" Temp5: ");             //uncomment for further DS18B20 sensor //Print temp5
 //Serial.print(" Temp5: ");
 //printTemperature(Probe05);

  int chkA = DHT.read21(DHT21A_PIN); ///DHT21A
    //Read data and store it to variables hum and temp
    humA = DHT.humidity;
    tempA= DHT.temperature;
    Logfile.print(" Hum6: ");
    Logfile.print(humA);
    Logfile.print(" %; Temp6: ");
    Logfile.print(tempA);
    Logfile.print(" C;");
    Serial.print(" Hum6: ");
    Serial.print(humA);
    Serial.print(" %; Temp6: ");
    Serial.print(tempA);
    Serial.print(" C;"); ///DHT21A

    int chkB = DHT.read21(DHT21B_PIN); ///DHT21B
    //Read data and store it to variables hum and temp
    humB = DHT.humidity;
    tempB= DHT.temperature;
    Logfile.print(" Hum7: ");
    Logfile.print(humB);
    Logfile.print(" %; Temp7: ");
    Logfile.print(tempB);
    Logfile.print(" C;");
    Serial.print(" Hum7: ");
    Serial.print(humB);
    Serial.print(" %; Temp7: ");
    Serial.print(tempB);
    Serial.print(" C;"); ///DHT21B

    uint16_t lux = lightMeter.readLightLevel(); ///BH1750
  Logfile.print(" Light: ");
  Logfile.print(lux);
  Logfile.print(" lx;");
  Serial.print(" Light: ");
  Serial.print(lux);
  Serial.print(" lx;"); ///BH1750

    Logfile.print("Temp8: "); ///BME280
    Logfile.print(bme.readTemperature());
    Logfile.print(" C;");
    Logfile.print(" Pr: ");
    Logfile.print(bme.readPressure() / 100.0F);
    Logfile.print(" hPa;");
    Logfile.print(" Hum8: ");
    Logfile.print(bme.readHumidity());
    Logfile.println(" %;");
    Serial.print(" Temp8: ");
    Serial.print(bme.readTemperature());
    Serial.print(" C;");
    Serial.print(" Pr: ");
    Serial.print(bme.readPressure() / 100.0F);
    Serial.print(" hPa;");
    Serial.print(" Hum8: ");
    Serial.print(bme.readHumidity());
    Serial.println(" %;"); ///BME280


 
    
 Logfile.close();                //Print saved
 }
 
delay(1000);                        //One data per second
}

Danois90

#13
Oct 31, 2018, 02:29 pm Last Edit: Oct 31, 2018, 02:31 pm by Danois90
I would like you to re-consider this part of your code:

Code: [Select]
void loop()
{
  //Missing code
  if(now.second()==00) {
    //Missing code
  }
  delay(1000);
}


This may become a problem if the call to delay "hits" just before (few millis) RTC seconds turnes to 0 since this could cause a reading to be lost. Instead you should use something like:

Code: [Select]
//Other code

byte lastMinute = 0xFF; //Initialize to an impossible value

//Other code

void loop()
{
  //Missing code
  if(now.minute() != lastMinute) {
    lastMinute = now.minute();
    //Missing code
  }
  delay(1000);
}


Doing it this way, you will never loose a reading. Good luck :)
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

phytopia

#14
Oct 31, 2018, 04:53 pm Last Edit: Oct 31, 2018, 04:55 pm by phytopia
Thanks for your suggestion, but if I put that in my sketch, I get this error:
Code: [Select]

Arduino: 1.8.7 (Windows 10), Board: "Arduino/Genuino Uno"

Sketch_working:101:1: error: stray '\302' in program

 Â  Â  lastMinute = now.minute();

 ^

Sketch_working:101:1: error: stray '\240' in program

Sketch_working:101:1: error: stray '\302' in program

Sketch_working:101:1: error: stray '\240' in program

exit status 1
stray '\302' in program



Where should I put this line: "byte lastMinute = 0xFF; //Initialize to an impossible value"?
I put it in void setup or before that, both produces the same error...

Go Up