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:
Tue Feb 17 21:33:39 2015 +0000
Revision:
5:5bccf48799d4
Parent:
4:0d3a207680b0
Child:
6:b7064d33e402
Tidied up - but is unstable - web server crashes after some time

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 5:5bccf48799d4 6 #include <stdarg.h>
Bobty 5:5bccf48799d4 7
Bobty 5:5bccf48799d4 8 // Web and UDB ports
Bobty 5:5bccf48799d4 9 const int WEBPORT = 80; // Port for web server
Bobty 5:5bccf48799d4 10 const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number
Bobty 5:5bccf48799d4 11
Bobty 5:5bccf48799d4 12 // Ticker collects data
Bobty 5:5bccf48799d4 13 Ticker ticker;
Bobty 5:5bccf48799d4 14 const int TICK_MS = 250;
Bobty 5:5bccf48799d4 15
Bobty 5:5bccf48799d4 16 // Debugging and status
Bobty 5:5bccf48799d4 17 RawSerial pc(USBTX, USBRX);
Bobty 5:5bccf48799d4 18 DigitalOut led1(LED1); //ticking (flashes)
Bobty 5:5bccf48799d4 19 DigitalOut led2(LED2); //server listning status (flashes)
Bobty 5:5bccf48799d4 20 DigitalOut led3(LED3); //socket connecting status
Bobty 5:5bccf48799d4 21 DigitalOut led4(LED4); //server status
Bobty 5:5bccf48799d4 22
Bobty 5:5bccf48799d4 23 // Web server
Bobty 5:5bccf48799d4 24 EthernetInterface eth;
Bobty 5:5bccf48799d4 25 NTPClient ntp;
Bobty 5:5bccf48799d4 26 UDPSocket sendUDPSocket;
Bobty 5:5bccf48799d4 27 Endpoint broadcastEndpoint;
Bobty 5:5bccf48799d4 28
Bobty 5:5bccf48799d4 29 // File system for SD card
Bobty 4:0d3a207680b0 30 SDFileSystem sd(p5, p6, p7, p8, "sd");
Bobty 5:5bccf48799d4 31
Bobty 5:5bccf48799d4 32 // Gas use counter
Bobty 5:5bccf48799d4 33 DigitalIn gasPulsePin(p21);
Bobty 5:5bccf48799d4 34 const char* gasPulseFileName = "/sd/curPulse.txt";
Bobty 5:5bccf48799d4 35 const char* logFilename = "/sd/log.txt";
Bobty 5:5bccf48799d4 36 GasUseCounter gasUseCounter(gasPulseFileName, gasPulsePin, pc);
Bobty 5:5bccf48799d4 37
Bobty 5:5bccf48799d4 38 void LogData(const char* format, ...)
Bobty 5:5bccf48799d4 39 {
Bobty 5:5bccf48799d4 40 FILE* fp = fopen(logFilename, "a");
Bobty 5:5bccf48799d4 41 if (fp == NULL)
Bobty 5:5bccf48799d4 42 {
Bobty 5:5bccf48799d4 43 pc.printf ("Log ... Filename %s not found\r\n", logFilename);
Bobty 5:5bccf48799d4 44 }
Bobty 5:5bccf48799d4 45 else
Bobty 5:5bccf48799d4 46 {
Bobty 5:5bccf48799d4 47 va_list argptr;
Bobty 5:5bccf48799d4 48 va_start(argptr, format);
Bobty 5:5bccf48799d4 49 vfprintf(fp, format, argptr);
Bobty 5:5bccf48799d4 50 va_end(argptr);
Bobty 5:5bccf48799d4 51 fclose(fp);
Bobty 5:5bccf48799d4 52 }
Bobty 5:5bccf48799d4 53 }
Bobty 5:5bccf48799d4 54
Bobty 5:5bccf48799d4 55 void SendInfoBroadcast()
Bobty 5:5bccf48799d4 56 {
Bobty 5:5bccf48799d4 57 led3 = true;
Bobty 5:5bccf48799d4 58 // Init the sending socket
Bobty 5:5bccf48799d4 59 sendUDPSocket.init();
Bobty 5:5bccf48799d4 60 sendUDPSocket.set_broadcasting();
Bobty 5:5bccf48799d4 61 broadcastEndpoint.set_address("255.255.255.255", BROADCAST_PORT);
Bobty 5:5bccf48799d4 62
Bobty 5:5bccf48799d4 63 // Format message
Bobty 5:5bccf48799d4 64 char outBuf[200];
Bobty 5:5bccf48799d4 65 sprintf(outBuf, "{\"e\":[{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasPulseRateMs\",\"v\":%d,\"u\":\"ms\"}]}",
Bobty 5:5bccf48799d4 66 gasUseCounter.GetCount(), gasUseCounter.GetPulseRateMs());
Bobty 5:5bccf48799d4 67
Bobty 5:5bccf48799d4 68 // Send
Bobty 5:5bccf48799d4 69 int bytesToSend = strlen(outBuf);
Bobty 5:5bccf48799d4 70 int rslt = sendUDPSocket.sendTo(broadcastEndpoint, outBuf, bytesToSend);
Bobty 5:5bccf48799d4 71 if (rslt == bytesToSend)
Bobty 5:5bccf48799d4 72 {
Bobty 5:5bccf48799d4 73 pc.printf("Broadcast Sent ok %s\n", outBuf);
Bobty 5:5bccf48799d4 74 }
Bobty 5:5bccf48799d4 75 else if (rslt == -1)
Bobty 5:5bccf48799d4 76 {
Bobty 5:5bccf48799d4 77 pc.printf("Broadcast Failed to send %s\n", outBuf);
Bobty 5:5bccf48799d4 78 }
Bobty 5:5bccf48799d4 79 else
Bobty 5:5bccf48799d4 80 {
Bobty 5:5bccf48799d4 81 pc.printf("Broadcast Didn't send all of %s\n", outBuf);
Bobty 5:5bccf48799d4 82 }
Bobty 5:5bccf48799d4 83
Bobty 5:5bccf48799d4 84 // Log
Bobty 5:5bccf48799d4 85 char timeBuf[32];
Bobty 5:5bccf48799d4 86 time_t seconds = time(NULL);
Bobty 5:5bccf48799d4 87 strftime(timeBuf, 32, "%Y-%m-%d %H:%M:%S", localtime(&seconds));
Bobty 5:5bccf48799d4 88 LogData("%s\t%d\t%d ms\n", timeBuf, gasUseCounter.GetCount(), gasUseCounter.GetPulseRateMs());
Bobty 5:5bccf48799d4 89
Bobty 5:5bccf48799d4 90 led3 = false;
Bobty 5:5bccf48799d4 91 }
Bobty 0:f6611c8f453c 92
Bobty 5:5bccf48799d4 93 // Ticker's tick function
Bobty 5:5bccf48799d4 94 void TickFunction()
Bobty 5:5bccf48799d4 95 {
Bobty 5:5bccf48799d4 96 }
Bobty 5:5bccf48799d4 97
Bobty 5:5bccf48799d4 98 char* getGasUseCallback(int method, char* cmdStr, char* argStr)
Bobty 5:5bccf48799d4 99 {
Bobty 5:5bccf48799d4 100 char* pResp = gasUseCounter.getGasUseCallback(cmdStr, argStr);
Bobty 5:5bccf48799d4 101 pc.printf("Returning gas use %s\r\n", pResp);
Bobty 5:5bccf48799d4 102 return pResp;
Bobty 5:5bccf48799d4 103 }
Bobty 5:5bccf48799d4 104
Bobty 5:5bccf48799d4 105 char* setGasUseCallback(int method, char* cmdStr, char* argStr)
Bobty 5:5bccf48799d4 106 {
Bobty 5:5bccf48799d4 107 pc.printf("Setting gas use count %s\n\r", argStr);
Bobty 5:5bccf48799d4 108 int newGasUse = 0;
Bobty 5:5bccf48799d4 109 char* eqStr = strchr(argStr, '=');
Bobty 5:5bccf48799d4 110 if (eqStr == NULL)
Bobty 5:5bccf48799d4 111 return "SetGasValue FAILED";
Bobty 5:5bccf48799d4 112 sscanf(eqStr+1, "%d", &newGasUse);
Bobty 5:5bccf48799d4 113 gasUseCounter.SetCount(newGasUse);
Bobty 5:5bccf48799d4 114 return "SetGasValue OK";
Bobty 5:5bccf48799d4 115 }
Bobty 5:5bccf48799d4 116
Bobty 5:5bccf48799d4 117 void http_thread(void const* arg)
Bobty 5:5bccf48799d4 118 {
Bobty 5:5bccf48799d4 119 char* baseWebFolder = "/sd/";
Bobty 5:5bccf48799d4 120
Bobty 5:5bccf48799d4 121 RdWebServer webServer;
Bobty 5:5bccf48799d4 122 webServer.addCommand("", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, "index.htm", false);
Bobty 5:5bccf48799d4 123 webServer.addCommand("gear-gr.png", RdWebServerCmdDef::CMD_SDORUSBFILE, NULL, NULL, true);
Bobty 5:5bccf48799d4 124 webServer.addCommand("getgascount", RdWebServerCmdDef::CMD_CALLBACK, &getGasUseCallback);
Bobty 5:5bccf48799d4 125 webServer.addCommand("setgascount", RdWebServerCmdDef::CMD_CALLBACK, &setGasUseCallback);
Bobty 0:f6611c8f453c 126
Bobty 5:5bccf48799d4 127 webServer.init(WEBPORT, &led4, baseWebFolder);
Bobty 5:5bccf48799d4 128
Bobty 5:5bccf48799d4 129 webServer.run();
Bobty 5:5bccf48799d4 130 }
Bobty 5:5bccf48799d4 131
Bobty 5:5bccf48799d4 132 void ntp_thread(void const* arg)
Bobty 5:5bccf48799d4 133 {
Bobty 5:5bccf48799d4 134 while (1)
Bobty 5:5bccf48799d4 135 {
Bobty 5:5bccf48799d4 136 pc.printf("Trying to update time...\r\n");
Bobty 5:5bccf48799d4 137 if (ntp.setTime("0.pool.ntp.org") == 0)
Bobty 5:5bccf48799d4 138 {
Bobty 5:5bccf48799d4 139 printf("Set time successfully\r\n");
Bobty 5:5bccf48799d4 140 time_t ctTime;
Bobty 5:5bccf48799d4 141 ctTime = time(NULL);
Bobty 5:5bccf48799d4 142 printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
Bobty 5:5bccf48799d4 143 }
Bobty 5:5bccf48799d4 144 else
Bobty 5:5bccf48799d4 145 {
Bobty 5:5bccf48799d4 146 printf("Cannot set from NTP\r\n");
Bobty 5:5bccf48799d4 147 }
Bobty 5:5bccf48799d4 148 // 1 hour
Bobty 5:5bccf48799d4 149 for (int i = 0; i < 60; i++)
Bobty 5:5bccf48799d4 150 {
Bobty 5:5bccf48799d4 151 for (int j = 0; j < 60; j++)
Bobty 5:5bccf48799d4 152 {
Bobty 5:5bccf48799d4 153 osDelay(1000);
Bobty 5:5bccf48799d4 154 }
Bobty 5:5bccf48799d4 155 pc.printf("Waited %d mins\r\n", i);
Bobty 5:5bccf48799d4 156 }
Bobty 5:5bccf48799d4 157 }
Bobty 5:5bccf48799d4 158 }
Bobty 5:5bccf48799d4 159
Bobty 0:f6611c8f453c 160 int main()
Bobty 0:f6611c8f453c 161 {
Bobty 0:f6611c8f453c 162 pc.baud(115200);
Bobty 5:5bccf48799d4 163 pc.printf("Gas Monitor V2 - Rob Dobson 2014\r\n");
Bobty 0:f6611c8f453c 164
Bobty 5:5bccf48799d4 165 ticker.attach(&TickFunction,TICK_MS / 1000.0);
Bobty 5:5bccf48799d4 166
Bobty 5:5bccf48799d4 167 // Get the current count from the SD Card
Bobty 5:5bccf48799d4 168 gasUseCounter.Init();
Bobty 0:f6611c8f453c 169
Bobty 5:5bccf48799d4 170 // setup ethernet interface
Bobty 5:5bccf48799d4 171 eth.init(); //Use DHCP
Bobty 5:5bccf48799d4 172 eth.connect();
Bobty 2:6bfef0839102 173
Bobty 5:5bccf48799d4 174 pc.printf("IP Address is %s\n\r", eth.getIPAddress());
Bobty 5:5bccf48799d4 175
Bobty 5:5bccf48799d4 176 Thread ntpTimeSetter(&ntp_thread);
Bobty 0:f6611c8f453c 177
Bobty 5:5bccf48799d4 178 Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25));
Bobty 5:5bccf48799d4 179
Bobty 5:5bccf48799d4 180 while(true)
Bobty 0:f6611c8f453c 181 {
Bobty 5:5bccf48799d4 182 osDelay(250);
Bobty 5:5bccf48799d4 183 led1 = !led1;
Bobty 5:5bccf48799d4 184
Bobty 5:5bccf48799d4 185 // Service gas count
Bobty 5:5bccf48799d4 186 if (gasUseCounter.Service())
Bobty 5:5bccf48799d4 187 SendInfoBroadcast();
Bobty 0:f6611c8f453c 188 }
Bobty 5:5bccf48799d4 189 }