LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2017, 01:19 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mageia
Posts: 312

Rep: Reputation: 30
Arduino data.print two temperatures to SD card


I'm trying to write the temperature from two sensor to an SD card.

I have these lines at appropriate places, which pass the Arduino test:

Code:
      #define DHT11_PIN 7
      #define DHT111_PIN 8


  int chk = DHT.read(DHT11_PIN);
      chk = DHT.read(DHT111_PIN);    //second temperature

      int chk = DHT.read(DHT11_PIN);
      Serial.print(",,Outside Temp,");
      Serial.print(DHT.temperature);

      chk = DHT.read(DHT111_PIN);    //second temperature read
      Serial.print(",,Inside Temp,");
      Serial.print(DHT.temperature);
How do it modify the code below (which worked with one sensor) to write the second temperature sensor to the SD card?

Code:
   if (dataFile) {
     
      dataFile.print(message);    
     
      dataFile.print(dataString);

      dataFile.print(",,Temperature,");
      dataFile.print(DHT.temperature);

      dataFile.close();
 
Old 06-10-2017, 06:38 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,337
Blog Entries: 1

Rep: Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329
I think we will need a more complete example of your code to provide a useful answer.

Code:
I have these lines at appropriate places, which pass the Arduino test
That may mean something to you, but it is not at all obvious to us what the meaning of appropriate places and arduino test are.

Your example code reads the return value of DHT.read(DHT11_PIN), but never makes use of it in any way.

As your question seems to imply that your code works with a single sensor, but not with two, a more complete example of the single sensor code and what it produces would be a good place to start.

A quick search turned up this page which appears to be a complete implemention with the DHT11 device. Perhaps it may be helpful to you in understanding your own uses.

Please also review this page in the Site FAQ for guidance in asking a more complete question which will help members here provide the assistance you nee.
 
Old 06-10-2017, 09:47 PM   #3
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mageia
Posts: 312

Original Poster
Rep: Reputation: 30
Thank you for swithing forum, and remarks.

In the code below, everything works as required up to line 126 [ dataFile.println(" xx,"); ]. I have inserted " xx," because I know not what I need to do.

Code:
#include <dht11.h>

dht11 DHT;

#define DHT11_PIN 7
#define DHT111_PIN 8   //  add second DHT

const int chipSelect = 10;    
//The RTC Library needs Wire
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;


//The SDlibrary needs SPI
#include <SPI.h>
#include "SD.h"
//Some SD Card code borrowed from Adafruit library examples
// set up variables using the SD utility library functions:

File dataFile;
#define LOGFILE "datalog.txt"


void setup() {
  Serial.begin(9600); 
  //Initialize the Sensor
//  Serial.println("Initializing datalogger with RTC version 1.0");


{
  int chk = DHT.read(DHT11_PIN);
      chk = DHT.read(DHT111_PIN);   //  add second DHT read  
  delay(1000);
}

//  Serial.println("Starting SDCard reader and card");
  pinMode(chipSelect, OUTPUT);
  pinMode(SS, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("SD Card initialization failed!");
    return;
  }
  
//  Serial.println("Opening logfile for write.");
  // Open up the file we're going to log to!
  dataFile = SD.open(LOGFILE, FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening log file");
    // Wait forever since we cant write data
    while (1) ;
  }
  
  
//  Serial.println("Starting Real Time Clock");
  #ifdef AVR
    Wire.begin();
  #else
    Wire1.begin();     // Shield I2C pins connect to alt I2C bus on Arduino Due
  #endif

  rtc.begin();

  if (! rtc.isrunning()) {       //code borrowed from adafruit example rtc code
    Serial.println("RTC is NOT running!");
  }

  DateTime now = rtc.now();

}

void loop()
{
  delay(3000);   // set for 10minutes  =  600000

{
  
void logThis(char* logmessage);{
  char message[120];
  DateTime now = rtc.now();
  long epoch = now.unixtime();
  int Year = now.year();
  int Month = now.month();
  int Day = now.day();
  int Hour = now.hour();
  int Minute = now.minute();
  int Second = now.second();
  sprintf(message, "%d/%d/%d,,%02d:%02d:%02d,%s,",Year,Month,Day,Hour,Minute,Second );

{

 // make a string for assembling the data to log:
  String dataString = "";

  // read sensor and append to the string:
  int analogPin = 1; {
    int sensor = analogRead(A1);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = round(sensor * 2.40);
     
    dataString += String(voltage,0);  //the ",0" removes decimals
    if (analogPin < 2) {
    }
 
 }
  // 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.print(message);    
     
//    dataFile.print(",,");     
      dataFile.print(dataString);

      dataFile.print(", Inside Temp,");
      dataFile.print(DHT.temperature);
      dataFile.print(" Outside Temp,");
      dataFile.println(" xx,");     //    (DHT.xxxxxxx); ????????

      dataFile.close();
 
 //   print to the serial port too:
      Serial.print(message);
      Serial.print(",");
      Serial.print(dataString);
    

      int chk = DHT.read(DHT11_PIN);
      Serial.print(", Outside Temp,");
      Serial.print(DHT.temperature);

      chk = DHT.read(DHT111_PIN);       //  add second DHT read  
      Serial.print(", Inside Temp,");
      Serial.println(DHT.temperature);

    
   }}
 
   }}}
Any help would be appreciated.
 
Old 06-11-2017, 01:36 AM   #4
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mageia
Posts: 312

Original Poster
Rep: Reputation: 30
Looks like I have answered my own question. I rearranged the dataFile.print and Serial.print lines:
Code:
  // if the file is available, write to it:
  
   if (dataFile) {
     
      dataFile.print(message);    
     
//    dataFile.print(",,");     
      dataFile.print(dataString);


//    print to the serial port too:
      Serial.print(message);
      Serial.print(",");
      Serial.print(dataString);     


      int chk = DHT.read(DHT11_PIN);
      Serial.print(", Outside Temp,");
      Serial.print(DHT.temperature);      

      dataFile.print(", Outside Temp,");
      dataFile.print(DHT.temperature);


      chk = DHT.read(DHT111_PIN);       //  add second DHT read  
      Serial.print(", Inside Temp,");
      Serial.println(DHT.temperature);
      
      dataFile.print(" Inside Temp,");
      dataFile.println(DHT.temperature);

      dataFile.close();
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Altering script to print input data with the results of the query (add IP address to NSlookup data) cilbuper Programming 7 10-31-2016 11:41 AM
LXer: Open source data logger board is Arduino compatible LXer Syndicated Linux News 0 06-09-2016 03:31 PM
LXer: New Arduino Srl SBC merges Arduino, WiFi, and Linux LXer Syndicated Linux News 0 05-17-2016 01:04 AM
Ubuntu 12.10 Attempted Installation of Arduino IDE with arduino-1.0.5-linux32.tar.gz mahnac72 Linux - Newbie 2 06-21-2013 04:26 PM
[SOLVED] Arduino MEGA 2560 REV3, Arduino IDE working software versions jamesf Slackware 1 05-02-2012 12:56 AM


All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration