View Single Post
Old 12-13-10, 03:38 AM   #18
strider3700
Master EcoRenovator
 
Join Date: Dec 2008
Location: Vancouver Island BC
Posts: 745
Thanks: 23
Thanked 37 Times in 30 Posts
Default

Ok I've spent too much time on this lately but it's now doing the following.

The arduino dumps back all of the sensor data every 2 seconds with a 30 millisecond delay between each.

The java program collects the data and every 60 seconds prints the average for each sensor to a csv file the clears the average so we're getting a minute by minute recording. The format going into the csv file is Date,Time,S1,S2.... with the first line being a Header which loads the "common name" for the sensor so in my case, office, Rec room. I don't know if there is a guarantee that s1 will always be office and s2 always rec room. The program will always put the values to the correct sensor name however and it won't flip part way through a run. Worst case you run the program and it lists them office, rec room and then you stop the program restart it and it will continue appending but flipped so recroom, office. The Header will be rewritten if you were to do that.... Common names are currently hardcoded. Long term none of that should matter because I want to move away from dumb csv's and into some form of database.

anyways right now it's running collecting from the two different rooms and in the morning I can generate a new chart, this one with labels and two lines... That's enough to keep me happy for awhile data wise so when I return to this project it will be to get current readings working.

Now the code if anyone is interested. It's still rough... The arduino is the same as last time I posted I believe.

Logger.java the new main class
Code:
package datalogger;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;

public class Logger {

	private static Date today;
	private static SimpleDateFormat  formatter = new SimpleDateFormat("yyyy/MM/dd,HH:mm");
	public static List<Sensor> sensorList=new ArrayList<Sensor>();
	private static SerialHandling handler ;
	private static Timer timer;
	public  static void main(String[] args) {
		loadSensorList();
		timer = new Timer();
		setTimer();
		
		handler  = new SerialHandling();
		handler.initialize();
		
	}
		
	
	private static void setTimer(){
		timer.scheduleAtFixedRate(
				new writeData(),  // calls the writeData class run method to write the data to csv
				0,  				// start right away
				60000             // run every 60 seconds
				//5000			  // run every 5 seconds for testing	
		);
	}
	public static void parseText(String line)
	{
		//System.out.println(line);
		if (line.contains(","))
		{
			String[] split = line.split(",");					
			String sensor = split[0];
			String value = split[1];

			averageValues(sensor,value);

			String name = getSensorName(sensor);					
			today = new Date();
			String time = formatter.format(today);
			//System.out.println(name+","+time+","+value);
			System.out.println(time+","+name+","+value);
			//writeText(time+" "+name+" "+ value+newline);
			//writeText(name+","+time+","+value+newline);
		}
	}
	// fills the List with the Sensors.  Currently hardcoded ID's
	public static void loadSensorList()	
	{		
		Sensor office = new Sensor("282895C50200009B","office");
		Sensor recroom = new Sensor("28BF90C50200007D","recroom");
		sensorList.add(office);
		sensorList.add(recroom);		
	}

	public static int getSensorCount()
	{
		return sensorList.size();
	}
	
	
	// find the sensor based on the ID
	private static Sensor getSensor(String inID)
	{
		int i =0;
		boolean done = false;
		Sensor temp = new Sensor("default","default");
		while ( i < sensorList.size() && done == false)
		{
			temp = sensorList.get(i);

			if (inID.equals(temp.getID()))
			{
				done = true;						
			}		
			i++;
		}
		return temp;
	}

	// returns the sensors common Name if in the list else the sensor ID is used as the name
	private static String getSensorName(String inID){

		String name = inID; //default to the ID if not found
		Sensor temp = getSensor(inID);				
		name = temp.getName();
		return name;
	}
	
	private static void averageValues(String id, String value){

		Sensor s = getSensor(id);
		try
		{
			float f = Float.valueOf(value.trim()).floatValue();
			s.addValueToAverage(f);
		}
		catch (NumberFormatException nfe)
		{
			System.out.println("NumberFormatException: " + nfe.getMessage());
		}

		//  System.out.println(id+" value "+value+" average "+s.returnAverage());

	}
}
WriteData.java - the timer that writes to the csv every minute
Code:
package datalogger;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;


// the Timer task the calls the outputting to the CSV file
public class writeData extends TimerTask {

	private static Date today;
	private static SimpleDateFormat  formatter = new SimpleDateFormat("yyyy/MM/dd,HH:mm");
	private boolean first = true;
	
	public void run() {
		//System.out.println("Generating csv");
		String row = buildRow();
		if (row.contains("NaN")==false) // done to stop that initial hit of empty from being written I could also delay 
		{								// the timer by 60 seconds but this always works no matter what the timer timing
			writeText(row);
		}		
	}
	
	//builds the row to output to CSV file. It hardcodes the header line in the first time called.   
	private String buildRow(){
		
		//row = Date,Time,s1,s2,...
		String row = "";
		// if the first line then dump in the header
		if (first == true)
		{
			row = "Date,Time";
			for (int i=0;i< Logger.getSensorCount() ;i++)
			{
				row+=","+Logger.sensorList.get(i).getName();
			}
			first = false;
			writeText(row);
			row ="";
		}
		
		today = new Date();
		String time = formatter.format(today);
		row += time;  // add the time to the row
		
		// now add all of the average values in the sensors
		for (int i=0;i< Logger.getSensorCount() ;i++)
		{
			
			row+=","+Logger.sensorList.get(i).returnAverage();	//get the average
			Logger.sensorList.get(i).resetValue();  // clear the averages
		}
		
		//System.out.println("writing -"+row);
		return row;
	}
	
	// for writing the text strings to the file	
	public synchronized void writeText(String inString){
		String strFilePath = "/home/strider/Logs/tempData.csv";
		String newline = System.getProperty("line.separator");
		try
		{
			Writer output = new BufferedWriter(new FileWriter(strFilePath,true));

			//FileWriter always assumes default encoding is OK!
			output.write( inString+newline);
			output.close();
		}
		catch(FileNotFoundException ex)
		{
			System.out.println("FileNotFoundException : " + ex);
		}
		catch(IOException ioe)
		{
			System.out.println("IOException : " + ioe);
		}
	}	
}
strider3700 is offline