How to graph sensor data with rrdtool and the Bridge

raxpa,

Here is my sketch as it stands right now, not very clean code as I am still experimenting. I basically generate the new line in the csv file in the routines 'checkTemperatures' and 'logEntry'.

/*
  Controls the Green House
  
  This version samples the temperature every five minutes and stores
  the result on the SD card.
  
  The data can be retrieved from the SD card via the web interface.
  
  created 26 October 2013
  by Bjarne B. Christensen
  
  This code is in the public domain
  
*/

#include <Bridge.h>
#include <FileIO.h>
#include <Process.h>
#include <DS18B20.h>
#include "SensorAddresses.h"
#include "EmailAccount.h"  // contains sending email account information
#include <X10Firecracker.h>

#define RTS_PIN  4    // RTS line for CM17A - DB9 pin 7
#define DTR_PIN  3    // DTR line for CM17A - DB9 pin 4
                      // GND line for CM17A - DB9 pin 5
#define BIT_DELAY 1    // mS delay between bits (1 mS OK)

#define HEAT_LAMP 4   // Greenhouse Heat Lamp
#define LIGHTS    5   // Greenhouse Lights

#define good_strength 25

Process time;
Process wifiCheck;  // process used to get the wifi status
String minuteString;
String timeString;
String dateString;
String nowMinuteString = "";
float boardTemperature;
float outdoorTemperature;
float indoorTemperature;
int voltagePin = A0;
int voltageValue = 0;
float inputVoltage = 0;
char charBuf[40];
int nextMinute = 0;
int minute;
boolean armEmail = true;
int tries;
int test = 0;
int led = 13;   // LED on pin 13
String ledTest;
byte previous_state = 0;
int wifiLevel;

DS18B20 ds(10);

void setup() {
   pinMode(led, OUTPUT);
   Serial.begin(9600);

   // Bridge startup
   pinMode(13,OUTPUT);
   digitalWrite(13, LOW);
   Bridge.begin();
   digitalWrite(13, HIGH);
 
   delay(60000);
   getTime();

   nextMinute = (minute - minute%5) + 5;
   if(nextMinute > 59) nextMinute -= 60;

   FileSystem.begin();

   String message = "Greenhouse starting at ";
   message += timeString;
   sendEmail("Start message", message);

   X10.init(RTS_PIN, DTR_PIN, BIT_DELAY);
   X10.sendCmd(hcC, LIGHTS, cmdOff);
   X10.sendCmd(hcC, HEAT_LAMP, cmdOff);
}

void loop() {
   getTime();
   
   ledTest = timeString.substring(7);
   if((ledTest == "0") || (ledTest == "2") || (ledTest == "4") || (ledTest == "6") || (ledTest == "8")) {
      digitalWrite(led, 1);
   } else {
      digitalWrite(led, 0);
   }

   if(timeString.substring(0,5) == "00:00") {
      if(armEmail) {
         String message = "Greenhouse daily log for ";
         message += dateString;
         sendEmail("Greenhouse message", message);
         armEmail = false;
         String file = dateString;
         file += ".csv";
         message = "Time,Voltage,Board,Outside,Inside,";
         message += dateString;
         logEntry(file, message);
      }
   } else {
      armEmail = true;
   }
   
   if(minute == nextMinute) {
      nextMinute = minute + 5;
      if(nextMinute > 59) nextMinute -= 60;
      checkwifistatus();
      checkTemperatures();
/*
      if(test == 0) {
        X10.sendCmd(hcC, HEAT_LAMP, cmdOn);
        X10.sendCmd(hcC, LIGHTS, cmdOff);
        X10.sendCmd(hcC, HEAT_LAMP, cmdOn);
        X10.sendCmd(hcC, LIGHTS, cmdOff);
        test = 1;
      } else {
        X10.sendCmd(hcC, HEAT_LAMP, cmdOff);
        X10.sendCmd(hcC, LIGHTS, cmdOn);
        X10.sendCmd(hcC, HEAT_LAMP, cmdOff);
        X10.sendCmd(hcC, LIGHTS, cmdOn);
        test = 0;
      }
*/ 
   }

   delay(50); // Poll every 50ms
}

void checkTemperatures()
{
   String message = timeString.substring(0,5);
   message += ",";
   voltageValue = analogRead(voltagePin);
   //inputVoltage = (float (voltageValue * 40)) / 1024;
   inputVoltage = (float (voltageValue * 67)) / 2048;  // adjust for resistor values
   message += inputVoltage;
   message += ",";
   boardTemperature = ds.read(boardAddress);
   message += boardTemperature;
   message += ",";
   outdoorTemperature = ds.read(outdoorAddress);
   message += outdoorTemperature;
   message += ",";
   indoorTemperature = ds.read(indoorAddress);
   message += indoorTemperature;
   message += ",";
   message += wifiLevel;

   String file = dateString;
   file += ".csv";
   logEntry(file, message);
}

void getTime() {
   Process time;
   String tempString;
   boolean valid = false;
   
   tries = 0;
   while(!valid) {
     tries++;
      // get the time from the server:
      time.runShellCommand("date +\"%Y%m%d %T\"");
      //while(time.running());  
      tempString = "";
      while(time.available()) {
         char c = time.read();
         if(c != '\n')
            tempString += c;
      }
      dateString = tempString.substring(0, 4) + "-" + tempString.substring(4, 6) + "-" + tempString.substring(6,8);
      if(dateString != "2013-10-20") valid = true;
  }
   timeString = tempString.substring(9);
   minuteString = tempString.substring(12,14);

   minuteString.toCharArray(charBuf, 30);
   minute = atoi(charBuf);
}

void logEntry(String file, String message)
{
   String fileName;
   
   fileName = "/mnt/sd/data/greenhouse/";
   fileName += file;
   fileName.toCharArray(charBuf, 40);
   File dataFile = FileSystem.open(charBuf, FILE_APPEND);
   
   dataFile.println(message);
   dataFile.close();
}

void sendEmail(String subject, String message) {
  Process p;
  String dataString;

  dataString +="From: ";
  dataString +=GMAIL_USER_NAME;
  dataString +="\nTo: ";
  dataString +=TO_EMAIL_ADDRESS;
  dataString +="\nSubject: ";
  dataString +=subject;
  dataString +="\n\n";
  dataString +=message;
  dataString +="\n";
  File dataFile = FileSystem.open("/mnt/sd/data/mail.txt", FILE_WRITE);
  // if the file is available, write to it and send email
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
    dataString = "cat /mnt/sd/data/mail.txt | ssmtp ";
    dataString += TO_EMAIL_ADDRESS;
    dataString += "  2>&1";
    p.runShellCommand(dataString);
    while(p.running());  
  }  
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void checkwifistatus(void)
{
   wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal"); // grab just the signal data
   String strength = wifiCheck.readString();
   byte totallength = strength.length();
   strength = strength.substring(8, totallength-1);
   int level = strength.toInt();

   wifiLevel = level;

}

HTH,
Bjarne