coolers/sensors.c

Mon, 05 May 2014 23:33:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 05 May 2014 23:33:31 +0200
changeset 26
9322c619c525
child 27
4703cc10b99a
permissions
-rw-r--r--

Added coolers program, first draft

/*****************************************************************************
 * Copyright (C) 2014
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This 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, or (at your option) any
 * later version.
 *
 * mbsePi-apps 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 EC-65K; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "../lib/mbselib.h"
#include "sensors.h"

#ifdef HAVE_WIRINGPI_H


extern char		*myhostname;
extern bool		debug;
extern sys_config	Config;
extern int		lcdHandle;
extern int		lcdupdate;



void my_sensors_loop(void)
{
    w1_therm		*tmp1, *old1;
    char		*device, line[60], *p = NULL;
    FILE		*fp;
    int			temp, rc, deviation;

    /*
     * Here send our 1-wire sensors values
     */
    for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
	old1 = tmp1->next;

	/*
	 * Build path and alias topic
	 */
	device = xstrcpy((char *)"/sys/bus/w1/devices/");
	device = xstrcat(device, tmp1->master);
	device = xstrcat(device, (char *)"/");
	device = xstrcat(device, tmp1->name);
	device = xstrcat(device, (char *)"/w1_slave");

	/*
	 * Read sensor data
	 */
	if ((fp = fopen(device, "r"))) {
		    /*
		     * The output looks like:
		     * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
		     * 72 01 4b 46 7f ff 0e 10 57 t=23125
		     */
		    fgets(line, 50, fp);
		    line[strlen(line)-1] = '\0';
		    if ((line[36] == 'Y') && (line[37] == 'E')) {
			/*
			 * CRC is Ok, continue
			 */
		    	fgets(line, 50, fp);
			line[strlen(line)-1] = '\0';
			strtok(line, (char *)"=");
			p = strtok(NULL, (char *)"=");
			rc = sscanf(p, "%d", &temp);
			if ((rc == 1) && (tmp1->lastval != temp)) {
			    /*
			     * It is possible to have read errors or extreme values.
			     * This can happen with bad connections so we compare the
			     * value with the previous one. If the difference is too
			     * much, we don't send that value. That also means that if
			     * the next value is ok again, it will be marked invalid too.
			     * Maximum error is 20 degrees for now.
			     */
			    deviation = 20000;
			    if ((tmp1->lastval == 0) ||
				(tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) {
			    	/*
			    	 * Temperature is changed and valid, update and publish this.
			    	 */
				tmp1->update = TRUE;
			    } else {
				syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
				if (debug) {
				    fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
				}
			    }
			    tmp1->lastval = temp;
			    lcdupdate = TRUE;
			}
		    } else {
			syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
		    }
		    fclose(fp);
		    tmp1->present = 1;
	} else {
		    tmp1->present = 0;
		    if (debug)
		    	printf("sensor %s is missing\n", tmp1->name);
	}

	free(device);
	device = NULL;
    }
}



#endif

mercurial