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
Diff: main.cpp
- Revision:
- 2:6bfef0839102
- Parent:
- 1:518f39df3485
- Child:
- 3:9f00be404f8f
--- a/main.cpp Fri Nov 07 14:44:23 2014 +0000 +++ b/main.cpp Fri Nov 07 16:09:15 2014 +0000 @@ -1,11 +1,12 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "PulsePin.h" DigitalIn gpPin(p21); +PulsePin pulsePin(gpPin, false, 200); DigitalOut led(LED1); const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number -const int NUM_GASPULSE_VALS_IN_PACKET = 10; Serial pc(USBTX, USBRX); int main() @@ -22,12 +23,9 @@ Timer connectRetryTimer; connectRetryTimer.start(); bool isConnected = false; - - // Gas Meter Pulse sample timer - Timer gasPulseTimer; - gasPulseTimer.start(); - int gasPulseCount = 0; - bool gasPulseVals[NUM_GASPULSE_VALS_IN_PACKET]; + + // Count of gas pulses + int gasCount = 0; // Forever while (true) @@ -54,41 +52,30 @@ } else { - if (gasPulseTimer.read_ms() > 1000) + led = gpPin; + // Check for an edge + bool edgeDetected = pulsePin.Service(); + if (edgeDetected) { - gasPulseTimer.reset(); - - // Read from Gas Pulse pin - gasPulseVals[gasPulseCount++] = gpPin; - led = gpPin; - if (gasPulseCount >= NUM_GASPULSE_VALS_IN_PACKET) + gasCount++; + char outBuf[200]; + sprintf(outBuf, "[{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasInterPulse\",\"v\":%d,\"u\":\"ms\"}]", + gasCount, pulsePin.GetLastCycleTimeMs()); + int bytesToSend = strlen(outBuf); + int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); + if (rslt == bytesToSend) { - gasPulseCount = 0; - - // Send the packet - char outBuf[] = "{\"gaspulseval\":[0,0,0,0,0,0,0,0,0,0]}\n"; - char* pOutBuf = strchr(outBuf, '[') + 1; - for (int i = 0; i < NUM_GASPULSE_VALS_IN_PACKET; i++) - { - *pOutBuf = gasPulseVals[i] ? '1' : '0'; - pOutBuf+=2; - } - int bytesToSend = sizeof(outBuf) - 2; - int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); - if (rslt == bytesToSend) - { - printf("Sent ok %s", outBuf); - } - else if (rslt == -1) - { - printf("Failed to send %s", outBuf); - isConnected = false; - } - else - { - printf("Didn't send all of %s", outBuf); - isConnected = false; - } + printf("Sent ok %s\n", outBuf); + } + else if (rslt == -1) + { + printf("Failed to send %s\n", outBuf); + isConnected = false; + } + else + { + printf("Didn't send all of %s\n", outBuf); + isConnected = false; } }