Skip to content

Instantly share code, notes, and snippets.

@Miceuz
Created August 7, 2014 11:28
Show Gist options
  • Save Miceuz/61b02f1fff593c323a61 to your computer and use it in GitHub Desktop.
Save Miceuz/61b02f1fff593c323a61 to your computer and use it in GitHub Desktop.
#include <SD.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <stdlib.h>
#include "RTClib.h"
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
#define NUMBER_OF_SENSORS 20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RTC_DS1307 rtc;
const int chipSelect = 10;
const int led = 7;
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
unsigned long startTime = 0;
DeviceAddress addresses[] = {
{0x28, 0x2C, 0x5A, 0xDC, 0x05, 0x00, 0x00, 0xDB},
{0x28, 0x62, 0x02, 0xD9, 0x05, 0x00, 0x00, 0x97},
{0x28, 0x41, 0x37, 0xB6, 0x05, 0x00, 0x00, 0x24},
{0x28, 0xC9, 0x5E, 0xDD, 0x05, 0x00, 0x00, 0x89},
{0x28, 0x0E, 0x70, 0xB5, 0x05, 0x00, 0x00, 0x45},
{0x28, 0xC1, 0x04, 0xB5, 0x05, 0x00, 0x00, 0x8C},
{0x28, 0xC3, 0x17, 0xDD, 0x05, 0x00, 0x00, 0x5C},
{0x28, 0x25, 0x2C, 0xB5, 0x05, 0x00, 0x00, 0xBF},
{0x28, 0x60, 0x3D, 0xDD, 0x05, 0x00, 0x00, 0x1C},
{0x28, 0x1D, 0x09, 0xD9, 0x05, 0x00, 0x00, 0x5B},
{0x28, 0xBC, 0xE9, 0xDC, 0x05, 0x00, 0x00, 0x6B},
{0x28, 0x42, 0x99, 0xD9, 0x05, 0x00, 0x00, 0xE6},
{0x28, 0x28, 0xB4, 0xB4, 0x05, 0x00, 0x00, 0xF3},
{0x28, 0xBB, 0xD3, 0xDC, 0x05, 0x00, 0x00, 0xD7},
{0x28, 0x88, 0x35, 0xD9, 0x05, 0x00, 0x00, 0xA4},
{0x28, 0x7C, 0x0E, 0xDA, 0x05, 0x00, 0x00, 0x76},
{0x28, 0xA4, 0x2A, 0xDD, 0x05, 0x00, 0x00, 0x72},
{0x28, 0xDD, 0x39, 0xB6, 0x05, 0x00, 0x00, 0x4A},
{0x28, 0x90, 0xCD, 0xDC, 0x05, 0x00, 0x00, 0x47},
{0x28, 0xFD, 0x04, 0xB5, 0x05, 0x00, 0x00, 0x1C}
};
void scanSensors(void) {
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
}
#define INTERVAL_TEST 5
#define INTERVAL_SHORT 60
#define INTERVAL_MID 60*5
#define INTERVAL_LONG 60*15
unsigned int getLoggingInterval() {
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(3, INPUT);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
if(LOW == digitalRead(5)){
return INTERVAL_SHORT;
}
if(LOW == digitalRead(4)){
return INTERVAL_MID;
}
if(LOW == digitalRead(3)){
return INTERVAL_LONG;
}
return INTERVAL_TEST;
}
unsigned int logInterval;
void setup() {
pinMode(led, OUTPUT);
logInterval = getLoggingInterval();
startTime = millis();
Wire.begin();
rtc.begin();
Serial.begin(57600);
digitalWrite(led, LOW);
pinMode(chipSelect, OUTPUT);
while (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
delay(500);
}
}
void logTemperature() {
digitalWrite(led, LOW);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
DateTime now = rtc.now();
String temperatures = getTemperatures();
String lineToWrite = String(now.unixtime());
lineToWrite += ", ";
lineToWrite += temperatures;
dataFile.println(lineToWrite);
dataFile.close();
Serial.println(lineToWrite);
} else {
while(1) {
//NOTHING
}
}
digitalWrite(led, HIGH);
}
String getTemperatures(void) {
String temperatures = "";
for(int i = 0; i < NUMBER_OF_SENSORS; i++) {
sensors.setResolution(addresses[i], 9);
}
sensors.requestTemperatures(); // Send the command to get temperatures
for(int i = 0; i < NUMBER_OF_SENSORS; i++) {
float tempC = sensors.getTempC(addresses[i]);
char buff[10];
temperatures += String(dtostrf(tempC, 6, 2, buff));
temperatures += String(", ");
}
return temperatures;
}
unsigned int lastBlink = 0;
unsigned int lastLog = 0;
void loop() {
unsigned int now = rtc.now().unixtime();
if(now - lastBlink >= 1) {
lastBlink = now;
digitalWrite(led, !digitalRead(led));
}
if(now - lastLog >= logInterval) {
logTemperature();
lastLog = now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment