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:
20:7933076df5af
Parent:
19:0367cb46d003
Child:
21:ccf053bab795
--- a/main.cpp	Mon Oct 05 14:05:33 2015 +0000
+++ b/main.cpp	Tue Oct 13 18:35:20 2015 +0000
@@ -40,6 +40,9 @@
 NTPClient ntp;
 const int NTP_REFRESH_INTERVAL_HOURS = 1;
 
+// Mutex for SD card access
+Mutex sdCardMutex;
+
 // File system for SD card
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 
@@ -50,15 +53,15 @@
 const char* dataLogFileBase = "/sd/";
 
 // Logger
-Logger logger(eventLogFileName, dataLogFileBase);
+Logger logger(eventLogFileName, dataLogFileBase, sdCardMutex);
 
 // Gas use counter
 DigitalIn gasPulsePin(p21);
-GasUseCounter gasUseCounter(gasPulseFileName1, gasPulseFileName2, gasPulsePin, pc);
+GasUseCounter gasUseCounter(gasPulseFileName1, gasPulseFileName2, gasPulsePin, logger, sdCardMutex);
 
 // Thermometers - DS18B20 OneWire Thermometer connections
 const PinName tempSensorPins[] = { p22 };
-Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS);
+Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS, logger);
 
 // Voltage Sensors / Alerters
 const int NUM_VOLT_ALERTERS = 3;
@@ -144,15 +147,15 @@
     int rslt = sendUDPSocket.sendTo(broadcastEndpoint, broadcastMsgBuffer, bytesToSend);
     if (rslt == bytesToSend)
     {
-        pc.printf("Broadcast (len %d) Sent ok %s\r\n", bytesToSend, broadcastMsgBuffer);
+        logger.LogDebug("Broadcast (len %d) Sent ok %s", bytesToSend, broadcastMsgBuffer);
     }
     else if (rslt == -1)
     {
-        pc.printf("Broadcast Failed to send %s\r\n", broadcastMsgBuffer);
+        logger.LogDebug("Broadcast Failed to send %s", broadcastMsgBuffer);
     }
     else
     {
-        pc.printf("Broadcast Didn't send all of %s\r\n", broadcastMsgBuffer);
+        logger.LogDebug("Broadcast Didn't send all of %s", broadcastMsgBuffer);
     }
     
     // Log the data
@@ -172,7 +175,7 @@
 char* setGasUseCallback(int method, char* cmdStr, char* argStr, char* msgBuffer, int msgLen, 
                 int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
 {
-    pc.printf("Setting gas use count %s\r\n", argStr);
+    logger.LogDebug("Setting gas use count %s", argStr);
     int newGasUse = 0;
     char* eqStr = strchr(argStr, '=');
     if (eqStr == NULL)
@@ -186,7 +189,7 @@
 void http_thread(void const* arg)
 {
     char* baseWebFolder = "/sd/";
-    RdWebServer webServer;
+    RdWebServer webServer(&sdCardMutex);
     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);
@@ -201,18 +204,18 @@
 {
     while (1)
     {
-        pc.printf("Trying to update time...\r\n");
+        logger.LogDebug("Trying to update time...");
         if (ntp.setTime("0.pool.ntp.org") == NTP_OK)
         {
             osDelay(1000);  // This delay is simply to try to improve printf output
-            printf("Set time successfully\r\n");
+            logger.LogDebug("Set time successfully");
             time_t ctTime;
             ctTime = time(NULL);
-            pc.printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+            logger.LogDebug("Time is set to (UTC): %s", ctime(&ctTime));
         }
         else
         {
-            pc.printf("Cannot set from NTP\r\n");
+            logger.LogDebug("Cannot set from NTP");
         }
         
         // Refresh time every K hours
@@ -225,7 +228,7 @@
                 {
                     osDelay(1000);
                 }
-                pc.printf("%d mins to next NTP time refresh\r\n", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i));
+                logger.LogDebug("%d mins to next NTP time refresh", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i));
             }
         }
     }
@@ -240,7 +243,8 @@
 int main()
 {
     pc.baud(115200);
-    pc.printf("\r\n\r\nGas Monitor V2 - Rob Dobson 2014\r\n");
+    logger.SetDebugDest(true, true);
+    logger.LogDebug("\r\n\r\nGas Monitor V2 - Rob Dobson 2015");
 
     // Initialise thermometers
     thermometers.Init();
@@ -251,8 +255,8 @@
     // Setup ethernet interface
     char macAddr[6];
     mbed_mac_address(macAddr);
-    pc.printf("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); 
-    pc.printf("Connecting to ethernet ...\r\n");
+    logger.LogDebug("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); 
+    logger.LogDebug("Connecting to ethernet ...");
 
     // Init ethernet
     EthernetInterface::init();
@@ -263,7 +267,7 @@
 
     // Connect ethernet
     EthernetInterface::connect();
-    pc.printf("IP Address: %s HostName %s\r\n", EthernetInterface::getIPAddress(), EthernetInterface::getName());
+    logger.LogDebug("IP Address: %s HostName %s", EthernetInterface::getIPAddress(), EthernetInterface::getName());
     
     // NTP Time setter
     Thread ntpTimeSetter(&ntp_thread);
@@ -275,8 +279,8 @@
     bool watchdogCausedRestart = watchdog.WatchdogCausedRestart();
     bool restartCauseRecorded = false;
     
-    // Setup the watchdog for 10s reset
-    watchdog.SetTimeoutSecs(10);
+    // Setup the watchdog for reset
+    watchdog.SetTimeoutSecs(60);
     
     // Time of last broadcast
     time_t timeOfLastBroadcast = time(NULL);
@@ -293,12 +297,12 @@
                 if (watchdogCausedRestart)
                 {
                     logger.LogEvent("Watchdog Restart");
-                    pc.printf("Watchdog Restart\r\n");
+                    logger.LogDebug("Watchdog Restart");
                 }
                 else
                 {
                     logger.LogEvent("Normal Restart");
-                    pc.printf("Normal Restart\r\n");
+                    logger.LogDebug("Normal Restart");
                 }
                 restartCauseRecorded = true;
             }