Coolers working with a hardcode reference

Tue, 06 May 2014 21:37:06 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 06 May 2014 21:37:06 +0200
changeset 28
32ed1ea4d0b6
parent 27
4703cc10b99a
child 29
ac763b87ee25

Coolers working with a hardcode reference

coolers/coolers.c file | annotate | diff | comparison | revisions
coolers/mosquitto.c file | annotate | diff | comparison | revisions
coolers/mosquitto.h file | annotate | diff | comparison | revisions
lib/lcd-pcf8574.c file | annotate | diff | comparison | revisions
lib/mbselib.h file | annotate | diff | comparison | revisions
lib/rc-switch.c file | annotate | diff | comparison | revisions
thermometers/main.c file | annotate | diff | comparison | revisions
--- 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;
 }
--- a/coolers/mosquitto.c	Tue May 06 13:24:25 2014 +0200
+++ b/coolers/mosquitto.c	Tue May 06 21:37:06 2014 +0200
@@ -21,7 +21,7 @@
  *****************************************************************************/
 
 #include "../lib/mbselib.h"
-#include "coolers.h"
+#include "mosquitto.h"
 
 #ifdef HAVE_WIRINGPI_H
 
@@ -128,7 +128,7 @@
     }
 
     mymosq = mosquitto_new(id, true, NULL);
-    if(!mymosq) {
+    if (!mymosq) {
 	switch(errno) {
 	    case ENOMEM:
 		syslog(LOG_NOTICE, "mosquitto_new: Out of memory");
@@ -228,11 +228,12 @@
 	    if (rc == MOSQ_ERR_NO_CONN)
 		mosquitto_reconnect(mymosq);
 	    else
-		syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
+		syslog(LOG_NOTICE, "my_mosquitto_init: error %d from mosquitto_publish", rc);
 	}
 
 	free(alias);
 	alias = NULL;
+	my_mosquitto_switch(tmp2->address, 0);
     }
 
     return 0;
@@ -240,6 +241,44 @@
 
 
 
+int my_mosquitto_switch(char *address, int state)
+{
+    char    *cmd = NULL, buf[10];
+    int	    rc;
+
+    cmd = xstrcpy(address);
+    if (state)
+	cmd = xstrcat(cmd, (char *)",1");
+    else
+        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 = xstrcpy((char *)"/raw/");
+    cmd = xstrcat(cmd, myhostname);
+    cmd = xstrcat(cmd, (char *)"/coolers/rcswitch/");
+    cmd = xstrcat(cmd, address);
+    cmd = xstrcat(cmd, (char *)"/state");
+    sprintf(buf, "%d", state);
+
+    if ((rc = mosquitto_publish(mymosq, &mid_sent, cmd, strlen(buf), buf, qos, 1))) {
+	if (rc == MOSQ_ERR_NO_CONN)
+	    mosquitto_reconnect(mymosq);
+	else
+	    syslog(LOG_NOTICE, "my_mosquitto_switch: error %d from mosquitto_publish", rc);
+    }
+
+    free(cmd);
+    cmd = NULL;
+
+    return rc;
+}
+
+
+
 int my_mosquitto_loop(void)
 {
     w1_therm		*tmp1, *old1;
@@ -253,20 +292,20 @@
 	    for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
 		old1 = tmp1->next;
 
-		/*
-		 * Build path and alias topic
-		 */
-		alias = xstrcpy((char *)"/raw/");
-		alias = xstrcat(alias, myhostname);
-		alias = xstrcat(alias, (char *)"/coolers/w1/");
-		alias = xstrcat(alias, tmp1->master);
-		alias = xstrcat(alias, (char *)"/");
-		alias = xstrcat(alias, tmp1->name);
-		alias = xstrcat(alias, (char *)"/temperature");
-
 		if (tmp1->update) {
 		    /*
-		     * Temperature is changed and valid, update and publish this.
+		     * Build path and alias topic
+		     */
+		    alias = xstrcpy((char *)"/raw/");
+		    alias = xstrcat(alias, myhostname);
+		    alias = xstrcat(alias, (char *)"/coolers/w1/");
+		    alias = xstrcat(alias, tmp1->master);
+		    alias = xstrcat(alias, (char *)"/");
+		    alias = xstrcat(alias, tmp1->name);
+		    alias = xstrcat(alias, (char *)"/temperature");
+
+		    /*
+		     * Publish the temperature.
 		     */
 		    sprintf(buf, "%.1f", tmp1->lastval / 1000.0);
 		    if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
@@ -277,10 +316,10 @@
 		    }
 		    tmp1->update = FALSE;
 		    lcdupdate = TRUE;
-		} 
 
-		free(alias);
-		alias = NULL;
+		    free(alias);
+		    alias = NULL;
+		}
 	    }
 
 	    if (shutdown) {
@@ -296,8 +335,6 @@
 		lcdPuts(lcdHandle, "Shuting down ...");
 	    }
 
-	    usleep(100000);
-
 	} else if (status == STATUS_WAITING) {
 	    if (debug)
 	    	fprintf(stdout, (char *)"Waiting\n");
@@ -305,7 +342,6 @@
 		mosquitto_disconnect(mymosq);
 		disconnect_sent = true;
 	    }
-	    usleep(100000);
 	}
 	rc = MOSQ_ERR_SUCCESS;
 
