coolers/coolers.c

changeset 28
32ed1ea4d0b6
parent 27
4703cc10b99a
child 29
ac763b87ee25
--- a/coolers/coolers.c	Tue May 06 13:24:25 2014 +0200
+++ b/coolers/coolers.c	Tue May 06 21:37:06 2014 +0200
@@ -28,6 +28,11 @@
 #ifdef HAVE_WIRINGPI_H
 
 
+int			tempA = 240;
+int			tempB = 250;
+int			coolerA = 0;
+int			coolerB = 0;
+
 bool			shutdown = false;
 static pid_t		pgrp, mypid;
 
@@ -76,6 +81,27 @@
 
 
 
+void stopRCswitch(void)
+{
+    rc_switch   *tmp, *old;
+    char	*cmd = NULL;
+    int		rc;
+
+    for (tmp = Config.rcswitch; tmp; tmp = old) {
+	old = tmp->next;
+	cmd = xstrcpy(tmp->address);
+	cmd = xstrcat(cmd, (char *)",0");
+	rc = toggleSwitch(cmd);
+	if (debug)
+	    fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
+	syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
+	free(cmd);
+	cmd = NULL;
+    }
+}
+
+
+
 int main(int argc, char *argv[])
 {
     int		rc, c, i;
@@ -123,6 +149,9 @@
 	    signal(i, (void (*))die);
     }
 
+    if (wiringPiSetup () )
+	return 1;
+
     if ((rc = initLCD (16, 2))) {
 	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
 	return 1;
@@ -134,6 +163,13 @@
     sprintf(buf, "Version %s", VERSION);
     lcdPuts(lcdHandle, buf);
 
+    if (Config.tx433 != -1) {
+	if (debug)
+	    fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433);
+	syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433);
+    	enableTransmit(Config.tx433);
+    }
+
     if (debug) {
 	/*
 	 * For debugging run in foreground.
@@ -202,16 +238,15 @@
 {
     char                buf[1024];
     w1_therm		*tmp1, *old1;
+    rc_switch		*tmp2, *old2;
     int			rc, run = 0;
 
     my_mosquitto_init();
 
-    rc = piThreadCreate (my_sensors_loop);
+    rc = piThreadCreate(my_sensors_loop);
     if (rc) {
-	fprintf(stderr, "my_sensors_loop thread didn't start\n");
-    } else {
-	if (debug)
-	    fprintf(stdout, "Thread my_sensors_loop started\n");
+	fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
+	syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc);
     }
 
     do {
@@ -219,25 +254,66 @@
 
 	run = my_mosquitto_loop();
 
+	tmp1 = Config.w1therms;
+	tmp2 = Config.rcswitch;
+	if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
+	    my_mosquitto_switch(tmp2->address, 0);
+	    coolerA = 0;
+	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
+	}
+	if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
+	    my_mosquitto_switch(tmp2->address, 1);
+	    coolerA = 1;
+	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
+	}
+	old1 = tmp1->next;
+	tmp1 = old1;
+	old2 = tmp2->next;
+	tmp2 = old2;
+	if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
+	    my_mosquitto_switch(tmp2->address, 0);
+	    coolerB = 0;
+	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
+	}
+	if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
+	    my_mosquitto_switch(tmp2->address, 1);
+	    coolerB = 1;
+	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
+	}
+
 	if (run && lcdupdate) {
 	    lcdPosition(lcdHandle, 0, 0);
 	    tmp1 = Config.w1therms;
-	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
+	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias);
 	    lcdPuts(lcdHandle, buf);
 	    old1 = tmp1->next;
 	    tmp1 = old1;
 	    lcdPosition(lcdHandle, 0, 1);
-	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
+	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '+' : ' ', tmp1->alias);
 	    lcdPuts(lcdHandle, buf);
 	}
+	usleep(100000);
 
     } while (run);
 
     if (debug)
 	fprintf(stdout, (char *)"Out of loop\n");
 
+    /*
+     * Give threads time to cleanup
+     */
+    usleep(1500000);
+
+    if (Config.tx433 != -1) {
+	stopRCswitch();
+    }
+
     my_mosquitto_exit();
     stopLCD();
+    disableTransmit();
+
+    if (debug)
+	fprintf(stdout, "Goodbye\n");
 
     return 0;
 }

mercurial