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

Revision:
14:3c3aa4fd7e1a
Parent:
12:a52996515063
Child:
16:89778849e9f7
--- a/main.cpp	Sun Feb 22 22:33:25 2015 +0000
+++ b/main.cpp	Tue Mar 03 12:53:37 2015 +0000
@@ -73,9 +73,9 @@
 //      "bt": %d
 // }
 const char broadcastMsgPrefix[] = "{\"e\":[";
-const char broadcastMsgGasFormat[] = "{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}";
+const char broadcastMsgGasFormat[] = "{\"n\":\"gasCount\",\"v\":%d,\"u\":\"count\"},{\"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 broadcastVoltAlerterFormat[] = "{\"n\":\"pump_%d\",\"bv\":%d}";
 const char broadcastMsgSuffix[] = "],\"bt\":%d}";
 
 // Broadcast message length and buffer
@@ -87,16 +87,9 @@
             60;
 char broadcastMsgBuffer[broadcastMsgLen];
     
-// Send broadcast message with current data
-void SendInfoBroadcast()
+// Format broadcast message
+void GenBroadcastMessage()
 {
-    led3 = true;
-    
-    // Init the sending socket
-    sendUDPSocket.init();
-    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);
@@ -104,7 +97,7 @@
 //    {
 //        printf("Temp: %.1f, Addr: %s, Time: %d\r\n", tempValues[tempIdx].tempInCentigrade, tempValues[tempIdx].address, tempValues[tempIdx].timeStamp);
 //    }
-    
+
     // Format the broadcast message
     time_t timeNow = time(NULL);
     strcpy(broadcastMsgBuffer, broadcastMsgPrefix);
@@ -121,7 +114,21 @@
     strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ",");
     sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 3, voltAlerter3.GetState());
     sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgSuffix, timeNow);
+}
+
+// Send broadcast message with current data
+void SendInfoBroadcast()
+{
+    led3 = true;
+    
+    // Init the sending socket
+    sendUDPSocket.init();
+    sendUDPSocket.set_broadcasting();
+    broadcastEndpoint.set_address("255.255.255.255", BROADCAST_PORT);
         
+    // Format the message
+    GenBroadcastMessage();
+            
     // Send
     int bytesToSend = strlen(broadcastMsgBuffer);
     int rslt = sendUDPSocket.sendTo(broadcastEndpoint, broadcastMsgBuffer, bytesToSend);
@@ -144,11 +151,11 @@
     led3 = false;
 }
 
-char* getGasUseCallback(int method, char* cmdStr, char* argStr)
+char* getCurDataCallback(int method, char* cmdStr, char* argStr)
 {
-    char* pResp = gasUseCounter.getGasUseCallback(cmdStr, argStr);
-    pc.printf("Returning gas use %s\r\n", pResp);
-    return pResp;
+    // Format message
+    GenBroadcastMessage();
+    return broadcastMsgBuffer;
 }
 
 char* setGasUseCallback(int method, char* cmdStr, char* argStr)
@@ -171,7 +178,7 @@
     webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false);
     webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true);
     webServer.addCommand("listfiles", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "/", false);
-    webServer.addCommand("getgascount", RdWebServerCmdDef::CMD_CALLBACK, &getGasUseCallback);
+    webServer.addCommand("getcurdata", RdWebServerCmdDef::CMD_CALLBACK, &getCurDataCallback);
     webServer.addCommand("setgascount", RdWebServerCmdDef::CMD_CALLBACK, &setGasUseCallback);
     webServer.init(WEBPORT, &led4, baseWebFolder);
     webServer.run();
@@ -183,7 +190,7 @@
     while (1)
     {
         pc.printf("Trying to update time...\r\n");
-        if (ntp.setTime("0.pool.ntp.org") == 0)
+        if (ntp.setTime("0.pool.ntp.org") == NTP_OK)
         {
           printf("Set time successfully\r\n");
           time_t ctTime;
@@ -239,13 +246,11 @@
     
     // Web Server
     Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 3));
+
+    // Store reason for restart
+    bool watchdogCausedRestart = watchdog.WatchdogCausedRestart();
+    bool restartCauseRecorded = false;
     
-    // Record the reason for restarting in the log file
-    if (watchdog.WatchdogCausedRestart())
-        logger.LogEvent("Watchdog Restart");
-    else
-        logger.LogEvent("Normal Restart");
-        
     // Setup the watchdog for 10s reset
     watchdog.SetTimeoutSecs(10);
     
@@ -254,7 +259,25 @@
     const int TIME_BETWEEN_BROADCASTS_IN_SECS = 60;
     while(true)
     {
+        // Check if we can record the reason for restart (i.e. if time is now set)
+        if (!restartCauseRecorded)
+        {
+            time_t nowTime = time(NULL);
+            if (nowTime > 1000000000)
+            {
+                // Record the reason for restarting in the log file
+                if (watchdogCausedRestart)
+                    logger.LogEvent("Watchdog Restart");
+                else
+                    logger.LogEvent("Normal Restart");
+                restartCauseRecorded = true;
+            }
+        }
+        
+        // Loop delay
         osDelay(LOOP_DELAY_IN_MS);
+        
+        // Feed the watchdog and show the flashing LED
         led1 = !led1;
         watchdog.Feed();