Skip to content

Instantly share code, notes, and snippets.

@adamabernathy
Created April 6, 2016 23:16
Show Gist options
  • Select an option

  • Save adamabernathy/080eef040707f5692aa7550ce6033b58 to your computer and use it in GitHub Desktop.

Select an option

Save adamabernathy/080eef040707f5692aa7550ce6033b58 to your computer and use it in GitHub Desktop.
/***********************************************************************
* File name: penny.ino *
* *
* Purpose: Logs data from an array of digital weather sensors and *
* stores the data in CSV format on a standard SD Card. *
* *
* - File references: *
* Name I/O Description *
* ---------------------------------------------------------------- *
* *
* - External variables: *
* source: < > *
* Name Type I/O Description *
* ---------------------------------------------------------------- *
* *
* - External references: *
* Name Description *
* ---------------------------------------------------------------- *
* *
* - Abnormal termination conditions, error and warning messages *
* Code Description *
* ---------------------------------------------------------------- *
* *
* - Assumptions, constraints, restrictions and notes: *
* See `penny-errata.md` *
* *
* - Development history: *
* See repository & commit logs for additional information. *
* Repo: [email protected] *
* *
* Date Author Release Description Of Change *
* ---------------------------------------------------------------- *
* 9/27/15 A.Abernathy v0.1.0 Initial release. *
* *
* - Algorithm (pdl) *
* See `penny-PDL.md` in source code *
**********************************************************************/
//
// Punchlist - Complete before release
// -----------------------------------
// TODO: Write the Errata document
// TODO: Finsh the program header
// TODO: Add a CSV map of the output data
// TODO: Create local versions of the #Includes since they have some
// changes to them.
// TODO: Standardize the error messages, add new ones for FATALS.
// TODO: Determine if the output filename should be a global variables.
// It seems like it should be, but we had some trouble with
// getting the function to work correctly before.
// TODO: FEATURE: If the TL gets too hot, then shutdown?
//
/* Includes and headers */
#include <Sensirion.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
/* Definitions */
#define LOCAL_ALT 1319.0 /* Locally averaged ALT. */
/*
* I/O Pins, Global
* TODO: Put a full pinout map here
*/
const uint8_t TEMP_1_DP = 8; /* SHT-10 # 1 */
const uint8_t TEMP_2_DP = 9; /* SHT-10 # 2 */
const uint8_t TEMP_CLOCK_DP = 7; /* Clock sync for SHT-10's */
const int SD_CARD_DP = 10; /* for SD Card interface */
//const int LED_1 = 2; /* For SD Card status light */
/* Other global variables */
const int OBS_DELAY = 2; /* Obvervation collect delay, [s] */
const int dOBS = OBS_DELAY * 1e3; /* Converts OBS_DELAY to [ms] */
const float LOCAL_ALT = 1390.2; /* Local altitude, [m] */
/* Create sensor & clock instances */
RTC_DS1307 RTC; /* Real-Time clock */
SFE_BMP180 BMP180; /* BMP180 temp and pressure */
/* Sensirion temperature and humidity sensors */
Sensirion SHT10_1 = Sensirion(TEMP_1_DP, TEMP_CLOCK_DP);
Sensirion SHT10_2 = Sensirion(TEMP_2_DP, TEMP_CLOCK_DP);
/***********************************************************************
* *
* Function: setup *
* Purpose: Initializes the system. *
* Returns: void *
* *
* - Arguments *
* Argument Type I/O Description *
* ---------------------------------------------------------------- *
* None. *
* *
* - Local variables *
* Name Type Default Description *
* ---------------------------------------------------------------- *
* TODO: Fill this out!
* *
**********************************************************************/
void setup() {
Serial.begin(115200);
Wire.begin();
RTC.begin(); /* Start the Real-Time clock */
if ( ! RTC.isrunning() ) {
Serial.println("RTC is NOT running!");
return; /* Fatal Error! */
/*
* The following statement updates the Arduino's RTC
* with the time of the computer compiling this code at
* the time of compiling. If you need to set the RTC, then
* uncomment the following line. It is not recommended that
* this line be left enabled.
*/
//RTC.adjust(DateTime(__DATE__, __TIME__));
} /* End of RTC initialization */
/*
* Initialize the pressure sensor. If success then move on,
* if not then wait for two seconds and continue on.
*
* TODO: Need to investigate if this qualifies as a fatal error.
*/
Serial.println("Initializing BMP180...");
if ( BMP180.begin() ) {
Serial.println("OK");
} else {
Serial.println("FALIED but continuing");
delay(2000); /* Wait 2 seconds and hope for the best */
} /* End of BMP180 Initialization */
/*
* Initialize the SD Card interface. If success then move on,
* if not then return a fatal error. Logically without the SD Card
* logging data then any operation is futile.
*
* TODO: Find a method of restarting the device that is kosher.
*/
Serial.print("Initializing SD card...");
if ( !SD.begin(chipSelect) ) {
Serial.println("Card failed, or not present");
return; /* Get out of here! Fatal Error! */
} else {
Serial.println("OK");
digitalWrite(LED1, LOW);
} /* End SD Card Initialization */
} /* End setup() */
/***********************************************************************
* *
* Function: loop *
* Purpose: Main operational loop. *
* Returns: void *
* *
* - Arguments *
* Argument Type I/O Description *
* ---------------------------------------------------------------- *
* None. *
* *
* - Local variables *
* Name Type Default Description *
* ---------------------------------------------------------------- *
* T1 F -99999 Temperature from SHT10_1 *
* T2 F -99999 Temperature from SHT10_2 *
* TL F -99999 Local temperature BMP180 *
* H1 F -99999 Humidity from SHT10_1 *
* H1 F -99999 Humidity from SHT10_2 *
* DP1 F -99999 Dewpoint from SHT10_1, Derived *
* DP2 F -99999 Dewpoint from SHT10_2, Derived *
* P F -99999 Pressure from on board BMP180 *
* P0 F -99999 SLP from BMP180 *
* bmp_flg C null BMP180 working status: *
* --> 0:Fail, 1:Pass *
* data_csv S null CSV test string to store *
* data_file Obj SD I/O File I/O object for SD Card *
* now Obj RTC DateTime object from RTC *
* *
* - Notes *
* Some of the data collection variables do not follow standard *
* ISO/NASA compliance, this is to keep their atmospheric science *
* and mathematical meanings consistant. *
**********************************************************************/
void loop() {
/* Local variables */
float T1, T2, TL, H1, H2, DP1, DP2, P, P0, ALT = -99999.;
char bmp_flg;
String data_csv;
DateTime now = RTC.now();
/*
* Begin collecting observations
*/
/* Collect temperature from the two SHT-10's */
SHT10_1.measure(&T1, &H1, &DP1);
SHT10_2.measure(&T2, &H2, &DP2);
/*
* Collect pressure from the BMP180
* Please note that this is a 4-level nested IF/THEN stucture.
* Before we continue to the next level, we wait for a status
* flag from the current function. If any of these fail, we
* skip the data_csvervation and move on.
*
* The pressure sensor returns abolute pressure, which varies
* with altitude. We also want to know what the sea-level
* pressure is (SLP = P0) therefore to remove the effects of
* altitude, we use BMP180.h sealevel function and our current
* altitude. This yields us the local pressure and SLP.
*/
bmp_flg = BMP180.startTemperature();
if ( bmp_flg != 0 ) {
delay(bmp_flg);
bmp_flg = BMP180.getTemperature(TL);
if ( bmp_flg != 0 ) {
bmp_flg = BMP180.startPressure(3);
if ( bmp_flg != 0 ) {
delay(bmp_flg;
bmp_flg = BMP180.getPressure(P, TL);
if ( bmp_flg != 0 ) {
P0 = BMP180.sealevel(P, LOCAL_ALT);
} else { Serial.println("ERR BMP180 - 4\n"); }
} else { Serial.println("ERR BMP180 - 3"); }
} else { Serial.println("ERR BMP180 - 2n"); }
} else { Serial.println("ERR BMP180 - 1\n"); }
/* Pring data to serial port. */
data_csv = String(now.unixtime()) + "," + \
String(T1) + "," + String(T2) + "," + String(TL) + "," + \
String(H1) + "," + String(H2) + "," + \
String(P) + "," + String(P0);
Serial.println( data_csv );
/* Save observations to SD Card */
// TODO: Build more reduncey here. If error, then reboot.
// There is a known flaw that if the I/O process fails then
// the system will continue, but no data is stored.
File data_file = SD.open("datalog.txt", FILE_WRITE);
if ( data_file ) {
data_file.println(data_csv);
data_file.close();
} else {
Serial.println("ERROR OPENING FILE!");
digitalWrite(LED1, HIGH);
}
delay(data_csv); /* Wait to collect the next observation */
} /* End loop() */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment