HOBO driver

OnSet Computer Corporation refused to give out information needed to use their product HOBO H8 Temp logger. This made impossible for me to use it in my server room monitoring scripts under Linux. I decided to save investment and together with friend Andrei Errapart wrote Linux code reading out temperature from it. You (including OnSet Computer Corporation) are free to use it under terms of General Public Licence.

/*

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


=======================================================================

This program reads out current temperature from HOBO Temp made by
OnSet Computer Corporation. Example of usage:


-----------------------------------------------------------------------
tonu@linux:~> gcc hobo.c -o hobo
tonu@linux:~> ./hobo
H**Ovision version 69, Copyright (C) 2002, Tõnu Samuel & Andrei Errapart
H**Ovision comes with ABSOLUTELY NO WARRANTY

New versions MAY appear on http://no.spam.ee/~tonu/ and http://no.spam.ee/~andreie/

Send beer and postcards to:
        Vabaduse 15
        Rapina 64506
        Estonia
Reading data...
25.67 Celsius, 78.19 Fahrenheit
-----------------------------------------------------------------------

OnSet Computer Corporation propietary protocol description:

1200 baud, 8N1

E - Copyright string
C - Read out current temperature
H - Hangup / Reset
D - Handshake ?

=======================================================================
From:|  Paul Borsari 
To:|    tonu@please.do.not.remove.this.spam.ee
Subject:|       Re: Support question
Date:|  11 Oct 2002 08:33:09 -0400
Hello Tonu,

The communication protocol is the proprietary property of Onset Computer
Corporation, and is not available to the public.|  I have included a blurb
below, to which you may have already known.|  If I can be of further
assistance, please feel free to contact me.

Best regards,

Paul Borsari
Technical Support Representative
Onset Computer Corporation
(508) 759-9500 ext. 155
(800) 564-4377 ext. 155
We don't have a Linux version of BoxCar Pro available. But, some folks
have used RedHat Linux 5.2 and an emulator to successfully run BoxCar Pro
called WINE. This is a freeware program that can be found at
http://www.winehq.com/. I would also consult the manufacturer of your
Linux system to see if an emulator is available. Also, LogUtil, version
3.5, is a DOS utility program that performs many of the functions of
BoxCar Pro for Windows, such as launch, readout and export which will
operate all of the HOBO and StowAway products. This can be downloaded for
free at our web site,
http://www.onsetcomp.com/Support/HS_Support/2279_hsfiles.html.


tonu@please.do.not.remove.this.spam.ee writes:
>Hi!
>
>got this addres from Frank Gibbons, Microdaq.com.
>
>I had a question about "HOBO Temp" product. I am experienced programmer
>and need to write Linux driver for this product. But I was not able to
>find any technical information about protocol used in this product nor
>identofy it by chipsets used. Can you provide me technical details about
>this?
>
>|  Tõnu
>+372 50 41 396
>
>

=======================================================================

*/
#include 
#include 
#include 
#include 
#include 
#include 
int
main()
{
    unsigned char buffer[255];				   /* Input buffer */
    unsigned char *bufptr;				   /* Current char in buffer */

    int nbytes;						   /* Number of bytes read */
    int fd = 0;
    struct termios options;

    printf
	("H**Ovision version 69, Copyright (C) 2002, Tõnu Samuel & Andrei Errapart\n");
    printf("H**Ovision comes with ABSOLUTELY NO WARRANTY\n");
    printf
	("New versions MAY appear on http://no.spam.ee/~tonu/ and http://no.spam.ee/~andreie/ \n");
    printf("Send beer and postcards to:\n");
    printf("   Vabaduse 15\n   Rapina 64506\n   Rapina 64506\n   Estonia\n");
    printf("Reading data...\n");

/* Absolutely no warranty as you see...
*/
    fd = open("/dev/ttyS0", O_RDWR);

/* ..amd error checking either */


/*
 * Get the current options for the port...
 */

    tcgetattr(fd, &options);

/*
 * Set the baud rates to 1200...
 */

    cfsetispeed(&options, B1200);
    cfsetospeed(&options, B1200);

/*
 * Enable the receiver and set local mode...
 */

    options.c_cflag |= (CLOCAL | CREAD);


    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);



    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    options.c_cflag &= ~CRTSCTS;


/*
 * Set the new options for the port...
 */
    tcsetattr(fd, TCSANOW, &options);
    while (1) {
	int counter = 0;
	write(fd, "D", 1);
	sleep(2);
	/* send an AT command followed by a CR */
	write(fd, "C", 1);

	/* read characters into our string buffer until we get a CR or NL */
	bufptr = buffer;
	while ((nbytes =
		read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0) {
	    int i;
	    for (i = 0; i < nbytes; ++i) {
/*            printf ("--> %u %c guess - %.2f\n", bufptr[i], bufptr[i],
                      (192 - bufptr[i]) * 0.4);*/
		if (bufptr[i] == 255)
		    ++counter;
	    };
	    bufptr += nbytes;
	    if (counter >= 2) {
		if (buffer[1] == 255 && buffer[7] == 255) {
		    printf("%.2f Celsius, %.2f Fahrenheit\n", (192 - buffer[2]) * 0.388888888889, (237.7 - buffer[2]) * 0.7);

		}
		break;
	    }
	}
	sleep(1);
    }
    return (0);
}