#define pdMSTOTICKS( xTimeInMs ) ( (xTimeInMs / portTICK_PERIOD_MS) + 1 )
#ifdef SERIAL_DBG
static struct BoilerRegulatorDebugInfo debugInfo;
aImportStaticQueue(temperatureDebug);
#endif
aImportStaticSemaphore(temperaturePoll);
// ---------------------------------------------------
// -------------- Static variables -------------------
// ---------------------------------------------------
static Logic logic; // boiler management logic
static float oneWire_Temp = 127.5f; // last read temperature
static bool boilerOn = false; // the boiler status
TickType_t xLastWakeTime_BoilerRegulator; // used for the final delay
static uint8_t deviceCount = 0; // the number of ds18x20 devices
static bool validMeasure = false; // is the last measure valid?
extern struct StatusPoll lastMeasure;
// ---------------------------------------------------
// ------------- Most important Task -----------------
// ---------------------------------------------------
void TaskBoilerRegulator(void *pvParameters __attribute__((unused))) {
#ifdef SERIAL_DBG
logic.setThresholdTemperature(DBG_TEMPERATURE);
logic.setHysteresis(DBG_HYSTERESIS);
#endif
// Set the heater pin
pinMode(BOILER_DIGITAL_PIN, OUTPUT);
digitalWrite(BOILER_DIGITAL_PIN, LOW);
// setup thermometers
OneWire ow(ONEWIRE_DIGITAL_PIN);
DallasTemperature sensors(&ow);
// This one is used for the good delay
xLastWakeTime_BoilerRegulator = xTaskGetTickCount();
for (;;) {
// A few seconds delay to let other tasks run
vTaskDelayUntil(&xLastWakeTime_BoilerRegulator, pdMSTOTICKS( 5000 ));
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
sensors.setResolution(DS18X20_PRECISION_BITS);
// Get the number of devices
deviceCount = sensors.getDeviceCount();
// Read the temperature from the OneWire bus (if any device is present)
if (deviceCount) {
// Send the command to get temperatures
sensors.requestTemperatures();
float temp = 127.5f;
// get the lowest temperature from the group of ds18b20
for (int i = 0; i < deviceCount; i++) {
// read the temperature
float currentTemp = sensors.getTempCByIndex(i);
// skip useless values!
if (currentTemp < -60.0f) continue;
else if (currentTemp > +50.0f) continue;
// try updating the best temperature with the smallest one
if (currentTemp < temp) {
temp = currentTemp;
validMeasure = true;
}
}
// Update the current temperature
if (validMeasure) oneWire_Temp = temp;
validMeasure = false;
// Update logic status
logic.notifyTemperature(oneWire_Temp);
// Edit pin value only if needed
if ((bool)logic.heat() != boilerOn) {
// Update the logic
boilerOn = (logic.heat() == 0x01) ? true : false;
// New mode for the boiler
digitalWrite(BOILER_DIGITAL_PIN, (boilerOn != false) ? HIGH : LOW);
}
}
// update the temperature for the GSM Shield task
if (xSemaphoreTake(aGetStaticSemaphoreName(temperaturePoll), 3)) {
lastMeasure.boilerOn = boilerOn;
lastMeasure.temperatureValue = oneWire_Temp;
}
#ifdef SERIAL_DBG
// Build the debug info structure
debugInfo.count = (uint8_t)deviceCount;
debugInfo.value = oneWire_Temp;
debugInfo.enabled = boilerOn;
// Attempt to send debug informations to the debug task
xQueueSend(aGetStaticQueueName(temperatureDebug), (const void *)&debugInfo, 8);
#endif
}
}