thermferm/thermferm.c

changeset 545
c382a6c58c20
parent 544
e2889b17f48c
child 547
0e4d4b45249f
equal deleted inserted replaced
544:e2889b17f48c 545:c382a6c58c20
993 993
994 if (lockprog((char *)"thermferm")) { 994 if (lockprog((char *)"thermferm")) {
995 syslog(LOG_NOTICE, "Can't lock"); 995 syslog(LOG_NOTICE, "Can't lock");
996 return 1; 996 return 1;
997 } 997 }
998 998 #ifdef HAVE_MOSQUITTO_H
999 mqtt_connect(); 999 mqtt_connect();
1000 #endif
1000 1001
1001 if ((rc = devices_detect())) { 1002 if ((rc = devices_detect())) {
1002 syslog(LOG_NOTICE, "Detected %d new devices", rc); 1003 syslog(LOG_NOTICE, "Detected %d new devices", rc);
1003 wrconfig(); 1004 wrconfig();
1004 } 1005 }
1161 #endif 1162 #endif
1162 lcd_buf_write(row, "Room temp N/A "); 1163 lcd_buf_write(row, "Room temp N/A ");
1163 #ifdef HAVE_WIRINGPI_H 1164 #ifdef HAVE_WIRINGPI_H
1164 piUnlock(LOCK_LCD); 1165 piUnlock(LOCK_LCD);
1165 #endif 1166 #endif
1166 bool updateHT = false; 1167 int updateHT = 0;
1167 if (Config.temp_address) { 1168 if (Config.temp_address) {
1168 rc = device_in(Config.temp_address, &temp); 1169 rc = device_in(Config.temp_address, &temp);
1169 if (rc == DEVPRESENT_YES) { 1170 if (rc == DEVPRESENT_YES) {
1170 if (Config.temp_value != temp) 1171 if (Config.temp_value != temp)
1171 updateHT = true; 1172 updateHT = 1;
1172 Config.temp_value = temp; 1173 Config.temp_value = temp;
1173 Config.temp_state = 0; 1174 Config.temp_state = 0;
1174 #ifdef HAVE_WIRINGPI_H 1175 #ifdef HAVE_WIRINGPI_H
1175 piLock(LOCK_LCD); 1176 piLock(LOCK_LCD);
1176 #endif 1177 #endif
1196 1197
1197 if (Config.hum_address) { 1198 if (Config.hum_address) {
1198 rc = device_in(Config.hum_address, &temp); 1199 rc = device_in(Config.hum_address, &temp);
1199 if (rc == DEVPRESENT_YES) { 1200 if (rc == DEVPRESENT_YES) {
1200 if (Config.hum_value != temp) 1201 if (Config.hum_value != temp)
1201 updateHT = true; 1202 updateHT = 1;
1202 Config.hum_value = temp; 1203 Config.hum_value = temp;
1203 Config.hum_state = 0; 1204 Config.hum_state = 0;
1204 #ifdef HAVE_WIRINGPI_H 1205 #ifdef HAVE_WIRINGPI_H
1205 piLock(LOCK_LCD); 1206 piLock(LOCK_LCD);
1206 #endif 1207 #endif
1218 /* 1219 /*
1219 * If TH(B) changed. or if 5 minutes without 1220 * If TH(B) changed. or if 5 minutes without
1220 * update, send the NDATA message. 1221 * update, send the NDATA message.
1221 */ 1222 */
1222 if (updateHT || (now > (ndata + 300))) { 1223 if (updateHT || (now > (ndata + 300))) {
1224 #ifdef HAVE_MOSQUITTO_H
1223 publishNData(false, 0); 1225 publishNData(false, 0);
1226 #endif
1224 ndata = now; 1227 ndata = now;
1225 } 1228 }
1226 1229
1227 LCDunit = 0; 1230 LCDunit = 0;
1228 for (unit = Config.units; unit; unit = unit->next) { 1231 for (unit = Config.units; unit; unit = unit->next) {
1242 * Maximum error is 40 degrees for now. 1245 * Maximum error is 40 degrees for now.
1243 */ 1246 */
1244 deviation = 40000; 1247 deviation = 40000;
1245 if ((unit->air_temperature == 0) || 1248 if ((unit->air_temperature == 0) ||
1246 (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { 1249 (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) {
1250 #ifdef HAVE_MOSQUITTO_H
1247 if (unit->air_temperature != temp) { 1251 if (unit->air_temperature != temp) {
1248 unit->mqtt_flag |= MQTT_FLAG_DATA; 1252 unit->mqtt_flag |= MQTT_FLAG_DATA;
1249 pub_domoticz_temp(unit->air_idx, temp); 1253 pub_domoticz_temp(unit->air_idx, temp);
1250 } 1254 }
1255 #endif
1251 unit->air_temperature = temp; 1256 unit->air_temperature = temp;
1252 unit->air_state = 0; 1257 unit->air_state = 0;
1253 } else { 1258 } else {
1254 syslog(LOG_NOTICE, "deviation error air deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); 1259 syslog(LOG_NOTICE, "deviation error air deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp);
1255 } 1260 }
1264 rc = device_in(unit->beer_address, &temp); 1269 rc = device_in(unit->beer_address, &temp);
1265 if (rc == DEVPRESENT_YES) { 1270 if (rc == DEVPRESENT_YES) {
1266 deviation = 40000; 1271 deviation = 40000;
1267 if ((unit->beer_temperature == 0) || 1272 if ((unit->beer_temperature == 0) ||
1268 (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { 1273 (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) {
1274 #ifdef HAVE_MOSQUITTO_H
1269 if (unit->beer_temperature != temp) { 1275 if (unit->beer_temperature != temp) {
1270 unit->mqtt_flag |= MQTT_FLAG_DATA; 1276 unit->mqtt_flag |= MQTT_FLAG_DATA;
1271 pub_domoticz_temp(unit->beer_idx, temp); 1277 pub_domoticz_temp(unit->beer_idx, temp);
1272 } 1278 }
1279 #endif
1273 unit->beer_temperature = temp; 1280 unit->beer_temperature = temp;
1274 unit->beer_state = 0; 1281 unit->beer_state = 0;
1275 } else { 1282 } else {
1276 syslog(LOG_NOTICE, "deviation error beer deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); 1283 syslog(LOG_NOTICE, "deviation error beer deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp);
1277 } 1284 }
1286 rc = device_in(unit->chiller_address, &temp); 1293 rc = device_in(unit->chiller_address, &temp);
1287 if (rc == DEVPRESENT_YES) { 1294 if (rc == DEVPRESENT_YES) {
1288 deviation = 40000; 1295 deviation = 40000;
1289 if ((unit->chiller_temperature == 0) || 1296 if ((unit->chiller_temperature == 0) ||
1290 (unit->chiller_temperature && (temp > (int)unit->chiller_temperature - deviation) && (temp < ((int)unit->chiller_temperature + deviation)))) { 1297 (unit->chiller_temperature && (temp > (int)unit->chiller_temperature - deviation) && (temp < ((int)unit->chiller_temperature + deviation)))) {
1298 #ifdef HAVE_MOSQUITTO_H
1291 if (unit->chiller_temperature != temp) { 1299 if (unit->chiller_temperature != temp) {
1292 unit->mqtt_flag |= MQTT_FLAG_DATA; 1300 unit->mqtt_flag |= MQTT_FLAG_DATA;
1293 pub_domoticz_temp(unit->chiller_idx, temp); 1301 pub_domoticz_temp(unit->chiller_idx, temp);
1294 } 1302 }
1303 #endif
1295 unit->chiller_temperature = temp; 1304 unit->chiller_temperature = temp;
1296 unit->chiller_state = 0; 1305 unit->chiller_state = 0;
1297 } else { 1306 } else {
1298 syslog(LOG_NOTICE, "deviation error chiller deviation=%d, old=%d new=%d", deviation, unit->chiller_temperature, temp); 1307 syslog(LOG_NOTICE, "deviation error chiller deviation=%d, old=%d new=%d", deviation, unit->chiller_temperature, temp);
1299 } 1308 }
1312 if (rc == DEVPRESENT_YES) { 1321 if (rc == DEVPRESENT_YES) {
1313 if (temp) { 1322 if (temp) {
1314 if (unit->door_state == 0) { 1323 if (unit->door_state == 0) {
1315 syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name); 1324 syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name);
1316 unit->door_state = 1; 1325 unit->door_state = 1;
1326 #ifdef HAVE_MOSQUITTO_H
1317 pub_domoticz_output(unit->door_idx, unit->door_state); 1327 pub_domoticz_output(unit->door_idx, unit->door_state);
1318 unit->mqtt_flag |= MQTT_FLAG_DATA; 1328 unit->mqtt_flag |= MQTT_FLAG_DATA;
1329 #endif
1319 } 1330 }
1320 } else { 1331 } else {
1321 if (unit->door_state) { 1332 if (unit->door_state) {
1322 syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name); 1333 syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name);
1323 unit->door_state = 0; 1334 unit->door_state = 0;
1335 #ifdef HAVE_MOSQUITTO_H
1324 pub_domoticz_output(unit->door_idx, unit->door_state); 1336 pub_domoticz_output(unit->door_idx, unit->door_state);
1325 unit->mqtt_flag |= MQTT_FLAG_DATA; 1337 unit->mqtt_flag |= MQTT_FLAG_DATA;
1338 #endif
1326 } 1339 }
1327 /* 1340 /*
1328 * If unit is active and the door is open 1341 * If unit is active and the door is open
1329 */ 1342 */
1330 if (unit->mode != UNITMODE_NONE) { 1343 if (unit->mode != UNITMODE_NONE) {
1346 if (rc == DEVPRESENT_YES) { 1359 if (rc == DEVPRESENT_YES) {
1347 if (temp) { 1360 if (temp) {
1348 if (unit->psu_state == 0) { 1361 if (unit->psu_state == 0) {
1349 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name); 1362 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name);
1350 unit->psu_state = 1; 1363 unit->psu_state = 1;
1364 #ifdef HAVE_MOSQUITTO_H
1351 pub_domoticz_output(unit->psu_idx, unit->psu_state); 1365 pub_domoticz_output(unit->psu_idx, unit->psu_state);
1352 unit->mqtt_flag |= MQTT_FLAG_DATA; 1366 unit->mqtt_flag |= MQTT_FLAG_DATA;
1367 #endif
1353 } 1368 }
1354 } else { 1369 } else {
1355 if (unit->psu_state) { 1370 if (unit->psu_state) {
1356 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name); 1371 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name);
1357 unit->psu_state = 0; 1372 unit->psu_state = 0;
1373 #ifdef HAVE_MOSQUITTO_H
1358 pub_domoticz_output(unit->psu_idx, unit->psu_state); 1374 pub_domoticz_output(unit->psu_idx, unit->psu_state);
1359 unit->mqtt_flag |= MQTT_FLAG_DATA; 1375 unit->mqtt_flag |= MQTT_FLAG_DATA;
1376 #endif
1360 } 1377 }
1361 unit->alarm_flag |= ALARM_FLAG_PSU; 1378 unit->alarm_flag |= ALARM_FLAG_PSU;
1362 } 1379 }
1363 } else { 1380 } else {
1364 unit->psu_state = 1; 1381 unit->psu_state = 1;
1709 } else { 1726 } else {
1710 int power = round(unit->PID_heat->OutP); 1727 int power = round(unit->PID_heat->OutP);
1711 if (unit->heater_state != power) { 1728 if (unit->heater_state != power) {
1712 syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power); 1729 syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power);
1713 unit->heater_state = power; 1730 unit->heater_state = power;
1731 #ifdef HAVE_MOSQUITTO_H
1714 pub_domoticz_output(unit->heater_idx, unit->heater_state); 1732 pub_domoticz_output(unit->heater_idx, unit->heater_state);
1715 if (unit->heater_address) 1733 if (unit->heater_address)
1716 unit->mqtt_flag |= MQTT_FLAG_DATA; 1734 unit->mqtt_flag |= MQTT_FLAG_DATA;
1735 #endif
1717 } 1736 }
1718 } 1737 }
1719 } else { 1738 } else {
1720 if (unit->heater_wait > 0) { 1739 if (unit->heater_wait > 0) {
1721 unit->heater_wait--; 1740 unit->heater_wait--;
1722 } else { 1741 } else {
1723 if (unit->heater_state) { 1742 if (unit->heater_state) {
1724 syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name); 1743 syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name);
1725 unit->heater_state = 0; 1744 unit->heater_state = 0;
1745 #ifdef HAVE_MOSQUITTO_H
1726 pub_domoticz_output(unit->heater_idx, unit->heater_state); 1746 pub_domoticz_output(unit->heater_idx, unit->heater_state);
1727 if (unit->heater_address) 1747 if (unit->heater_address)
1728 unit->mqtt_flag |= MQTT_FLAG_DATA; 1748 unit->mqtt_flag |= MQTT_FLAG_DATA;
1749 #endif
1729 } 1750 }
1730 } 1751 }
1731 } 1752 }
1732 if (unit->door_state) { 1753 if (unit->door_state) {
1733 device_out(unit->heater_address, unit->heater_state); 1754 device_out(unit->heater_address, unit->heater_state);
1743 } else { 1764 } else {
1744 int power = round(unit->PID_cool->OutP); 1765 int power = round(unit->PID_cool->OutP);
1745 if (unit->cooler_state != power) { 1766 if (unit->cooler_state != power) {
1746 syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power); 1767 syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power);
1747 unit->cooler_state = power; 1768 unit->cooler_state = power;
1769 #ifdef HAVE_MOSQUITTO_H
1748 pub_domoticz_output(unit->cooler_idx, unit->cooler_state); 1770 pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
1749 if (unit->cooler_address) 1771 if (unit->cooler_address)
1750 unit->mqtt_flag |= MQTT_FLAG_DATA; 1772 unit->mqtt_flag |= MQTT_FLAG_DATA;
1773 #endif
1751 } 1774 }
1752 } 1775 }
1753 } else { 1776 } else {
1754 if (unit->cooler_wait > 0) { 1777 if (unit->cooler_wait > 0) {
1755 unit->cooler_wait--; 1778 unit->cooler_wait--;
1756 } else { 1779 } else {
1757 if (unit->cooler_state) { 1780 if (unit->cooler_state) {
1758 syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name); 1781 syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name);
1759 unit->cooler_state = 0; 1782 unit->cooler_state = 0;
1783 #ifdef HAVE_MOSQUITTO_H
1760 pub_domoticz_output(unit->cooler_idx, unit->cooler_state); 1784 pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
1761 if (unit->cooler_address) 1785 if (unit->cooler_address)
1762 unit->mqtt_flag |= MQTT_FLAG_DATA; 1786 unit->mqtt_flag |= MQTT_FLAG_DATA;
1787 #endif
1763 } 1788 }
1764 } 1789 }
1765 } 1790 }
1766 if (unit->door_state) { 1791 if (unit->door_state) {
1767 device_out(unit->cooler_address, unit->cooler_state); 1792 device_out(unit->cooler_address, unit->cooler_state);
1784 unit->fan_wait++; 1809 unit->fan_wait++;
1785 } else { 1810 } else {
1786 if (! unit->fan_state) { 1811 if (! unit->fan_state) {
1787 syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name); 1812 syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name);
1788 unit->fan_state = 100; 1813 unit->fan_state = 100;
1814 #ifdef HAVE_MOSQUITTO_H
1789 pub_domoticz_output(unit->fan_idx, unit->fan_state); 1815 pub_domoticz_output(unit->fan_idx, unit->fan_state);
1790 if (unit->fan_address) 1816 if (unit->fan_address)
1791 unit->mqtt_flag |= MQTT_FLAG_DATA; 1817 unit->mqtt_flag |= MQTT_FLAG_DATA;
1818 #endif
1792 } 1819 }
1793 } 1820 }
1794 } else { 1821 } else {
1795 if (unit->fan_wait > 0) { 1822 if (unit->fan_wait > 0) {
1796 unit->fan_wait--; 1823 unit->fan_wait--;
1797 } else { 1824 } else {
1798 if (unit->fan_state) { 1825 if (unit->fan_state) {
1799 syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name); 1826 syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name);
1800 unit->fan_state = 0; 1827 unit->fan_state = 0;
1828 #ifdef HAVE_MOSQUITTO_H
1801 pub_domoticz_output(unit->fan_idx, unit->fan_state); 1829 pub_domoticz_output(unit->fan_idx, unit->fan_state);
1802 if (unit->fan_address) 1830 if (unit->fan_address)
1803 unit->mqtt_flag |= MQTT_FLAG_DATA; 1831 unit->mqtt_flag |= MQTT_FLAG_DATA;
1832 #endif
1804 } 1833 }
1805 } 1834 }
1806 } 1835 }
1807 device_out(unit->fan_address, unit->fan_state); 1836 device_out(unit->fan_address, unit->fan_state);
1808 } 1837 }
1858 #endif 1887 #endif
1859 1888
1860 /* 1889 /*
1861 * Publish MQTT messages set in flag 1890 * Publish MQTT messages set in flag
1862 */ 1891 */
1892 #ifdef HAVE_MOSQUITTO_H
1863 if (unit->mqtt_flag) { 1893 if (unit->mqtt_flag) {
1864 if (unit->mqtt_flag & MQTT_FLAG_BIRTH) { 1894 if (unit->mqtt_flag & MQTT_FLAG_BIRTH) {
1865 publishDBirth(unit); 1895 publishDBirth(unit);
1866 unit->mqtt_flag &= ~MQTT_FLAG_BIRTH; 1896 unit->mqtt_flag &= ~MQTT_FLAG_BIRTH;
1867 } else { 1897 } else {
1871 if (unit->mqtt_flag & MQTT_FLAG_DEATH) { 1901 if (unit->mqtt_flag & MQTT_FLAG_DEATH) {
1872 publishDDeath(unit); 1902 publishDDeath(unit);
1873 unit->mqtt_flag &= ~MQTT_FLAG_DEATH; 1903 unit->mqtt_flag &= ~MQTT_FLAG_DEATH;
1874 } 1904 }
1875 } 1905 }
1906 #endif
1876 1907
1877 /* 1908 /*
1878 * Handle changed alarms 1909 * Handle changed alarms
1879 */ 1910 */
1880 if (unit->alarm_flag != unit->alarm_last) { 1911 if (unit->alarm_flag != unit->alarm_last) {
1998 * Turn everything off 2029 * Turn everything off
1999 */ 2030 */
2000 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; 2031 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0;
2001 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 2032 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2002 device_out(unit->heater_address, unit->heater_state); 2033 device_out(unit->heater_address, unit->heater_state);
2034 #ifdef HAVE_MOSQUITTO_H
2003 pub_domoticz_output(unit->heater_idx, unit->heater_state); 2035 pub_domoticz_output(unit->heater_idx, unit->heater_state);
2036 #endif
2004 device_out(unit->cooler_address, unit->cooler_state); 2037 device_out(unit->cooler_address, unit->cooler_state);
2038 #ifdef HAVE_MOSQUITTO_H
2005 pub_domoticz_output(unit->cooler_idx, unit->cooler_state); 2039 pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
2040 #endif
2006 device_out(unit->fan_address, unit->fan_state); 2041 device_out(unit->fan_address, unit->fan_state);
2042 #ifdef HAVE_MOSQUITTO_H
2007 pub_domoticz_output(unit->fan_idx, unit->fan_state); 2043 pub_domoticz_output(unit->fan_idx, unit->fan_state);
2044 #endif
2008 device_out(unit->light_address, unit->light_state); 2045 device_out(unit->light_address, unit->light_state);
2046 #ifdef HAVE_MOSQUITTO_H
2009 if (unit->mode != UNITMODE_OFF) { 2047 if (unit->mode != UNITMODE_OFF) {
2010 /* 2048 /*
2011 * If unit ia active, publish we are dying. 2049 * If unit ia active, publish we are dying.
2012 */ 2050 */
2013 unit->mqtt_flag = MQTT_FLAG_DATA; 2051 unit->mqtt_flag = MQTT_FLAG_DATA;
2014 publishDData(unit); 2052 publishDData(unit);
2015 publishDDeath(unit); 2053 publishDDeath(unit);
2016 } 2054 }
2055 #endif
2017 syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]); 2056 syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]);
2018 } 2057 }
2019 2058
2020 usleep(100000); 2059 usleep(100000);
2060 #ifdef HAVE_MOSQUITTO_H
2021 mqtt_disconnect(); 2061 mqtt_disconnect();
2062 #endif
2022 2063
2023 syslog(LOG_NOTICE, "Out of loop"); 2064 syslog(LOG_NOTICE, "Out of loop");
2024 if (debug) 2065 if (debug)
2025 fprintf(stdout, (char *)"Out of loop\n"); 2066 fprintf(stdout, (char *)"Out of loop\n");
2026 2067

mercurial