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
main.cpp@14:3c3aa4fd7e1a, 2015-03-03 (annotated)
- Committer:
- Bobty
- Date:
- Tue Mar 03 12:53:37 2015 +0000
- Revision:
- 14:3c3aa4fd7e1a
- Parent:
- 12:a52996515063
- Child:
- 16:89778849e9f7
Added other sensor readings to web view
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:3c3aa4fd7e1a | 76 | const char broadcastMsgGasFormat[] = "{\"n\":\"gasCount\",\"v\":%d,\"u\":\"count\"},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}"; |
Bobty | 10:72eb217def1f | 77 | const char broadcastTemperatureFormat[] = "{\"n\":\"temp_%s\",\"v\":%0.1f,\"u\":\"degC\"}"; |
Bobty | 14:3c3aa4fd7e1a | 78 | const char broadcastVoltAlerterFormat[] = "{\"n\":\"pump_%d\",\"bv\":%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 | 14:3c3aa4fd7e1a | 90 | // Format broadcast message |
Bobty | 14:3c3aa4fd7e1a | 91 | void GenBroadcastMessage() |
Bobty | 5:5bccf48799d4 | 92 | { |
Bobty | 9:0e103c2f869a | 93 | // Get temperature values |
Bobty | 9:0e103c2f869a | 94 | TemperatureValue tempValues[Thermometers::MAX_THERMOMETERS]; |
Bobty | 9:0e103c2f869a | 95 | int numTempValues = thermometers.GetTemperatureValues(Thermometers::MAX_THERMOMETERS, tempValues, 100); |
Bobty | 11:30182b9aa833 | 96 | // for (int tempIdx = 0; tempIdx < numTempValues; tempIdx++) |
Bobty | 11:30182b9aa833 | 97 | // { |
Bobty | 11:30182b9aa833 | 98 | // printf("Temp: %.1f, Addr: %s, Time: %d\r\n", tempValues[tempIdx].tempInCentigrade, tempValues[tempIdx].address, tempValues[tempIdx].timeStamp); |
Bobty | 11:30182b9aa833 | 99 | // } |
Bobty | 14:3c3aa4fd7e1a | 100 | |
Bobty | 12:a52996515063 | 101 | // Format the broadcast message |
Bobty | 10:72eb217def1f | 102 | time_t timeNow = time(NULL); |
Bobty | 10:72eb217def1f | 103 | strcpy(broadcastMsgBuffer, broadcastMsgPrefix); |
Bobty | 10:72eb217def1f | 104 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgGasFormat, gasUseCounter.GetCount(), gasUseCounter.GetPulseRateMs()); |
Bobty | 10:72eb217def1f | 105 | strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ","); |
Bobty | 10:72eb217def1f | 106 | for (int tempIdx = 0; tempIdx < numTempValues; tempIdx++) |
Bobty | 10:72eb217def1f | 107 | { |
Bobty | 10:72eb217def1f | 108 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastTemperatureFormat, tempValues[tempIdx].address, tempValues[tempIdx].tempInCentigrade); |
Bobty | 10:72eb217def1f | 109 | strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ","); |
Bobty | 10:72eb217def1f | 110 | } |
Bobty | 10:72eb217def1f | 111 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 1, voltAlerter1.GetState()); |
Bobty | 10:72eb217def1f | 112 | strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ","); |
Bobty | 10:72eb217def1f | 113 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 2, voltAlerter2.GetState()); |
Bobty | 10:72eb217def1f | 114 | strcpy(broadcastMsgBuffer+strlen(broadcastMsgBuffer), ","); |
Bobty | 10:72eb217def1f | 115 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastVoltAlerterFormat, 3, voltAlerter3.GetState()); |
Bobty | 10:72eb217def1f | 116 | sprintf(broadcastMsgBuffer+strlen(broadcastMsgBuffer), broadcastMsgSuffix, timeNow); |
Bobty | 14:3c3aa4fd7e1a | 117 | } |
Bobty | 14:3c3aa4fd7e1a | 118 | |
Bobty | 14:3c3aa4fd7e1a | 119 | // Send broadcast message with current data |
Bobty | 14:3c3aa4fd7e1a | 120 | void SendInfoBroadcast() |
Bobty | 14:3c3aa4fd7e1a | 121 | { |
Bobty | 14:3c3aa4fd7e1a | 122 | led3 = true; |
Bobty | 14:3c3aa4fd7e1a | 123 | |
Bobty | 14:3c3aa4fd7e1a | 124 | // Init the sending socket |
Bobty | 14:3c3aa4fd7e1a | 125 | sendUDPSocket.init(); |
Bobty | 14:3c3aa4fd7e1a | 126 | sendUDPSocket.set_broadcasting(); |
Bobty | 14:3c3aa4fd7e1a | 127 | broadcastEndpoint.set_address("255.255.255.255", BROADCAST_PORT); |
Bobty | 10:72eb217def1f | 128 | |
Bobty | 14:3c3aa4fd7e1a | 129 | // Format the message |
Bobty | 14:3c3aa4fd7e1a | 130 | GenBroadcastMessage(); |
Bobty | 14:3c3aa4fd7e1a | 131 | |
Bobty | 5:5bccf48799d4 | 132 | // Send |
Bobty | 10:72eb217def1f | 133 | int bytesToSend = strlen(broadcastMsgBuffer); |
Bobty | 10:72eb217def1f | 134 | int rslt = sendUDPSocket.sendTo(broadcastEndpoint, broadcastMsgBuffer, bytesToSend); |
Bobty | 5:5bccf48799d4 | 135 | if (rslt == bytesToSend) |
Bobty | 5:5bccf48799d4 | 136 | { |
Bobty | 10:72eb217def1f | 137 | pc.printf("Broadcast (len %d) Sent ok %s\r\n", bytesToSend, broadcastMsgBuffer); |
Bobty | 5:5bccf48799d4 | 138 | } |
Bobty | 5:5bccf48799d4 | 139 | else if (rslt == -1) |
Bobty | 5:5bccf48799d4 | 140 | { |
Bobty | 10:72eb217def1f | 141 | pc.printf("Broadcast Failed to send %s\r\n", broadcastMsgBuffer); |
Bobty | 5:5bccf48799d4 | 142 | } |
Bobty | 5:5bccf48799d4 | 143 | else |
Bobty | 5:5bccf48799d4 | 144 | { |
Bobty | 10:72eb217def1f | 145 | pc.printf("Broadcast Didn't send all of %s\r\n", broadcastMsgBuffer); |
Bobty | 5:5bccf48799d4 | 146 | } |
Bobty | 5:5bccf48799d4 | 147 | |
Bobty | 12:a52996515063 | 148 | // Log the data |
Bobty | 12:a52996515063 | 149 | logger.LogData(broadcastMsgBuffer); |
Bobty | 5:5bccf48799d4 | 150 | |
Bobty | 5:5bccf48799d4 | 151 | led3 = false; |
Bobty | 5:5bccf48799d4 | 152 | } |
Bobty | 0:f6611c8f453c | 153 | |
Bobty | 14:3c3aa4fd7e1a | 154 | char* getCurDataCallback(int method, char* cmdStr, char* argStr) |
Bobty | 5:5bccf48799d4 | 155 | { |
Bobty | 14:3c3aa4fd7e1a | 156 | // Format message |
Bobty | 14:3c3aa4fd7e1a | 157 | GenBroadcastMessage(); |
Bobty | 14:3c3aa4fd7e1a | 158 | return broadcastMsgBuffer; |
Bobty | 5:5bccf48799d4 | 159 | } |
Bobty | 5:5bccf48799d4 | 160 | |
Bobty | 5:5bccf48799d4 | 161 | char* setGasUseCallback(int method, char* cmdStr, char* argStr) |
Bobty | 5:5bccf48799d4 | 162 | { |
Bobty | 9:0e103c2f869a | 163 | pc.printf("Setting gas use count %s\r\n", argStr); |
Bobty | 5:5bccf48799d4 | 164 | int newGasUse = 0; |
Bobty | 5:5bccf48799d4 | 165 | char* eqStr = strchr(argStr, '='); |
Bobty | 5:5bccf48799d4 | 166 | if (eqStr == NULL) |
Bobty | 5:5bccf48799d4 | 167 | return "SetGasValue FAILED"; |
Bobty | 5:5bccf48799d4 | 168 | sscanf(eqStr+1, "%d", &newGasUse); |
Bobty | 5:5bccf48799d4 | 169 | gasUseCounter.SetCount(newGasUse); |
Bobty | 5:5bccf48799d4 | 170 | return "SetGasValue OK"; |
Bobty | 5:5bccf48799d4 | 171 | } |
Bobty | 5:5bccf48799d4 | 172 | |
Bobty | 11:30182b9aa833 | 173 | // Create, configure and run the web server |
Bobty | 5:5bccf48799d4 | 174 | void http_thread(void const* arg) |
Bobty | 5:5bccf48799d4 | 175 | { |
Bobty | 5:5bccf48799d4 | 176 | char* baseWebFolder = "/sd/"; |
Bobty | 5:5bccf48799d4 | 177 | RdWebServer webServer; |
Bobty | 5:5bccf48799d4 | 178 | webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false); |
Bobty | 5:5bccf48799d4 | 179 | webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true); |
Bobty | 12:a52996515063 | 180 | webServer.addCommand("listfiles", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "/", false); |
Bobty | 14:3c3aa4fd7e1a | 181 | webServer.addCommand("getcurdata", RdWebServerCmdDef::CMD_CALLBACK, &getCurDataCallback); |
Bobty | 5:5bccf48799d4 | 182 | webServer.addCommand("setgascount", RdWebServerCmdDef::CMD_CALLBACK, &setGasUseCallback); |
Bobty | 5:5bccf48799d4 | 183 | webServer.init(WEBPORT, &led4, baseWebFolder); |
Bobty | 5:5bccf48799d4 | 184 | webServer.run(); |
Bobty | 5:5bccf48799d4 | 185 | } |
Bobty | 5:5bccf48799d4 | 186 | |
Bobty | 11:30182b9aa833 | 187 | // Network time protocol (NTP) thread to get time from internet |
Bobty | 5:5bccf48799d4 | 188 | void ntp_thread(void const* arg) |
Bobty | 5:5bccf48799d4 | 189 | { |
Bobty | 5:5bccf48799d4 | 190 | while (1) |
Bobty | 5:5bccf48799d4 | 191 | { |
Bobty | 5:5bccf48799d4 | 192 | pc.printf("Trying to update time...\r\n"); |
Bobty | 14:3c3aa4fd7e1a | 193 | if (ntp.setTime("0.pool.ntp.org") == NTP_OK) |
Bobty | 5:5bccf48799d4 | 194 | { |
Bobty | 5:5bccf48799d4 | 195 | printf("Set time successfully\r\n"); |
Bobty | 5:5bccf48799d4 | 196 | time_t ctTime; |
Bobty | 5:5bccf48799d4 | 197 | ctTime = time(NULL); |
Bobty | 5:5bccf48799d4 | 198 | printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); |
Bobty | 5:5bccf48799d4 | 199 | } |
Bobty | 5:5bccf48799d4 | 200 | else |
Bobty | 5:5bccf48799d4 | 201 | { |
Bobty | 5:5bccf48799d4 | 202 | printf("Cannot set from NTP\r\n"); |
Bobty | 10:72eb217def1f | 203 | } |
Bobty | 10:72eb217def1f | 204 | |
Bobty | 10:72eb217def1f | 205 | // Refresh time every K hours |
Bobty | 10:72eb217def1f | 206 | for (int k = 0; k < NTP_REFRESH_INTERVAL_HOURS; k++) |
Bobty | 5:5bccf48799d4 | 207 | { |
Bobty | 10:72eb217def1f | 208 | // 1 hour |
Bobty | 10:72eb217def1f | 209 | for (int i = 0; i < 60; i++) |
Bobty | 5:5bccf48799d4 | 210 | { |
Bobty | 10:72eb217def1f | 211 | for (int j = 0; j < 60; j++) |
Bobty | 10:72eb217def1f | 212 | { |
Bobty | 10:72eb217def1f | 213 | osDelay(1000); |
Bobty | 10:72eb217def1f | 214 | } |
Bobty | 10:72eb217def1f | 215 | pc.printf("%d mins to next NTP time refresh\r\n", (NTP_REFRESH_INTERVAL_HOURS-k-1)*60 + (59-i)); |
Bobty | 5:5bccf48799d4 | 216 | } |
Bobty | 5:5bccf48799d4 | 217 | } |
Bobty | 5:5bccf48799d4 | 218 | } |
Bobty | 5:5bccf48799d4 | 219 | } |
Bobty | 9:0e103c2f869a | 220 | |
Bobty | 12:a52996515063 | 221 | // #define TEST_WATCHDOG 1 |
Bobty | 12:a52996515063 | 222 | #ifdef TEST_WATCHDOG |
Bobty | 12:a52996515063 | 223 | int watchdogTestLoopCount = 0; |
Bobty | 12:a52996515063 | 224 | #endif |
Bobty | 12:a52996515063 | 225 | |
Bobty | 11:30182b9aa833 | 226 | // Main |
Bobty | 0:f6611c8f453c | 227 | int main() |
Bobty | 0:f6611c8f453c | 228 | { |
Bobty | 0:f6611c8f453c | 229 | pc.baud(115200); |
Bobty | 5:5bccf48799d4 | 230 | pc.printf("Gas Monitor V2 - Rob Dobson 2014\r\n"); |
Bobty | 0:f6611c8f453c | 231 | |
Bobty | 9:0e103c2f869a | 232 | // Initialise thermometers |
Bobty | 9:0e103c2f869a | 233 | thermometers.Init(); |
Bobty | 9:0e103c2f869a | 234 | |
Bobty | 5:5bccf48799d4 | 235 | // Get the current count from the SD Card |
Bobty | 5:5bccf48799d4 | 236 | gasUseCounter.Init(); |
Bobty | 0:f6611c8f453c | 237 | |
Bobty | 5:5bccf48799d4 | 238 | // setup ethernet interface |
Bobty | 5:5bccf48799d4 | 239 | eth.init(); //Use DHCP |
Bobty | 5:5bccf48799d4 | 240 | eth.connect(); |
Bobty | 2:6bfef0839102 | 241 | |
Bobty | 9:0e103c2f869a | 242 | pc.printf("IP Address is %s\r\n", eth.getIPAddress()); |
Bobty | 8:5980547ae71c | 243 | |
Bobty | 8:5980547ae71c | 244 | // NTP Time setter |
Bobty | 5:5bccf48799d4 | 245 | Thread ntpTimeSetter(&ntp_thread); |
Bobty | 0:f6611c8f453c | 246 | |
Bobty | 8:5980547ae71c | 247 | // Web Server |
Bobty | 6:b7064d33e402 | 248 | Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 3)); |
Bobty | 14:3c3aa4fd7e1a | 249 | |
Bobty | 14:3c3aa4fd7e1a | 250 | // Store reason for restart |
Bobty | 14:3c3aa4fd7e1a | 251 | bool watchdogCausedRestart = watchdog.WatchdogCausedRestart(); |
Bobty | 14:3c3aa4fd7e1a | 252 | bool restartCauseRecorded = false; |
Bobty | 5:5bccf48799d4 | 253 | |
Bobty | 12:a52996515063 | 254 | // Setup the watchdog for 10s reset |
Bobty | 12:a52996515063 | 255 | watchdog.SetTimeoutSecs(10); |
Bobty | 12:a52996515063 | 256 | |
Bobty | 9:0e103c2f869a | 257 | // Time of last broadcast |
Bobty | 9:0e103c2f869a | 258 | time_t timeOfLastBroadcast = time(NULL); |
Bobty | 9:0e103c2f869a | 259 | const int TIME_BETWEEN_BROADCASTS_IN_SECS = 60; |
Bobty | 5:5bccf48799d4 | 260 | while(true) |
Bobty | 0:f6611c8f453c | 261 | { |
Bobty | 14:3c3aa4fd7e1a | 262 | // Check if we can record the reason for restart (i.e. if time is now set) |
Bobty | 14:3c3aa4fd7e1a | 263 | if (!restartCauseRecorded) |
Bobty | 14:3c3aa4fd7e1a | 264 | { |
Bobty | 14:3c3aa4fd7e1a | 265 | time_t nowTime = time(NULL); |
Bobty | 14:3c3aa4fd7e1a | 266 | if (nowTime > 1000000000) |
Bobty | 14:3c3aa4fd7e1a | 267 | { |
Bobty | 14:3c3aa4fd7e1a | 268 | // Record the reason for restarting in the log file |
Bobty | 14:3c3aa4fd7e1a | 269 | if (watchdogCausedRestart) |
Bobty | 14:3c3aa4fd7e1a | 270 | logger.LogEvent("Watchdog Restart"); |
Bobty | 14:3c3aa4fd7e1a | 271 | else |
Bobty | 14:3c3aa4fd7e1a | 272 | logger.LogEvent("Normal Restart"); |
Bobty | 14:3c3aa4fd7e1a | 273 | restartCauseRecorded = true; |
Bobty | 14:3c3aa4fd7e1a | 274 | } |
Bobty | 14:3c3aa4fd7e1a | 275 | } |
Bobty | 14:3c3aa4fd7e1a | 276 | |
Bobty | 14:3c3aa4fd7e1a | 277 | // Loop delay |
Bobty | 9:0e103c2f869a | 278 | osDelay(LOOP_DELAY_IN_MS); |
Bobty | 14:3c3aa4fd7e1a | 279 | |
Bobty | 14:3c3aa4fd7e1a | 280 | // Feed the watchdog and show the flashing LED |
Bobty | 5:5bccf48799d4 | 281 | led1 = !led1; |
Bobty | 12:a52996515063 | 282 | watchdog.Feed(); |
Bobty | 5:5bccf48799d4 | 283 | |
Bobty | 5:5bccf48799d4 | 284 | // Service gas count |
Bobty | 5:5bccf48799d4 | 285 | if (gasUseCounter.Service()) |
Bobty | 8:5980547ae71c | 286 | { |
Bobty | 9:0e103c2f869a | 287 | SendInfoBroadcast(); |
Bobty | 9:0e103c2f869a | 288 | timeOfLastBroadcast = time(NULL); |
Bobty | 9:0e103c2f869a | 289 | } |
Bobty | 8:5980547ae71c | 290 | |
Bobty | 9:0e103c2f869a | 291 | // Service thermometers |
Bobty | 9:0e103c2f869a | 292 | thermometers.Service(); |
Bobty | 9:0e103c2f869a | 293 | |
Bobty | 9:0e103c2f869a | 294 | // Check if ready for a broadcast |
Bobty | 9:0e103c2f869a | 295 | if ((time(NULL) - timeOfLastBroadcast) >= TIME_BETWEEN_BROADCASTS_IN_SECS) |
Bobty | 9:0e103c2f869a | 296 | { |
Bobty | 9:0e103c2f869a | 297 | SendInfoBroadcast(); |
Bobty | 9:0e103c2f869a | 298 | timeOfLastBroadcast = time(NULL); |
Bobty | 8:5980547ae71c | 299 | } |
Bobty | 10:72eb217def1f | 300 | |
Bobty | 10:72eb217def1f | 301 | // Service volt alerters |
Bobty | 10:72eb217def1f | 302 | voltAlerter1.Service(); |
Bobty | 10:72eb217def1f | 303 | voltAlerter2.Service(); |
Bobty | 10:72eb217def1f | 304 | voltAlerter3.Service(); |
Bobty | 11:30182b9aa833 | 305 | |
Bobty | 11:30182b9aa833 | 306 | // Set LED2 to the state of the first volt alerter |
Bobty | 10:72eb217def1f | 307 | led2 = voltAlerter1.GetState(); |
Bobty | 12:a52996515063 | 308 | |
Bobty | 12:a52996515063 | 309 | #ifdef TEST_WATCHDOG |
Bobty | 12:a52996515063 | 310 | // After about 20 seconds of operation we'll hang to test the watchdog |
Bobty | 12:a52996515063 | 311 | if (watchdogTestLoopCount++ > 80) |
Bobty | 12:a52996515063 | 312 | { |
Bobty | 12:a52996515063 | 313 | // This should cause watchdog to kick in and reset |
Bobty | 12:a52996515063 | 314 | osDelay(20000); |
Bobty | 12:a52996515063 | 315 | } |
Bobty | 12:a52996515063 | 316 | #endif |
Bobty | 0:f6611c8f453c | 317 | } |
Bobty | 5:5bccf48799d4 | 318 | } |
Bobty | 9:0e103c2f869a | 319 |