Hello everyone,
Ive been encountering some issues regarding the mentioned modules...
If i use only 2 of the 3 , it will always work, but once i try to connect the third, any one of the 3 will fail for some reason. im still flabbergasted if this is a coding error or maybe a lack of current error.
ive just gained a little bit of understanding on the SDI and I2C communications. so please bear with me im still a bit of a noob.
i would be really happy if someone could help me or show me why this isnt working... thanks in forward. much love !
PS All the 2 second delays were made to see if the serial interface was gettting in the way of the setup.
Code:
/////////////////////// SD AND SENSOR //////////////////
#include <SPI.h>
#include <SD.h>
#include <BME280Spi.h>
#define SERIAL_BAUD 115200
#define DEVICE_PIN 7
BME280Spi::Settings settings(DEVICE_PIN); // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
BME280Spi bme(settings);
File myFile;
const int chipSelect = 8;
////////////// RTC /////////////////////
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
#define countof(a) (sizeof(a) / sizeof(a[0]))
const char data[] = "Sensor data and Time:";
////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
/////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Serial.begin(SERIAL_BAUD);
while(!Serial) {} // Wait
/////////////// RTC SETUP /////////////////////
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
Serial.println(" trying to set time more accurate ");
Rtc.SetDateTime(compiled);
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
delay(2000);
/////////////////// BME 280 SENSOR//////////////////
SPI.begin();
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
delay(2000);
//////////////////// SD CARD /////////////////////////////////
pinMode(chipSelect,OUTPUT);
// 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.");
}
//////////////////////////////////////////////////////////////////////
void loop() {
// put your main code here, to run repeatedly:
/////////////////////// RTC ////////////////////////////////////////////////////////
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println(" +");
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
// delay(5000);
// read data
uint8_t buff[20];
const uint8_t count = sizeof(buff);
// get our data
uint8_t gotten = Rtc.GetMemory(buff, count);
if (gotten != count)
{
Serial.print("something didn't match, count = ");
Serial.print(count, DEC);
Serial.print(", gotten = ");
Serial.print(gotten, DEC);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.print(") = \"");
// print the string, but terminate if we get a null
for (uint8_t ch = 0; ch < gotten && buff[ch]; ch++)
{
Serial.print((char)buff[ch]);
}
Serial.println("\"");
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// SD CARD ////////////////////////////////
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += "Sensor: ";
dataString += analogPin;
dataString += " = ";
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// 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");
}
//////////////// BME280 Sensor//////////////////////////////////////////////
printBME280Data(&Serial);
delay(500);
//////////////////////////////////////////////////////////////
delay(500);
}
//////////////////////////////////////////////////////////////
void printBME280Data
(
Stream* client
)
{
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit);
client->print("Temp: ");
client->print(temp);
client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
client->print("\t\tHumidity: ");
client->print(hum);
client->print("% RH");
client->print("\t\tPressure: ");
client->print(pres);
client->println(" Pa");
delay(1000);
}
//////////////////////////////////////// RTC FUNCTION ///////////////////
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}