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