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