I am attempting to integrate code for temperature sensors using OneWire 2.3.7 with
PFC8523 RTC using RTClib 2.0.3 on the Adafruit Datalogger Shield. Individual test programs work fine. Adding the RTC code into the datalogger I am writing caused the program to hang when I call rtc.start().
RTClib says that it uses Wire.lib
from the timestamp example:
// Build a Date and time using a PCF8523 RTC connected via I2C and Wire lib #include "RTClib.h"
// Include the libraries needed
#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
RTC_PCF8523 rtc;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address, arbitrary limit of 10 devices
DeviceAddress sensorAddress[10];
uint8_t DeviceCount = 0; // Number of devices found
bool tCent = false; // select temperature scale to report
// create strings for assembling the data to log:
String dataRecord = ""; // assembled data record to write to CSV file
String retStr = "";
String dt_buffer = ""; // Date-time buffer
/*
Setup function. Here we do the basics
*/
void setup(void)
{
// start serial port
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// if (! rtc.initialized() || rtc.lostPower()) {
// Serial.println("RTC is NOT initialized, let's set the time!");
// // When time needs to be set on a new device, or after a power loss, the
// // following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// }
/* When the RTC was stopped and stays connected to the battery, it has
to be restarted by clearing the STOP bit. Let's do this to ensure
the RTC is running. */
// rtc.start(); // UNCOMMENTING THIS LINE CAUSES THE PROGRAM TO HANG
dataRecord = "Dallas OneWire Temperature Sensor Data Logger";
Serial.println(dataRecord);
// locate devices on the bus, report the number found
Serial.print("Locating devices... ");
sensors.begin();
DeviceCount = sensors.getDeviceCount();
dataRecord = String(DeviceCount, DEC) + " devices found";
Serial.println(dataRecord);
// report parasite power requirements
dataRecord = "Parasite power is: ";
if (sensors.isParasitePowerMode()) {
dataRecord += "ON";
}
else {
dataRecord += "OFF";
}
Serial.println(dataRecord);
// Search for devices on the bus and assign based on an index. Ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them once you know
// the devices on your bus (and assuming they don't change).
for (uint8_t i = 0; i < DeviceCount; i++)
{
if (!sensors.getAddress(sensorAddress[i], i)) Serial.println("Unable to find address for Device");
}
// show the addresses we found on the bus
for (uint8_t i = 0; i < DeviceCount; i++)
{
strAddress(sensorAddress[i]);
dataRecord = "Device ";
dataRecord += String(i, DEC);
dataRecord += ", Address: ";
dataRecord += retStr;
// set the resolution to 10 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(sensorAddress[i], 10);
dataRecord += ", Resolution: ";
delay(100);
dataRecord += String(sensors.getResolution(sensorAddress[i]), DEC);
dataRecord += " bits";
Serial.println(dataRecord);
}
// report temperature scale
if (tCent == true)
{
dataRecord = "Degrees C";
}
else
{
dataRecord = "Degrees F";
}
Serial.println(dataRecord);
// print device indexes for found devices, comma delimited list
dataRecord = "0";
for (uint8_t i = 1; i < DeviceCount; i++)
{
dataRecord += ",";
dataRecord += String(i, DEC);
}
Serial.println(dataRecord);
// print device addresses for found devices, comma delimited list
strAddress(sensorAddress[0]);
dataRecord = retStr;
for (uint8_t i = 1; i < DeviceCount; i++)
{
dataRecord += ",";
strAddress(sensorAddress[i]);
dataRecord += retStr;
}
Serial.println(dataRecord);
}
/*
Main function. It will request the tempC from the sensors and display on Serial.
*/
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus, builds CSV record of temperatures
sensors.requestTemperatures(); // Send the command to get temperatures
dataRecord = "";
getTemperature(sensorAddress[0]); // Use a simple function to add temperature to the data recoed string
for (uint8_t i = 1; i < DeviceCount; i++)
{
dataRecord += ",";
getTemperature(sensorAddress[i]); // temps 2 thru n devices
}
Serial.println(dataRecord);
delay(5000);
}
// function to format a device address
void strAddress(DeviceAddress deviceAddress)
{
retStr = "";
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) {
// add leading '0' to addresses less than 10h
retStr += "0";
}
retStr += String(deviceAddress[i], HEX);
}
}
// function to format the temperature for a device
void getTemperature(DeviceAddress deviceAddress )
{
float tempC = sensors.getTempC(deviceAddress);
float tempF = DallasTemperature::toFahrenheit(tempC); // Converts tempC to Fahrenheit
if (tempC == DEVICE_DISCONNECTED_C) // returns -127C for a disconnected device
{
dataRecord += "Error";
return;
}
if (tCent == true)
{
dataRecord += String(tempC, 2);
}
else
{
dataRecord += String(tempF, 2);
}
}