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

Committer:
Bobty
Date:
Sun Feb 22 22:08:37 2015 +0000
Revision:
12:a52996515063
Parent:
11:30182b9aa833
Child:
14:3c3aa4fd7e1a
Watchdog implemented and working; Improved logging of events and data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 0:f6611c8f453c 1 #include "mbed.h"
Bobty 0:f6611c8f453c 2 #include "EthernetInterface.h"
Bobty 5:5bccf48799d4 3 #include "NTPClient.h"
Bobty 5:5bccf48799d4 4 #include "RdWebServer.h"
Bobty 5:5bccf48799d4 5 #include "GasUseCounter.h"
Bobty 9:0e103c2f869a 6 #include "Thermometers.h"
Bobty 10:72eb217def1f 7 #include "VoltAlerter.h"
Bobty 12:a52996515063 8 #include "Watchdog.h"
Bobty 12:a52996515063 9 #include "Logger.h"
Bobty 5:5bccf48799d4 10
Bobty 5:5bccf48799d4 11 // Web and UDB ports
Bobty 5:5bccf48799d4 12 const int WEBPORT = 80; // Port for web server
Bobty 5:5bccf48799d4 13 const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number
Bobty 5:5bccf48799d4 14
Bobty 11:30182b9aa833 15 // Main loop delay between data collection passes
Bobty 9:0e103c2f869a 16 const int LOOP_DELAY_IN_MS = 250;
Bobty 5:5bccf48799d4 17
Bobty 5:5bccf48799d4 18 // Debugging and status
Bobty 5:5bccf48799d4 19 RawSerial pc(USBTX, USBRX);
Bobty 5:5bccf48799d4 20 DigitalOut led1(LED1); //ticking (flashes)
Bobty 11:30182b9aa833 21 DigitalOut led2(LED2); //state of the 1st voltage alerter
Bobty 5:5bccf48799d4 22 DigitalOut led3(LED3); //socket connecting status
Bobty 5:5bccf48799d4 23 DigitalOut led4(LED4); //server status
Bobty 5:5bccf48799d4 24
Bobty 5:5bccf48799d4 25 // Web server
Bobty 5:5bccf48799d4 26 EthernetInterface eth;
Bobty 5:5bccf48799d4 27 UDPSocket sendUDPSocket;
Bobty 5:5bccf48799d4 28 Endpoint broadcastEndpoint;
Bobty 5:5bccf48799d4 29
Bobty 10:72eb217def1f 30 // Network Time Protocol (NTP)
Bobty 10:72eb217def1f 31 NTPClient ntp;
Bobty 10:72eb217def1f 32 const int NTP_REFRESH_INTERVAL_HOURS = 1;
Bobty 10:72eb217def1f 33
Bobty 5:5bccf48799d4 34 // File system for SD card
Bobty 4:0d3a207680b0 35 SDFileSystem sd(p5, p6, p7, p8, "sd");
Bobty 5:5bccf48799d4 36
Bobty 12:a52996515063 37 // Log file names
Bobty 12:a52996515063 38 const char* gasPulseFileName = "/sd/curPulse.txt";
Bobty 12:a52996515063 39 const char* eventLogFileName = "/sd/log.txt";
Bobty 12:a52996515063 40 const char* dataLogFileBase = "/sd/";
Bobty 12:a52996515063 41
Bobty 12:a52996515063 42 // Logger
Bobty 12:a52996515063 43 Logger logger(eventLogFileName, dataLogFileBase);
Bobty 12:a52996515063 44
Bobty 5:5bccf48799d4 45 // Gas use counter
Bobty 5:5bccf48799d4 46 DigitalIn gasPulsePin(p21);
Bobty 5:5bccf48799d4 47 GasUseCounter gasUseCounter(gasPulseFileName, gasPulsePin, pc);
Bobty 5:5bccf48799d4 48
Bobty 8:5980547ae71c 49 // Thermometers - DS18B20 OneWire Thermometer connections
Bobty 8:5980547ae71c 50 const PinName tempSensorPins[] = { p22 };
Bobty 9:0e103c2f869a 51 Thermometers thermometers(sizeof(tempSensorPins)/sizeof(PinName), tempSensorPins, LOOP_DELAY_IN_MS);
Bobty 8:5980547ae71c 52
Bobty 10:72eb217def1f 53 // Voltage Sensors / Alerters
Bobty 10:72eb217def1f 54 const int NUM_VOLT_ALERTERS = 3;
Bobty 10:72eb217def1f 55 VoltAlerter voltAlerter1(p23);
Bobty 10:72eb217def1f 56 VoltAlerter voltAlerter2(p24);
Bobty 10:72eb217def1f 57 VoltAlerter voltAlerter3(p25);
Bobty 10:72eb217def1f 58
Bobty 12:a52996515063 59 // Watchdog
Bobty 12:a52996515063 60 Watchdog watchdog;
Bobty 12:a52996515063 61
Bobty 11:30182b9aa833 62 // Broadcast message format
Bobty 11:30182b9aa833 63 // Data format of the broadcast message - senml - https://tools.ietf.org/html/draft-jennings-senml-08
Bobty 11:30182b9aa833 64 // {
Bobty 11:30182b9aa833 65 // "e": [
Bobty 11:30182b9aa833 66 // {"n":"gasCount","v":%d},
Bobty 11:30182b9aa833 67 // {"n":"gasPulseRateMs","v":%d,"u":"ms"},
Bobty 11:30182b9aa833 68 // {"n":"temp_%s","v":%0.1f,"u":"degC"},
Bobty 11:30182b9aa833 69 // ...
Bobty 11:30182b9aa833 70 // {"n":"pump_%d","v":%d},
Bobty 11:30182b9aa833 71 // ...
Bobty 11:30182b9aa833 72 // ],
Bobty 11:30182b9aa833 73 // "bt": %d
Bobty 11:30182b9aa833 74 // }
Bobty 10:72eb217def1f 75 const char broadcastMsgPrefix[] = "{\"e\":[";
Bobty 10:72eb217def1f 76 const char broadcastMsgGasFormat[] = "{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}";
Bobty 10:72eb217def1f 77 const char broadcastTemperatureFormat[] = "{\"n\":\"temp_%s\",\"v\":%0.1f,\"u\":\"degC\"}";
Bobty 10:72eb217def1f 78 const char broadcastVoltAlerterFormat[] = "{\"n\":\"pump_%d\",\"v\":%d}";
Bobty 10:72eb217def1f 79 const char broadcastMsgSuffix[] = "],\"bt\":%d}";
Bobty 10:72eb217def1f 80
Bobty 11:30182b9aa833 81 // Broadcast message length and buffer
Bobty 10:72eb217def1f 82 const int broadcastMsgLen = sizeof(broadcastMsgPrefix) +
Bobty 10:72eb217def1f 83 sizeof(broadcastMsgGasFormat) +
Bobty 10:72eb217def1f 84 (sizeof(broadcastTemperatureFormat)*Thermometers::MAX_THERMOMETERS) +
Bobty 10:72eb217def1f 85 (sizeof(broadcastVoltAlerterFormat)*NUM_VOLT_ALERTERS) +
Bobty 10:72eb217def1f 86 sizeof(broadcastMsgSuffix) +
Bobty 10:72eb217def1f 87 60;
Bobty 10:72eb217def1f 88 char broadcastMsgBuffer[broadcastMsgLen];
Bobty 10:72eb217def1f 89
Bobty 9:0e103c2f869a 90 // Send broadcast message with current data
Bobty 5:5bccf48799d4 91 void SendInfoBroadcast()
Bobty 5:5bccf48799d4 92 {
Bobty 5:5bccf48799d4 93 led3 = true;
Bobty 12:a52996515063 94
Bobty 5:5bccf48799d4 95 // Init the sending socket
Bobty 5:5bccf48799d4 96 sendUDPSocket.init();
Bobty 5:5bccf48799d4 97 sendUDPSocket.set_broadcasting();
Bobty 5:5bccf48799d4 98 broadcastEndpoint.set_address("255.255.255.255", BROADCAST_PORT);
Bobty 5:5bccf48799d4 99
Bobty 9:0e103c2f869a 100 // Get temperature values
Bobty 9:0e103c2f869a 101 TemperatureValue tempValues[Thermometers::MAX_THERMOMETERS];
Bobty 9:0e103c2f869a 102 int numTempValues = thermometers.GetTemperatureValues(Thermometers::MAX_THERMOMETERS, tempValues, 100);
Bobty 11:30182b9aa833 103 // for (int tempIdx = 0; tempIdx < numTempValues; tempIdx++)
Bobty 11:30182b9aa833 104 // {
Bobty 11:30182b9aa833 105 // printf("Temp: %.1f, Addr: %s, Time: %d\r\n", tempValues[tempIdx].tempInCentigrade, tempValues[tempIdx].address, tempValues[tempIdx].timeStamp);
Bobty 11:30182b9aa833 106 // }
Bobty 9:0e103c2f869a 107
Bobty 12:a52996515063 108 // Format the broadcast message
Bobty 10:72eb217def1f 109 time_t timeNow = time(NULL);
Bobty 10:72eb217def1f 110 strcpy(broadcastMsgBuffer, broadcastMsgPrefix);
Bobty 10:72eb217def1f 111 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgGasFormat, gasUseCounter.GetCount(), gasUseCounter.GetPulseRateMs());
Bobty 10:72eb217def1f 112 strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ",");
Bobty 10:72eb217def1f 113 for (int tempIdx = 0; tempIdx < numTempValues; tempIdx++)
Bobty 10:72eb217def1f 114 {
Bobty 10:72eb217def1f 115 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastTemperatureFormat, tempValues[tempIdx].address, tempValues[tempIdx].tempInCentigrade);
Bobty 10:72eb217def1f 116 strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ",");
Bobty 10:72eb217def1f 117 }
Bobty 10:72eb217def1f 118 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 1, voltAlerter1.GetState());
Bobty 10:72eb217def1f 119 strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ",");
Bobty 10:72eb217def1f 120 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 2, voltAlerter2.GetState());
Bobty 10:72eb217def1f 121 strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ",");
Bobty 10:72eb217def1f 122 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 3, voltAlerter3.GetState());
Bobty 10:72eb217def1f 123 sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgSuffix, timeNow);
Bobty 10:72eb217def1f 124
Bobty 5:5bccf48799d4 125 // Send
Bobty 10:72eb217def1f 126 int bytesToSend = strlen(broadcastMsgBuffer);
Bobty 10:72eb217def1f 127 int rslt = sendUDPSocket.sendTo(broadcastEndpoint, broadcastMsgBuffer, bytesToSend);
Bobty 5:5bccf48799d4 128 if (rslt == bytesToSend)
Bobty 5:5bccf48799d4 129 {
Bobty 10:72eb217def1f 130 pc.printf("Broadcast (len %d) Sent ok %s\r\n", bytesToSend, broadcastMsgBuffer);
Bobty 5:5bccf48799d4 131 }
Bobty 5:5bccf48799d4 132 else if (rslt == -1)
Bobty 5:5bccf48799d4 133 {
Bobty 10:72eb217def1f 134 pc.printf("Broadcast Failed to send %s\r\n", broadcastMsgBuffer);
Bobty 5:5bccf48799d4 135 }
Bobty 5:5bccf48799d4 136 else
Bobty 5:5bccf48799d4 137 {
Bobty 10:72eb217def1f 138 pc.printf("Broadcast Didn't send all of %s\r\n", broadcastMsgBuffer);
Bobty 5:5bccf48799d4 139 }
Bobty 5:5bccf48799d4 140
Bobty 12:a52996515063 141 // Log the data
Bobty 12:a52996515063 142 logger.LogData(broadcastMsgBuffer);
Bobty 5:5bccf48799d4 143
Bobty 5:5bccf48799d4 144 led3 = false;
Bobty 5:5bccf48799d4 145 }
Bobty 0:f6611c8f453c 146
Bobty 5:5bccf48799d4 147 char* getGasUseCallback(int method, char* cmdStr, char* argStr)
Bobty 5:5bccf48799d4 148 {
Bobty 5:5bccf48799d4 149 char* pResp = gasUseCounter.getGasUseCallback(cmdStr, argStr);
Bobty 5:5bccf48799d4 150 pc.printf("Returning gas use %s\r\n", pResp);
Bobty 5:5bccf48799d4 151 return pResp;
Bobty 5:5bccf48799d4 152 }
Bobty 5:5bccf48799d4 153
Bobty 5:5bccf48799d4 154 char* setGasUseCallback(int method, char* cmdStr, char* argStr)
Bobty 5:5bccf48799d4 155 {
Bobty 9:0e103c2f869a 156 pc.printf("Setting gas use count %s\r\n", argStr);
Bobty 5:5bccf48799d4 157 int newGasUse = 0;
Bobty 5:5bccf48799d4 158 char* eqStr = strchr(argStr, '=');
Bobty 5:5bccf48799d4 159 if (eqStr == NULL)
Bobty 5:5bccf48799d4 160 return "SetGasValue FAILED";
Bobty 5:5bccf48799d4 161 sscanf(eqStr+1, "%d", &newGasUse);
Bobty 5:5bccf48799d4 162 gasUseCounter.SetCount(newGasUse);
Bobty 5:5bccf48799d4 163 return "SetGasValue OK";
Bobty 5:5bccf48799d4 164 }
Bobty 5:5bccf48799d4 165
Bobty 11:30182b9aa833 166 // Create, configure and run the web server
Bobty 5:5bccf48799d4 167 void http_thread(void const* arg)
Bobty 5:5bccf48799d4 168 {
Bobty 5:5bccf48799d4 169 char* baseWebFolder = "/sd/";
Bobty 5:5bccf48799d4 170 RdWebServer webServer;
Bobty 5:5bccf48799d4 171 webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false);
Bobty 5:5bccf48799d4 172 webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true);
Bobty 12:a52996515063 173 webServer.addCommand("listfiles", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "/", false);
Bobty 5:5bccf48799d4 174 webServer.addCommand("getgascount", RdWebServerCmdDef::CMD_CALLBACK, &getGasUseCallback);
Bobty 5:5bccf48799d4 175 webServer.addCommand("setgascount", RdWebServerCmdDef::CMD_CALLBACK, &setGasUseCallback);
Bobty 5:5bccf48799d4 176 webServer.init(WEBPORT, &led4, baseWebFolder);
Bobty 5:5bccf48799d4 177 webServer.run();
Bobty 5:5bccf48799d4 178 }
Bobty 5:5bccf48799d4 179
Bobty 11:30182b9aa833 180 // Network time protocol (NTP) thread to get time from internet
Bobty 5:5bccf48799d4 181 void ntp_thread(void const* arg)
Bobty 5:5bccf48799d4 182 {
Bobty 5:5bccf48799d4 183 while (1)
Bobty 5:5bccf48799d4 184 {
Bobty 5:5bccf48799d4 185 pc.printf("Trying to update time...\r\n");
Bobty 5:5bccf48799d4 186 if (ntp.setTime("0.pool.ntp.org") == 0)
Bobty 5:5bccf48799d4 187 {
Bobty 5:5bccf48799d4 188 printf("Set time successfully\r\n");
Bobty 5:5bccf48799d4 189 time_t ctTime;
Bobty 5:5bccf48799d4 190 ctTime = time(NULL);
Bobty 5:5bccf48799d4 191 printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
Bobty 5:5bccf48799d4 192 }
Bobty 5:5bccf48799d4 193 else
Bobty 5:5bccf48799d4 194 {
Bobty 5:5bccf48799d4 195 printf("Cannot set from NTP\r\n");
Bobty 10:72eb217def1f 196 }
Bobty 10:72eb217def1f 197
Bobty 10:72eb217def1f 198 // Refresh time every K hours
Bobty 10:72eb217def1f 199 for (int k = 0; k < NTP_REFRESH_INTERVAL_HOURS; k++)
Bobty 5:5bccf48799d4 200 {
Bobty 10:72eb217def1f 201 // 1 hour
Bobty 10:72eb217def1f 202 for (int i = 0; i < 60; i++)
Bobty 5:5bccf48799d4 203 {
Bobty 10:72eb217def1f 204 for (int j = 0; j < 60; j++)
Bobty 10:72eb217def1f 205 {
Bobty 10:72eb217def1f 206 osDelay(1000);
Bobty 10:72eb217def1f 207 }
Bobty 10:72eb217def1f 208 pc.printf("%d mins to next NTP time refresh\r\n", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i));
Bobty 5:5bccf48799d4 209 }
Bobty 5:5bccf48799d4 210 }
Bobty 5:5bccf48799d4 211 }
Bobty 5:5bccf48799d4 212 }
Bobty 9:0e103c2f869a 213
Bobty 12:a52996515063 214 // #define TEST_WATCHDOG 1
Bobty 12:a52996515063 215 #ifdef TEST_WATCHDOG
Bobty 12:a52996515063 216 int watchdogTestLoopCount = 0;
Bobty 12:a52996515063 217 #endif
Bobty 12:a52996515063 218
Bobty 11:30182b9aa833 219 // Main
Bobty 0:f6611c8f453c 220 int main()
Bobty 0:f6611c8f453c 221 {
Bobty 0:f6611c8f453c 222 pc.baud(115200);
Bobty 5:5bccf48799d4 223 pc.printf("Gas Monitor V2 - Rob Dobson 2014\r\n");
Bobty 0:f6611c8f453c 224
Bobty 9:0e103c2f869a 225 // Initialise thermometers
Bobty 9:0e103c2f869a 226 thermometers.Init();
Bobty 9:0e103c2f869a 227
Bobty 5:5bccf48799d4 228 // Get the current count from the SD Card
Bobty 5:5bccf48799d4 229 gasUseCounter.Init();
Bobty 0:f6611c8f453c 230
Bobty 5:5bccf48799d4 231 // setup ethernet interface
Bobty 5:5bccf48799d4 232 eth.init(); //Use DHCP
Bobty 5:5bccf48799d4 233 eth.connect();
Bobty 2:6bfef0839102 234
Bobty 9:0e103c2f869a 235 pc.printf("IP Address is %s\r\n", eth.getIPAddress());
Bobty 8:5980547ae71c 236
Bobty 8:5980547ae71c 237 // NTP Time setter
Bobty 5:5bccf48799d4 238 Thread ntpTimeSetter(&ntp_thread);
Bobty 0:f6611c8f453c 239
Bobty 8:5980547ae71c 240 // Web Server
Bobty 6:b7064d33e402 241 Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 3));
Bobty 5:5bccf48799d4 242
Bobty 12:a52996515063 243 // Record the reason for restarting in the log file
Bobty 12:a52996515063 244 if (watchdog.WatchdogCausedRestart())
Bobty 12:a52996515063 245 logger.LogEvent("Watchdog Restart");
Bobty 12:a52996515063 246 else
Bobty 12:a52996515063 247 logger.LogEvent("Normal Restart");
Bobty 12:a52996515063 248
Bobty 12:a52996515063 249 // Setup the watchdog for 10s reset
Bobty 12:a52996515063 250 watchdog.SetTimeoutSecs(10);
Bobty 12:a52996515063 251
Bobty 9:0e103c2f869a 252 // Time of last broadcast
Bobty 9:0e103c2f869a 253 time_t timeOfLastBroadcast = time(NULL);
Bobty 9:0e103c2f869a 254 const int TIME_BETWEEN_BROADCASTS_IN_SECS = 60;
Bobty 5:5bccf48799d4 255 while(true)
Bobty 0:f6611c8f453c 256 {
Bobty 9:0e103c2f869a 257 osDelay(LOOP_DELAY_IN_MS);
Bobty 5:5bccf48799d4 258 led1 = !led1;
Bobty 12:a52996515063 259 watchdog.Feed();
Bobty 5:5bccf48799d4 260
Bobty 5:5bccf48799d4 261 // Service gas count
Bobty 5:5bccf48799d4 262 if (gasUseCounter.Service())
Bobty 8:5980547ae71c 263 {
Bobty 9:0e103c2f869a 264 SendInfoBroadcast();
Bobty 9:0e103c2f869a 265 timeOfLastBroadcast = time(NULL);
Bobty 9:0e103c2f869a 266 }
Bobty 8:5980547ae71c 267
Bobty 9:0e103c2f869a 268 // Service thermometers
Bobty 9:0e103c2f869a 269 thermometers.Service();
Bobty 9:0e103c2f869a 270
Bobty 9:0e103c2f869a 271 // Check if ready for a broadcast
Bobty 9:0e103c2f869a 272 if ((time(NULL) - timeOfLastBroadcast) >= TIME_BETWEEN_BROADCASTS_IN_SECS)
Bobty 9:0e103c2f869a 273 {
Bobty 9:0e103c2f869a 274 SendInfoBroadcast();
Bobty 9:0e103c2f869a 275 timeOfLastBroadcast = time(NULL);
Bobty 8:5980547ae71c 276 }
Bobty 10:72eb217def1f 277
Bobty 10:72eb217def1f 278 // Service volt alerters
Bobty 10:72eb217def1f 279 voltAlerter1.Service();
Bobty 10:72eb217def1f 280 voltAlerter2.Service();
Bobty 10:72eb217def1f 281 voltAlerter3.Service();
Bobty 11:30182b9aa833 282
Bobty 11:30182b9aa833 283 // Set LED2 to the state of the first volt alerter
Bobty 10:72eb217def1f 284 led2 = voltAlerter1.GetState();
Bobty 12:a52996515063 285
Bobty 12:a52996515063 286 #ifdef TEST_WATCHDOG
Bobty 12:a52996515063 287 // After about 20 seconds of operation we'll hang to test the watchdog
Bobty 12:a52996515063 288 if (watchdogTestLoopCount++ > 80)
Bobty 12:a52996515063 289 {
Bobty 12:a52996515063 290 // This should cause watchdog to kick in and reset
Bobty 12:a52996515063 291 osDelay(20000);
Bobty 12:a52996515063 292 }
Bobty 12:a52996515063 293 #endif
Bobty 0:f6611c8f453c 294 }
Bobty 5:5bccf48799d4 295 }
Bobty 9:0e103c2f869a 296