Sunday, April 26, 2009

Doorbell and meters code

Just before I update the code to include the power orb, which, by the way is already mounted on its place, I decided to put here the current code...since I am not great a keeping versions.
Once I setup a cvs server at my computer, but that was z
years ago when I did some serious software development under Linux. Now...I didn't yet have the patience to do it, so I thought blogger could be my repository. So here is the code (hope that this time it comes out better than the previous time - I am using code labels to get it fine):


--- Update: code removed...I don't know which version it was, but it was definitely not the right one...I really should do some configuration management...

Wednesday, April 22, 2009

Progress in the power orb front

So, if you have been following this blog (not likely I know!) you should be waiting for the ambient orb, or power orb as I just renamed it, to materialize. So today, after watering the plants and practicing the music, I decided to progress with it.
So I made two little breakout boards (is this what they are called?). One for the RGB LED and one for the button that will serve to reset the door bell indicator. Here they are in all their glory:



I like to use telephone cable, as it has plenty of conductors (5).

Now I will place it in the wall of the meters cabinet, from the inside, wire it up to the arduino that is doing the measurements, and hope that it will all go according to plan!

I'll try to do it quickly so that you don't have to wait long!!

Sunday, April 12, 2009

System reboot

I have been thinking of rebooting the system for a long while now. This morning I found out that the Arduino that is doing the data acquisition from the meters stopped registering yesterday evening, around 9 o'clock. I wonder if there is something in the Arduino internals that prevent it from running more than 209 days (give or take)...I once read something about the millis, but I thought that was solved...a quick google didn't come up with anything...

So, after lunch I decided it was time to do the feared task of rebooting. Usually nothing should go wrong, but with computers we never know, and sometimes the disks get confused, so I was a bit worried.

So I got over all the fears, and typed halt. The system halted. After a few seconds with the power off, I plugged it in again (I have no reset or power button - the system wakes up alone when power is restored). It booted, taking a long time to check the hard disk, since it had been more than 230 days without checking, but after all that , it came to life, as if nothing had happened. All systems up.

I ran the newly adapted program to listem to the Arduino, which includes a signal handler to take care of properly closing the USB port when a Ctrl-C shows up, and it also ran. Including the handling of the Ctrl-C. So now I can update the Arduino software without having to restart the server!

Here is the code to listen to the Arduino. Maybe I should consider running the Arduino at a higher speed. I will also now be able to install my ambient orb with energy consumption indication.
One more note, I copied some of the code below from 2 sources, one about connecting to MySQL, the other about catching a signal. I can't remember where from, I'm not very good at keeping records, but I think it must have been the online manual of MySQL and the sig man pages...


/* Simple C program that connects to MySQL Database server*/
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

int keep_looping = TRUE;

void sigfun(int sig)
{
printf("You have pressed Ctrl-C , aborting!");
keep_looping = FALSE;
}

int put_into_database(char *type_of_meter) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *server = "";
char *user = "";
char *password = ""; /* set me first */
char *database = "DATABASE NAME";
char *query;

conn = mysql_init(NULL);

/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return(1);
}

/* send SQL query */
if (mysql_query(conn, "use DATABASE NAME")) {
fprintf(stderr, "%s\n", mysql_error(conn));
return(1);
}
/* send SQL query */

if (type_of_meter == "e")
{
query = "insert into electricity values()";
}
if (type_of_meter == "g")
{
query = "insert into gas values()";
}
if (type_of_meter == "B")
{
query = "insert into door_bell values()";
}

if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return(1);
}


mysql_close(conn);
}

int main(int argc, char *argv[]) {
{
int fd,c, res;

struct termios oldtio,newtio;
char buf[255];

if(argc != 2)
{
printf("Incorrect number of args: usage arduinomonitor
e.g. /dev/ttyUSB0\n");
return(-1);
}

printf("Installing signal handler...");
(void) signal(SIGINT, sigfun);
printf("done\n");

printf("connecting to port %s\n", argv[1]);

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 chars received */

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

while (keep_looping) { /* loop for input */
res = read(fd,buf,255); /* returns after 1 chars have been input */
buf[res]=0; /* so we can printf... */

if (buf[0]=='e')//electricity pulse
{
printf("elec\n");
put_into_database("e");
}else
if (buf[0]=='g')//gas pulse
{
printf("gas\n");
put_into_database("g");
}else
if (buf[0]=='B')//door bell on pulse
{
printf("Door bell\n");
put_into_database("B");
}else
if (buf[0]=='b')//door bell off pulse
{
printf("Door bell off\n");
//put_into_database("B");
}else
{
printf("other\n");
}
}
tcsetattr(fd,TCSANOW,&oldtio);
return (0);
}
}



and of course, the command to compile it:

gcc -o arduinomonitor $(mysql_config --cflags) arduinomonitor.c $(mysql_config --libs)

Saturday, April 11, 2009

Boiler on, boiler off...

After having switched off the boiler last week, I found myself having to turn it on again, since, as usual, there is no way you can trust the dutch weather to evolve in one (logical) direction, so after a very nice last weekend, it turned cold again, so I had to turn the boiler on.
And again off, yesterday, so it was only 3 or 4 days extra to all those statistics. I hope this time it is for good.
Since also soon we'll be getting a new boiler, which shouldn't have the pilot light, and as such does not need to be turned on and off. I hope I can keep my computer control for the new boiler. It would be great to be able to interface with it via the OpenTherm or similar protocol, but, as far as I know, it is not yet possible...I'll keep googling for it!

Here is a graphic of the gas consumption:


And enjoy the good weather!!

Saturday, April 4, 2009

Boiler off!!!

Yesterday, 3rd of April we had an almost tropical day here! so I decided to turn the boiler off, thereby ending the heating season...from now on, if the cold comes it is to be fought with blankets.

so, for the statistics:

6 Months of heating - from the 4th of October to the 3rd of March.

1640.81 m3 of gas for the winter, 712.71 m3 in 2008, 928.10 m3 in 2009, it was a pretty cold winter with plenty of ice! At about 60 cents per cubic meter, that is about 985 euro.

A total of 23464 minutes that the boiler was burning gas, or 391 hours, or 16 and a bit days, that in a total of 181 days, which makes it that the boiler was on for 9% of the time. Doesn't look like much does it...in the coldest month, January, that total was 14% of the time, in October it was a mere 2.5%, and in the current April 0.4%. Maybe I could derive an indicator to tell me when to switch it off based on the usage.

This total of minutes leads to 0.07 m3 per minute. Although this last number is not very reliable, since we also used some of that gas to take showers and cook...I should try to calculate the baseline cooking/showering gas consumption to be able to derive this number more accurately.

And all this heating was achieved with the server being up for 201 days, non-stop!

The doorbell rang a total of 99 times. A number of them (not many) it was me testing and making sure the battery is still working - should have made an indicator - an another (quite a few) number of them was a TV crew that came to shoot a program with my wife's research, and they had to film the presenter ringing the bell, which led to almost 20 rings, that added to the fact that they were trying to hear the bell ringing (which it didn't at the time!)

For some more statistics, there were yesterday, when I switched off the boiler, 46 daffodils flowering around the house! I'll try to keep track of how many blackbirds will be born this year on the garden!

For the coming times, I'll try to do some statistical analysis on all this data to derive occupancy rates, or in other words, to see statistically how often are we home on a certain weekday. Maybe I could adapt the heating with a neural network to heat according to the probability of us being home that day. That is the future!