Monitor for central heating system (e.g. 2zones+hw) Supports up to 15 temp probes (DS18B20/DS18S20) 3 valve monitors Gas pulse meter recording Use stand-alone or with nodeEnergyServer See http://robdobson.com/2015/09/central-heating-monitor
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
Diff: main.cpp
- Revision:
- 9:0e103c2f869a
- Parent:
- 8:5980547ae71c
- Child:
- 10:72eb217def1f
--- a/main.cpp Sat Feb 21 19:00:08 2015 +0000 +++ b/main.cpp Sun Feb 22 11:57:12 2015 +0000 @@ -3,7 +3,7 @@ #include "NTPClient.h" #include "RdWebServer.h" #include "GasUseCounter.h" -#include "RdDS18B20.h" +#include "Thermometers.h" #include <stdarg.h> // Web and UDB ports @@ -13,6 +13,7 @@ // Ticker collects data //Ticker ticker; //const int TICK_MS = 250; +const int LOOP_DELAY_IN_MS = 250; // Debugging and status RawSerial pc(USBTX, USBRX); @@ -38,9 +39,9 @@ // Thermometers - DS18B20 OneWire Thermometer connections const PinName tempSensorPins[] = { p22 }; -const int NUM_THERM_BUSES = sizeof(tempSensorPins)/sizeof(int); -DS18B20* thermometerBuses[NUM_THERM_BUSES]; +Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS); +// Utility function to log data void LogData(const char* format, ...) { FILE* fp = fopen(logFilename, "a"); @@ -58,6 +59,7 @@ } } +// Send broadcast message with current data void SendInfoBroadcast() { led3 = true; @@ -66,6 +68,15 @@ sendUDPSocket.set_broadcasting(); broadcastEndpoint.set_address("255.255.255.255", BROADCAST_PORT); + // Get temperature values + TemperatureValue tempValues[Thermometers::MAX_THERMOMETERS]; + int numTempValues = thermometers.GetTemperatureValues(Thermometers::MAX_THERMOMETERS, tempValues, 100); + char tempStr[200]; + for (int tempIdx = 0; tempIdx < numTempValues; tempIdx++) + { + printf("Temp: %.1f, Addr: %s, Time: %d\r\n", tempValues[tempIdx].tempInCentigrade, tempValues[tempIdx].address, tempValues[tempIdx].timeStamp); + } + // Format message char outBuf[200]; sprintf(outBuf, "{\"e\":[{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}]}", @@ -110,7 +121,7 @@ char* setGasUseCallback(int method, char* cmdStr, char* argStr) { - pc.printf("Setting gas use count %s\n\r", argStr); + pc.printf("Setting gas use count %s\r\n", argStr); int newGasUse = 0; char* eqStr = strchr(argStr, '='); if (eqStr == NULL) @@ -158,12 +169,12 @@ { osDelay(1000); } - pc.printf("Waited %d mins\r\n", i); + pc.printf("%d mins since NTP\r\n", i); } break; } } - + int main() { pc.baud(115200); @@ -171,18 +182,9 @@ // ticker.attach(&TickFunction,TICK_MS / 1000.0); - // Setup the thermometers - for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++) - thermometerBuses[thermIdx] = new DS18B20(tempSensorPins[thermIdx]); - - // Initialise thermometers - for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++) - { - DS18B20* pThermBus = thermometerBuses[thermIdx]; - pThermBus->SearchToGetAddresses(); - pThermBus->ReqConvert(); - } - + // Initialise thermometers + thermometers.Init(); + // Get the current count from the SD Card gasUseCounter.Init(); @@ -190,7 +192,7 @@ eth.init(); //Use DHCP eth.connect(); - pc.printf("IP Address is %s\n\r", eth.getIPAddress()); + pc.printf("IP Address is %s\r\n", eth.getIPAddress()); // NTP Time setter Thread ntpTimeSetter(&ntp_thread); @@ -198,70 +200,30 @@ // Web Server Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 3)); - const int loopDelayInMs = 250; - const int numSecondsBetweenThermReadings = 10; - const int numLoopsPerThermReading = numSecondsBetweenThermReadings*1000/loopDelayInMs; - const int timeForThermReadingInSecs = 2; - const int loopCountForRequestingThermReading = numLoopsPerThermReading - (timeForThermReadingInSecs*1000/loopDelayInMs); - int countForThermReadings = 0; - const int reGetThermometerAddressesAfterNumReadings = 100; - int countForGetThermometerAddresses = 0; + // Time of last broadcast + time_t timeOfLastBroadcast = time(NULL); + const int TIME_BETWEEN_BROADCASTS_IN_SECS = 60; while(true) { - osDelay(loopDelayInMs); + osDelay(LOOP_DELAY_IN_MS); led1 = !led1; // Service gas count if (gasUseCounter.Service()) - SendInfoBroadcast(); - - // Check if thermometer addresses need to be got - if (countForThermReadings++ == 0) - { - if (countForGetThermometerAddresses++ == 0) - { - printf("Requested Addresses\n\r"); - for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++) - { - DS18B20* pThermBus = thermometerBuses[thermIdx]; - pThermBus->SearchToGetAddresses(); - } - } - else if (countForGetThermometerAddresses > reGetThermometerAddressesAfterNumReadings) - { - countForGetThermometerAddresses = 0; - } - } - else { - // Check if time to request thermometer readings - if (countForThermReadings == loopCountForRequestingThermReading) - { - printf("Requested Convert\n\r"); - for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++) - { - DS18B20* pThermBus = thermometerBuses[thermIdx]; - printf("Bus %d Num therms %d\n\r", thermIdx, pThermBus->GetNumAddresses()); - pThermBus->ReqConvert(); - } - } + SendInfoBroadcast(); + timeOfLastBroadcast = time(NULL); + } - // Read thermometers - if (countForThermReadings > numLoopsPerThermReading) - { - countForThermReadings = 0; - printf("Reading Temp\n\r"); - for (int thermIdx = 0; thermIdx < NUM_THERM_BUSES; thermIdx++) - { - DS18B20* pThermBus = thermometerBuses[thermIdx]; - for (int addrIdx = 0; addrIdx < pThermBus->GetNumAddresses(); addrIdx++) - { - printf("Bus %d Therm %d === %.2fC ... Addr = ", thermIdx, addrIdx, pThermBus->GetTemperature(addrIdx)); - pThermBus->DebugPrintAddress(addrIdx); - printf("\r\n"); - } - } - } + // Service thermometers + thermometers.Service(); + + // Check if ready for a broadcast + if ((time(NULL) - timeOfLastBroadcast) >= TIME_BETWEEN_BROADCASTS_IN_SECS) + { + SendInfoBroadcast(); + timeOfLastBroadcast = time(NULL); } } } +