Energy monitor -- code quality


i'm looking little feed on sketch.  basically i'm using ir photo-transistor listen test pulses electric smart meter sends out every 2 w hr.  time between pulses sent out serial interface visual basic program, passes data onto rrdtool.  since arduino wasn't busy added on rtc, sdcard, , ds18b20's.

overall works ok.  i have memory problems.  the queuearray library i'm using buffer pulse timing, hogging memory anytime had resize it's array.  i've fixed increasing default size in library.

the rtc , ds18b20 libraries annoyed me.  it seems had kind of compromise.  i wanted time library it's unix timestamp function, couldn't set clock properly.  kept coming several hours off.  the ds1307new library okay, looks doing alot of work need.  

i still need write function set rtc time.  need kind of rolling log file on sdcard, takes long time dump @ 115200 bps.  need able dump data given time period.  my goal arduino self-sufficient.  dump data processing when asked.  right have keep desktop running, goes against whole motivation of project--saving energy.  need figure out clean way of dropping first 2 ir samples.  the first sample inaccurate.

looking tips , pointers.  not c++ guy.  any different strategies or techniques should consider.  i haven't had arduino less month.  really amazed @ it's flexibility, , how easy is.  thanx.



code: [select]
//1wire
#include <onewire.h>
#include <dallastemperature.h>

// data wire plugged port 3 on arduino
#define one_wire_bus 3
#define temperature_precision 9

// setup onewire instance communicate onewire devices (not maxim/dallas temperature ics)
onewire onewire(one_wire_bus);

// pass our onewire reference dallas temperature.
dallastemperature sensors(&onewire);

// arrays hold device addresses
deviceaddress waterheater={0x28,0x4e,0x57,0x3f,0x03,0x0,0x0,0x8a};
deviceaddress room={0x28,0x95,0x5e,0x3f,0x03,0x00,0x00,0x45};

boolean onewirepresent=false;

//real time clock
#include <wire.h>
#include <ds1307new.h>
boolean rtcpresent=false;

//sd card
#include <sd.h>
const int chipselect=10;
boolean sdcardpresent=false;

//fifo queue
#include <queuearray.h>
queuearray <unsigned int> queue;

boolean serialout=true;

const int irsensor=2;

void setup(){
 //initialize serial port @ 9600 baud
 serial.begin(115200);

 //init sd
 pinmode(chipselect,output);
 if(sd.begin(chipselect)){
   sdcardpresent=true;
 }
 
 //init rtc -- set sqw pin 1 hz flash led
 if(rtc.ispresent()){
   rtc.ctrl=0x10;
   rtc.setctrl();
   rtcpresent=true;
 }

 //init 1wire
 sensors.begin();

 //make sure there 2 sensors
 if(sensors.getdevicecount()==2){
   onewirepresent=true;
   sensors.setresolution(waterheater,temperature_precision);
   sensors.setresolution(room,temperature_precision);
   sensors.setwaitforconversion(true);
 }

 //enable external interrupt of digital i/o pin 2
 pinmode(irsensor,input);
 //turn on internal pull-up
 digitalwrite(irsensor,high);
 attachinterrupt(0,timepulse,falling);
}

void timepulse(){
 //push current millis queue
 queue.push(millis());
}

void loop() {
 if(queue.count()>1){
   //calculate instant power.
   unsigned int oldtime=queue.pop();
   unsigned int newtime=queue.peek();
   float currentw=(float)7200000/(newtime-oldtime);

   //send out serial 1 digit 00.0
   if(serialout){
     serial.print("w:");
     serial.println(currentw,1);
   }
   
   // if sd card available, log current wattage
   if(sdcardpresent){
     file datafile=sd.open("watt.txt",file_write);
     if(datafile){
       if(rtcpresent){
         rtc.gettime();
         datafile.print(rtc.time2000);
         datafile.print(":");
       }
       datafile.println(currentw);
       datafile.close();
     }
   }
 }
}

//handle incoming serial
void serialevent(){
 while (serial.available()){
   //incoming commands begin !
   if(serial.read()=='!'){
     //delay, otherwise missing next character, why!?!?
     delay(1);
     switch(serial.read()){
       case 's':
         serialout = false;
         break;
       case 's':
         serialout = true;
         break;
       case 'd':{
         //dump log file
         file datafile=sd.open("watt.txt",file_read);
         if(datafile){
           while(datafile.available()){
             serial.write(datafile.read());
           }
           datafile.close();
         }
         break;
         }
       case 't':
         //print current time
         rtc.gettime();
         serial.println(rtc.time2000);
         serial.print(rtc.month);
         serial.print("-");
         serial.print(rtc.day);
         serial.print("-");
         serial.print(rtc.year);
         serial.print(" ");
         serial.print(rtc.hour);
         serial.print(":");
         serial.print(rtc.minute);
         serial.print(":");
         serial.println(rtc.second);
         break;
       case 't':
         //set rtc time
         break;
       case 'w':
         if(onewirepresent){
           sensors.requesttemperatures();
           float tempc=sensors.gettempc(waterheater);
           serial.print("wh:");
           serial.println(dallastemperature::tofahrenheit(tempc));
         }
         break;
       case 'r':
         if(onewirepresent){
           sensors.requesttemperatures(); //request addr not working reason
           float tempc=sensors.gettempc(room);
           serial.print("r:");
           serial.println(dallastemperature::tofahrenheit(tempc));
         }
         break;
     }
   }
 }
}


moderator edit: added [font=verdana][[/font]code] [font=verdana][[/font]/code] tags

generated graphs @ http://kmitchel.dyndns.org/rrdtool

kind of jumbled mess.  want dynamically generated graphs going.  long term graphs problem.  can't seem clean usable graphs, messy tell going on.  easy tell when refrigerator cycles on , off, a/c and   water heater.  plasma tv adds alot of jitter; it's power consumption relative content being displayed. 

water heater sensor needs bonded tank better.  it's shoved between tank , insulation through service access.  screwed ordering 2 ds18b20's.  want sensor on each heating element.  can think of half dozen places want them.

hacking water meter on water softner next on list.  hoping can without optocoupler. 


Arduino Forum > Using Arduino > Programming Questions > Energy monitor -- code quality


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial