mash/sensors.c

Sun, 27 Dec 2015 17:52:26 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 27 Dec 2015 17:52:26 +0100
changeset 477
9167ad4c2e77
parent 147
0e7c51a66b5e
permissions
-rw-r--r--

Renamed Mash-in step to Prepare on the display. Don't run the pump when the mash is added. When preparing the mash, first heat the HLT, and then the MLT so that both have the chance to reach their target temperatures.

/*****************************************************************************
 * 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 "mash.h"
#include "xutil.h"


#ifdef HAVE_WIRINGPI_H

/* wiringPi */
#include <wiringPi.h>
#include <pcf8574.h>
#include <lcd.h>

extern int		debug;
extern sys_config	Config;
extern int		my_shutdown;



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

    syslog(LOG_NOTICE, "Thread my_sensors_loop started");
    if (debug)
	fprintf(stdout, "Thread my_sensors_loop started\n");

    /*
     * Loop forever until the external shutdown variable is set.
     */
    for (;;) {
    	/*
    	 * Here send our 1-wire sensors values
    	 */
    	for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
	    old1 = tmp1->next;

	    if (my_shutdown) {
		syslog(LOG_NOTICE, "Thread my_sensors_loop stopped");
		if (debug)
		    fprintf(stdout, "Thread my_sensors_loop stopped\n");
	    	return 0;
	    }

	    /*
	     * Build path to the on-wire sensor
	     */
	    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, set flag.
			     */
			    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;
		    }
		} 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