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:
Mon Oct 05 14:05:33 2015 +0000
Revision:
19:0367cb46d003
Parent:
18:d419ccebc666
Child:
20:7933076df5af
Added retries on getting addresses from thermometers; Doubled up on non-volatile storage of latest gas count

Who changed what in which revision?

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