View Single Post
Old 11-16-15, 02:06 AM   #37
TechShop
FNG
 
Join Date: Jul 2015
Location: Washington
Posts: 71
Thanks: 8
Thanked 19 Times in 13 Posts
Default

Code:
/*
   DS18B20 temperature sensor datalogger.
   20151115 version
   Some code was borrowed and/or ispired by code from:
      -Arduino.cc reference for SD library
      -pjrc.com  for the one wire library (DS18B20 sensors).

   This sketch will run on some Arduino boards including the Duimilanove (with Atmega 328P) and the Sparkfun Pro-Micro 5v.
   With some minor tweaks to the pin assignments inside the SD library, it should run on the Arduino MEGA (2560 and other variants).
   Depending on your hardware, you'll have to change the value of #define SDMMCCSPIN.
   Some of the ethernet shields and other popular hardware use pin 4 for the SD CS pin.
   
   Connecting to the serial port at 9600 baud is also possible to monitor the data in CSV format
   commands are integers sent in CSV format.
   1    reads the datalog.txt file and sends it back through the serial port
   2    sends a timestamp to the arduino which is written into the file as text
        EXAMPLE:   2,14,30,59
          This saves the time stamp 14:30:59  (which would be 2:30:59 PM in 24 hour format).
   3    sends or requests the data refresh/logging interval.
        EXAMPLE:   3,0
          This requests the current log interval, it will be reported back via the serial port as text
        EXAMPLE:   3,45
          This changes the log interval to once every 45 seconds
   4    Request / Manage sensors list
        EXAMPLE:   4,0
          This requests the current list of sensors (at startup, all sensors are discovered and enabled (up to the quantity MAXSENSORSALLOWED limit defined below)
        EXAMPLE:   4,1,3
          This enables sensor number 3 fromt the list
        EXAMPLE:   4,2,3
          This disables sensor number 3 fromt the list
        EXAMPLE:   4,3
          This enables all available sensors in the list
        EXAMPLE:   4,4
          This disables all available sensors in the list
   9    This deletes the datalog.txt file on the SD card.  A new log is immediately started after the command completes.

   Commands can be stacked and will be executed sequentially.
        EXAMPLE: 1,3,15,4,2,2,5,2,22,30,00
        This will (1) read the datalog (3) Change the interval to 15 seconds (4) Disable sensor 2 (5) delete the file (2) log the timestamp @ 22:30:00.
        After execution of commands logging will resume with the settings change.
        Note:  I have not yet implimented a settings/script file at startup, so if you power cycle the arduino, the default settings will be used.


    Connect your DS18B20 temperature sensors to 5V and GND pins on the Arduino.
        Connect a 4.7K Ohm resistor between the 5V and Digital I/O pin #2 (Or whichever pin you have specified with #define SENSORPIN below).
        Connect the sensor data wire to Digital I/O pin #2 (Or whichever pin you have specified with #define SENSORPIN below).

*/

#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#define SENSORPIN 2     //which digital I/O pin are your DS18B20 sensors connected to?
#define SDMMCCSPIN 10   //Which digital I/O pin is your SD MMC card CS pin connected to?
#define MAXSENSORSALLOWED 7 // Maximum number of sensors allowed on the network.  Watch memory usage at compile time.
#define LOGFILENAME "datalog.txt" // name of the file to append with data.
//#define INIFILE "LOGSCRIPT.INI" // Name of the file for startup settings and script actions.  (This feature is not implimented yet).

OneWire ds(SENSORPIN);  // DS18B20 temperature sensors connected to SENSORPIN, set it above in the definitions.

boolean logging = true;
unsigned long G_TimePoint = 0;
byte G_LogInterval = 30;
byte G_SensorQty = 0;
byte G_CurSensor = 1;
String G_dString = "";   // make a string for assembling the data to log
byte G_AddressList[MAXSENSORSALLOWED][8];
boolean G_ActiveSensors[MAXSENSORSALLOWED];

void setup() {
  byte s, i;
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    delay(100); // wait for serial port to connect. Needed for native USB port only
  }
  for (s = 0; s < MAXSENSORSALLOWED; s++) {
    G_ActiveSensors[s] = false;  // make sure we start out expecting to log no sensors (we haven't found any sensors yet).
  }
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(SDMMCCSPIN)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
  SetCheckInterval(G_LogInterval);
  File LogFile = SD.open(LOGFILENAME, FILE_WRITE);
  if (LogFile) {
    LogFile.println("START NEW DATA RECORD");
    Serial.println("START NEW DATA RECORD");
    Serial.println(LOGFILENAME);
    LogFile.println("INTERVAL = " + String(G_LogInterval));
    LogFile.close();
  }
  else {
    Serial.println("Error opening " + String(LOGFILENAME));
  }
  G_SensorQty = FillAddressList(MAXSENSORSALLOWED);     //call FillAddressList() for initial sensor discovery
  Serial.print(String(G_SensorQty) + " Sensors found on pin " + String(SENSORPIN) + ":\n");
  if (G_SensorQty > 0) {
    for (s = 0; s < G_SensorQty; s++) {
      G_ActiveSensors[s] = true; // log every sensor found by default.  (User can change this with a command)
    }
  }
  SensorList(true);
}

The remainder of the sketch is in my next post
TechShop is offline   Reply With Quote