Simple weather reader issues.

Hello all,

Working on this bit of code and I can’t figure out what I did wrong. It’s so simple, just want to take the output from my temp sensor and write it to my SD card. I think it’s the brackets, but I’m not sure.

I have confirmed that my hardware is good by running each bit of code by itself, but I cant seem to assimilate them all together.

I have attached my code, any help is greatly appreciated.

Many thanks,

-Z

sketch_jun11e.ino (4.63 KB)

You will be better off using the Dallas temperature library

Also please use the " please read" to insert code in the proper manner using </>

Many thanks, but I'm still missing the write to Sd card functionality. Is there any way i can incorporate it in?

It's probably in there somewhere, but it's just too messy to see.

It is better to keep the tasks together

/* Basic 2xDS18B20 code for serial monitor, bluetooth, Excel or w.h.y.
Derived from Hacktronics. USE THEIR ADDRESS SNIFFER and substitute your 
numbers. Use Hacktronics connections diagram. 
http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
Stay away from using parasite power
-127C means bad connection
85 means you haven't gotten a read yet, probably just the 
wrong order of commands
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include "Wire.h"               
#include <SD.h>
#include <SPI.h>                

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
  
byte Thermo1[8] = {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};
byte Thermo2[8] = {0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
float tempC,Temp1,Temp2;  

char filename[] = "Test.CSV";
File myFile;

void setup(){
  Serial.begin(9600);
  sensors.begin();
  delay(500);//Wait for newly restarted system to stabilize

  //pinMode(10, OUTPUT);// Uno
  pinMode(53, OUTPUT);//MEGA
    if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed");
    // don't do anything more:
    return;
  }
    Serial.println("CARD OK");
}

void loop() {
 sensors.requestTemperatures();  // call readings from the addresses
  Temp1 = sensorValue(Thermo1);
  Temp2 = sensorValue(Thermo2);  

Serial.print("      Temp1 = ");
Serial.print(Temp1);
Serial.print("      Temp2 = "); 
Serial.println(Temp2);

  myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(Temp1);
  myFile.print(",");
  myFile.println(Temp2);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
       
delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

Here's what im getting from running that bit of code,

Arduino: 1.6.4 (Windows 7), Board: "Arduino Uno"

Build options changed, rebuilding all

sketch_jun12b.ino: In function 'void setup()':
sketch_jun12b:40: error: 'chipSelect' was not declared in this scope
'chipSelect' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Got it, just blocked out the check for the sd card, info shows on serial moniter just fine but no write to sd, gonna tinker some more and see if i can find anything.

Many thanks again for the help, Nick.

-Z

Got it, finally have a output in a text file of temps, but now I need a time stamp next to the temps I can tell what happened when.

Any suggestions? I'm currently going to try to implement the Time lib from Arduino.

There are several ways. The following uses a DS1307 RTC. The DS3231 is the same. I have never used a library for this. Strip out the LCD stuff etc.

/*
This programme reads temperatures from  three DS18B20 sensors at one 
second intervals.

The temperatures are displayed on Nokia 5110 LCD  

Two of the temperatures, plus the difference between them, is transmitted 
over bluetooth in a format suitable for BlueTooth Terminal/Graphics (CETIN)
in order to display three graphs in real time.

Every tenth reading is recorded on SD card in real numbers to two decimal
places. Each line is timestamped.

A new file is created at midnight using the date as the filename.

Daily files can be read via bluetooth by sending MMDD

Credit to Hacktronics, Mellis & Igoe, Garage Box, Zoomkat, Bildr.org, etc.
Kudos to lar3ry in Saskatchewan, Stanley in KL, Coding Badly in Tx.
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h>             // Nokia 5110
#include "Wire.h"                
#include <SD.h>
#include <SPI.h>                   // SD   5110

#define DS1307_ADDRESS 0x68

char filename[] = "00000000.CSV";
File myFile;
char dumpName[] = "00000000.CSV";
File dumpFile;

static PCD8544 lcd;

// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};

// Green group Dee Why (red or amber LED shields)
byte InThermo[8] =  {0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0x9F };
byte OutThermo[8] = {0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {0x28, 0x54, 0xF7, 0x2D, 0x04, 0x00, 0x00, 0x68}; 
  
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
byte  second, minute, hour, weekDay, day, month, year;
int k=0;

const int chipSelect = 4;

float tempC, InTemp, OutTemp, DrainTemp, diff; 

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char strIn[8];
char strOut[8];
char strDrain[8];
char strdiff[8];
char charBuf [13];

String readString;
String stringOne, stringTwo, stringThree, stringFour;
String stringFive, stringSix;

//_________________________________________

void setup() {
  lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  
  Wire.begin();
  Serial.begin(9600);
  
  delay(300);//Wait for newly restarted system to stabilize
    
  lcd.setCursor (0,0);
  lcd.println("Init SD CARD");
  // make sure that the default chip select pin 53 is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);//MEGA
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) 
  {
    lcd.println("Card failed");
    // don't do anything more:
    return;
  }
  lcd.println("CARD OK");
  delay(2000);
      GetClock();
      getFileName();
      lcd.setCursor(0,3); 
      lcd.println(filename);
        delay(2000);
  
    lcd.clear();
        
  //Sequence for bluetooth
  // "E256,-321,982\n" 
  //so insert three variable between four strings, two are the same twice, 
  //to make a fourth string 
  
  stringOne = "E";
  stringTwo = ",";
  stringThree = "\n";

    running();
  }
//________________________________________________________________

void loop() {
   GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
  
    while (Serial.available()) 
  {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }// end while

  if (readString.length() >0) 
  {  
    getDump();   
   readString="";  
   } // end if
  
  //get the values from the DS8B20's 
  sensors.requestTemperatures();

  InTemp = sensorValue(InThermo);
  OutTemp = sensorValue(OutThermo);  
  DrainTemp = sensorValue(DrainThermo); 

  diff = OutTemp - InTemp;
  
dtostrf(InTemp,4, 2, strIn);
dtostrf(OutTemp,4, 2, strOut);
dtostrf(diff,4, 2, strdiff);

stringFour = stringOne + strIn + stringTwo + strOut;
stringFour = stringFour + stringTwo + strdiff + stringThree;

  Serial.println(stringFour);

  lcd.setCursor(49,0);
  lcd.print(InTemp);
  lcd.setCursor(49,1);
  lcd.print (OutTemp);
  lcd.setCursor(49,2);
  lcd.print(DrainTemp);
  lcd.setCursor(49,3);
  lcd.print(diff);  
  lcd.print(" ");//suppress trailing fig > 9.99
  lcd.setCursor(17,5);

  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);
  lcd.print("   ");

  k=k+1;  

  if (k>9 )
  {  
           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.print(diff);
  myFile.println();
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
      k=0;
  }
  delay(850);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal bers
  return ( (val/16*10) + (val%16) );
}

void running(){
  lcd.setCursor(0,0);
  lcd.print("In");
  lcd.setCursor(31,0);
  lcd.print("\001C ");
  lcd.setCursor(0,1);
  lcd.print("Out");
  lcd.setCursor(31,1);
  lcd.print("\001C ");
  lcd.setCursor(0,2);
  lcd.print("Drain");
  lcd.setCursor(31,2);
  lcd.print("\001C ");
  lcd.setCursor(0,3);
  lcd.print("diff");
  lcd.setCursor(31,3);
  lcd.print("\001C "); 
  lcd.setCursor(15,4);
  lcd.print("Bluetooth!");  
  }

void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  day = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void getDump() {
   stringSix = "2015" + readString + ".csv";
   stringSix.toCharArray(charBuf, 15);  
     Serial.println("");
     Serial.println(charBuf);
  File dumpFile = SD.open(charBuf);
  if (dumpFile) 
  {
    lcd.clear();
      lcd.setCursor (0,1);
      lcd.println("DUMP FROM ");
      lcd.setCursor (0,2);
      lcd.println(charBuf);
    while (dumpFile.available())
    {
      Serial.write(dumpFile.read());
    }
    dumpFile.close();
      lcd.clear();
      running();
  }  
  else {
    Serial.println("error opening file");
       }
}

Thanks for all the help, but I have a new challenge now. The team wants to collect data for a few days, consecutive, but only once per hour. I tried a test over the weekend (started Friday) with a 9v battery, and it didn’t make it to today (Monday). So my question is, is there a power saving mode I can put it into to get a little more time out of it? I've read about watchdog timers but I don’t think that would apply to me, since I don’t really have a third pin to detect on to wake it up again.

Help anyone?

Thanks,
-Z

I think the first thing to do is get rid of the 9v battery and use 6xD, 6xAA, or 2x18650

The next thing to do is trawl the forum. There is a large school of low-power battery users with sleep modes, timed wake-up, and God knows what all else. With one hour between collections, you should have no problem. If you can't get it together, raise a new topic, not this one.