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 20:28:21 2015 +0000
Revision:
11:30182b9aa833
Parent:
10:72eb217def1f
Child:
12:a52996515063
Tidied up a bit

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