--- a/coolers/mosquitto.h	Tue May 06 13:24:25 2014 +0200
+++ b/coolers/mosquitto.h	Tue May 06 21:37:06 2014 +0200
@@ -1,8 +1,13 @@
 #ifndef	MY_MOSQUITTO_H
 #define MY_MOSQUITTO_H
 
-void my_mosquitto_init(void);
+#ifdef HAVE_WIRINGPI_H
+
+int  my_mosquitto_switch(char *, int);
+int  my_mosquitto_init(void);
 int  my_mosquitto_loop(void);
 void my_mosquitto_exit(void);
 
 #endif
+
+#endif
--- a/lib/lcd-pcf8574.c	Tue May 06 13:24:25 2014 +0200
+++ b/lib/lcd-pcf8574.c	Tue May 06 21:37:06 2014 +0200
@@ -63,7 +63,6 @@
     return EXIT_FAILURE ;
   }
 
-  wiringPiSetupSys () ;
   pcf8574Setup (AF_BASE, 0x27) ;
 
   setBacklight (1) ;
--- a/lib/mbselib.h	Tue May 06 13:24:25 2014 +0200
+++ b/lib/mbselib.h	Tue May 06 21:37:06 2014 +0200
@@ -119,11 +119,11 @@
 #define PROTOCOL3_1_HIGH_CYCLES  9
 #define PROTOCOL3_1_LOW_CYCLES   6
 
-
-void toggleTypeA(char *, char *, bool);
-void toggleTypeB(int, int, bool);
-void toggleTypeC(char, int, int, bool);
-void toggleTypeE(char, int, bool);
+int  toggleSwitch(char *);
+int  toggleTypeA(char *, char *, bool);
+int  toggleTypeB(int, int, bool);
+int  toggleTypeC(char, int, int, bool);
+int  toggleTypeE(char, int, bool);
 
 void enableReceiveIRQ(int interrupt);
 void enableReceive(void);
--- a/lib/rc-switch.c	Tue May 06 13:24:25 2014 +0200
+++ b/lib/rc-switch.c	Tue May 06 21:37:06 2014 +0200
@@ -148,14 +148,54 @@
 
 
 /*
+ * Toggle switch, a command looks like B,3,2,1 which means switch type B,
+ * group 3, device 2, status on.
+ */
+int toggleSwitch(char *command)
+{
+    static char	*cmd = NULL;
+    char    *s, cType;
+    int	    rc, iGroup, iDevice, iState;
+
+    cmd = xstrcpy(command);
+    s = strtok(cmd, ",\0");
+    cType = s[0];
+
+    if (cType == 'A') {
+
+    } else if (cType == 'B') {
+	s = strtok(NULL, ",\0");
+	rc = sscanf(s, "%d", &iGroup);
+	if (rc != 1)
+	    return 1;
+	s = strtok(NULL, ",\0");
+	rc = sscanf(s, "%d", &iDevice);
+	if (rc != 1)
+	    return 1;
+	s = strtok(NULL, ",\0");
+	rc = sscanf(s, "%d", &iState);
+	if (rc != 1)
+	    return 1;
+	free(cmd);
+	return toggleTypeB(iGroup, iDevice, iState);
+    }
+
+    free(cmd);
+    return 1;
+}
+
+
+
+/*
  * Switch a remote switch on (Type E REV)
  *
  * @param sGroup   Code of the switch group (A,B,C,D)
  * @param nDevice  Number of the switch itself (1..3)
  * @param bStatus  Status to toggle to
  */
-void toggleTypeE(char sGroup, int nDevice, bool bStatus) {
+int toggleTypeE(char sGroup, int nDevice, bool bStatus) {
     sendTriState( getCodeWordE(sGroup, nDevice, bStatus) );
+    return 0;
 }
 
 
@@ -168,8 +208,9 @@
  * @param nDevice  Number of device (1..4)
  * @param bStatus  Status to toggle to
  */
-void toggleTypeC(char sFamily, int nGroup, int nDevice, bool bStatus) {
+int toggleTypeC(char sFamily, int nGroup, int nDevice, bool bStatus) {
     sendTriState( getCodeWordC(sFamily, nGroup, nDevice, bStatus) );
+    return 0;
 }
 
 
@@ -181,14 +222,18 @@
  * @param iDevice   Number of the switch itself (1..4)
  * @param bStatus   Status to toggle to
  */
-void toggleTypeB(int iGroup, int iDevice, bool bStatus)
+int toggleTypeB(int iGroup, int iDevice, bool bStatus)
 {
     char  *str = xstrcpy(getCodeWordB(iGroup, iDevice, bStatus));
 
+    if (strlen(str) == 0)
+	return 1;
+
 //    save(TYPE_B);
     sendTriState( str );
 //    load();
     free(str);
+    return 0;
 }
 
 
@@ -200,8 +245,9 @@
  * @param sDevice   Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")
  * @param bStatus   Status to toggle to
  */
-void toggleTypeA(char* sGroup, char* sDevice, bool bStatus) {
+int toggleTypeA(char* sGroup, char* sDevice, bool bStatus) {
     sendTriState( getCodeWordA(sGroup, sDevice, bStatus) );
+    return 0;
 }
 
 
--- a/thermometers/main.c	Tue May 06 13:24:25 2014 +0200
+++ b/thermometers/main.c	Tue May 06 21:37:06 2014 +0200
@@ -185,6 +185,10 @@
     }
 
 #ifdef HAVE_WIRINGPI_H
+
+    if (wiringPiSetup () )
+	return 1;
+
     if ((rc = initLCD (16, 2))) {
 	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
 	return 1;

mercurial