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)

No comments: