#include <SD.h>
#include <SPI.h>
#include <Bounce.h> //SWitch/Button debouncing
#include <TimeLib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h> //LCD displays
#include <elapsedMillis.h>
#include <nRF24L01.h>
#include <RF24.h>
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
#define BUTTON1 30 // Preset +
#define ONE_WIRE_BUS 36
File myFile;
const int chipSelect = BUILTIN_SDCARD;
int debounce_time = 100;
int ButtCntr = 0;
Bounce bouncer1 = Bounce(BUTTON1, debounce_time);
String tHH, tMM, tSS, tDD, tMMM, tYYYY;
String MyDate1, MyDate2, MyTime, MyTimeStamp, MyLogFile;
unsigned long currentMillis;
unsigned long previousMillis = 0;
unsigned long SampleRate = 2000; //60000 = 1 min
unsigned long Blink_Time = 10000;
int led = 13;
boolean LED_Status = true;
unsigned long BLINKpreviousMillis = 0;
unsigned long ReadingCount =0;
unsigned long LED_Flash_Gap = 150;
boolean RTC_Set = false;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//LCD setup
#define RS 21
#define D4 19
#define D5 18
#define D6 17
#define D7 16
LiquidCrystal lcd = LiquidCrystal(RS, 20, D4, D5, D6, D7);
RF24 radio(9, 10);
//const uint64_t pipe = 0xE8E8F0F0E1LL;
const uint64_t pipe = 0xE6E6E6E6E6E6;
float SENT_temperature = 0;
int SentMsg = 0;
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
void printDigits(int digits)
{
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void setup()
{
pinMode(BUTTON1,INPUT_PULLUP);
pinMode(led, OUTPUT);
//Set the Time library to use Teensy 3.0's RTC to keep time
setSyncProvider(getTeensy3Time);
//Open serial communications and wait for port to open:
Serial.begin(9600);
//Serial.begin(115200);
//while (!Serial) //********* commented out as this hangs up code if not connected to a PC
{
; // wait for serial port to connect.
}
delay(100);
//Start LCD & display config msg
lcd.begin(20, 4);
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(0, 0); lcd.print("Config/Setup.");
delay(1000);
//Check if RTC was able to be setup
if (timeStatus()!= timeSet)
{
Serial.println("Unable to sync with the RTC");
RTC_Set = false;
MyDate2 = "RTC-NA";
}
else
{
Serial.print("Teensy's RTC has been set with the system time: ");
RTC_Set = true;
digitalClockDisplay();
MyDate2 = String(tYYYY) + String(tMMM) + String(tDD);
}
//Initialse onboard SD card
Serial.println("Initializing SD card ...");
if (!SD.begin(chipSelect))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization COMPLETE.");
//Update Serial Prompt & Log file with initialisation data.
Serial.println(MyTimeStamp);
MyLogFile = MyDate2 + ".txt";
Serial.print("SD card Log File Name will be: "); Serial.println(MyLogFile);
Serial.println("***********************************************");
Serial.println("");
//Create Log file name depending if RTC setup was successfull.
//Open Log file, add a bootup header entry, close file
if (RTC_Set == true)
{
myFile = SD.open(MyLogFile.c_str(), FILE_WRITE);
}
else
{
//myFile = SD.open("RTCNA.TXT", FILE_WRITE);
myFile = SD.open(MyLogFile.c_str(), FILE_WRITE);
}
if (myFile)
{
myFile.println("");
myFile.println("***********************************************");
myFile.print("SD card initialised & RTC set - ");
myFile.println(MyTimeStamp);
myFile.println("***********************************************");
}
else
{
//
}
myFile.close();
//Start Sensor
sensors.begin();
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
//Take a reading from Sensor
Read_Temperature();
}
void loop()
{
bouncer1.update();
currentMillis = millis();
if (Serial.available())
{
time_t t = processSyncMessage();
if (t != 0)
{
Teensy3Clock.set(t); // set the RTC
setTime(t);
}
}
if ( bouncer1.fallingEdge()) //Button Pressed
{
Read_Temperature();
}
if (currentMillis - previousMillis >= SampleRate)
{
Read_Temperature();
previousMillis = currentMillis;
}
if (radio.available())
{
if (!radio.read(&SENT_temperature, sizeof(float)) )
{
Serial.println("ACK not received by client.");
}
radio.read(&SENT_temperature, sizeof(float)); // Read information from the NRF24L01
Serial.print("****** SENT Temperature : ");
Serial.println(SENT_temperature);
Serial.println(sizeof(float));
delay(1000);
}
else
{
Serial.println("###### RADIO NOT AVAILABLE ######");
}
BlinkLED();
}
void Read_Temperature()
{
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
SENT_temperature = temperature;
ReadingCount = ReadingCount + 1;
digitalClockDisplay();
ButtCntr = ButtCntr + 1;
Serial.print(ButtCntr); Serial.print(" - ");
Serial.print(MyTimeStamp); Serial.print("_"); Serial.print("Temp:_"); Serial.println(temperature);
//MyDate1 = String(tYYYY) + "-" + String(tMMM) + "-" + String(tDD);
if (RTC_Set == true)
{
MyDate2 = String(tYYYY) + String(tMMM) + String(tDD);
MyLogFile = MyDate2 + ".txt";
myFile = SD.open(MyLogFile.c_str(), FILE_WRITE);
}
else
{
MyDate2 = "TRCNA";
MyLogFile = MyDate2 + ".txt";
myFile = SD.open("TRCNA.TXT", FILE_WRITE);
}
//myFile = SD.open(MyLogFile.c_str(), FILE_WRITE);
if (myFile)
{
myFile.print(ReadingCount); myFile.print("_"); myFile.print(MyTimeStamp); myFile.print("_"); myFile.print("Sensor-A-Temp:_"); myFile.println(temperature);
}
else
{
// if the file didn't open, print an error:
Serial.print("error opening:- ");
Serial.println(MyLogFile);
}
myFile.close();
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(0, 0); lcd.print(MyDate1);
lcd.setCursor(0, 1); lcd.print(" ");
lcd.setCursor(0, 1); lcd.print("Sensor A");
lcd.setCursor(10, 1); lcd.print(temperature);
lcd.setCursor(0, 2); lcd.print(" ");
lcd.setCursor(0, 2); lcd.print(MyTime);
lcd.setCursor(0, 3); lcd.print(" ");
lcd.setCursor(0, 3); lcd.print(ReadingCount);
}