# HG changeset patch # User Michiel Broek # Date 1408829898 -7200 # Node ID d0014ccec61555d0581bdb4140e169fc65f3cc9b # Parent e4341cfbc2a808d953aac379386b7860d96da330 Simulation of fridge cold loss to the room added for testing. diff -r e4341cfbc2a8 -r d0014ccec615 configure --- a/configure Sat Aug 23 12:48:55 2014 +0200 +++ b/configure Sat Aug 23 23:38:18 2014 +0200 @@ -3547,6 +3547,7 @@ else CFLAGS="-g -O2 -fomit-frame-pointer -fno-strict-aliasing -Wall -Wshadow -Wwrite-strings -Wstrict-prototypes -Winline" fi +LIBS="$LIBS -lm" { $as_echo "$as_me:${as_lineno-$LINENO}: checking for xmlParseFile in -lxml2" >&5 diff -r e4341cfbc2a8 -r d0014ccec615 configure.ac --- a/configure.ac Sat Aug 23 12:48:55 2014 +0200 +++ b/configure.ac Sat Aug 23 23:38:18 2014 +0200 @@ -58,6 +58,7 @@ else CFLAGS="-g -O2 -fomit-frame-pointer -fno-strict-aliasing -Wall -Wshadow -Wwrite-strings -Wstrict-prototypes -Winline" fi +LIBS="$LIBS -lm" AC_CHECK_LIB(xml2,xmlParseFile,result=yes,result=no) diff -r e4341cfbc2a8 -r d0014ccec615 thermferm/devices.c --- a/thermferm/devices.c Sat Aug 23 12:48:55 2014 +0200 +++ b/thermferm/devices.c Sat Aug 23 23:38:18 2014 +0200 @@ -474,6 +474,9 @@ #endif { devices_list *device; +#ifdef USE_SIMULATOR + simulator_list *simulator; +#endif char *addr = NULL, line[60], *p = NULL; FILE *fp; int temp, rc; @@ -610,15 +613,18 @@ #ifdef HAVE_WIRINGPI_H piLock(LOCK_DEVICES); #endif - if (device->subdevice == 0) { - device->value = 20000; - device->timestamp = time(NULL); - } else if (device->subdevice == 1) { - device->value = 20125; - device->timestamp = time(NULL); - } else if (device->subdevice == 2) { - device->value = 20250; - device->timestamp = time(NULL); + if (Config.simulators) { + simulator = Config.simulators; + if (device->subdevice == 0) { + device->value = (int)(simulator->room_temperature * 1000); + device->timestamp = time(NULL); + } else if (device->subdevice == 1) { + device->value = (int)(simulator->air_temperature * 1000); + device->timestamp = time(NULL); + } else if (device->subdevice == 2) { + device->value = (int)(simulator->beer_temperature * 1000); + device->timestamp = time(NULL); + } } #ifdef HAVE_WIRINGPI_H piUnlock(LOCK_DEVICES); diff -r e4341cfbc2a8 -r d0014ccec615 thermferm/rdconfig.c --- a/thermferm/rdconfig.c Sat Aug 23 12:48:55 2014 +0200 +++ b/thermferm/rdconfig.c Sat Aug 23 23:38:18 2014 +0200 @@ -637,15 +637,15 @@ syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%.1f", simulator->air_temperature)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%f", simulator->air_temperature)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_TEMPERATURE", "%.1f", simulator->beer_temperature)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_TEMPERATURE", "%f", simulator->beer_temperature)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_TEMP", "%.1f", simulator->cooler_temp)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER_TEMP", "%f", simulator->cooler_temp)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } @@ -657,7 +657,7 @@ syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_TEMP", "%.1f", simulator->heater_temp)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER_TEMP", "%f", simulator->heater_temp)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } @@ -681,7 +681,7 @@ syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_YEAST_HEAT", "%.1f", simulator->s_yeast_heat)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_YEAST_HEAT", "%f", simulator->s_yeast_heat)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } @@ -689,11 +689,11 @@ syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_COOL_TEMP", "%.1f", simulator->s_cool_temp)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_COOL_TEMP", "%f", simulator->s_cool_temp)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } - if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HEAT_TEMP", "%.1f", simulator->s_heat_temp)) < 0) { + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HEAT_TEMP", "%f", simulator->s_heat_temp)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterWriteElement"); return 1; } diff -r e4341cfbc2a8 -r d0014ccec615 thermferm/simulator.c --- a/thermferm/simulator.c Sat Aug 23 12:48:55 2014 +0200 +++ b/thermferm/simulator.c Sat Aug 23 23:38:18 2014 +0200 @@ -27,6 +27,7 @@ extern int my_shutdown; extern int debug; +extern sys_config Config; @@ -36,17 +37,73 @@ void *my_simulator_loop(void *threadid) #endif { + simulator_list *simulator; + time_t now, last = (time_t)0; + int seconds = 0; + double k_room_air, sqm_room_air, thick_room_air, heat_transfer; + double t_change, vhc_air = 0.00121, vhc_water = 4.1796; syslog(LOG_NOTICE, "Thread my_simulator_loop started"); if (debug) fprintf(stdout, "Thread my_simulator_loop started\n"); for (;;) { + for (simulator = Config.simulators; simulator; simulator = simulator->next) { + if (my_shutdown) + break; - if (my_shutdown) - break; + now = time(NULL); + if (now != last) { + last = now; + /* + * Each second + */ + seconds++; + + /* + * First, calculate temperature difference between the room and the air in the + * fridge. We use the volume air to roughly calculate the total area between + * the in and outside. Calculate the effect and shift the air temperature towards + * the room temperature. + */ + sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100; /* square meters all fridge sides */ + thick_room_air = 0.02; /* 4 cm walls */ + k_room_air = 0.03; /* Polystrene */ + heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air; + if (heat_transfer != 0) + t_change = vhc_air / (heat_transfer * (simulator->volume_air * 1000)); + else + t_change = 0.0; + simulator->air_temperature = simulator->air_temperature + t_change; + fprintf(stdout, "room_air=%f room=%f air=%.15f heat_transfer=%f t_change=%.15f\n", + sqm_room_air, simulator->room_temperature, simulator->air_temperature, heat_transfer, t_change); - usleep(100000); + /* + * If heating, calculate temperature of the heating plate. If heating is off but + * the plate is warmer then the air, calculate the cooling down temperature. + * Finally, calculate the new air and plate temperature. + */ + + /* + * If cooling, calculate temperature of the cooling plate. If cooling is off but + * the plate is colder then the air, calculate the warming up temperature. + * Finsally, calculate the new air and plate temperature. + */ + + /* + * Calculate the extra beer temperatur rise to simulate the heat produced by the + * fermentation process. Peak about one day after start and slowly decrease after + * that. + */ + + /* + * Calculate final temperature of the beer and the air. + */ + + } + usleep(100000); + } + usleep(50000); } syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); diff -r e4341cfbc2a8 -r d0014ccec615 thermferm/thermferm.h --- a/thermferm/thermferm.h Sat Aug 23 12:48:55 2014 +0200 +++ b/thermferm/thermferm.h Sat Aug 23 23:38:18 2014 +0200 @@ -30,6 +30,7 @@ #include #include #include +#include #ifndef HAVE_WIRINGPI_H #include #endif @@ -233,13 +234,13 @@ char *name; /* Simulator name */ int volume_air; /* Volume air of the frigo */ int volume_beer; /* Volume beer inside frigo */ - float room_temperature; /* Temp outside frigo */ - float air_temperature; /* Simulated air temperature */ - float beer_temperature; /* Simulated beer temperature */ - float cooler_temp; /* Lowest cooler temperature */ + double room_temperature; /* Temp outside frigo */ + double air_temperature; /* Simulated air temperature */ + double beer_temperature; /* Simulated beer temperature */ + double cooler_temp; /* Lowest cooler temperature */ int cooler_time; /* Time to reach temperature */ float cooler_size; /* Size of cooler in square mtr */ - float heater_temp; /* Highest heater temperature */ + double heater_temp; /* Highest heater temperature */ int heater_time; /* Time to reach temperature */ float heater_size; /* Size of heater in square mtr */ int heater_state; /* Heater status */ @@ -249,10 +250,10 @@ * Status values, maintained by the simulator but stored * here so they don't get lost over program restarts. */ - float s_yeast_heat; /* Heat generated by yeast */ + double s_yeast_heat; /* Heat generated by yeast */ time_t s_yeast_started; /* Start date/time fermentation */ - float s_cool_temp; /* Temp cooler */ - float s_heat_temp; /* Temp heater */ + double s_cool_temp; /* Temp cooler */ + double s_heat_temp; /* Temp heater */ time_t s_cool_changed; /* Start date/time cooler */ time_t s_heat_changed; /* Start date/time heater */ } simulator_list; diff -r e4341cfbc2a8 -r d0014ccec615 www-thermferm/simulator.php --- a/www-thermferm/simulator.php Sat Aug 23 12:48:55 2014 +0200 +++ b/www-thermferm/simulator.php Sat Aug 23 23:38:18 2014 +0200 @@ -92,11 +92,11 @@ * Build the update command */ if ($_POST['key'] == 'Delete') { - send_cmd("DEVICE DEL ".$_POST['UUID']); + send_cmd("SIMULATOR DEL ".$_POST['UUID']); } if ($_POST['key'] == 'Save') { - $cmd = array("DEVICE PUT ".$_POST['UUID']); + $cmd = array("SIMULATOR PUT ".$_POST['UUID']); $cmd[] = "NAME,".$_POST['Name']; $cmd[] = "VOLUME_AIR,".$_POST['VolumeAir']; $cmd[] = "VOLUME_BEER,".$_POST['VolumeBeer'];