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:
- 11:30182b9aa833
- Parent:
- 10:72eb217def1f
- Child:
- 12:a52996515063
--- a/main.cpp Sun Feb 22 20:23:43 2015 +0000 +++ b/main.cpp Sun Feb 22 20:28:21 2015 +0000 @@ -11,15 +11,13 @@ const int WEBPORT = 80; // Port for web server const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number -// Ticker collects data -//Ticker ticker; -//const int TICK_MS = 50; +// Main loop delay between data collection passes const int LOOP_DELAY_IN_MS = 250; // Debugging and status RawSerial pc(USBTX, USBRX); DigitalOut led1(LED1); //ticking (flashes) -DigitalOut led2(LED2); // +DigitalOut led2(LED2); //state of the 1st voltage alerter DigitalOut led3(LED3); //socket connecting status DigitalOut led4(LED4); //server status @@ -51,14 +49,26 @@ VoltAlerter voltAlerter2(p24); VoltAlerter voltAlerter3(p25); -// Broadcast message +// Broadcast message format +// Data format of the broadcast message - senml - https://tools.ietf.org/html/draft-jennings-senml-08 +// { +// "e": [ +// {"n":"gasCount","v":%d}, +// {"n":"gasPulseRateMs","v":%d,"u":"ms"}, +// {"n":"temp_%s","v":%0.1f,"u":"degC"}, +// ... +// {"n":"pump_%d","v":%d}, +// ... +// ], +// "bt": %d +// } const char broadcastMsgPrefix[] = "{\"e\":["; const char broadcastMsgGasFormat[] = "{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}"; const char broadcastTemperatureFormat[] = "{\"n\":\"temp_%s\",\"v\":%0.1f,\"u\":\"degC\"}"; const char broadcastVoltAlerterFormat[] = "{\"n\":\"pump_%d\",\"v\":%d}"; const char broadcastMsgSuffix[] = "],\"bt\":%d}"; -// Format message +// Broadcast message length and buffer const int broadcastMsgLen = sizeof(broadcastMsgPrefix) + sizeof(broadcastMsgGasFormat) + (sizeof(broadcastTemperatureFormat)*Thermometers::MAX_THERMOMETERS) + @@ -97,24 +107,12 @@ // Get temperature values TemperatureValue tempValues[Thermometers::MAX_THERMOMETERS]; int numTempValues = thermometers.GetTemperatureValues(Thermometers::MAX_THERMOMETERS, tempValues, 100); - 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); - } +// 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); +// } - // Data format of the message - senml - https://tools.ietf.org/html/draft-jennings-senml-08 - // { - // "e": [ - // {"n":"gasCount","v":%d}, - // {"n":"gasPulseRateMs","v":%d,"u":"ms"}, - // {"n":"temp_%s","v":%0.1f,"u":"degC"}, - // ... - // {"n":"pump_%d","v":%d}, - // ... - // ], - // "bt": %d - // } - + // Send the broadcast time_t timeNow = time(NULL); strcpy(broadcastMsgBuffer, broadcastMsgPrefix); sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgGasFormat, gasUseCounter.GetCount(), gasUseCounter.GetPulseRateMs()); @@ -156,11 +154,6 @@ led3 = false; } -// Ticker's tick function -//void TickFunction() -//{ -//} - char* getGasUseCallback(int method, char* cmdStr, char* argStr) { char* pResp = gasUseCounter.getGasUseCallback(cmdStr, argStr); @@ -180,21 +173,20 @@ return "SetGasValue OK"; } +// Create, configure and run the web server void http_thread(void const* arg) { char* baseWebFolder = "/sd/"; - RdWebServer webServer; webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false); webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true); webServer.addCommand("getgascount", RdWebServerCmdDef::CMD_CALLBACK, &getGasUseCallback); webServer.addCommand("setgascount", RdWebServerCmdDef::CMD_CALLBACK, &setGasUseCallback); - webServer.init(WEBPORT, &led4, baseWebFolder); - webServer.run(); } +// Network time protocol (NTP) thread to get time from internet void ntp_thread(void const* arg) { while (1) @@ -228,15 +220,12 @@ } } +// Main int main() { pc.baud(115200); pc.printf("Gas Monitor V2 - Rob Dobson 2014\r\n"); - printf("Broadcast Msg Len %d\r\n", broadcastMsgLen); - -// ticker.attach(&TickFunction,TICK_MS / 1000.0); - // Initialise thermometers thermometers.Init(); @@ -284,6 +273,8 @@ voltAlerter1.Service(); voltAlerter2.Service(); voltAlerter3.Service(); + + // Set LED2 to the state of the first volt alerter led2 = voltAlerter1.GetState(); } }